如何绘制点和线串列表?

How to plot a list of Points and LINESTRING?

你好,有没有办法绘制 LINESTRING 列表和点列表

比如我有

line_string = [LINESTRING (-1.15.12 9.9, -1.15.13 9.93), LINESTRING (-2.15.12 8.9, -2.15.13 8.93)]
point = [POINT (5.41 3.9), POINT (6.41 2.9)]

我的目标是制作一张地图或图表,其中显示点与 LINESTRING 的连接位置。

提前致谢

编辑

谢谢大家的回答,我画的时候很伤心的样子。我认为问题是有些 LINESTRINGS 有 4 分(LINESTRING (-1.15.12 9.9, -1.15.13 9.93, -5.15.13 5.53, -3.15.13 2.23)),有些有 3 分。有没有办法更好地绘制这些?

您可以使用 geopandas 脚本层轻松访问 matplotlib。

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

line_strings = [LineString([(-1.15, 0.12), (9.9, -1.15), (0.13, 9.93)]),
                    LineString([(-2.15, 0.12), (8.9, -2.15), (0.13 , 8.93)])]
points = [Point(5.41, 3.9), Point (6.41, 2.9)]

geom = line_strings + points
gdf = gpd.GeoDataFrame(geometry=geom)

gdf.plot()

根据您的评论进行编辑。如果您想放大某些区域,可以使用 Bokeh 制作交互式绘图。

from bokeh.plotting import figure, show

p = figure(title="interactive plot example", x_axis_label='x', y_axis_label='y')

for ls in line_strings:

    x, y = ls.coords.xy
    p.line(x, y, legend_label="lines", color="blue", line_width=2)
    

for point in points:
    
    p.circle(point.x, point.y, legend_label="points", size=5, color="red", alpha=0.5)
    

show(p)

  • 你可以绘制成图层
  • 已在matplotlibfolium
  • 中演示
  • 您的某些几何图形无效
import shapely.wkt
import geopandas as gpd
import pandas as pd

# line_string = ["LINESTRING (-1.15.12 9.9, -1.15.13 9.93)", "LINESTRING (-2.15.12 8.9, -2.15.13 8.93)"]
# invalid geometry - modified
line_string = ["LINESTRING (-1.15 9.9, -1.15 9.93)", "LINESTRING (-2.15 8.9, -2.15 8.93)"]
point = ["POINT (5.41 3.9)", "POINT (6.41 2.9)"]

gs_ls = gpd.GeoSeries(pd.Series(line_string).apply(shapely.wkt.loads))
gs_p = gpd.GeoSeries(pd.Series(point).apply(shapely.wkt.loads))

# matplotlib
ax = gs_ls.plot()
ax = gs_p.plot(ax=ax)

# folium
m = gs_ls.explore()
m = gs_p.explore(m=m)
m

使用替代几何结构

  • 关于它的外观。显然,几何结构有所不同
  • 已创建 6 条线串作为德国边界的一部分(4 分)。然后各自的点作为这些线串的中心
  • 只有 4 点情节很好。不需要更长的线串。
import geopandas as gpd
import shapely.geometry
import numpy as np

world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))

# exteriod of germany
ls = world.loc[world["iso_a3"].isin(["DEU"])].exterior.values[0]

# generate a series of linestring segments from germany bounday
gs_l = gpd.GeoSeries(
    [
        shapely.geometry.LineString(list(ls.coords)[s : s + 4])
        for s in np.random.randint(0, len(list(ls.coords)) - 4, 6)
    ], crs="epsg:4326"
)

# folium
m = gs_l.explore(style_kwds={"weight":6}, height=400, width=400)
gs_l.centroid.explore(m=m, color="red", marker_kwds={"radius":10})

# matplotlib
ax = gs_l.plot()
ax = gs_l.centroid.plot(color="red", markersize=50, ax=ax)

m

我制作了一个库,它提供了一种非常简单的方法来绘制众所周知的文本字符串和有形状的对象。该库称为 WKTPlot,它环绕 Bokeh 库,自动化了@matthew-borish 在他们的解决方案中提到的很多内容。使用 WKTPlot 这个解决方案会是什么样子:

from shapely.geometry import LineString, Point
from wktplot import WKTPlot

line_strings = [
    LineString([(-1.15, 0.12), (9.9, -1.15), (0.13, 9.93)]),
    LineString([(-2.15, 0.12), (8.9, -2.15), (0.13 , 8.93)]),
]
points = [Point(5.41, 3.9), Point (6.41, 2.9)]

// Create plot object
plot = WKTPlot("test1", save_dir=".")

# Add shapes using Bokeh styling arguments
# - https://docs.bokeh.org/en/latest/docs/user_guide/styling.html
for line_string in line_strings:
    plot.add_shape(line_string, line_width=3)

for point in points:
    plot.add_shape(point, line_width=7)

# Save plot to disk [./test1.html]
plot.save()