繁体   English   中英

geopandas中多边形到点的距离

[英]Distance from polygon to Point in geopandas

我有一个多边形的主多边形,其区域在 map 上绘制

在此处输入图像描述

和一堆点,例如下面的示例

    NAME   lt         ln
      1 12.9336806  77.6122669
      2 12.974768   77.767745
      3 12.9314583  77.6299858
      4 12.943219   77.6213569
      5 12.95120049 77.56729126
      6 13.0292902  77.54489136
      7 13.00134159 77.60814171
      8 13.0347 77.653
      9 12.88734436 77.57913454

其中一些在内部,其中一些在多边形之外

import io
import pandas as pd
import geopandas as gpd
import shapely

df = pd.read_csv(io.StringIO("""NAME   value   geometry  
BNG   10000    POLYGON ((77.544620205933 13.080009969947, 77.54687842279 13.081767336162, 77.543973488861 13.09731788964, 77.563642026 13.099993855304, 77.56128297238899 13.109043490523, 77.574909239345 13.109806495724, 77.57990367382099 13.111021266518, 77.58119926142 13.107866932403, 77.583009833151 13.105214129905, 77.593084532791 13.105888221511, 77.599030888611 13.105889067656, 77.62177194696 13.1176238629, 77.63936316400201 13.104531490855, 77.653018935256 13.091558385666, 77.656070270591 13.07934476693, 77.65723333077899 13.067046935098, 77.676682673508 13.056350290571, 77.68731712299 13.048701049623, 77.724215763145 13.060700785769, 77.76648309927501 13.04163756999, 77.764118477398 13.023743607526, 77.768440059357 13.005858466046, 77.772117911153 12.982577525022, 77.76335031312099 12.945178985189, 77.75820798369701 12.934327952869, 77.74246556424499 12.924961348198, 77.73714593953 12.921040614018, 77.73568869579699 12.910050558543, 77.732945869708 12.888278169276, 77.714259993233 12.85631617411, 77.716948505652 12.834909462871, 77.72935798040599 12.813457521761, 77.723914671957 12.805395461228, 77.710746601545 12.79465477261, 77.693115335332 12.798979730761, 77.66432544275099 12.806643557235, 77.669395756776 12.81497669372, 77.670110870893 12.817190820933, 77.668036487634 12.818358785314, 77.66556141954101 12.819564868588, 77.66314328866601 12.820386894414, 77.660971921021 12.820204627205, 77.658887393155 12.819796762003, 77.657789918208 12.820037502885, 77.656654892334 12.819990555544, 77.655642132701 12.820113541267, 77.65450062703501 12.820163297278, 77.653233057546 12.820945348698, 77.65182813770799 12.823038949471, 77.649192083934 12.825237144547, 77.643529489244 12.828291916414, 77.64062420542 12.830086107711, 77.635036712581 12.832889760396, 77.629375445718 12.834718303092, 77.625559538657 12.837090788265, 77.624490160519 12.837479895226, 77.62098884206399 12.838770677593, 77.616839205796 12.840154466343, 77.613285019928 12.842066501745, 77.60995319848701 12.843589250082, 77.61629878717 12.853375524099, 77.598528893524 12.857138857722, 77.59700466352101 12.853487786931, 77.59383285945 12.852635226043, 77.591691023642 12.850443770367, 77.58825493007301 12.849941544208, 77.581962100082 12.842891030037, 77.576350183541 12.836781502148, 77.563507031494 12.84115347288, 77.54892176156601 12.847344964928, 77.52772752862499 12.857720266329, 77.524428994232 12.85897438922, 77.53784301286299 12.879390494455, 77.528855221802 12.883991848334, 77.47377570825201 12.907246757608, 77.481996327453 12.959693115283, 77.48516249948101 12.976879345887, 77.487642026001 12.987540940378, 77.48885933023099 12.99828214478, 77.487923029953 13.002272680094, 77.486128422791 13.007433950863, 77.48409983545 13.011687626541, 77.483345925385 13.015071660086, 77.48336449151699 13.018288398117, 77.48410333543499 13.023659809358, 77.48003566079799 13.028194892116, 77.478938427979 13.039606109147, 77.48239022164999 13.054444934849, 77.495326306396 13.062343354028, 77.50338860803301 13.059745771181, 77.512206036621 13.064447067198, 77.516443089539 13.066212422937, 77.518963528686 13.066890861125, 77.51988453393599 13.067223534285, 77.52174967675801 13.06789063889, 77.537261899047 13.074075787597, 77.53987395196501 13.075475741235, 77.542056851441 13.077293710937, 77.544620205933 13.080009969947))
"""), sep="\s\s+", engine="python")

gdf = gpd.GeoDataFrame(df, geometry=df["geometry"].apply(shapely.wkt.loads), crs="epsg:4386")

gdf.plot(column="value")

df2 = pd.read_csv(io.StringIO("""
NAME   lt   ln
1     12.9336806      77.6122669
2    12.974768    77.767745
3    12.9314583   77.6299858
4    12.943219    77.6213569
5    12.95120049      77.56729126
6    13.0292902   77.54489136
7    13.00134159      77.60814171
8    13.0347          77.653
9    12.88734436      77.57913454
"""), sep="\s\s+", engine="python")

df2['lat'] = df2['lt'].astype(float)
df2['lon'] = df2['ln'].astype(float)

gdf2 = gpd.GeoDataFrame(df2,geometry=gpd.points_from_xy(df2.lon, df2.lat, crs="EPSG:4326"))

gdf2['dist']=gdf.boundary.distance(gdf2)
gdf2['dis_out']=gdf.exterior.distance(gdf2)

这给出了第一点的距离。 我该如何解决

请参阅geopandas.Geoseries.distance文档:

该操作以 1 对 1 的逐行方式工作。

[...]

我们可以检查 GeoSeries 的每个几何图形到单个几何图形的距离

具有许多行映射到单个匀称几何图形的匀称 docs 图像

我们还可以逐行检查两个 GeoSeries。 上面的 GeoSeries 有不同的索引。 我们可以根据索引值对齐两个 GeoSeries,并使用align=True使用具有相同索引的元素,或者使用align=False忽略索引并根据它们的匹配顺序使用元素

形状优美的文档图像,其中许多行以 1:1 映射到其他行

geopandas 中的许多操作都是像这样的二元谓词- 左侧参数的索引确定结果的形状,并且当使用GeoSeries作为other调用此类参数时,假定索引是对齐的,操作采用逐行放置。

因此,要使用distance方法的版本,将距离从一个 GeoSeries 中的每个元素应用到另一个中的单个元素,我们可以访问shapely几何以触发多对一比较:

# get the shapely object, not a GeoSeries
geom = gdf.geometry.item()

# apply distance from a GeoSeries to the single shapely object
gdf2['dist']=gdf2.boundary.distance(geom)

不确定这是否只是一个错字,但请确保对两个 gdf 使用相同的投影。 否则,你最终会得到复杂的距离。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM