I have a table of linestrings together with some attributes that, when displayed all at the same time, represent a connected path from an origin to a destination (each linestring has one predecessor, except for the first one, and one successor, except for the last one). However, the feature order in my table is randomized.
For another application, I need the features in the table to be ordered from origin to destination (or vice versa), so that for each row/linestring in the table, the preceding row represents the current row's predecessor linestring and the succeeding row represents the current row's successor linestring.
I am in control over the data, so writing them out in any format suitable for this operation should be possible (csv, geojson, shapefile, ...).
I have QGIS, ArcMap, PostGIS and Python easily available but any other solution will also be fine.
As an example that everybody can easily visualize, take the following GeoJSON:
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "someAttribute": 2 }, "geometry": { "type": "LineString", "coordinates": [ [ 6.88175, 52.294 ], [ 6.88198, 52.29404 ] ] } },
{ "type": "Feature", "properties": { "someAttribute": 8 }, "geometry": { "type": "LineString", "coordinates": [ [ 6.88945, 52.29465 ], [ 6.88955, 52.29465 ] ] } },
{ "type": "Feature", "properties": { "someAttribute": 7 }, "geometry": { "type": "LineString", "coordinates": [ [ 6.88934, 52.29465 ], [ 6.88945, 52.29465 ] ] } },
{ "type": "Feature", "properties": { "someAttribute": 4 }, "geometry": { "type": "LineString", "coordinates": [ [ 6.88294, 52.29418 ], [ 6.8857, 52.29448 ] ] } },
{ "type": "Feature", "properties": { "someAttribute": 3 }, "geometry": { "type": "LineString", "coordinates": [ [ 6.88198, 52.29404 ], [ 6.88294, 52.29418 ] ] } },
{ "type": "Feature", "properties": { "someAttribute": 5 }, "geometry": { "type": "LineString", "coordinates": [ [ 6.8857, 52.29448 ], [ 6.88836, 52.29462 ] ] } },
{ "type": "Feature", "properties": { "someAttribute": 9 }, "geometry": { "type": "LineString", "coordinates": [ [ 6.88955, 52.29465 ], [ 6.88963, 52.29466 ] ] } },
{ "type": "Feature", "properties": { "someAttribute": 6 }, "geometry": { "type": "LineString", "coordinates": [ [ 6.88836, 52.29462 ], [ 6.88934, 52.29465 ] ] } },
{ "type": "Feature", "properties": { "someAttribute": 11 }, "geometry": { "type": "LineString", "coordinates": [ [ 6.89216, 52.29463 ], [ 6.89285, 52.2946 ] ] } },
{ "type": "Feature", "properties": { "someAttribute": 12 }, "geometry": { "type": "LineString", "coordinates": [ [ 6.89285, 52.2946 ], [ 6.89528, 52.29444 ] ] } },
{ "type": "Feature", "properties": { "someAttribute": 1 }, "geometry": { "type": "LineString", "coordinates": [ [ 6.88108, 52.29389 ], [ 6.88175, 52.294 ] ] } },
{ "type": "Feature", "properties": { "someAttribute": 13 }, "geometry": { "type": "LineString", "coordinates": [ [ 6.89528, 52.29444 ], [ 6.89794, 52.29414 ] ] } },
{ "type": "Feature", "properties": { "someAttribute": 10 }, "geometry": { "type": "LineString", "coordinates": [ [ 6.88963, 52.29466 ], [ 6.89216, 52.29463 ] ] } },
{ "type": "Feature", "properties": { "someAttribute": 14 }, "geometry": { "type": "LineString", "coordinates": [ [ 6.89794, 52.29414 ], [ 6.90067, 52.2937 ] ] } },
{ "type": "Feature", "properties": { "someAttribute": 16 }, "geometry": { "type": "LineString", "coordinates": [ [ 6.9034, 52.29311 ], [ 6.90369, 52.29304 ] ] } },
{ "type": "Feature", "properties": { "someAttribute": 15 }, "geometry": { "type": "LineString", "coordinates": [ [ 6.90067, 52.2937 ], [ 6.9034, 52.29311 ] ] } }
]
}
For clarification, if you visualize this (e.g. with QGIS), you get the following picture:
The single linestrings form a connected path from 1
to 16
, even though they are unordered in the data source. What I want is to sort my data source accordingly. The enumeration in someAttribute
of course is only for clarification and not given in the real data, so simply sorting by attribute is not an option ;-)