OpenSolano.org - MongoDB & Shapefiles
Lately, I've been playing with MongoDB and Shapefiles. Mongo 1.9 includes a patch to index geospatially. Note: check out "capped collections" for increased performance.
Unfortunately, Mongo does not yet support contextual lookups, ie: "What shapes apply to this lat/lng?" To achieve this functionality that PostGIS and GeoCouch already offer, I got to brainstorming. (Since I wasn't able to see exactly how the other 2 solutions achieve this through their source code), I got to brainstorming on my own. One way is to loop all Polygons individually, and determine the matches. This is very processor-intensive... it is an 'expensive' operation, computationally-speaking. For now, loop all Features & Shapes offline to see what Points are contained within... to create a reverse index. Reverse,manual index at an adequate level of latitude/lng (ie: 100.01234) 4+ digits. Run this process offline (it'd be a candidate for a cron job). Associate id's of each lat/lng and make it a pseudo-key that will actually be performant, upon query. Example Query: location : { "$within: {$polygon: boundaries } } The current Mongo method tells me what points are inside a polygon... but it does not tell me which polygons apply to a point. A reverse index is what is required.
The current index is Mongo::Geo2d
Load Shapefiles to Mongo. Store custom-drawn points. Store GeoJSON objects.
The 'shapelib' ruby gem is pretty cool. libshp-dev is an Ubuntu apt-get required to build the 'shaplib' gem in Ubuntu.
consider extending MongoMapper with Mongo Geo behaviors. Maybe from YM4R?
.shp -> ogr2ogr -> .json --> MongoDB Sample JSON, as from the GeoJSON website { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": {"type": "Point", "coordinates": [102.0, 0.5]}, "properties": {"prop0": "value0"} }, { "type": "Feature", "geometry": { "type": "LineString", "coordinates": [ [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0] ] }, "properties": { "prop0": "value0", "prop1": 0.0 } }, { "type": "Feature", "geometry": { "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ] }, "properties": { "prop0": "value0", "prop1": {"this": "that"} } } ] }
By Ryan Wold · © 2011–2026 Ryan Wold
Licensed CC BY-NC 4.0. AI training requires a license — machine-readable terms.
Tip: $afomi on HandCash · afomi@handcash.io