MongoDB and geodata part 2 – getting spatial

After importing geodata from the GIS to MongoDB and creating a spatial index (part 1), the exciting (spatial) adventure starts. With “normal” (relational) databases and their spatial extensions (Oracle spatial, PostgreSQL/PostGIS, SQLite/Spatialite,…) a lot of spatial queries and geoprocessing are possible. So let’s try to find out which adresses have to be evacuated 250m around some “event”…

QGIS_Adressen_Umkreis
How to perform a spatial query like this with MongoDB ?

MongoDB (V 3.2) already supports basic spatial queries like intersections, proximity and some more.

Our “event” takes place at 12.75982E/46.84674N and all objects within a distance of 250m have to be evacuated:

> db.adrtirol.find({geometry : { $near : { coordinates : [12.75982, 46.84674]}, $maxDistance: 250 } }  )

MongoUmkreisCLI

Instead of $near (Proximity) we could also use the $geoWithin-Operator (Distance in radians!):

> db.adrtirol.find({geometry : { $geoWithin : { $center : [[LON,LAT],Distance in radians]}, } } )

In case we want to know the distance of each object to the event:

> db.runCommand({ geoNear: "adrtirol", near: [12.75982, 46.84674], spherical: true, maxDistance: RADIANS!})

MongoUmkreisDistance

MongoDB’s documentation on geospatial query operators is very helpful. Browse through it and you will find some nice geo-features within MongoDB. MongoDB as a direct spatial datasource (incl. editing) in GIS would be an important step towards the classical Desktop-GIS-community. In some scenarios MongoDB is a performance-beast, so maybe it could be interesting for organizations with large amounts of spatial data.