Skip to content

Commit

Permalink
Merge branch 'master' of github.com:conveyal/isomorphic-mapzen-search
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorgerhardt committed Jan 31, 2017
2 parents a02f3b5 + dab6eef commit 4f066cd
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ lib-cov

# Coverage directory used by tools like istanbul
coverage
tmp

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
Expand All @@ -25,6 +26,8 @@ build/Release

# Dependency directory
node_modules

# built files
build/*

# Optional npm cache directory
Expand Down
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.DS_Store
tmp
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ notifications:
node_js:
- '6'
before_install:
- npm i -g npm@^3.0.0
- npm i -g codecov
before_script:
- npm prune
script:
- npm run lint
- npm run cover
- codecov
after_success:
- npm run semantic-release
branches:
Expand Down
66 changes: 66 additions & 0 deletions __tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
exports[`reverse should successfully reverse geocode and format 1`] = `
Array [
Object {
"accuracy": "centroid",
"address": "Takoma, Takoma Park, MD, USA",
"confidence": 1,
"country": "United States",
"country_a": "USA",
"country_gid": "whosonfirst:country:85633793",
"county": "Montgomery County",
"county_gid": "whosonfirst:county:102082719",
"distance": 0,
"gid": "whosonfirst:neighbourhood:85851759",
"id": "85851759",
"label": "Takoma, Takoma Park, MD, USA",
"latlng": Object {
"lat": 38.976745,
"lon": -77.023104,
},
"layer": "neighbourhood",
"locality": "Takoma Park",
"locality_gid": "whosonfirst:locality:85949501",
"name": "Takoma",
"neighbourhood": "Takoma",
"neighbourhood_gid": "whosonfirst:neighbourhood:85851759",
"region": "Maryland",
"region_a": "MD",
"region_gid": "whosonfirst:region:85688501",
"source": "whosonfirst",
"source_id": "85851759",
},
]
`;

exports[`search should successfully geocode and format 1`] = `
Array [
Object {
"accuracy": "centroid",
"address": "Takoma, Takoma Park, MD, USA",
"confidence": 0.965,
"country": "United States",
"country_a": "USA",
"country_gid": "whosonfirst:country:85633793",
"county": "Montgomery County",
"county_gid": "whosonfirst:county:102082719",
"gid": "whosonfirst:neighbourhood:85851759",
"id": "85851759",
"label": "Takoma, Takoma Park, MD, USA",
"latlng": Object {
"lat": 38.976745,
"lon": -77.023104,
},
"layer": "neighbourhood",
"locality": "Takoma Park",
"locality_gid": "whosonfirst:locality:85949501",
"name": "Takoma",
"neighbourhood": "Takoma",
"neighbourhood_gid": "whosonfirst:neighbourhood:85851759",
"region": "Maryland",
"region_a": "MD",
"region_gid": "whosonfirst:region:85688501",
"source": "whosonfirst",
"source_id": "85851759",
},
]
`;
54 changes: 54 additions & 0 deletions __tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* globals describe, expect, it */

const nock = require('nock')

const geocoder = require('../index.js')
const mockReverseResult = require('./mock-reverse-result.json')
const mockSearchResult = require('./mock-search-result.json')
const mockKey = 'test-key'

describe('search', () => {
const searchQuery = '123 abc st'

it('should successfully geocode', async () => {
nock('https://search.mapzen.com/')
.get(/v1\/search/)
.reply(200, mockSearchResult)

const result = await geocoder.search(mockKey, searchQuery)
expect(result.features[0].geometry.coordinates[0]).toEqual(-77.023104)
})

it('should successfully geocode and format', async () => {
nock('https://search.mapzen.com/')
.get(/v1\/search/)
.reply(200, mockSearchResult)

const result = await geocoder.search(mockKey, searchQuery, { format: true })
expect(result).toMatchSnapshot()
expect(result[0].address).toEqual('Takoma, Takoma Park, MD, USA')
})
})

