Use yandex geocode API to get data (json array of object) on set of input addresses in strings. Like http://geocode-maps.yandex.ru/1.x/?format=json&geocode=petach%20tikva&kind=locality&lang=en-US.
Created
May 10, 2014 21:40
-
-
Save sudodoki/99f7adf5df13c251ff79 to your computer and use it in GitHub Desktop.
Batch geolocate using Yandex geolocator
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
/* jshint strict: false */ | |
/* global process, console, require */ | |
var fs = require('fs'); | |
var request = require('request'); | |
var typeNeeded = process.argv[2] || 'locality'; | |
var source = process.argv[3] || './payload.json'; | |
var output = process.argv[4] || 'output.json'; | |
var arrayOfNames = require(source); | |
var toDo = arrayOfNames.length, | |
done = 0, | |
resultSet = []; | |
console.log('Will take names in ', source, ' and search for ', typeNeeded, ' type in features'); | |
console.log('result will be outputed to ', output); | |
function getListOfFeatures(body) { | |
var result; | |
try { | |
result = body.response.GeoObjectCollection.featureMember; | |
} catch (e) { | |
console.log('Errored at ', body.response); | |
result = []; | |
} | |
return result; | |
} | |
function getTypeOfFeature(feature) { | |
return feature.GeoObject.metaDataProperty.GeocoderMetaData.kind; | |
} | |
function featureByType(feature) { | |
return getTypeOfFeature(feature) === typeNeeded; | |
} | |
arrayOfNames.forEach(function(name) { | |
var url = 'http://geocode-maps.yandex.ru/1.x/?format=json&geocode=%NAME%&kind=%TYPENEEDED%&lang=en-US' | |
.replace('%NAME%', encodeURIComponent(name)) | |
.replace('%TYPENEEDED%', encodeURIComponent(typeNeeded)); | |
request(url, function(error, response, body) { | |
done++; | |
if (error) return console.error('Error occurred: ', error.message); | |
if (!error & response.statusCode == 200) { | |
resultSet = resultSet.concat(getListOfFeatures(JSON.parse(body)).filter(featureByType)); | |
} | |
if (toDo == done) { | |
fs.writeFile(output, JSON.stringify(resultSet, null, 2)); | |
} | |
}); | |
}); |
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
{ | |
"name": "getLatLong", | |
"version": "0.0.0", | |
"description": "", | |
"main": "index.js", | |
"author": "sudodoki <smd.deluzion@gmail.com>", | |
"license": "MIT", | |
"dependencies": { | |
"request": "~2.34.0" | |
} | |
} |
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
[ | |
"eilat", | |
"ashdod", | |
"ashkelon", | |
"beer sheva", | |
"beit shean", | |
"zichron yaakov", | |
"hadera", | |
"haifa", | |
"tveria", | |
"jerusalem", | |
"lod", | |
"metula", | |
"naharia", | |
"natzrat", | |
"netanya", | |
"sdom", | |
"ein gedi", | |
"afula", | |
"arad", | |
"petach tikva", | |
"tzfat", | |
"rehovot", | |
"ramla", | |
"tel aviv" | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment