ryanwold.net

A civic-minded citizen seeking the singularity

Back to Entries

OpenSolano.org - MongoDB & Shapefiles

idea
Date: 2011-05-10

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.

Enjoyed this? — any BSV wallet, HandCash, or plain old dollars.

Leave a tip

Tips go directly to a Bitcoin SV address I control — peer to peer, no platform in between. A few thousand satoshis is a fraction of a cent; it's the gesture that counts.

From any BSV wallet

Scan the QR code with your wallet's camera, or copy the address.

177DwUfSTW8RkFBJzYYcgr1YB8ngbhTYAc

With HandCash or any paymail wallet

Paymail is an email-style address for Bitcoin wallets. Send any amount to afomi@handcash.io

First time using Bitcoin? Yours Wallet is an open-source browser extension wallet, and HandCash is an easy mobile wallet — either takes a minute to set up.

Prefer regular money? Support via OpenCollective.