Skip to content

Instantly share code, notes, and snippets.

@calvinmetcalf
Last active December 15, 2015 09:39
Show Gist options
  • Save calvinmetcalf/5239767 to your computer and use it in GitHub Desktop.
Save calvinmetcalf/5239767 to your computer and use it in GitHub Desktop.
express server bounding boxes

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
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment