Reputation: 21
I have two django models:
class Region(models.Model):
geometry = models.PolygonField()
class Position(models.Model):
coordinates = models.PointField()
I am trying to check if a Position is geographically contained inside a Region:
def check(region, position):
return position.coordinates.intersect(region.geometry)
But it always return False, even when the Position is contained inside the Region (I am rendering both the PointField and the RegionField with django-leaflet). I also tried using:
def check(region, position):
return position.coordinates.within(region.geometry)
but no results so far. Here is the test data that I am using (geojson):
{"coordinates": [46.2071762, 11.1245718], "type": "Point"}
{"coordinates": [[[11.102371215820312, 46.21939582902924], [11.106491088867188, 46.22111800038881], [11.134214401245117, 46.22188999070486], [11.140050888061523, 46.21791115519151], [11.141080856323242, 46.21422899084459], [11.137990951538086, 46.207695510993354], [11.13412857055664, 46.20122065978115], [11.12485885620117, 46.198844376182535], [11.102371215820312, 46.21939582902924]]], "type": "Polygon"}
Any hint on what the problem could be? Thanks in advance!
Upvotes: 1
Views: 1168
Reputation: 461
Your mistake is logical, I guess.Point's latitude and longitude should be swapped. I mean
{"coordinates": [11.1245718, 46.2071762], "type": "Point"}
Instead of
{"coordinates": [46.2071762, 11.1245718], "type": "Point"}
Upvotes: 3