Skip to content

Commit

Permalink
update tilehosting service to maptiler
Browse files Browse the repository at this point in the history
  • Loading branch information
Natalia Kowalczyk committed Jan 25, 2025
1 parent b5218c4 commit 579e3ba
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 243 deletions.
12 changes: 6 additions & 6 deletions demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ if (process.env.POSITIONSTACK_KEY) {
positionstack_key: process.env.POSITIONSTACK_KEY
});
}
if (process.env.TILEHOSTING_KEY) {
service('tilehosting', {
order: ['tilehosting'],
tilehosting_parameters: { interval: 1000 },
tilehosting_enable() { return true; },
tilehosting_key: process.env.TILEHOSTING_KEY
if (process.env.MAPTILER_KEY) {
service('maptiler', {
order: ['maptiler'],
maptiler_parameters: { interval: 1000 },
maptiler_enable() { return true; },
maptiler_key: process.env.MAPTILER_KEY
});
}

Expand Down
4 changes: 2 additions & 2 deletions lib/geocode.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ function furkotGeocode(options) {
synchronous: {
init: require('./service/synchronous')
},
tilehosting: {
init: require('./service/tilehosting')
maptiler: {
init: require('./service/maptiler')
}
};

Expand Down
93 changes: 93 additions & 0 deletions lib/service/maptiler/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const normalize = require('../normalize');
const status = require('../status');
const util = require('../util');

module.exports = init;

/*
* https://docs.maptiler.com/cloud/api/geocoding/
*/
function getStatus(err, response) {
if (!response) {
return;
}
if (err) {
return err.status ? status.error : status.failure;
}
if (!(response.type === 'FeatureCollection' && response.features?.length)) {
return status.empty;
}
return status.success;
}

function getUrl(url, key, op, query) {
let q;
if (op === 'forward') {
q = encodeURIComponent(query.address || query.place);
} else {
q = query.ll.join(',');
}
q += '.json?key=' + key;
if (query.max) {
q += '&limit=' + query.max;
}
if (query.bounds) {
q += '&bbox=' + [
query.bounds[0][0], // west
query.bounds[0][1], // south
query.bounds[1][0], // east
query.bounds[1][1] // north
].join(',');
}
return url + q;
}

function prepareRequest() {
return true;
}

function map(result) {
const res = {
ll: result.center
};

res.place = result.text;
res.house = result.address;
if (result.properties) {
res.type = result.properties.kind;
res.country = normalize.country(result.properties.country_code?.toUpperCase());
}
res.address = result.place_name;
if (res.street !== res.place) {
const addr = res.address.split(', ');
if (addr.length > 1 && addr[0] === res.place) {
addr.shift();
res.address = addr.join(', ');
}
}
// remove empties
return util.removeEmpties(res);
}

function processResponse(response, query, result) {
if (!(response?.type === 'FeatureCollection' && response?.features?.length)) {
return;
}
result.places = response.features.map(map);
return result;
}

function init(options) {

options = util.defaults(options, {
forward: true,
reverse: true,
url: getUrl.bind(undefined,
options.maptiler_url || 'https://api.maptiler.com/geocoding/',
options.maptiler_key),
status: getStatus,
prepareRequest,
processResponse
});
return require('..')(options);
}
99 changes: 0 additions & 99 deletions lib/service/tilehosting/index.js

This file was deleted.

21 changes: 21 additions & 0 deletions test/replay/api.maptiler.com-443/forward
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
GET /geocoding/Rua%20Cafel%C3%A2ndia%2C%20Carapicu%C3%ADba%2C%20Brasil.json?key=furkot
accept: application/json
accept-encoding: gzip,deflate

HTTP/1.1 200 OK
date: Sat, 25 Jan 2025 22:56:23 GMT
content-type: application/json; charset=utf-8
transfer-encoding: chunked
connection: close
cf-ray: 907bdb2f4be1530c-SLC
cf-cache-status: MISS
access-control-allow-origin: *
cache-control: public, max-age=14400
last-modified: Sat, 25 Jan 2025 22:56:23 GMT
vary: Origin, User-Agent, Accept-Encoding
x-maptiler-free: 1
set-cookie: _cfuvid=h.uowQ3mc8FR9xn5KGzZHxn_1JnEZvF3QCDYk7QUX0c-1737845783428-0.0.1.1-604800000; path=/; domain=.maptiler.com; HttpOnly; Secure; SameSite=None
server: cloudflare
alt-svc: h3=":443"; ma=86400

{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"ref":"osm:w185327107","country_code":"br","kind":"street"},"geometry":{"type":"Point","coordinates":[-46.83655746281147,-23.537200177660463]},"bbox":[-46.83744963258505,-23.537359554959938,-46.8356652930379,-23.537040800360987],"center":[-46.83655746281147,-23.537200177660463],"place_name":"Rua Cafelândia, Carapicuíba, São Paulo 99999-999, Brasil","place_type":["address"],"relevance":1,"id":"address.10695521","text":"Rua Cafelândia","matching_place_name":"Rua Cafelândia, Carapicuíba, São Paulo 99999-999, Brasilia lihttodásseváldi","context":[{"ref":"oa:brazil-output/33763901","country_code":"br","id":"postal_code.1142734","text":"99999-999"},{"ref":"osm:n5277763294","country_code":"br","id":"place.371014","text":"Parque José Alexandre","kind":"place","osm:place_type":"suburb"},{"ref":"osm:r298480","country_code":"br","id":"municipality.42945","text":"Carapicuíba","wikidata":"Q176891","kind":"admin_area"},{"ref":"osm:r2661855","country_code":"br","id":"joint_submunicipality.163","text":"Região Metropolitana de São Paulo","wikidata":"Q1415666","kind":"admin_area"},{"ref":"osm:r4810113","country_code":"br","id":"joint_municipality.9864","text":"Região Imediata de São Paulo","wikidata":"Q48878625","kind":"admin_area"},{"ref":"osm:r4807856","country_code":"br","id":"county.5545","text":"Região Geográfica Intermediária de São Paulo","wikidata":"Q48878425","kind":"admin_area"},{"ref":"osm:r298204","country_code":"br","id":"subregion.251","text":"São Paulo","wikidata":"Q175","kind":"admin_area"},{"ref":"osm:r3073228","country_code":"br","id":"region.1190","text":"Região Sudeste","wikidata":"Q1088815","kind":"admin_area"},{"ref":"osm:w653288653","country_code":"br","id":"major_landform.5926264","text":"Córrego da Fábrica","categories":["stream"],"osm:tags":{"waterway":"stream"}},{"ref":"osm:r59470","country_code":"br","id":"country.99","text":"Brasil","wikidata":"Q155","kind":"admin_area"},{"ref":"osm:n36966069","country_code":"","id":"continental_marine.6","text":"South America","wikidata":"Q18","categories":["continent"],"osm:tags":{"population":"385742554","wikipedia":"en:South America","sqkm":"17840000","place":"continent"}}]},{"type":"Feature","properties":{"ref":"osm:w227589442","country_code":"br","kind":"street"},"geometry":{"type":"Point","coordinates":[-46.895270850509405,-23.61090479385711]},"bbox":[-46.896274499595165,-23.611554540880288,-46.89426720142365,-23.61025504683393],"center":[-46.895270850509405,-23.61090479385711],"place_name":"Rua Cafelândia, Cotia, São Paulo 06716-205, Brasil","place_type":["address"],"relevance":0.851852,"id":"address.11518318","text":"Rua Cafelândia","matching_place_name":"Rua Cafelândia, Cotia, São Paulo 06716-205, Brasilia lihttodásseváldi","context":[{"ref":"osm:w1042493663","country_code":"br","id":"postal_code.2143108","text":"06716-205"},{"ref":"osm:n4348155027","country_code":"br","id":"place.4031887","text":"Maranhão","kind":"place","osm:place_type":"suburb"},{"ref":"osm:r298252","country_code":"br","id":"municipality.42244","text":"Cotia","wikidata":"Q669305","kind":"admin_area"},{"ref":"osm:r2661855","country_code":"br","id":"joint_submunicipality.163","text":"Região Metropolitana de São Paulo","wikidata":"Q1415666","kind":"admin_area"},{"ref":"osm:r4810113","country_code":"br","id":"joint_municipality.9864","text":"Região Imediata de São Paulo","wikidata":"Q48878625","kind":"admin_area"},{"ref":"osm:r4807856","country_code":"br","id":"county.5545","text":"Região Geográfica Intermediária de São Paulo","wikidata":"Q48878425","kind":"admin_area"},{"ref":"osm:r298204","country_code":"br","id":"subregion.251","text":"São Paulo","wikidata":"Q175","kind":"admin_area"},{"ref":"osm:r3073228","country_code":"br","id":"region.1190","text":"Região Sudeste","wikidata":"Q1088815","kind":"admin_area"},{"ref":"osm:w775027337","country_code":"br","id":"major_landform.5514859","text":"Rio Cotia","categories":["river"],"osm:tags":{"waterway":"river"}},{"ref":"osm:r59470","country_code":"br","id":"country.99","text":"Brasil","wikidata":"Q155","kind":"admin_area"},{"ref":"osm:n36966069","country_code":"","id":"continental_marine.6","text":"South America","wikidata":"Q18","categories":["continent"],"osm:tags":{"population":"385742554","wikipedia":"en:South America","sqkm":"17840000","place":"continent"}}]},{"type":"Feature","properties":{"ref":"osm:w385907228","country_code":"br","kind":"street"},"geometry":{"type":"Point","coordinates":[-46.83837621781419,-23.42315807849958]},"bbox":[-46.843193918466575,-23.42518380263653,-46.8342125415802,-23.4201675939382],"center":[-46.83837621781419,-23.42315807849958],"place_name":"Rua Cafelândia, Cajamar, São Paulo 07750-000, Brasil","place_type":["address"],"relevance":0.851852,"id":"address.8653306","text":"Rua Cafelândia","matching_place_name":"Rua Cafelândia, Cajamar, São Paulo 07750-000, Brasilia lihttodásseváldi","context":[{"ref":"oa:brazil-output/29794557","country_code":"br","id":"postal_code.605605","text":"07750-000"},{"ref":"osm:r3032216","country_code":"br","id":"municipal_district.10006","text":"Polvilho","kind":"admin_area"},{"ref":"osm:r298462","country_code":"br","id":"municipality.42900","text":"Cajamar","wikidata":"Q1760376","kind":"admin_area"},{"ref":"osm:r2661855","country_code":"br","id":"joint_submunicipality.163","text":"Região Metropolitana de São Paulo","wikidata":"Q1415666","kind":"admin_area"},{"ref":"osm:r4810113","country_code":"br","id":"joint_municipality.9864","text":"Região Imediata de São Paulo","wikidata":"Q48878625","kind":"admin_area"},{"ref":"osm:r4807856","country_code":"br","id":"county.5545","text":"Região Geográfica Intermediária de São Paulo","wikidata":"Q48878425","kind":"admin_area"},{"ref":"osm:r298204","country_code":"br","id":"subregion.251","text":"São Paulo","wikidata":"Q175","kind":"admin_area"},{"ref":"osm:r3073228","country_code":"br","id":"region.1190","text":"Região Sudeste","wikidata":"Q1088815","kind":"admin_area"},{"ref":"osm:r13951160","country_code":"br","id":"major_landform.5061051","text":"Área de Proteção Ambiental Cajamar","categories":["protected area"],"osm:tags":{"boundary":"protected_area","contact:country":"BR","protect_class":"6","contact:postcode":"13070-178","contact:street":"Avenida Brasil","protection_title":"Área de Proteção Ambiental","website":"https://guiadeareasprotegidas.sp.gov.br/ap/area-de-protecao-ambiental-cajamar/","type":"boundary","operator":"Fundação Florestal - Secretaria de Infraestrutura e Meio Ambiente do Estado de São Paulo","contact:housenumber":"2340","related_law":"Lei Estadual nº 4.055, de 4 de Junho de 1984","opening_hours":"Mo-Fr 09:00-17:00","contact:city":"Campinas","start_date":"1984-06-04"}},{"ref":"osm:r59470","country_code":"br","id":"country.99","text":"Brasil","wikidata":"Q155","kind":"admin_area"},{"ref":"osm:n36966069","country_code":"","id":"continental_marine.6","text":"South America","wikidata":"Q18","categories":["continent"],"osm:tags":{"population":"385742554","wikipedia":"en:South America","sqkm":"17840000","place":"continent"}}]},{"type":"Feature","properties":{"ref":"osm:w250617796","country_code":"br","kind":"street"},"geometry":{"type":"Point","coordinates":[-46.890295517393156,-23.48591837620205]},"bbox":[-46.89501989632845,-23.48671031137588,-46.88546620309353,-23.48520201843251],"center":[-46.890295517393156,-23.48591837620205],"place_name":"Rua Cafelândia, Barueri, São Paulo 06415-070, Brasil","place_type":["address"],"relevance":0.851852,"id":"address.8526912","text":"Rua Cafelândia","matching_place_name":"Rua Cafelândia, Barueri, São Paulo 06415-070, Brasilia lihttodásseváldi","context":[{"ref":"osm:w671774095","country_code":"br","id":"postal_code.2140408","text":"06415-070"},{"ref":"osm:n5883128161","country_code":"br","id":"place.3899507","text":"Vila Engenho Novo - Gleba C","kind":"place","osm:place_type":"suburb"},{"ref":"osm:r298021","country_code":"br","id":"municipality.41418","text":"Barueri","wikidata":"Q461773","kind":"admin_area"},{"ref":"osm:r2661855","country_code":"br","id":"joint_submunicipality.163","text":"Região Metropolitana de São Paulo","wikidata":"Q1415666","kind":"admin_area"},{"ref":"osm:r4810113","country_code":"br","id":"joint_municipality.9864","text":"Região Imediata de São Paulo","wikidata":"Q48878625","kind":"admin_area"},{"ref":"osm:r4807856","country_code":"br","id":"county.5545","text":"Região Geográfica Intermediária de São Paulo","wikidata":"Q48878425","kind":"admin_area"},{"ref":"osm:r298204","country_code":"br","id":"subregion.251","text":"São Paulo","wikidata":"Q175","kind":"admin_area"},{"ref":"osm:r3073228","country_code":"br","id":"region.1190","text":"Região Sudeste","wikidata":"Q1088815","kind":"admin_area"},{"ref":"osm:w683187453","country_code":"br","id":"major_landform.5526341","text":"Córrego Nilo da Barra","categories":["stream"],"osm:tags":{"waterway":"stream"}},{"ref":"osm:r59470","country_code":"br","id":"country.99","text":"Brasil","wikidata":"Q155","kind":"admin_area"},{"ref":"osm:n36966069","country_code":"","id":"continental_marine.6","text":"South America","wikidata":"Q18","categories":["continent"],"osm:tags":{"population":"385742554","wikipedia":"en:South America","sqkm":"17840000","place":"continent"}}]},{"type":"Feature","properties":{"ref":"oa:brazil-output/15326861","country_code":"br","kind":"virtual_street"},"geometry":{"type":"Point","coordinates":[-54.809185014285845,-22.2456514930148]},"bbox":[-54.8108323,-22.258399200000017,-54.8075247,-22.232994900000016],"center":[-54.809185014285845,-22.2456514930148],"place_name":"RUA CAFELANDIA, Jardim Água Bôa, Dourados, Mato Grosso do Sul 79812-020, Brasil","place_type":["address"],"relevance":0.703704,"id":"address.1749266","text":"RUA CAFELANDIA","context":[{"ref":"oa:brazil-output/15327740","country_code":"br","id":"postal_code.1445792","text":"79812-020"},{"ref":"osm:r7127492","country_code":"br","id":"neighbourhood.21057","text":"Jardim Água Bôa","kind":"admin_area"},{"ref":"osm:r334017","country_code":"br","id":"municipality.29120","text":"Dourados","wikidata":"Q651874","kind":"admin_area"},{"ref":"osm:r12112250","country_code":"br","id":"joint_municipality.15917","text":"Região Geográfica Imediata de Dourados","wikidata":"Q56349056","kind":"admin_area"},{"ref":"osm:r4831653","country_code":"br","id":"county.5557","text":"Região Geográfica Intermediária de Dourados","wikidata":"Q65171895","kind":"admin_area"},{"ref":"osm:r334051","country_code":"br","id":"subregion.194","text":"Mato Grosso do Sul","wikidata":"Q43319","kind":"admin_area"},{"ref":"osm:r3359944","country_code":"br","id":"region.1408","text":"Região Centro-Oeste","wikidata":"Q980175","kind":"admin_area"},{"ref":"osm:w495042640","country_code":"br","id":"major_landform.5399550","text":"Córrego Água Bôa","categories":["stream"],"osm:tags":{"waterway":"stream"}},{"ref":"osm:r59470","country_code":"br","id":"country.99","text":"Brasil","wikidata":"Q155","kind":"admin_area"},{"ref":"osm:n36966069","country_code":"","id":"continental_marine.6","text":"South America","wikidata":"Q18","categories":["continent"],"osm:tags":{"population":"385742554","wikipedia":"en:South America","sqkm":"17840000","place":"continent"}}]}],"query":["rua","cafelândia","carapicuíba","brasil"],"attribution":"<a href=\"https://www.maptiler.com/copyright/\" target=\"_blank\">&copy; MapTiler</a> <a href=\"https://www.openstreetmap.org/copyright\" target=\"_blank\">&copy; OpenStreetMap contributors</a>"}
Loading

0 comments on commit 579e3ba

Please sign in to comment.