[SERVER-20672] Validation of 2dsphere Index does not conform to GeoJSON specification Created: 28/Sep/15  Updated: 21/Oct/15  Resolved: 20/Oct/15

Status: Closed
Project: Core Server
Component/s: Geo
Affects Version/s: 3.0.5
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Nima Mehrafshan Assignee: Siyuan Zhou
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PDF File Sample Polygon.pdf    
Operating System: ALL
Steps To Reproduce:

Create database "regions", then:

db.regions.insert(
	{
		"location":
		{ "type": "MultiPolygon", 
		    "coordinates":[
		        [[[1,1],[1.5,1],[2,1],[1.5,2],[1.5,1],[1,2],[1,1]]]
		    ]
		}
	}
)
db.regions.createIndex( { location: "2dsphere" } )

Sprint: RPL A (10/09/15), Repl B (10/30/15)
Participants:

 Description   

I am trying to import data on German administrative regions (from www.bkg.bund.de) into a mongoDB. The data contains borders as GeoJSON conforming MultiPolygons (confirmed by geojsonlint.com). Howerver, when trying to create a 2dsphere index on data containing MultiPolygons with duplicate vertices or self intersections (e. g. the state of Bavaria), mongoDB will through an error.

In the issues SERVER-16238 and SERVER-13735 there are several mentions of duplicate vertices or self intersections within MultiPolygons being invalid according to the GeoJSON spec. Therefore, these bug reports have been resolved as "Works as Designed". However, I do not find these restrictions in the GeoJSON spec. I also don't find mentions of restrictions of the 2dsphere index that go beyond the GeoJSON spec in the mongoDB docs.

Are these restrictions necessary or could they be removed in future versions? A lot of valid geospatial data out there will not be usable in mongoDB with these restrictions, and many people seem to have noticed. See, also:
http://gis.stackexchange.com/questions/135871/sqlserver-shapefile-export-wont-load-in-mongodb-due-to-self-intersection
http://stackoverflow.com/questions/24398292/mongodb-version-2-6-still-fails-on-2dsphere-geoindexing-citing-inability-to-extr?rq=1
http://stackoverflow.com/questions/23290114/mongodb-malformed-geometry-when-creating-a-geosphere-index?noredirect=1#comment35654181_23290114



 Comments   
Comment by Siyuan Zhou [ 09/Oct/15 ]

Thanks for the detailed description. The links you cited have different issues, so I'll just focus on your case.

GeoJSON specifies the syntax, not all the semantics. For example, it doesn't define the winding order of a polygon, i.e. which side of a polygon on the Earth is the inner. MongoDB assumes the smaller part is the inner, unless you use big polygon explicitly. MongoDB and its geospatial library do have some restrictions, e.g. duplicate vertices or self intersections are not allowed, but for good reasons. Internally, MongoDB follows left-hand rule in terms of winding order and converts the smaller polygon defined by the coordinates to this form. Imagine you are walking along the edges in the given order, the inner is always on you left. Now It's obvious to see why self-intersection is ambiguous. What's on your left when walking along the edges of an "8" shape with one stroke? Duplicated vertices have the same problem.

On the other hand, these restrictions don't affect expressiveness. By separating the self-intersected polygon into two polygons using MultiPolygon, one can define the same shape without ambiguity. In your case, the MultiPolygon could be as follow.

{
  "type": "MultiPolygon",
  "coordinates": [
    [[[1, 1], [1.5, 1], [1, 2], [1, 1]]],
    [[[1.5, 1], [2, 1], [1.5, 2], [1.5, 1]]]
  ]
}

Generated at Thu Feb 08 03:54:56 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.