Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store GeoJSON polygons in MongoDB

I have the following problem with MongoDB. I got some geo data from my home country and i have to store them into mongodb to set up a simple Web Feature Service. This service will mostly do bounding box queries using the $within operator. The data is in GeoJSON format. Therefore i imported at first the Villages and Cities which are represented as points ([1,2]) in this format. No problem. Next step rivers and streets which are LineStrings and according to GeoJSON represented this way [[1,2],[3,4]]. But when importing the districts (which are in fact polygon and according to the GeoJSON specification 3 dim arrrays) i got the error geo values have to be numbers when creating the index.

db.collection.ensureIndex({"geometry.coordinates" : "2d"});

All data are valid GeoJSON, and are in plain 2d coordinates in EPSG:4326 projection.

Does anybody have an idea?

like image 217
Dominik Nöger Avatar asked Jun 21 '12 11:06

Dominik Nöger


People also ask

Does MongoDB support GeoJSON?

Overview. MongoDB supports the GeoJSON object types listed on this page. a field named coordinates that specifies the object's coordinates.

What is the preferred format to store geospatial data in MongoDB?

In MongoDB, you can store geospatial data as GeoJSON objects or as legacy coordinate pairs.

Is GeoJSON Lat Long or Long LAT?

Nothing is in the wrong order. Leaflet uses lat-lng (or northing-easting ) whereas GeoJSON uses lng-lat (or easting-northing ).

What is a Multipolygon GeoJSON?

A multiPolygon is an array of Polygon coordinate arrays. This adheres to the RFC 7946 internet standard when serialized into JSON. When deserialized, this class becomes an immutable object which should be initiated using its static factory methods.


1 Answers

With MongoDB 2.4 use the "2dsphere" index for GeoJSON Points, LineStrings and Polygons.

For example, you can create this index:

db.mycoll.ensureIndex( { loc : "2dsphere" } )

And store this LineString:

{ loc : { type : "LineString" , coordinates : [ [ 1 , 2 ] , [ 3 , 4 ] ] } }

See http://docs.mongodb.org/manual/applications/2dsphere/.

like image 78
Bob Grabar Avatar answered Sep 19 '22 01:09

Bob Grabar