我有一组LineString,这些LineString与其他LineString相交,并且我想在这些交点将LineString分成单独的段。我有一个解决方案,但我认为这不是最好的方法。
假设我们正在处理一个LineString:
>>> import shapely
>>> from shapely.geometry import *
>>> import geopandas as gpd
>>>
>>> MyLine=LineString([(0,0),(5,0),(10,3)])
>>> MyLine
<shapely.geometry.linestring.LineString object at 0x1277EEB0>
>>>
Run Code Online (Sandbox Code Playgroud)
与该LineString相交的2行:
>>> IntersectionLines=gpd.GeoSeries([LineString([(2,1.5),(3,-.5)]), LineString([(5,.5),(7,.5)])])
>>> IntersectionLines
0 LINESTRING (2 1.5, 3 -0.5)
1 LINESTRING (5 0.5, 7 0.5)
dtype: object
>>>
Run Code Online (Sandbox Code Playgroud)
我可以得到交点如下:
>>> IntPoints=MyLine.intersection(IntersectionLines.unary_union)
>>> IntPointCoords=[x.coords[:][0] for x in IntPoints]
>>> IntPointCoords
[(2.75, 0.0), (5.833333333333333, 0.5)]
>>>
Run Code Online (Sandbox Code Playgroud)
然后,我提取起点和终点,并用这些点和相交点创建对,这些对点将用于形成线段:
>>> StartPoint=MyLine.coords[0]
>>> EndPoint=MyLine.coords[-1]
>>> SplitCoords=[StartPoint]+IntPointCoords+[EndPoint]
>>>
>>> def pair(list):
... for i in range(1, len(list)): …Run Code Online (Sandbox Code Playgroud)