I'm using GeoDjango with PostGIS and trying to use a polygon to get records from a database which fall inside it.
If I define a polygon which is bigger than half the area of the earth it assumes the 'inside' of my polygon is the smaller area which I intended as the '开发者_运维技巧outside' and returns only results which are outside it.
I can just use this smaller, wrong area to exclude results. Polygon.area seems to know what I intend so I can use this to determine when to make my search inclusive or exclusive. I feel like this problem is probably common, is there a better way to solve it?
Update: If 180 degrees longitude is inside my polygon this doesn't work at all. It seems GEOS is to blame this time. This image shows what I believe is the reason. Green is the polygon I define, Red is how it seems to be interpreting it.
Again this seems like a problem which would crop up often and one that libraries like GEOS are made to deal with. Is there a way?Alright, no answers. Here's what I've done.
Because GEOS doesn't like things crossing the 180th meridian:
First check if the polygon crosses the 180th meridian - If so, break it into 2 polygons along that line.
Because PostGIS assumes a polygon is as small as possible you can't make one cover more than half the world, so:
Check if the polygon or each of the split polygons covers half the world or more - If so, break them in half.
Construct a MultiPolygon from the results.
精彩评论