Skip to content

Instantly share code, notes, and snippets.

@tai2
Created October 29, 2014 08:49
Show Gist options
  • Select an option

  • Save tai2/1690e9644f6adcf5a319 to your computer and use it in GitHub Desktop.

Select an option

Save tai2/1690e9644f6adcf5a319 to your computer and use it in GitHub Desktop.
var assert = require('assert');
var fs = require('fs');
var util = require('util');
var path = require('path');
var sprintf = require('sprintf-js').sprintf;
var jsonfile = require('jsonfile');
var Terraformer = require('terraformer');
var GeoStore = require('terraformer-geostore').GeoStore;
var RTree = require('terraformer-rtree').RTree;
var Memory = require('terraformer-geostore-memory').Memory;
var dirname = './geojson-2';
var files = fs.readdirSync(dirname);
var store = new GeoStore({
store: new Memory(),
index: new RTree()
});
var start;
start = Date.now();
files.forEach(function(filename) {
var geojson;
if (path.extname(filename) === '.json') {
geojson = jsonfile.readFileSync(dirname + '/' + filename);
appendId(geojson);
store.add(geojson, function (err, res) {
if (err) {
console.error(err);
}
});
}
});
console.log(Date.now() - start);
start = Date.now();
lookupTest();
console.log(Date.now() - start);
function appendId(geojson) {
if (geojson.type === 'FeatureCollection') {
geojson.features.forEach(function(feature) {
feature.id = feature.properties.iso_3166_2;
});
} else if (geojson.type === 'Feature') {
geojson.id = feature.properties.iso_3166_2;
}
}
function lookupTest() {
var lat, long;
for (lat = -180; lat < 180; lat++) {
for (long = -90; long < 90; long++) {
store.contains({
type : 'Point',
coordinates : [lat, long]
}, function(err, res) {
if (err) {
console.log(err);
} else {
// if (0 < res.length) {
// console.log(res[0].id, res[0].properties.gns_name, ' lat=' + lat + ' long=' + long);
// } else {
// console.log('Not found', ' lat=' + lat + ' long=' + long);
// }
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment