简体   繁体   English

匹配python中经纬度两个数据框

[英]Matching two data frames of longitudes and latitudes in python

I have a list of longitudes and latitudes that are stores located in a city.我有一个城市商店的经度和纬度列表。

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

I have another list of longitudes and latitudes that have stores located in the state.我有另一个经度和纬度列表,商店位于 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
…

Using python, how can I find which data points in the second data frame are located in the area made up by the first data frame?使用 python,如何找到第二个数据帧中的哪些数据点位于第一个数据帧组成的区域中?

In other words, this is my desired result because these ID's in the second data frame are located in the city made up by the first data frame.换句话说,这是我想要的结果,因为第二个数据框中的这些 ID 位于由第一个数据框组成的城市中。 All of the other ID's are filtered out because they span other areas.所有其他 ID 都被过滤掉,因为它们跨越其他区域。

ID    Latitude Longitude 
8392 28.73948 48.9284
7293 28.72847 48.92847
2848 28.92745 48.8293
  • your sample data does not have any points that intersect between state and convex hull of city您的示例数据在 state 和 city 的凸包之间没有任何相交点
  • to find an intersection you need a polygon that represents your city.要找到一个十字路口,您需要一个代表您所在城市的多边形。 This can be achieved with https://shapely.readthedocs.io/en/latest/manual.html#object.convex_hull这可以通过https://shapely.readthedocs.io/en/latest/manual.html#object.convex_hull来实现
  • once your have a polygon that represents the city you can sjoin() to other points.一旦你有一个代表城市的多边形,你就可以sjoin()到其他点。 I have simulated this to get some points我模拟了这个以获得一些分数
  • also provided visualisation to demonstrate还提供了可视化来演示
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 ID Latitude纬度 Longitude经度 geometry几何学 index_right index_right
1 1个 28.2828 28.2828 48.8392 48.8392 POINT (48.8392 28.2828)点 (48.8392 28.2828) 0 0
3 3个 27.9274 27.9274 48.9274 48.9274 POINT (48.9274 27.9274)点 (48.9274 27.9274) 0 0

visualisation可视化

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