如何优化重复检查点是否在多边形中

时间:2018-11-27 09:28:08

标签: python pandas shapely oracle-spatial

我有一个MultiPolygon代表一条道路,并想确定某些GPS点是否在距道路x距离内。我下面的geo_bufroad.buffer(x)。如下图所示,重复使用geo_buf.contains(Point)非常慢(大部分时间用于运行第297行)。

我如何优化速度?

from line_profiler import LineProfiler
from shapely.geometry import Point as shapely_Point

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   151                                           def filter_gps(gps_row, geo_buf):
   152    606446   62042960.0    102.3     83.3      pot = shapely_Point(gps_row['longitude'], gps_row['latitude'])
   153    606446   12433530.0     20.5     16.7      return geo_buf.contains(pot)



Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================

  294      1232      11850.0      9.6       0.0   if len(df_gps.index) > 1:
  295                                               geo_buf = shape(json.loads(srg_row['srg_buf']))
  296                                               # filter the GPS points
  297      1232   98465688.0  79923.4     68.4      df_filter = df_gps[df_gps.apply(lambda row: filter_gps(row, geo_buf), axis=1)]

1 个答案:

答案 0 :(得分:0)

这些可能会有所帮助:

Is there way to optimize speed of shapely.geometry.shape.contains(a_point) call?

https://gis.stackexchange.com/questions/102933/more-efficient-spatial-join-in-python-without-qgis-arcgis-postgis-etc/165413#165413

(未经测试),我相信最快的方法是将Polygon分成许多较小的Polygon,然后使用geopandas.tools.sjoin