Skip to content

Commit

Permalink
return normalized address in addition to human-readable one
Browse files Browse the repository at this point in the history
  • Loading branch information
Natalia Kowalczyk committed Feb 11, 2024
1 parent a7ab4dc commit c469e5e
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 40 deletions.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ If successful, geocoding service will return object with field `places` - an arr
- `place` - place name (may be absent if address doesn't correspond to a named place)
- `type` - place type
- `address` - formated address
- `normal` - normalized address: `house street,town,province,country_code`
- `house` - building number
- `street` - street name
- `community` - neighborhood or village
Expand Down
1 change: 1 addition & 0 deletions lib/service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ function init(options) {
switch (options.status(err, response)) {
case status.success:
const res = options.processResponse(response, query, {});
res.places?.forEach(p => p.normal = util.stringify(p) || '');
resolve(res);
break;
case status.failure:
Expand Down
2 changes: 1 addition & 1 deletion lib/service/opencage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function init(options) {
res.county = components.county;
}
if (components.state_code) {
res.province = components.state_code;
res.province = normalize.state(components.state) || components.state_code;
}
if (components.country) {
res.country = normalize.country(components.country);
Expand Down
2 changes: 1 addition & 1 deletion lib/service/positionstack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function map(f) {
house: f.number,
street: f.street,
town: guessCity(f),
province: f.region_code,
province: normalize.state(f.region) || f.region_code,
country: normalize.country(f.country)
};
if (f.type === 'address') {
Expand Down
3 changes: 3 additions & 0 deletions lib/service/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const { stringify } = require('@furkot/address');

module.exports = {
address,
defaults,
stringify,
removeEmpties,
toObject,
withTimeout
Expand Down
19 changes: 19 additions & 0 deletions test/replay/api.opencagedata.com-443/address
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
GET /geocode/v1/json?q=2200+S.+Jason+St%2C+Denver%2C+CO+80223&no_annotations=1&key=furkot
accept: application/json
accept-encoding: gzip,deflate

HTTP/1.1 200 OK
date: Sun, 11 Feb 2024 21:21:23 GMT
server: Apache
access-control-allow-origin: *
x-ratelimit-limit: 2500
x-ratelimit-remaining: 2365
x-ratelimit-reset: 1707696000
vary: Accept-Encoding
x-frame-options: DENY
x-xss-protection: 1; mode=block
content-type: application/json; charset=utf-8
strict-transport-security: max-age=31536000; includeSubDomains; preload
connection: close

{"documentation":"https://opencagedata.com/api","licenses":[{"name":"see attribution guide","url":"https://opencagedata.com/credits"}],"rate":{"limit":2500,"remaining":2365,"reset":1707696000},"results":[{"bounds":{"northeast":{"lat":39.676586,"lng":-104.999304},"southwest":{"lat":39.676486,"lng":-104.999404}},"components":{"ISO_3166-1_alpha-2":"US","ISO_3166-1_alpha-3":"USA","ISO_3166-2":["US-CO"],"_category":"building","_normalized_city":"Denver","_type":"building","city":"Denver","continent":"North America","country":"United States","country_code":"us","house_number":"2200","postcode":"80223","road":"South Jason Street","state":"Colorado","state_code":"CO"},"confidence":10,"formatted":"2200 South Jason Street, Denver, CO 80223, United States of America","geometry":{"lat":39.676536,"lng":-104.999354}},{"components":{"ISO_3166-1_alpha-2":"US","ISO_3166-1_alpha-3":"USA","ISO_3166-2":["US-CO"],"_category":"building","_normalized_city":"Denver","_type":"building","continent":"North America","country":"United States of America","country_code":"us","house_number":"2200","road":"S Jason St","state":"Colorado","state_code":"CO","town":"Denver"},"confidence":10,"formatted":"2200 S Jason St, Denver, CO, United States of America","geometry":{"lat":39.676659,"lng":-104.999461}},{"bounds":{"northeast":{"lat":39.725665,"lng":-104.98745},"southwest":{"lat":39.660455,"lng":-105.018673}},"components":{"ISO_3166-1_alpha-2":"US","ISO_3166-1_alpha-3":"USA","ISO_3166-2":["US-CO"],"_category":"postcode","_type":"postcode","continent":"North America","country":"United States of America","country_code":"us","county":"Denver County","postcode":"80223","state":"Colorado","state_code":"CO"},"confidence":7,"formatted":"Denver County, CO 80223, United States of America","geometry":{"lat":39.7002,"lng":-105.0028}}],"status":{"code":200,"message":"OK"},"stay_informed":{"blog":"https://blog.opencagedata.com","mastodon":"https://en.osm.town/@opencage"},"thanks":"For using an OpenCage API","timestamp":{"created_http":"Sun, 11 Feb 2024 21:21:23 GMT","created_unix":1707686483},"total_results":3}
2 changes: 2 additions & 0 deletions test/service/geocodio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('geocodio geocoding', function () {
result.places[0].should.deepEqual({
ll: [-87.12502, 39.52365],
address: 'Brazil, IN 47834',
normal: 'Brazil,IN,US',
town: 'Brazil',
county: 'Clay County',
province: 'IN',
Expand Down Expand Up @@ -45,6 +46,7 @@ describe('geocodio geocoding', function () {
result.places[0].should.deepEqual({
ll: [-111.400596, 45.284265],
address: '50 Big Sky Resort Rd, Big Sky, MT 59716',
normal: '50 Big Sky Resort,Big Sky,MT,US',
house: '50',
street: 'Big Sky Resort',
county: 'Madison County',
Expand Down
37 changes: 25 additions & 12 deletions test/service/graphhopper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ describe('graphhopper geocoding', function () {
ll: [-46.8359735, -23.5370962],
type: 'residential',
street: 'Rua Cafelândia',
address: 'Rua Cafelândia, Carapicuíba, São Paulo, Brasil',
address: 'Rua Cafelândia, Carapicuíba, São Paulo, Brazil',
normal: 'Rua Cafelândia,Carapicuíba,São Paulo,BR',
town: 'Carapicuíba',
province: 'São Paulo',
country: 'Brasil'
country: 'Brazil'
});
});

Expand All @@ -42,6 +43,7 @@ describe('graphhopper geocoding', function () {
place: 'SS Sołdek',
type: 'museum',
address: 'Długie Pobrzeże, Gdansk, Pomeranian Voivodeship, Poland',
normal: 'Długie Pobrzeże,Gdansk,Pomeranian Voivodeship,PL',
street: 'Długie Pobrzeże',
province: 'Pomeranian Voivodeship',
town: 'Gdansk',
Expand All @@ -64,14 +66,16 @@ describe('graphhopper geocoding', function () {
type: 'peak',
country: 'USA',
place: 'Main Street Cemetery',
address: 'USA'
address: 'USA',
normal: 'US'
});
result.places[1].should.deepEqual({
ll: [-69.2728254, 44.8350646],
type: 'dam',
country: 'USA',
place: 'Main Street Dam',
address: 'USA'
address: 'USA',
normal: 'US'
});
result.places[2].should.deepEqual({
ll: [-71.086670478147, 42.36274665],
Expand All @@ -82,22 +86,25 @@ describe('graphhopper geocoding', function () {
province: 'MA',
country: 'USA',
place: '325 Main Street',
address: '325 Main Street, Cambridge, MA, USA'
address: '325 Main Street, Cambridge, MA, USA',
normal: '325 Main Street,Cambridge,MA,US'
});
result.places[3].should.deepEqual({
ll: [-71.6192199, 42.5524712],
type: 'dam',
country: 'USA',
place: 'West Main Street Dam',
address: 'USA'
address: 'USA',
normal: 'US'
});
result.places[4].should.deepEqual({
ll: [-10.6756677, 6.5080848],
type: 'hamlet',
province: 'Montserrado County',
country: 'Liberia',
place: 'Main Street',
address: 'Montserrado County, Liberia'
address: 'Montserrado County, Liberia',
normal: 'Montserrado County,LR'
});
});

Expand All @@ -114,6 +121,7 @@ describe('graphhopper geocoding', function () {
place: 'Beryl\'s Restaurant',
type: 'restaurant',
address: 'Woermann St, Swakopmund, Erongo Region, Namibia',
normal: 'Woermann St,Swakopmund,Erongo Region,NA',
street: 'Woermann St',
province: 'Erongo Region',
town: "Swakopmund",
Expand All @@ -136,7 +144,8 @@ describe('graphhopper geocoding', function () {
province: 'MT',
country: 'USA',
place: 'Mountain Mall',
address: 'Black Eagle, MT, USA'
address: 'Black Eagle, MT, USA',
normal: 'Black Eagle,,MT,US'
});
result.places[1].should.deepEqual({
ll: [-111.4011158, 45.2839783],
Expand All @@ -147,7 +156,8 @@ describe('graphhopper geocoding', function () {
province: 'MT',
country: 'USA',
place: 'Big Sky Resort',
address: '50 Big Sky Resort Road, Big Sky, MT, USA'
address: '50 Big Sky Resort Road, Big Sky, MT, USA',
normal: '50 Big Sky Resort Road,Big Sky,MT,US'
});
result.places[2].should.deepEqual({
ll: [-111.40110501870444, 45.284622],
Expand All @@ -156,7 +166,8 @@ describe('graphhopper geocoding', function () {
province: 'MT',
country: 'USA',
place: 'Basecamp',
address: 'Black Eagle, MT, USA'
address: 'Black Eagle, MT, USA',
normal: 'Black Eagle,,MT,US'
});
result.places[3].should.deepEqual({
ll: [-111.4015212, 45.2842756],
Expand All @@ -166,7 +177,8 @@ describe('graphhopper geocoding', function () {
province: 'MT',
country: 'USA',
place: 'Different Spokes',
address: 'Mountain to Meadow, Big Sky, MT, USA'
address: 'Mountain to Meadow, Big Sky, MT, USA',
normal: 'Mountain to Meadow,Big Sky,MT,US'
});
result.places[4].should.deepEqual({
ll: [-111.40158763890292, 45.284196300000005],
Expand All @@ -176,7 +188,8 @@ describe('graphhopper geocoding', function () {
province: 'MT',
country: 'USA',
place: 'Snowcrest Lodge',
address: 'Mountain to Meadow, Big Sky, MT, USA'
address: 'Mountain to Meadow, Big Sky, MT, USA',
normal: 'Mountain to Meadow,Big Sky,MT,US'
});
});
});
6 changes: 4 additions & 2 deletions test/service/hogfish/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ describe('hogfish geocoding', function () {
},
osm: { id: '0000000041fc198163200000' }
},
address: '11005 E Briarwood Ave, Centennial, CO'
address: '11005 E Briarwood Ave, Centennial, CO',
normal: '11005 E Briarwood Ave,Centennial,CO,US'
});
});

Expand Down Expand Up @@ -86,7 +87,8 @@ describe('hogfish geocoding', function () {
url: 'https://www.booking.com/hotel/us/hyatt-house-denver-tech-center.html'
}
},
address: '9280 E Costilla Ave, Englewood, CO, United States'
address: '9280 E Costilla Ave, Englewood, CO, United States',
normal: '9280 E Costilla Ave,Englewood,CO,US'
});
});
});
11 changes: 9 additions & 2 deletions test/service/locationiq/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('locationiq geocoding', function () {
ll: [-46.8359735, -23.5370962],
type: 'road',
address: 'Rua Cafelândia, Carapicuíba, São Paulo, Brazil',
normal: 'Rua Cafelândia,Carapicuíba,São Paulo,BR',
street: 'Rua Cafelândia',
town: 'Carapicuíba',
province: 'São Paulo',
Expand All @@ -41,11 +42,12 @@ describe('locationiq geocoding', function () {
ll: [18.658631239705393, 54.35145095],
place: 'SS Sołdek',
type: 'museum',
address: 'Długie Pobrzeże, Gdańsk, województwo pomorskie, Polska',
address: 'Długie Pobrzeże, Gdańsk, województwo pomorskie, Poland',
normal: 'Długie Pobrzeże,Gdańsk,województwo pomorskie,PL',
street: 'Długie Pobrzeże',
province: 'województwo pomorskie',
town: 'Gdańsk',
country: 'Polska'
country: 'Poland'
});
});

Expand All @@ -67,6 +69,7 @@ describe('locationiq geocoding', function () {
type: 'restaurant',
ll: [-73.9904326, 40.7442736],
address: '30 West 26th Street, New York, NY, USA',
normal: '30 West 26th Street,New York,NY,US',
house: '30',
street: 'West 26th Street',
town: 'New York',
Expand All @@ -78,6 +81,7 @@ describe('locationiq geocoding', function () {
type: 'disused',
ll: [-73.9903515, 40.7442363],
address: '30 West 26th Street, New York, NY, USA',
normal: '30 West 26th Street,New York,NY,US',
house: '30',
street: 'West 26th Street',
town: 'New York',
Expand All @@ -89,6 +93,7 @@ describe('locationiq geocoding', function () {
type: 'company',
ll: [-73.9903727, 40.7442104],
address: '30 West 26th Street, New York, NY, USA',
normal: '30 West 26th Street,New York,NY,US',
house: '30',
street: 'West 26th Street',
town: 'New York',
Expand All @@ -110,6 +115,7 @@ describe('locationiq geocoding', function () {
place: 'Beryl\'s Restaurant',
type: 'restaurant',
address: 'Woermann St, Swakopmund, Erongo Region, Namibia',
normal: 'Woermann St,Swakopmund,Erongo Region,NA',
street: 'Woermann St',
province: 'Erongo Region',
town: "Swakopmund",
Expand All @@ -134,6 +140,7 @@ describe('locationiq geocoding', function () {
type: 'house_number',
ll: [-104.999354, 39.676536],
address: '2200 South Jason Street, Denver, CO, USA',
normal: '2200 South Jason Street,Denver,CO,US',
house: '2200',
street: 'South Jason Street',
town: 'Denver',
Expand Down
34 changes: 30 additions & 4 deletions test/service/opencage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@ describe('opencage geocoding', function () {
type: 'road',
place: 'Rua Cafelândia',
address: 'Rua Cafelândia, Parque José Alexandre, Carapicuíba - SP, 06321-665, Brazil',
normal: 'Rua Cafelândia,Carapicuíba,São Paulo,BR',
street: 'Rua Cafelândia',
town: 'Carapicuíba',
county: 'Região Metropolitana de São Paulo',
province: 'SP',
province: 'São Paulo',
country: 'Brazil'
});
result.places[1].should.deepEqual({
ll: [-46.835, -23.52272],
type: 'city',
address: 'Carapicuíba, Brazil',
normal: 'Carapicuíba,São Paulo,BR',
town: 'Carapicuíba',
county: 'Carapicuíba',
province: 'SP',
province: 'São Paulo',
country: 'Brazil'
});
});
Expand All @@ -53,11 +55,12 @@ describe('opencage geocoding', function () {
place: 'SS Sołdek',
type: 'museum',
address: 'Długie Pobrzeże, 80-838 Gdańsk, Polska',
normal: 'Długie Pobrzeże,Gdańsk,województwo pomorskie,PL',
street: 'Długie Pobrzeże',
community: 'Wyspa Spichrzów',
town: 'Gdańsk',
province: '22',
country: 'Polska'
province: 'województwo pomorskie',
country: 'Poland'
});
});

Expand All @@ -74,9 +77,32 @@ describe('opencage geocoding', function () {
place: 'Beryl\'s Restaurant',
type: 'restaurant',
address: 'Woermann St, Swakopmund 22001, Namibia',
normal: 'Woermann St,Swakopmund,,NA',
street: 'Woermann St',
town: 'Swakopmund',
country: 'Namibia'
});
});

it('address', async function () {

const query = {
address: '2200 S. Jason St, Denver, CO 80223',
partial: true
};
const result = await geocode('forward', 10, query);
should.exist(result);
result.should.have.property('places').with.length(3);
result.places[0].should.deepEqual({
type: 'building',
ll: [-104.999354, 39.676536],
address: '2200 South Jason Street, Denver, CO 80223, USA',
normal: '2200 South Jason Street,Denver,CO,US',
house: '2200',
street: 'South Jason Street',
town: 'Denver',
province: 'CO',
country: 'USA'
});
});
});
Loading

0 comments on commit c469e5e

Please sign in to comment.