public
Last active

express server bounding boxes

  • Download Gist
README.md
Markdown

how to set up a quick server that gives you geojson based on a bounding box, your going to need to remember to install express and rtree with

npm install rtree express
server.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
var express = require('express');
var rtree = require('rtree');
var yourData = require("./path/to/geoJson.json");
var app = express();
app.use(express.compress());
var yourIndex = rtree();
yourIndex.geoJSON(yourData);
app.get('/path', function(req, res){
var bbox,bboxParam;
if(req.query.bbox){
bbox = req.query.bbox.split(",");
bboxParam = [[parseFloat(bbox[0],10),parseFloat(bbox[1],10)],[parseFloat(bbox[2],10),parseFloat(bbox[3],10)]]
yourIndex.bbox(bboxParam, function(err,result){
res.jsonp({"type":"FeatureCollection","features":result});
});
}else{
res.jsonp(yourData);
}
});
app.listen(3000);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.