geopandas.sjoin_nearest#
- geopandas.sjoin_nearest(left_df, right_df, how='inner', max_distance=None, lsuffix='left', rsuffix='right', distance_col=None, exclusive=False)[来源]#
基于两个GeoDataFrame之间几何体的距离进行空间连接。
结果将包括单个输入记录的多个输出记录,其中有多个等距最近邻居或相交邻居。
距离是以坐标参考系统(CRS)单位计算的,并可以使用 distance_col 参数返回。
请查看用户指南页面 https://geopandas.readthedocs.io/en/latest/docs/user_guide/mergingdata.html 以获取更多详细信息。
- Parameters:
- left_df, right_dfGeoDataFrames
- howstring, default ‘inner’
连接的类型:
‘left’: 使用 left_df 的键;仅保留 left_df 几何列
‘right’: 使用来自 right_df 的键;仅保留 right_df 的几何列
‘inner’: 使用两个数据框的键的交集;仅保留left_df几何列
- max_distancefloat, default None
查询最近几何体的最大距离。 必须大于0。 用于在树中搜索最近项目的max_distance可能对性能产生显著影响,因为它减少了输入几何体的数量,从而评估树中的最近项目。
- lsuffixstring, default ‘left’
应用于重叠列名的后缀(左侧GeoDataFrame)。
- rsuffixstring, default ‘right’
应用于重叠列名的后缀(右侧GeoDataFrame)。
- distance_colstring, default None
如果设置,将在连接的GeoDataFrame中以此名称的列保存计算出的匹配几何之间的距离。
- exclusivebool, default False
如果为真,则与输入几何相等的最近几何将不会返回,默认为假。
另请参阅
sjoin二元谓词连接
GeoDataFrame.sjoin_nearest等效方法
笔记
由于这个连接依赖于距离,如果你的几何图形在地理坐标参考系中,结果将不准确。
GeoPandas中的每个操作都是平面的,即不考虑潜在的第三维度。
示例
>>> import geodatasets >>> groceries = geopandas.read_file( ... geodatasets.get_path("geoda.groceries") ... ) >>> chicago = geopandas.read_file( ... geodatasets.get_path("geoda.chicago_health") ... ).to_crs(groceries.crs)
>>> chicago.head() ComAreaID ... geometry 0 35 ... POLYGON ((-87.60914 41.84469, -87.60915 41.844... 1 36 ... POLYGON ((-87.59215 41.81693, -87.59231 41.816... 2 37 ... POLYGON ((-87.62880 41.80189, -87.62879 41.801... 3 38 ... POLYGON ((-87.60671 41.81681, -87.60670 41.816... 4 39 ... POLYGON ((-87.59215 41.81693, -87.59215 41.816... [5 rows x 87 columns]
>>> groceries.head() OBJECTID Ycoord ... Category geometry 0 16 41.973266 ... NaN MULTIPOINT ((-87.65661 41.97321)) 1 18 41.696367 ... NaN MULTIPOINT ((-87.68136 41.69713)) 2 22 41.868634 ... NaN MULTIPOINT ((-87.63918 41.86847)) 3 23 41.877590 ... new MULTIPOINT ((-87.65495 41.87783)) 4 27 41.737696 ... NaN MULTIPOINT ((-87.62715 41.73623)) [5 rows x 8 columns]
>>> groceries_w_communities = geopandas.sjoin_nearest(groceries, chicago) >>> groceries_w_communities[["Chain", "community", "geometry"]].head(2) Chain community geometry 0 VIET HOA PLAZA UPTOWN MULTIPOINT ((1168268.672 1933554.35)) 1 COUNTY FAIR FOODS MORGAN PARK MULTIPOINT ((1162302.618 1832900.224))
要包括距离:
>>> groceries_w_communities = geopandas.sjoin_nearest(groceries, chicago, distance_col="distances") >>> groceries_w_communities[["Chain", "community", "distances"]].head(2) Chain community distances 0 VIET HOA PLAZA UPTOWN 0.0 1 COUNTY FAIR FOODS MORGAN PARK 0.0
在以下示例中,我们为Uptown获取多个杂货,因为所有结果是等距的(在这种情况下为零,因为它们相交)。实际上,我们总共得到了4个结果:
>>> chicago_w_groceries = geopandas.sjoin_nearest(groceries, chicago, distance_col="distances", how="right") >>> uptown_results = chicago_w_groceries[chicago_w_groceries["community"] == "UPTOWN"] >>> uptown_results[["Chain", "community"]] Chain community 30 VIET HOA PLAZA UPTOWN 30 JEWEL OSCO UPTOWN 30 TARGET UPTOWN 30 Mariano's UPTOWN