python - 根据名称分割线

标签 python python-3.x pandas geolocation geopandas

我有一个 GeoPandas Dataframe,是从 shapefile 对象创建的。 然而,某些线路具有相同的名称,但位置却截然不同。

我想为每一行指定一个唯一的名称! 因此,如果它们在几何上分开,我需要以某种方式分割线并重命名它们。

人们可以尝试计算所有街道 block 之间的距离,并在它们靠近时重新组合它们。

距离的计算可以在 Geopandas 中轻松完成:Distance Between Linestring Geopandas

一组要尝试的行:

from shapely.geometry import Point, LineString
import geopandas as gpd


line1 = LineString([
    Point(0, 0),
    Point(0, 1),
    Point(1, 1),
    Point(1, 2),
    Point(3, 3),
    Point(5, 6),
])

line2 = LineString([
    Point(5, 3),
    Point(5, 5),
    Point(9, 5),
    Point(10, 7),
    Point(11, 8),
    Point(12, 12),
])

line3 = LineString([
    Point(9, 10),
    Point(10, 14),
    Point(11, 12),
    Point(12, 15),
])

df = gpd.GeoDataFrame(
    data={'name': ['A', 'A', 'A']},
    geometry=[line1, line2, line3]
)

最佳答案

一种可能的方法是使用每个数据点的空间聚类。以下代码使用 DBSCAN,但也许其他类型更适合。以下是它们工作原理的概述:http://scikit-learn.org/stable/modules/clustering.html

from matplotlib import pyplot as plt
from sklearn.cluster import DBSCAN
from sklearn.preprocessing import StandardScaler

import numpy as np
import pandas as pd
import geopandas as gpd

df = gpd.GeoDataFrame.from_file("stackex_dataset.shp")

df 的每一行都是多个点。我们想把它们全部取出来得到簇:<​​/p>

ids = []
coords = []

for row in df.itertuples():
    geom = np.asarray(row.geometry)

    coords.extend(geom)
    ids.extend([row.id] * geom.shape[0])

我们需要 ids 来在计算后将簇返回到 df 。 这是获取每个点的聚类(我们还对数据进行归一化以获得更好的质量):

clust = DBSCAN(eps=0.5)
clusters = clust.fit_predict(StandardScaler().fit_transform(coords))

下一部分有点困惑,但我们想确保每个 id 只得到一个簇。我们为每个 id 选择最频繁的点簇。

points_clusters = pd.DataFrame({"id":ids, "cluster":clusters})
points_clusters["count"] = points_clusters.groupby(["id", "cluster"])["id"].transform('size')

max_inds = points_clusters.groupby(["id", "cluster"])['count'].transform(max) == points_clusters['count']
id_to_cluster = points_clusters[max_inds].drop_duplicates(subset ="id").set_index("id")["cluster"]

然后我们将集群编号返回到我们的数据框中,以便我们可以借助该编号枚举我们的街道。

df["cluster"] = df["id"].map(id_to_cluster)

对于 DBSCAN 且 eps=0.5 的数据(您可以使用此参数 - 它是使它们处于一个簇中的点之间的最大距离。eps 越多,获得的簇就越少),我们有这种图片内容:

plt.scatter(np.array(coords)[:, 0], np.array(coords)[:, 1], c=clusters, cmap="autumn")
plt.show()

enter image description here

独立街道的数量为 8:

print(len(df["cluster"].drop_duplicates()))

如果我们降低 eps,例如clust = DBSCAN(eps=0.15) 我们得到更多的簇(此时为 12 个),这可以更好地分离数据:

enter image description here

关于代码的困惑部分:在源 DataFrame 中,我们有 170 行,每行都是一个单独的 LINESTRING 对象。每个 LINESTRING 由 2d 个点组成,各个 LINESTRING 之间的点数不同。因此,首先我们获取所有点(代码中的“坐标”列表)并预测每个点的聚类。我们有一种很小的可能性会在一个 LINESTRING 的点中呈现不同的簇。为了解决这种情况,我们获取每个簇的计数,然后过滤最大值。

关于python - 根据名称分割线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47988849/

相关文章:

python - 是否可以下载 Tor 洋葱网站?

python - 线性回归预测值错误 : "ValueError: shapes (1,1) and (132,132) not aligned: 1 (dim 1) != 132 (dim 0)"

python - 如何替换 Pandas 数据框中拼写错误的单词

Python str.contains 来自两个或多个字典

python - 克莱瑟-如何开始?

python - 为什么会抛出 IndexError?

python - 在 python 中使用 cv2.findContours() 时出错

python re match exact only only occurrence(没有重复连续)

python - 在 Python 3 中为 urllib.request.urlopen 更改用户代理

python - Pandas 在 groupby 对象上使用 groupby