本文介绍了快速的Python GIS库,支持大圆距和多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找python的地理库.我需要能够执行以下操作:

I was looking for a geographical library for python.I need to be able to do the following:

  1. 使用大圆距离获取2个点之间的距离(以米为单位)(不是班轮距离计算)
  2. 检查点是否在多边形内
  3. 每秒执行1-2次数千次
  1. Get the distance between 2 points (in meters) using Great-circle distance (not liner distance calculation)
  2. Check if a point is inside a polygon
  3. Perform 1 and 2 couple of thousands times per seconds

开始时,我看了这篇文章:适用于Python的Python模块存储和查询地理坐标,并开始使用地理.我遇到了2个问题:

At start I've looked at this post: Python module for storing and querying geographical coordinates and started to use geopy.I've encountered 2 problems:

  1. Geopy不支持多边形
  2. geoPy的CPU使用率很高(计算一个点和相对的5000个点之间的距离大约需要140ms的CPU)

我一直在寻找并发现最佳Python GIS库?https://gis.stackexchange.com/.当geos使用编译的C代码时,它看起来很有前途,该代码应该更快并且可以支持多边形.问题在于,geos/OGR会执行线性距离计算,而不是球面.这消除了所有其他基于geos的模块(例如GEODjango和shapely).我在这里想念什么吗?我不认为我是第一个使用python执行GIS计算并希望获得准确结果的人.

I've continued looking and found Best Python GIS library? and https://gis.stackexchange.com/ . It looked promising as geos is using complied C code which should be faster and shapely supports polygons.The problem is that geos/OGR performs linear distance calculations instead of sphere. This eliminates all other geos based modules (like GEODjango and shapely).Am I missing something here? I don't think that I'm the first person who is using python to perform GIS calculations and wants to get accurate results.

推荐答案

更新

现在继续完成该库中的其他576个函数,其中不包括完成的两个多边形函数,完成的三个球面距离算法以及两个新的函数,即angle_box_2d和angle_contains_ray_2d.另外,我切换到C版本,因此不需要外部代码,从而简化了工作.将旧的C ++版本放在old_c ++目录中,因此它仍然存在.

Moving on now to finishing out the other 576 functions in that library not including the two polygon functions that are finished, the three sphere distance algorithms that are done, and two new ones, an angle_box_2d and angle_contains_ray_2d. Also, I switched to the C version so that externs are not needed, simplifies the work. Put the old C++ version in directory old_c++, so its still there.

经过测试的性能与答案底部列出的性能相同.

Tested performance, it is identical as listed at the bottom of the answer.

更新2

因此,只是一个快速更新,我还没有完成整个库(我只完成了大约15%的时间),但是我添加了这些未经测试的功能,以防万一您需要它们,github,以增加多边形和球面距离算法中的旧点.

So just a quick update, I haven't finished the whole library yet (I'm only about 15% of the way through), but I've added these untested functions, in case you need them right away, on github, to add to the old point in polygon and sphere distance algorithms.

angle_box_2d
angle_contains_ray_2d
angle_deg_2d
angle_half_2d # MLM: double *
angle_rad_2d
angle_rad_3d
angle_rad_nd
angle_turn_2d
anglei_deg_2d
anglei_rad_2d
annulus_area_2d
annulus_sector_area_2d
annulus_sector_centroid_2d # MLM: double *
ball_unit_sample_2d # MLM: double *
ball_unit_sample_3d # MLM: double *
ball_unit_sample_nd # MLM; double *
basis_map_3d #double *
box_01_contains_point_2d
box_01_contains_point_nd
box_contains_point_2d
box_contains_point_nd
box_ray_int_2d
box_segment_clip_2d
circle_arc_point_near_2d
circle_area_2d
circle_dia2imp_2d
circle_exp_contains_point_2d
circle_exp2imp_2d
circle_imp_contains_point_2d
circle_imp_line_par_int_2d
circle_imp_point_dist_2d
circle_imp_point_dist_signed_2d
circle_imp_point_near_2d
circle_imp_points_2d # MlM: double *
circle_imp_points_3d # MLM: double *
circle_imp_points_arc_2d
circle_imp_print_2d
circle_imp_print_3d
circle_imp2exp_2d
circle_llr2imp_2d # MLM: double *
circle_lune_area_2d
circle_lune_centroid_2d # MLM; double *
circle_pppr2imp_3d