describe('reverse', () => {
const reverseQuery = { lat: 38.976745, lng: -77.023104 }

it('should successfully reverse geocode', async () => {
nock('https://search.mapzen.com/')
.get(/v1\/reverse/)
.reply(200, mockReverseResult)

const result = await geocoder.reverse(mockKey, reverseQuery)
expect(result.features[0].geometry.coordinates[0]).toEqual(-77.023104)
})

it('should successfully reverse geocode and format', async () => {
nock('https://search.mapzen.com/')
.get(/v1\/reverse/)
.reply(200, mockReverseResult)

const result = await geocoder.reverse(mockKey, reverseQuery, { format: true })
expect(result).toMatchSnapshot()
expect(result[0].address).toEqual('Takoma, Takoma Park, MD, USA')
})
})
71 changes: 71 additions & 0 deletions __tests__/mock-reverse-result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"geocoding": {
"version": "0.2",
"attribution": "https://search.mapzen.com/v1/attribution",
"query": {
"size": 10,
"private": false,
"point.lat": 38.976745,
"point.lon": -77.023104,
"boundary.circle.radius": 1,
"boundary.circle.lat": 38.976745,
"boundary.circle.lon": -77.023104,
"querySize": 20
},
"engine": {
"name": "Pelias",
"author": "Mapzen",
"version": "1.0"
},
"timestamp": 1481582055662
},
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-77.023104,
38.976745
]
},
"properties": {
"id": "85851759",
"gid": "whosonfirst:neighbourhood:85851759",
"layer": "neighbourhood",
"source": "whosonfirst",
"source_id": "85851759",
"name": "Takoma",
"confidence": 1,
"distance": 0,
"accuracy": "centroid",
"country": "United States",
"country_gid": "whosonfirst:country:85633793",
"country_a": "USA",
"region": "Maryland",
"region_gid": "whosonfirst:region:85688501",
"region_a": "MD",
"county": "Montgomery County",
"county_gid": "whosonfirst:county:102082719",
"locality": "Takoma Park",
"locality_gid": "whosonfirst:locality:85949501",
"neighbourhood": "Takoma",
"neighbourhood_gid": "whosonfirst:neighbourhood:85851759",
"label": "Takoma, Takoma Park, MD, USA"
},
"bbox": [
-77.017242,
38.973896,
-77.012062,
38.980594
]
}
],
"bbox": [
-77.023234,
38.973896,
-77.012062,
38.980594
]
}
75 changes: 75 additions & 0 deletions __tests__/mock-search-result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"geocoding": {
"version": "0.2",
"attribution": "https://search.mapzen.com/v1/attribution",
"query": {
"text": "takoma",
"size": 10,
"sources": [
"geonames",
"openaddresses",
"openstreetmap",
"whosonfirst"
],
"private": false,
"boundary.circle.radius": 25,
"boundary.circle.lat": 38.8886,
"boundary.circle.lon": -77.043,
"querySize": 20
},
"engine": {
"name": "Pelias",
"author": "Mapzen",
"version": "1.0"
},
"timestamp": 1479272483487
},
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-77.023104,
38.976745
]
},
"properties": {
"id": "85851759",
"gid": "whosonfirst:neighbourhood:85851759",
"layer": "neighbourhood",
"source": "whosonfirst",
"source_id": "85851759",
"name": "Takoma",
"confidence": 0.965,
"accuracy": "centroid",
"country": "United States",
"country_gid": "whosonfirst:country:85633793",
"country_a": "USA",
"region": "Maryland",
"region_gid": "whosonfirst:region:85688501",
"region_a": "MD",
"county": "Montgomery County",
"county_gid": "whosonfirst:county:102082719",
"locality": "Takoma Park",
"locality_gid": "whosonfirst:locality:85949501",
"neighbourhood": "Takoma",
"neighbourhood_gid": "whosonfirst:neighbourhood:85851759",
"label": "Takoma, Takoma Park, MD, USA"
},
"bbox": [
-77.017242,
38.973896,
-77.012062,
38.980594
]
}
],
"bbox": [
-77.017897,
38.904,
-76.978642,
39.001084
]
}
28 changes: 14 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import lonlat from '@conveyal/lonlat'
import fetch from 'isomorphic-fetch'
import lonlng from 'lonlng'
import qs from 'qs'

const mapzenUrl = 'https://search.mapzen.com/v1'
Expand All @@ -23,38 +23,38 @@ export function search (apiKey, text, {
}

if (focusLatlng) {
const {lat, lng} = lonlng(focusLatlng)
const {lat, lon} = lonlat(focusLatlng)
query['focus.point.lat'] = lat
query['focus.point.lon'] = lng
query['focus.point.lon'] = lon
}

if (boundary) {
if (boundary.country) query['boundary.country'] = boundary.country
if (boundary.rect) {
const min = lonlng(boundary.rect.minLatlng)
const max = lonlng(boundary.rect.maxLatlng)
const min = lonlat(boundary.rect.minLatlng)
const max = lonlat(boundary.rect.maxLatlng)
query['boundary.rect.min_lat'] = min.lat
query['boundary.rect.min_lon'] = min.lng
query['boundary.rect.min_lon'] = min.lon
query['boundary.rect.max_lat'] = max.lat
query['boundary.rect.max_lon'] = max.lng
query['boundary.rect.max_lon'] = max.lon
}
if (boundary.circle) {
const {lat, lng} = lonlng(boundary.circle.latlng)
const {lat, lon} = lonlat(boundary.circle.latlng)
query['boundary.circle.lat'] = lat
query['boundary.circle.lon'] = lng
query['boundary.circle.lon'] = lon
query['boundary.circle.radius'] = boundary.circle.radius
}
}

return run(searchUrl, query, format)
}

export function reverse (apiKey, latlng, {format} = {}) {
const {lng, lat} = lonlng(latlng)
export function reverse (apiKey, latlon, {format} = {}) {
const {lon, lat} = lonlat(latlon)
return run(reverseUrl, {
api_key: apiKey,
'point.lat': lat,
'point.lon': lng
'point.lon': lon
}, format)
}

Expand All @@ -69,7 +69,7 @@ function split ({
properties
}) {
return Object.assign({}, properties, {
address: `${properties.label} ${properties.postalcode}`,
latlng: lonlng.fromCoordinates(geometry.coordinates)
address: `${properties.label}${properties.postalcode ? ' ' + properties.postalcode : ''}`,
latlng: lonlat.fromCoordinates(geometry.coordinates)
})
}
Loading

0 comments on commit 4f066cd

Please sign in to comment.