在python中匹配经度和纬度的两个数据帧



我有一个位于城市中的经度和纬度商店列表。

ID   Latitude Longitude
1    28.2828  48.8392
2    28.3829  48.2947
3    27.9274  48.9274
4    28.9284  48.1937
5    27.2749  48.2804
… 
1000 27.9292  48.9284

我有另一份经度和纬度的列表,这些商店位于该州。

ID   Latitude Longitude
8392 28.73948 48.9284
7274 19.82744 27.2837
7293 28.72847 48.92847
8384 18.28474 83.29374
2848 28.92745 48.8293
…

使用python,我如何找到第二个数据帧中的哪些数据点位于第一个数据帧组成的区域中?

换句话说,这是我想要的结果,因为第二个数据帧中的这些ID位于由第一个数据帧组成的城市中。所有其他ID都会被过滤掉,因为它们跨越了其他区域。

ID    Latitude Longitude 
8392 28.73948 48.9284
7293 28.72847 48.92847
2848 28.92745 48.8293
  • 您的样本数据在状态和城市凸包之间没有任何相交点
  • 要找到一个十字路口,你需要一个代表你所在城市的多边形。这可以通过https://shapely.readthedocs.io/en/latest/manual.html#object.convex_hull
  • 一旦你有了一个代表城市的多边形,你就可以sjoin()到其他点。我模拟了一下以获得一些分数
  • 还提供了可视化演示
import pandas as pd
import geopandas as gpd
import io
import shapely
df_city = pd.read_csv(
io.StringIO(
"""ID   Latitude Longitude
1    28.2828  48.8392
2    28.3829  48.2947
3    27.9274  48.9274
4    28.9284  48.1937
5    27.2749  48.2804
1000 27.9292  48.9284"""
),
sep="s+",
)
df_state = pd.read_csv(
io.StringIO(
"""ID   Latitude Longitude
8392 28.73948 48.9284
7274 19.82744 27.2837
7293 28.72847 48.92847
8384 18.28474 83.29374
2848 28.92745 48.8293"""
),
sep="s+",
)
city_geom = shapely.geometry.MultiPoint(
gpd.points_from_xy(df_city["Longitude"], df_city["Latitude"])
).convex_hull

# have some overlapping points...
df_state2 = pd.concat([df_state, df_city.sample(2)])
gpd.GeoDataFrame(
df_state2, geometry=gpd.points_from_xy(df_state2["Longitude"], df_state2["Latitude"], crs="epsg:4326")
).sjoin(gpd.GeoDataFrame(geometry=[city_geom], crs="epsg:4326"))
>48.9274/table>
ID
128.282848.8392POINT(48.839228.2828(
327.9274

最新更新