Created
November 5, 2012 03:43
-
-
Save mheadd/4015200 to your computer and use it in GitHub Desktop.
Node.js modules for City of Philadelphia geolocation and polling location APIs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('./phlgeolocate'); | |
require('./phlfindpolls'); | |
var address = process.argv[2] || '1234 Market Street'; | |
var geo = new PHLgeolocate(); | |
geo.getCoordinates(address, function(coords) { | |
var options = {}; | |
options.outFields = ['WARD_1,DIVISION_1,POLLING_PL,ADDRESS,PARKING_AC,BUILDING_A']; | |
var poll = new PHLfindpolls(options); | |
poll.findLocation(coords[0].latitude, coords[0].longitude, function(result) { | |
for(item in result[0].attributes) { | |
console.log(item + ': ' + result[0].attributes[item]); | |
} | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
PHLfindpolls = function(options) { | |
var geometryType = options.geometryType || 'esriGeometryPoint'; | |
var inSR = options.inSR || 4326; | |
var spatialRel = options.spatialRel || 'esriSpatialRelWithin'; | |
var returnCountOnly = options.returnCountOnly || 'false'; | |
var returnIdsOnly = options.returnIdsOnly || 'false' | |
var returnGeometry = options.returnGeometry || 'false'; | |
var f = options.f || 'pjson'; | |
this.responseBody; | |
this.apiHost = 'http://gis.phila.gov/ArcGIS/rest/services/PhilaGov'; | |
this.locationPath = '/PollingPlaces/MapServer/1/query'; | |
this.settings = { | |
geometryType: geometryType, | |
inSR: inSR, | |
spatialRel: spatialRel, | |
returnCountOnly: returnCountOnly, | |
returnIdsOnly: returnIdsOnly, | |
returnGeometry: returnGeometry, | |
outFields: encodeURI(options.outFields.join(',')), | |
f: f | |
}; | |
}; | |
PHLfindpolls.prototype.findLocation = function(latitude, longitude, callback) { | |
var url = this.apiHost + this.locationPath; | |
url += '?'; | |
for(item in this.settings) { | |
url += item + '=' + encodeURI(this.settings[item]) + '&'; | |
} | |
url += 'geometry={"x":"' + longitude + '","y":"' + latitude + '"}'; | |
this.makeApiCall(url, callback); | |
}; | |
PHLfindpolls.prototype.makeApiCall = function (url, callback) { | |
var self = this; | |
self.responseBody = ''; | |
http.get(url, function(res) { | |
res.setEncoding('utf8'); | |
res.on('data', function(chunk) { | |
self.responseBody += chunk; | |
}); | |
res.on('end', function(){ | |
var result = JSON.parse(self.responseBody); | |
callback(result.features); | |
}); | |
}); | |
}; | |
exports.PHLfindpolls = PHLfindpolls; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('./phlgeolocate'); | |
var address = process.argv[2] || '1234 Market Street'; | |
var geo = new PHLgeolocate(); | |
geo.getCoordinates(address, function(result) { | |
console.log(result); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
PHLgeolocate = function() { | |
this.geoHost = 'http://services.phila.gov'; | |
this.locationPath = '/ULRS311/Data/Location/'; | |
this.liAddressKeyPath = '/ULRS311/Data/LIAddressKey/'; | |
this.minConfidence = 85; | |
this.responseBody; | |
}; | |
PHLgeolocate.prototype.getCoordinates = function (address, callback) { | |
var url = this.geoHost + this.locationPath + encodeURI(address); | |
this.makeApiCall(url, callback); | |
}; | |
PHLgeolocate.prototype.getAddressKey = function (address, callback) { | |
var url = this.geoHost + this.liAddressKeyPath + encodeURI(address); | |
this.makeApiCall(url, callback); | |
}; | |
PHLgeolocate.prototype.makeApiCall = function (url, callback) { | |
var self = this; | |
self.responseBody = ''; | |
http.get(url, function(res) { | |
res.setEncoding('utf8'); | |
res.on('data', function(chunk) { | |
self.responseBody += chunk; | |
}); | |
res.on('end', function(){ | |
var result = self.parseResponse(JSON.parse(self.responseBody)); | |
callback(result); | |
}); | |
}); | |
}; | |
PHLgeolocate.prototype.parseResponse = function (result) { | |
var self = this; | |
locations = []; | |
for(var i=0; i<result.Locations.length; i++) { | |
var location = result.Locations[i] | |
if(location.Address.Similarity >= self.minConfidence) { | |
var geometry = { address: location.Address.StandardizedAddress, similarity: location.Address.Similarity, latitude: location.YCoord, longitude: location.XCoord }; | |
locations.push(geometry); | |
} | |
} | |
return locations; | |
}; | |
exports.PHLgeolocate = PHLgeolocate; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
~$ node sample.js "20 N. Third Street"
WARD_1: 5
DIVISION_1: 16
POLLING_PL: OLD FIRST REFORMED CHURCH
ADDRESS: 330 RACE ST
PARKING_AC: N
BUILDING_A: R