我有一个名为geodf
的地理熊猫数据框架。该数据帧中每一行的几何图形是一个多多边形。例如,如果我们通过geodataframe考虑geometry列中的第一行:
bound = geodf['geometry'].iloc[0]
print(bound)
它看起来像这样:
MULTIPOLYGON (((-86.46228799973933 34.31021100007911, -86.46447100007661 34.31018399970606, -86.46447800010341 34.31197299998977, -86.4623920000716 34.31198799958079
多面体本身很大,所以我尝试为它创建一个长方体。
box = shapely.geometry.box(*geodf.bounds)
然后,我通过我们的Crate DB服务器通过以下查询传递了box
:
query = """
SELECT geohash, grid,shape
FROM geo
WHERE layer = 'pop'
AND MATCH(shape, '{}') USING within;
""".format(box)
geoCursor.execute(query)
results = geoCursor.fetchall()
District = gpd.GeoDataFrame(results, columns = ['geohash', 'grid','shape'])
其中,我通过.format(box)
传递了上面查询中的多边形box
我想要做的是在上面的查询中传递bound
而不是box
(注意,上面的查询适用于box
)。但是,当我尝试传递bound
时,我得到了以下错误:
ProgrammingError: SQLActionException[UnhandledServerException: java.lang.IllegalArgumentException: Cannot convert Map "{type=MultiPolygon, coordinates=[[[[D@2b59d486}" to shape]
我无法诊断上面的错误。我想知道为什么bounds
不能工作,我怎样才能让它工作?我们不想使用边界框,因为我们的多面边界中没有包含大量多余的区域
发布于 2018-02-12 10:28:27
您可以将多重多边形表示为类似于geoJSON格式的dict,然后使用参数替换。我不熟悉shapely,所以我不能评论你是如何获得字典表示的,但是使用手动制作的字典,它看起来如下所示:
multipolygon = {
"type": "MultiPolygon",
"coordinates": [
[
[
[ 40.0, 40.0 ],
[ 20.0, 45.0 ],
[ 45.0, 30.0 ],
[ 40.0, 40.0 ]
]
],
[
[
[ 20.0, 35.0 ],
[ 10.0, 30.0 ],
[ 10.0, 10.0 ],
[ 30.0, 5.0 ],
[ 45.0, 20.0 ],
[ 20.0, 35.0 ]
],
[
[ 30.0, 20.0 ],
[ 20.0, 15.0 ],
[ 20.0, 25.0 ],
[ 30.0, 20.0 ]
]
]
]
}
c.execute('select * from t where match(x, ?) using within', (multipolygon, ))
print(c.fetchall())
https://stackoverflow.com/questions/48688098
复制