The nodes:
You want two things, the end points of the polylines (without intermediate nodes) and the intersection points. There are an additional problem, some polylines end points are also intersection points:
A solution is to use Python and the modules Shapely and Fiona
1) Read the shapefile:
from shapely.geometry import Point, shape
import fiona
lines = [shape(line['geometry']) for line in fiona.open("your_shapefile.shp")]
2) Find the end Points of the lines (how would one get the end points of a polyline?):
endpts = [(Point(list(line.coords)[0]), Point(list(line.coords)[-1])) for line in lines]
# flatten the resulting list to a simple list of points
endpts= [pt for sublist in endpts for pt in sublist]
3) Compute the intersections (iterating through pairs of geometries in the layer with the itertools module). The result of some intersections are MultiPoints and we want a list of points:
import itertools
inters = []
for line1,line2 in itertools.combinations(lines, 2):
if line1.intersects(line2):
inter = line1.intersection(line2)
if "Point" == inter.type:
inters.append(inter)
elif "MultiPoint" == inter.type:
inters.extend([pt for pt in inter])
elif "MultiLineString" == inter.type:
multiLine = [line for line in inter]
first_coords = multiLine[0].coords[0]
last_coords = multiLine[len(multiLine)-1].coords[1]
inters.append(Point(first_coords[0], first_coords[1]))
inters.append(Point(last_coords[0], last_coords[1]))
elif "GeometryCollection" == inter.type:
for geom in inter:
if "Point" == geom.type:
inters.append(geom)
elif "MultiPoint" == geom.type:
inters.extend([pt for pt in geom])
elif "MultiLineString" == geom.type:
multiLine = [line for line in geom]
first_coords = multiLine[0].coords[0]
last_coords = multiLine[len(multiLine)-1].coords[1]
inters.append(Point(first_coords[0], first_coords[1]))
inters.append(Point(last_coords[0], last_coords[1]))
4) Eliminate duplicates between end points and intersection points (as you can see in the figures)
result = endpts.extend([pt for pt in inters if pt not in endpts])
5) Save the resulting shapefile
from shapely.geometry import mapping
# schema of the shapefile
schema = {'geometry': 'Point','properties': {'test': 'int'}}
# creation of the shapefile
with fiona.open('result.shp','w','ESRI Shapefile', schema) as output:
for i, pt in enumerate(result):
output.write({'geometry':mapping(pt), 'properties':{'test':i}})
Final result:
The line segments
If you want also the segments between the nodes, you need to "planarize" (Planar Graph, no edges cross each other) your shapefile. This can be done by the unary_union function of Shapely
from shapely.ops import unary_union
graph = unary_union(lines)
In Python using the fiona
library to read the file and shapely
to do the geometry operations it's easy to merge the geometries. For example:
import fiona
import shapely.geometry
import shapely.ops
with fiona.open(path) as src:
merged_geometries = shapely.ops.linemerge([
shapely.geometry.shape(feature["geometry"]) for feature in src])
The merged geometry will either be a LineString or a MultiLineString depending on whether a single contiguous line can be formed or not.
You can then write the result back out to a shapefile using fiona
(see the docs on writing files), or really do whatever you like with it.
with fiona.open(path) as src:
crs = src.crs
driver = src.driver
schema = {
"geometry": merged_geometries.geom_type,
"properties": {
"length": "float"
}
}
with fiona.open(out_path, "w", driver=driver, crs=crs, schema=schema) as dest:
dest.write({
"geometry": shapely.geometry.mapping(merged_geometries),
"id":"-1",
"properties": {
"length": merged_geometries.length
}
})
Best Answer
Dissolve Lines
update link (28th October 2014) http://resources.arcgis.com/en/help/main/10.2/index.html#//00170000005n000000 Single Part & DISSOLVE_LINES enable in the geoprocessing tool.