我正在尝试使用Polygon,其定义如下:
from django.contrib.gis.geos import Polygon
parcel = Parcel.objects.filter(objectid="22520").values()
geojson = parcel[0]['geom'].geojson
format_l = ast.literal_eval(geojson)
coordinates_data = format_l['coordinates'][0][0]
poly = Polygon(coordinates_data, srid=3492)
poly_parcel = poly.transform(4326, clone=True)
polygon = poly_parcel.coords
print(polygon)
然后 polygon
包含以下内容:
(((-121.49440799279022, 38.55725848782723),
(-121.49438844924772, 38.557303481514126),
(-121.4943760310021, 38.5573320694682),
(-121.49436263531841, 38.557362909896675),
(-121.49402385986245, 38.557269114460084),
(-121.49406987333441, 38.55716268909225),
(-121.49440799279022, 38.55725848782723)),)
我想在宗地数据中找到主数据,所以我尝试了这个:
Primary.objects.filter(geom__contains=polygon).values()
#and also tried with contained lookup
Primary.objects.filter(geom__contained=polygon).values()
当我尝试此查询时,它会抛出此错误:
" ValueError:不能将带有类型元组的对象用于空间查找参数。"
即使尝试使用List,我也会遇到同样的错误......
答案 0 :(得分:1)
让我们分析错误并从那里看(强调我的):
" ValueError:无法将带有类型元组的对象用于空间查找参数。"
因此,您尝试在空间查找中使用元组对象作为参数。如果您看到如何定义多边形,很明显这是罪魁祸首:
(((-121.49440799279022, 38.55725848782723),
(-121.49438844924772, 38.557303481514126),
(-121.4943760310021, 38.5573320694682),
(-121.49436263531841, 38.557362909896675),
(-121.49402385986245, 38.557269114460084),
(-121.49406987333441, 38.55716268909225),
(-121.49440799279022, 38.55725848782723)),) # Defenitely a tuple!!!
当您尝试在空间查询中使用它时,您收到错误:
Primary.objects.filter(geom__contains=polygon).values()
我建议坚持使用GeoDjango的几何图形(您已经在使用),如文档中所示,更具体地说是GEOS Polygon:
将多边形定义为GEOS多边形:
from django.contrib.gis.geos import Polygon
parcel = Parcel.objects.filter(objectid="22520").values()
geojson = parcel[0]['geom'].geojson
format_l = ast.literal_eval(geojson)
coordinates_data = format_l['coordinates'][0][0]
geos_poly = Polygon(coordinates_data, srid=3492)
polygon = geos_poly.transform(4326, clone=True)
在空间查询中使用此多边形:
Primary.objects.filter(geom__contains=polygon).values()