我上面评论过的那些可能不会起作用,其他的也许会起作用,但是-多边形&球距绝对可以.并且您可以指定米,公里,英里,海里,对于球面距离来说并没有关系,输出的单位与输入的单位相同-算法对这些单位是敏感的.

The ones that I've commented above probably won't work, the others might, but again - polygon & sphere distances definitely do. And you can specify meters, kilometers, miles, nautical miles, it doesn't really matter on the spherical distance ones, the output is in the same units as the input - the algorithms are agnnostic to the units.

我今天早上把这些放在一起,所以它目前仅提供多边形中的点,凸多边形中的点以及三种不同类型的球面距离算法,但是至少您所要求的那些现在可以在这里使用.我不知道是否与其他任何python库有名称冲突,这些天我只参与python,因此,如果有更好的名称,我欢迎您提出建议.

I put this together this morning so it currently only provides the point in polygon, point in convex polygon, and three different types of spherical distance algorithms, but at least those ones that you requested are there for you to use now. I don't know if there is a name conflict with any other python library out there, I only get peripherally involved with python these days, so if there's a better name for it I'm open to suggestions.

在github上: https://github.com/hoonto/pygeometry

这只是通向此处描述和实现的功能的python桥:

It is just a python bridge to the functions described and implemented here:

http://people.sc.fsu.edu/~jburkardt/cpp_src/geometry/geometry.html

GEOMETRY库实际上非常好,因此我认为桥接所有这些python函数将很有用,今晚我可能会这样做.

The GEOMETRY library is pretty good actually, so I think it'll be useful to bridge all of those functions for python, which I'll do probably tonight.

另外两件事

  1. 由于数学函数实际上是用C ++编译的,因此您当然需要确保共享库在路径中.您可以修改geometry.py,使其指向要放置共享库的任何位置.
  2. .o和.so仅针对linux编译,它们是在x86_64 fedora上编译的.
  3. 球面距离算法期望弧度,因此您需要将十进制纬度/经度转换为弧度,如geometry.py中所示.

如果您确实需要在Windows上使用此功能,请告诉我,只需几分钟即可在Visual Studio中实现.但是除非有人问我,否则可能暂时不要管它.

If you do need this on Windows let me know, it should only take a couple minutes to get it worked out in Visual Studio. But unless someone asks I'll probably just leave it alone for now.

希望这会有所帮助!

Rgds .... Hoonto/Matt

Rgds....Hoonto/Matt

(新提交:SHA:4fa2dbbe849c09252c7bd931edfe8db478de28e6-修复了一些问题,例如弧度转换以及py函数的返回类型.还添加了一些基本的性能测试,以确保库正确执行.)

(new commit: SHA: 4fa2dbbe849c09252c7bd931edfe8db478de28e6 - fixed some things, like radian conversions and also the return types for the py functions. Also added some basic performance tests to make sure the library performs appropriately.)

测试结果在每次迭代中,对sphere_distance1进行一次调用,对polygon_contains_point_2d进行一次调用,因此对库总数进行了两次调用.

Test ResultsIn each iteration, one call to sphere_distance1 and one call polygon_contains_point_2d so 2 calls to the library total.

  • 〜0.062s:2000次迭代,4000次调用
  • 〜0.603s:20000次迭代,40000次调用
  • 〜0.905s:30000次迭代,60000次调用
  • 〜1.198s:40000次迭代,80000次调用

这篇关于快速的Python GIS库,支持大圆距和多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 12:24
查看更多