Skip to content

Commit

Permalink
Add browser bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
demsking committed Nov 15, 2021
1 parent acf2a5c commit db454f4
Show file tree
Hide file tree
Showing 13 changed files with 1,952 additions and 1,555 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
/node_modules/
coverage/
/nominatim.browser.js
/nominatim.module.mjs
/nominatim.node.js
*.map
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ coverage
.gitlab-ci.yml
.eslintrc.js
*.tgz
lib
rollup.config.js
113 changes: 67 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Nominatim Client

A basic node module to handle geocoding and reverse geocoding via
A simple client to handle geocoding and reverse geocoding via
[OpenStreetMap (OSM)](http://openstreetmap.org/).
It attempts to adhere to the [Nominatim Usage Policy](https://operations.osmfoundation.org/policies/nominatim/).

Expand All @@ -26,11 +26,11 @@ const client = nominatim.createClient({
});
```

**Search**
**[Search](https://nominatim.org/release-docs/develop/api/Search/)**

```js
const query = {
q: 'Avenue Monseigneur Vogt, Yaounde, Cameroon',
q: '1 boulevard Anatole France Belfort',
addressdetails: '1'
};

Expand All @@ -39,61 +39,82 @@ nominatim.search(query).then((result) => console.log(result));

Output:

```js
[ { place_id: '81750916',
licence: 'Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright',
osm_type: 'way',
osm_id: '97815221',
boundingbox: [ '3.8623122', '3.8708124', '11.5208506', '11.5238461' ],
lat: '3.8667843',
lon: '11.5225728',
display_name: 'Avenue Monseigneur Vogt, Centre Commercial, Yaoundé I, CUY, Mfoundi, CE, 1561, Cameroun',
class: 'highway',
type: 'secondary',
importance: 0.4,
address:
{ road: 'Avenue Monseigneur Vogt',
suburb: 'Centre Commercial',
city: 'Yaoundé I',
county: 'CUY',
state: 'CE',
postcode: '1561',
country: 'Cameroun',
country_code: 'cm' } } ]
```json
[
{
"place_id": 273995170,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type": "way",
"osm_id": 929173018,
"boundingbox": [
"47.6405466",
"47.640965",
"6.8419814",
"6.8480751"
],
"lat": "47.6407423",
"lon": "6.844936",
"display_name": "Boulevard Anatole France, Le Mont Sud, Belfort, Territoire-de-Belfort, Bourgogne-Franche-Comté, Metropolitan France, 90000, France",
"class": "highway",
"type": "primary",
"importance": 0.5199999999999999,
"address": {
"road": "Boulevard Anatole France",
"suburb": "Le Mont Sud",
"city": "Belfort",
"municipality": "Belfort",
"county": "Territoire-de-Belfort",
"state": "Bourgogne-Franche-Comté",
"country": "France",
"postcode": "90000",
"country_code": "fr"
}
}
]
```

**Reverse**
**[Reverse](https://nominatim.org/release-docs/develop/api/Reverse/)**

```js
const query = {
lat: 3.869414,
lon: 11.523433
lat: 47.6407423,
lon: 6.844936
};

client.reverse(query).then((result) => console.log(result));
```

Output:

```js
{ place_id: '122705430',
licence: 'Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright',
osm_type: 'way',
osm_id: '270179822',
lat: '3.8696559',
lon: '11.5237866599278',
display_name: 'Le Bois d\'Ébène, Avenue Monseigneur Vogt, Centre Commercial, Yaoundé I, CUY, Mfoundi, CE, 1561, Cameroun',
address:
{ restaurant: 'Le Bois d\'Ébène',
road: 'Avenue Monseigneur Vogt',
suburb: 'Centre Commercial',
city: 'Yaoundé I',
county: 'CUY',
state: 'CE',
postcode: '1561',
country: 'Cameroun',
country_code: 'cm' },
boundingbox: [ '3.8696101', '3.8697112', '11.5237394', '11.5238284' ] }
```json
{
"place_id": 119321832,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type": "way",
"osm_id": 79763493,
"lat": "47.64105015",
"lon": "6.845205480320034",
"display_name": "C, 8A, Boulevard Anatole France, Barres Le Mont, Belfort, Territoire-de-Belfort, Bourgogne-Franche-Comté, Metropolitan France, 90000, France",
"address": {
"building": "C",
"house_number": "8A",
"road": "Boulevard Anatole France",
"suburb": "Barres Le Mont",
"city": "Belfort",
"municipality": "Belfort",
"county": "Territoire-de-Belfort",
"state": "Bourgogne-Franche-Comté",
"country": "France",
"postcode": "90000",
"country_code": "fr"
},
"boundingbox": [
"47.6409593",
"47.6411156",
"6.8449412",
"6.8454734"
]
}
```

## Versioning
Expand Down
4 changes: 2 additions & 2 deletions __tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const nominatim = require('..');
const nominatim = require('../lib/nominatim.node');

const client = nominatim.createClient({
useragent: "nominatim-client",
Expand All @@ -8,7 +8,7 @@ const client = nominatim.createClient({

test('search', () => {
const query = {
q: 'Avenue Monseigneur Vogt, Yaounde, Cameroon',
q: '7 Chemin des Prunais Villiers sur Marne',
addressdetails: 1,
};

Expand Down
57 changes: 0 additions & 57 deletions index.js

This file was deleted.

7 changes: 7 additions & 0 deletions lib/fetch.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = async function query(host, path, { headers, ...params }) {
const response = await fetch(host + path, { headers });

return params.format === 'json'
? await response.json()
: await response.text();
};
26 changes: 26 additions & 0 deletions lib/fetch.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const https = require('https');

module.exports = function fetch(host, path, { headers, ...params }) {
return new Promise((resolve, reject) => {
https.get({ host, path, headers }, (res) => {
let data = '';

res.setEncoding('utf8');
res.on('error', reject);

res.on('data', (chunk) => {
data += chunk;
});

res.on('end', () => {
try {
data = params.format === 'json' ? JSON.parse(data) : data;

resolve(data);
} catch (err) {
reject(err);
}
});
}).on('error', reject);
});
};
4 changes: 4 additions & 0 deletions lib/nominatim.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const nominatim = require('./nominatim');
const fetch = require('./fetch.browser');

module.exports = nominatim(fetch);
38 changes: 38 additions & 0 deletions lib/nominatim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const API_ENDPOINT = 'nominatim.openstreetmap.org';
const defaultParams = {
format: 'json',
};

function encode(params) {
const params_query = [];

for (const key in params) {
params_query.push(key + '=' + encodeURIComponent(params[key]));
}

return params_query.join('&');
};

module.exports = (query) => ({
createClient: ({ useragent, referer, ...options }) => {
const headers = {
'User-Agent': useragent,
referer,
};

return {
search(params) {
const queryParams = { ...defaultParams, ...options, ...params };
const search = encode(queryParams);

return query(API_ENDPOINT, `/?${search}`, { ...queryParams, headers });
},
reverse(params) {
const queryParams = { ...defaultParams, zoom: 18, ...options, ...params };
const search = encode(queryParams);

return query(API_ENDPOINT, `/reverse?${search}`, { ...queryParams, headers });
},
};
},
});
4 changes: 4 additions & 0 deletions lib/nominatim.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const nominatim = require('./nominatim');
const fetch = require('./fetch.node');

module.exports = nominatim(fetch);
Loading

0 comments on commit db454f4

Please sign in to comment.