数据帧到空间并与圆形缓冲区相交
dataframe to spatial and intersection with a circular buffer
我有一个包含 lon/lat 信息的数据框。目的是找到到特定点 st_p
的 rad
距离内的所有点。
事实上,我已经有了 R 中的代码,但我需要在 python.
上做同样的事情
我所做的是,我将数据帧转换为 sf
对象,我定义了一个缓冲区,然后与缓冲区相交。
这是R代码。
我只是不知道要在 Python 中使用什么库来做同样的事情。
within_radius <- function(df, st_p, rad) {
# Transform to an sf object and cahnge from lon/lat to utm
sf_df <- st_transform(st_as_sf(
df,
coords = c("lon", "lat"),
crs = 4326,
agr = "constant"
), 6622)
# Create an utm st point based on the coordinates of the stop point
cntr <- st_transform(st_sfc(st_p, crs = 4326), 6622)
# Craete a circular buffer with the given radius
buff <- st_buffer(cntr, rad)
# Filter the points that are within the buffer
intr <- st_intersects(sf_df, buff, sparse = F)
sf_df <- st_transform(sf_df, 4326)
sf_df <- sf_df[which(unlist(intr)), ]
# Compute the distance of esch point to the begining of the road segment
xy = st_coordinates(st_centroid(sf_df))
nc.sort = sf_df[order(xy[, "X"], xy[, "Y"]), ]
sf_df <- nc.sort %>%
mutate(dist = st_distance(
x = nc.sort,
y = nc.sort[1, ],
by_element = TRUE
))
}
您可以使用 geopandas
和 shapely
做几乎任何事情
- 从 pandas 数据框创建一个 geopandas 地理数据框,经纬度:
In [19]: import pandas as pd
In [20]: import geopandas as gpd
In [21]: from shapely.geometry import Point
In [22]: df = pd.DataFrame({"lat": [19.435175, 19.432909], "lng":[-99.141197, -99.146036]})
In [23]: gf = gpd.GeoDataFrame(df, geometry = [Point(x,y) for (x,y) in zip(df.lng, df.lat)], crs = "epsg:4326")
In [24]: gf
Out[24]:
lat lng geometry
0 19.435175 -99.141197 POINT (-99.14120 19.43518)
1 19.432909 -99.
146036 POINT (-99.14604 19.43291)
缓冲区、投影和其他操作可用于地理数据框,这就是转换为公制投影并创建 10 米缓冲区的方法:
In [27]: gf.to_crs(6622).buffer(10)
Out[27]:
0 POLYGON ((-3597495.980 -2115793.588, -3597496....
1 POLYGON ((-3598149.053 -2115813.383, -3598149....
dtype: geometry
您可以调用 intersects 来获取缓冲区和点之间的交集:
In [29]: gf.to_crs(6622).buffer(10).intersects(Point(-3597505.980,-2115793.588))
Out[29]:
0 True
1 False
dtype: bool
计算质心:
In [30]: gf.to_crs(6622).buffer(10).centroid
Out[30]:
0 POINT (-3597505.980 -2115793.588)
1 POINT (-3598159.053 -2115813.383)
dtype: geometry
使用缓冲区过滤:
In [31]: gf.loc[gf.to_crs(6622).buffer(10).intersects(Point(-3597505.980,-2115793.588))]
Out[31]:
lat lng geometry
0 19.435175 -99.141197 POINT (-99.14120 19.43518)
distance 给出了到几何图形中最近点的距离:
In [33]: gf.to_crs(6622).buffer(10).distance(Point(-3597505.980,-2115793.588))
Out[33]:
0 0.000000
1 643.377576
dtype: float64
还有很多可以做的,看看文档就知道了
https://geopandas.org/index.html
另请参阅 shapely 的文档以了解如何投影单点 https://shapely.readthedocs.io/en/latest/manual.html#shapely.ops.transform
我有一个包含 lon/lat 信息的数据框。目的是找到到特定点 st_p
的 rad
距离内的所有点。
事实上,我已经有了 R 中的代码,但我需要在 python.
我所做的是,我将数据帧转换为 sf
对象,我定义了一个缓冲区,然后与缓冲区相交。
这是R代码。 我只是不知道要在 Python 中使用什么库来做同样的事情。
within_radius <- function(df, st_p, rad) {
# Transform to an sf object and cahnge from lon/lat to utm
sf_df <- st_transform(st_as_sf(
df,
coords = c("lon", "lat"),
crs = 4326,
agr = "constant"
), 6622)
# Create an utm st point based on the coordinates of the stop point
cntr <- st_transform(st_sfc(st_p, crs = 4326), 6622)
# Craete a circular buffer with the given radius
buff <- st_buffer(cntr, rad)
# Filter the points that are within the buffer
intr <- st_intersects(sf_df, buff, sparse = F)
sf_df <- st_transform(sf_df, 4326)
sf_df <- sf_df[which(unlist(intr)), ]
# Compute the distance of esch point to the begining of the road segment
xy = st_coordinates(st_centroid(sf_df))
nc.sort = sf_df[order(xy[, "X"], xy[, "Y"]), ]
sf_df <- nc.sort %>%
mutate(dist = st_distance(
x = nc.sort,
y = nc.sort[1, ],
by_element = TRUE
))
}
您可以使用 geopandas
和 shapely
做几乎任何事情
- 从 pandas 数据框创建一个 geopandas 地理数据框,经纬度:
In [19]: import pandas as pd
In [20]: import geopandas as gpd
In [21]: from shapely.geometry import Point
In [22]: df = pd.DataFrame({"lat": [19.435175, 19.432909], "lng":[-99.141197, -99.146036]})
In [23]: gf = gpd.GeoDataFrame(df, geometry = [Point(x,y) for (x,y) in zip(df.lng, df.lat)], crs = "epsg:4326")
In [24]: gf
Out[24]:
lat lng geometry
0 19.435175 -99.141197 POINT (-99.14120 19.43518)
1 19.432909 -99.
146036 POINT (-99.14604 19.43291)
缓冲区、投影和其他操作可用于地理数据框,这就是转换为公制投影并创建 10 米缓冲区的方法:
In [27]: gf.to_crs(6622).buffer(10)
Out[27]:
0 POLYGON ((-3597495.980 -2115793.588, -3597496....
1 POLYGON ((-3598149.053 -2115813.383, -3598149....
dtype: geometry
您可以调用 intersects 来获取缓冲区和点之间的交集:
In [29]: gf.to_crs(6622).buffer(10).intersects(Point(-3597505.980,-2115793.588))
Out[29]:
0 True
1 False
dtype: bool
计算质心:
In [30]: gf.to_crs(6622).buffer(10).centroid
Out[30]:
0 POINT (-3597505.980 -2115793.588)
1 POINT (-3598159.053 -2115813.383)
dtype: geometry
使用缓冲区过滤:
In [31]: gf.loc[gf.to_crs(6622).buffer(10).intersects(Point(-3597505.980,-2115793.588))]
Out[31]:
lat lng geometry
0 19.435175 -99.141197 POINT (-99.14120 19.43518)
distance 给出了到几何图形中最近点的距离:
In [33]: gf.to_crs(6622).buffer(10).distance(Point(-3597505.980,-2115793.588))
Out[33]:
0 0.000000
1 643.377576
dtype: float64
还有很多可以做的,看看文档就知道了 https://geopandas.org/index.html
另请参阅 shapely 的文档以了解如何投影单点 https://shapely.readthedocs.io/en/latest/manual.html#shapely.ops.transform