[尝试为我的地区和城市生成多边形字段以进行地理标记。想法是将坐标与Gmaps分开保存。然后使用它们生成区域多边形。对于印度NCR等地区,您有多个城市,例如德里,诺伊达。古尔冈等。这些城市构成了多边形的子集。
因此,您创建了一个多边形并与每个城市一起保存,然后制作一个多面体以在区域级别进行保存。
遇到此错误。请帮助
from Geography.models import Region,City
from django.contrib.gis.geos import Polygon,MultiPolygon
coordinates={
'JAL':{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "Name": "jalandhar", "Description": "" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 75.510571, 31.384717, 0.0 ], [ 75.515237, 31.270770, 0.0 ], [ 75.683574,31.264225, 0.0 ], [ 75.672656,31.390200, 0.0 ] ] ] } }
]
}
}
def add_polygons():
r = Region.objects.last()
city_coordinates = coordinates[r.name]['features']
polygon=None
for cx in city_coordinates:
coo_list_ = cx['geometry']['coordinates'][0]
coo_list =[[item[0],item[1]] for item in coo_list_]
name = cx['properties']['Name']
city=City.objects.filter(name__iexact=name).first()
p = Polygon(coo_list)
if city:
city.poly=p
city.save()
if polygon:
polygon=polygon.union(p)
else: polygon=MultiPolygon(p)
r.poly=Polygon(polygon.shell)
r.save()
add_polygons()
这是我得到的错误。关于创建线性环函数的一些事情。我试图浏览图书馆本身,但无济于事。
GEOSExceptionTraceback (most recent call last)
<ipython-input-5-81f3fb947f4f> in <module>()
36 }
37
---> 38 add_polygons()
<ipython-input-5-81f3fb947f4f> in add_polygons()
11 name = cx['properties']['Name']
12 city=City.objects.filter(name__iexact=name).first()
---> 13 p = Polygon(coo_list)
14 if city:
15 city.poly=p
/home/ubuntu/.virtualenvs/C24Aegis/local/lib/python2.7/site-packages/django/contrib/gis/geos/polygon.pyc in __init__(self, *args, **kwargs)
46 n_holes = len(init_holes)
47
---> 48 polygon = self._create_polygon(n_holes + 1, (ext_ring,) + init_holes)
49 super(Polygon, self).__init__(polygon, **kwargs)
50
/home/ubuntu/.virtualenvs/C24Aegis/local/lib/python2.7/site-packages/django/contrib/gis/geos/polygon.pyc in _create_polygon(self, length, items)
79 rings.append(r)
80 else:
---> 81 rings.append(self._construct_ring(r))
82
83 shell = self._clone(rings.pop(0))
/home/ubuntu/.virtualenvs/C24Aegis/local/lib/python2.7/site-packages/django/contrib/gis/geos/polygon.pyc in _construct_ring(self, param, msg)
106 return param
107 try:
--> 108 ring = LinearRing(param)
109 return ring
110 except TypeError:
/home/ubuntu/.virtualenvs/C24Aegis/local/lib/python2.7/site-packages/django/contrib/gis/geos/linestring.pyc in __init__(self, *args, **kwargs)
74 # Calling the base geometry initialization with the returned pointer
75 # from the function.
---> 76 super(LineString, self).__init__(self._init_func(cs.ptr), srid=srid)
77
78 def __iter__(self):
/home/ubuntu/.virtualenvs/C24Aegis/local/lib/python2.7/site-packages/django/contrib/gis/geos/prototypes/threadsafe.pyc in __call__(self, *args)
54 # Call the threaded GEOS routine with pointer of the context handle
55 # as the first argument.
---> 56 return self.cfunc(self.thread_context.handle.ptr, *args)
57 else:
58 return self.cfunc(*args)
/home/ubuntu/.virtualenvs/C24Aegis/local/lib/python2.7/site-packages/django/contrib/gis/geos/prototypes/errcheck.pyc in check_geom(result, func, cargs)
31 "Error checking on routines that return Geometries."
32 if not result:
---> 33 raise GEOSException('Error encountered checking Geometry returned from GEOS C function "%s".' % func.__name__)
34 return result
35
GEOSException: Error encountered checking Geometry returned from GEOS C function "GEOSGeom_createLinearRing_r".
通过本白皮书https://tools.ietf.org/html/rfc7946#section-3.1:
由于第一个和最后一个坐标必须相同,所以问题已解决。
所以坐标字典应该像
coordinates={
'JAL':{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "Name": "jalandhar", "Description": "" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 75.510571, 31.384717, 0.0 ], [ 75.515237, 31.270770, 0.0 ], [ 75.683574,31.264225, 0.0 ], [ 75.672656,31.390200, 0.0 ],[ 75.510571, 31.384717, 0.0 ] ] ] } }
]
}
}