Reputation: 31
I have a model which is based on a shapefile:
My model is: from django.contrib.gis.db import models
class RegionBorder(models.Model):
region_code = models.CharField(max_length=9)
region_name = models.CharField(max_length=24)
mpoly = models.MultiPolygonField(srid=27700)
objects = models.GeoManager()
def __str__(self):
return self.region_name
I've successfully imported the polygons into my database:
len(RegionBorder.objects.all())
9
But when I try to find a point within the polygons, I get nothing back. I know the point must be in the polygon as they're maps of the regions of England and I'm trying to find Nelsons column bang in the middle of London.
p = GEOSGeometry("Point(-0.127907 51.507786)", srid=27700)
RegionBorder.objects.filter(mpoly__contains=p)
[]
What am I doing wrong? I've swapped the co-ordinates just in case I had them the wrong way round, I've removed the srid arguments and started again. This is my first time doing any GIS stuff so I'm completely lost
Upvotes: 1
Views: 960
Reputation: 31
I set the wrong SRID for the points. Fixed by using srid 4326 for lng/lat points and then transforming to correct projection before search
from django.contrib.gis.geos import GEOSGeometry
p = GEOSGeometry("Point(-0.127907 51.507786)", srid=4326) # 4326 for standard lng/lat coordinates
# Transform to same coordinate system as maps
p.transform(27700)
RegionBorder.objects.filter(mpoly__contains=p)
[<RegionBorder: London>]
Upvotes: 1