我想在 QGIS 中用特定值分割一条线。我也可以根据拆分的长度拆分值吗?
I want to split a line with a specific value in QGIS. Can I also split the value accordingly to the length splitted?
我有一些穿过多个多边形的多线字符串。当我拆分线时,我希望 QGIS 也相应地拆分分配给该线的值。例如。如果一条 100 米的线在分成 50/30/20 的段时具有值 20,我希望该值在相应的段中也拆分为 10/6/4。这可能吗,还是我在追求星星?
- 你已经标记了 geopandas 所以在 geopandas
中给出了一个解决方案
- 首先创建一个 GeoDataFrame 多边形和以不同方式相交的线串
- 现在是迭代行字符串的情况
- 为每条线找到它相交的多边形 https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoSeries.intersection.html
- 交点是一条线或multi-line通过比较线的长度来计算贡献
- 有可视化演示
import geopandas as gpd
import shapely
world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
# some polygons
poly = world.loc[world["iso_a3"].isin(["UKR", "POL", "HUN"])].drop(columns=["pop_est","gdp_md_est"]).copy()
# some linestrings
line = gpd.GeoDataFrame(
data={"val": [100, 200]},
geometry=[
shapely.geometry.LineString([poly.total_bounds[0:2], poly.total_bounds[2:4]]),
shapely.geometry.LineString([poly.total_bounds[0:2]+.5, poly.total_bounds[2:4], poly.total_bounds[[0,3]]-1]),
],
crs=poly.crs,
)
# initialise contibution from line, zero
poly = poly.assign(**{f"val{l}":0 for l in range(len(line))})
# iterate over the lines
for n, (i, r) in enumerate(line.iterrows()):
# iterate over intersections between line and polygons
for ii, ls in poly.intersection(r["geometry"]).iteritems():
if ls.is_empty or isinstance(ls,shapely.geometry.Point):
continue
geom = [ls] if isinstance(ls, shapely.geometry.LineString) else ls.geoms
# work out contribution of each intersection segment
for ls in geom:
poly.loc[ii, f"val{n}"] += (ls.length / r["geometry"].length) * r["val"]
# visualise ...
m = poly.explore(height=300, width=500)
line.explore(m=m)
我有一些穿过多个多边形的多线字符串。当我拆分线时,我希望 QGIS 也相应地拆分分配给该线的值。例如。如果一条 100 米的线在分成 50/30/20 的段时具有值 20,我希望该值在相应的段中也拆分为 10/6/4。这可能吗,还是我在追求星星?
- 你已经标记了 geopandas 所以在 geopandas 中给出了一个解决方案
- 首先创建一个 GeoDataFrame 多边形和以不同方式相交的线串
- 现在是迭代行字符串的情况
- 为每条线找到它相交的多边形 https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoSeries.intersection.html
- 交点是一条线或multi-line通过比较线的长度来计算贡献
- 有可视化演示
import geopandas as gpd
import shapely
world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
# some polygons
poly = world.loc[world["iso_a3"].isin(["UKR", "POL", "HUN"])].drop(columns=["pop_est","gdp_md_est"]).copy()
# some linestrings
line = gpd.GeoDataFrame(
data={"val": [100, 200]},
geometry=[
shapely.geometry.LineString([poly.total_bounds[0:2], poly.total_bounds[2:4]]),
shapely.geometry.LineString([poly.total_bounds[0:2]+.5, poly.total_bounds[2:4], poly.total_bounds[[0,3]]-1]),
],
crs=poly.crs,
)
# initialise contibution from line, zero
poly = poly.assign(**{f"val{l}":0 for l in range(len(line))})
# iterate over the lines
for n, (i, r) in enumerate(line.iterrows()):
# iterate over intersections between line and polygons
for ii, ls in poly.intersection(r["geometry"]).iteritems():
if ls.is_empty or isinstance(ls,shapely.geometry.Point):
continue
geom = [ls] if isinstance(ls, shapely.geometry.LineString) else ls.geoms
# work out contribution of each intersection segment
for ls in geom:
poly.loc[ii, f"val{n}"] += (ls.length / r["geometry"].length) * r["val"]
# visualise ...
m = poly.explore(height=300, width=500)
line.explore(m=m)