public
Last active

Node.js modules for City of Philadelphia geolocation and polling location APIs

  • Download Gist
phlfindpolls-sample.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
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]);
}
});
 
});
phlfindpolls.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
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;
phlgeolocate-sample.js
JavaScript
1 2 3 4 5 6 7 8
require('./phlgeolocate');
 
var address = process.argv[2] || '1234 Market Street';
 
var geo = new PHLgeolocate();
geo.getCoordinates(address, function(result) {
console.log(result);
});
phlgeolocate.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
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;

~$ 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.