簡體   English   中英

匹配python中經緯度兩個數據框

[英]Matching two data frames of longitudes and latitudes in 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

我有另一個經度和緯度列表,商店位於 state。

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
  • 您的示例數據在 state 和 city 的凸包之間沒有任何相交點
  • 要找到一個十字路口,您需要一個代表您所在城市的多邊形。 這可以通過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"))
ID 緯度 經度 幾何學 index_right
1個 28.2828 48.8392 點 (48.8392 28.2828) 0
3個 27.9274 48.9274 點 (48.9274 27.9274) 0

可視化

m = gpd.GeoDataFrame(
    df_state, geometry=gpd.points_from_xy(df_state["Longitude"], df_state["Latitude"], crs="epsg:4326")
).explore()
gpd.GeoDataFrame(geometry=[city_geom], crs="epsg:4326").explore(m=m)


在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM