如何从地理库中调用GEOSDistance_r

问题描述 投票:0回答:1

我试图通过直接调用GEOS库来加快多边形和点/多边形之间的空间距离计算。但是我找不到任何帮助如何正确调用此功能。任何人都可以请我指向我可以找到此功能的参考的位置或指出我做错了什么?

工作实例:

from shapely.geos import lgeos

points_geom = np.array([x._geom for x in points])
polygons_geom = np.array([x._geom for x in polygons])  
lgeos._lgeos.GEOSContains_r(lgeos.geos_handle,polygons_geom[0],points_geom[0])

不工作:

lgeos._lgeos.GEOSDistance_r(lgeos.geos_handle,polygons_geom[0],points_geom[0])


TypeError                                 Traceback (most recent call last)
<ipython-input-138-392cb700cfbc> in <module>()
----> 1 lgeos._lgeos.GEOSDistance_r(lgeos.geos_handle,polygons_geom[0],points_geom[0])

TypeError: this function takes at least 4 arguments (3 given)
ctypes shapely geos
1个回答
1
投票

GEOSDistance_r需要4个参数,你只传递3个:

extern int GEOS_DLL GEOSDistance_r(GEOSContextHandle_t handle,
                                   const GEOSGeometry* g1,
                                   const GEOSGeometry* g2, double *dist);

(来自https://github.com/OSGeo/geos/blob/5a730fc50dab2610a9e6c037b521accc66b7777b/capi/geos_c.h.in#L1100

您正在使用shape的私有接口到GEOS,它看起来像使用ctypes,因此您需要使用ctypes调用来通过引用传递double:

import ctypes
dist = ctypes.c_double()
lgeos._lgeos.GEOSContains_r(
    lgeos.geos_handle, polygons_geom[0], points_geom[0], ctypes.byref(dist))
dist = dist.value
© www.soinside.com 2019 - 2024. All rights reserved.