data = "GEOMETRYCOLLECTION (POINT (-8.9648437500000000 -4.1308593750000000), POINT (2.0214843750000000 -2.6367187500000000), POINT (-1.4062500000000000 -11.1621093750000000), POINT (-11.9531250000000000,-10.8984375000000000), POLYGON ((-21.6210937500000000 1.8457031250000000,2.4609375000000000 2.1972656250000000, -18.9843750000000000 -3.6914062500000000, -22.6757812500000000 -3.3398437500000000, -22.1484375000000000 -2.6367187500000000, -21.6210937500000000 1.8457031250000000)),LINESTRING (-11.9531250000000000 11.3378906250000000, 7.7343750000000000 11.5136718750000000, 12.3046875000000000 2.5488281250000000, 12.2167968750000000 1.6699218750000000, 14.5019531250000000 3.9550781250000000))"
出于这个原因,我想创建3个单独的词组。点。多边形。 LineString。 对于每个Point = {co-ord1,cord2,cord3 ...} 类似于Polygon和LineString。什么是最好的方法呢?
我想创建3个独立的变量。 一个是拥有所有坐标的Point。与Polygon和LineString相同。
答案 0 :(得分:1)
好的,如果你可以确保用,
分隔所有数字,那么我可以用一些简单的python类和eval
函数来做你想做的事情。基本上,为每个POINT
,POLYGON
等定义一个类。这样,您可以将字符串转换为一个表达式,该表达式使得一个GEOMETRYCOLLECTION对象脱离POINT,POLYGON,LINESTRING对象。请记住,在字符串中最好将coords定义为size-2元组,((A,B), (C,D))
而不是(A B, C, D)
。如果你能做到,那么剩下的就是
class Coords(object) :
"""
Simple base class, stores ctor arguments as a tuple.
"""
def __init__(self, *args) :
self.coords = tuple(args)
class POINT(Coords) :
"""
Dummy class, to have handle on class name.
"""
pass
# similar definitions for POLYGON and LINESTRING
class GEOMETRYCOLLECTION(object) :
def __init__(self, *args) :
self.data = {} #store data in a type name -> coord tuple map
for arg in args:
if type(arg).__name__ not in self.data.keys() :
self.data[type(arg).__name__] = [arg.coords]
else :
self.data[type(arg).__name__].append(arg.coords)
def __getitem__(self,key) :
# allow access via [] operator
return self.data.get(key)
然后在你的代码中(一旦你将字符串修改为我建议的格式)
data = "GEOMETRYCOLLECTION (POINT (-8.9648437500000000, ......"
geom = eval(data)
point_coords = geom['POINT']
等等。这有点像黑客,我相信有更优雅的方法来实现这一点。
这一行会产生两个POINT坐标和一个POLYGON:
data="GEOMETRYCOLLECTION (POINT (-8.9648437500000000, -4.1308593750000000), POINT (2.0214843750000000, -2.6367187500000000), POLYGON ((-21.6210937500000000, 1.8457031250000000),(2.4609375000000000, 2.1972656250000000), (-18.9843750000000000, -3.6914062500000000), (-22.6757812500000000, -3.3398437500000000)))"