3

I want to reverse the line direction for all lines in a shapefile from the Linux commandline (that is to say, using a script). Currently I'm using the SwapVectorDirection QGIS plugin which works well but has to be run manually. Ideally I'd like a solution using ogr since I have it installed on my system.

This question is similar but I'm looking specifically for something I can include in a Linux script.

1 Answer 1

7

Use the ogr2ogr utility with the ST_REVERSE function in a SQL statement and OGR SQLite dialect:

For example:

ogr2ogr -dialect SQLITE -sql "select st_reverse(GEOMETRY), * from input" output.shp input.shp

Note: You need to pass in the st_reverse(GEOMETRY) first as the GEOMETRY field is automatically selected if the * wildcard is used and ogr will write only the first geometry out to a single geometry type layer.

5
  • Snap, I just figured that out, see edit.
    – user2856
    Jan 23, 2020 at 1:52
  • Thanks, that works a treat. The only tweak I had to make was to swap the columns in the query to be st_reverse(GEOMETRY), *; without this I wasn't seeing any reversal. BTW, with the aforementioned caveat the pre-edit version using * worked just fine with ogr2ogr from GDAL 2.2.3. Presumably by putting the function first it overrode the default GEOMETRY column. I'll accept this answer in 24 hours unless a much better answer shows up. Jan 23, 2020 at 1:52
  • Do you know if there's a list of all the supported spacial functions? I took on the GDAL site and didn't see one. Jan 23, 2020 at 2:08
  • 2
    Depends on the implementation and data source, but the OGC Simple Feature Access - Part 2: SQL Option standard is the place to start
    – user2856
    Jan 23, 2020 at 2:28
  • 1
    SQL dialect SQLite is using the spatial functions from SpatiaLite gaia-gis.it/gaia-sins/spatialite-sql-latest.html.
    – user30184
    Jan 23, 2020 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.