Skip to content

Instantly share code, notes, and snippets.

@joakimsk
Last active March 5, 2018 02:08
Show Gist options
  • Save joakimsk/46d8a1d63359e5562d6063c8b6240467 to your computer and use it in GitHub Desktop.
Save joakimsk/46d8a1d63359e5562d6063c8b6240467 to your computer and use it in GitHub Desktop.
Oneliners to manipulate and search geographical data with ogr, gdal and more
# Find all files recursively of dxf type, run ogrinfo and sort by geometry types
find . -iname "*.dxf" -print0 | xargs -0 -I {} ogrinfo -dialect SQLITE -sql "SELECT ST_GeometryType(geometry), COUNT(ST_GeometryType(geometry)) FROM entities e GROUP BY ST_GeometryType(geometry);" {}
# Convert from dxf to sqlite using proper arguments and discard unneeded fields, drop Z dimension
ogr2ogr -f SQLite testspatialite.sqlite a18ascii.dxf -s_srs "+proj=sterea +lat_0=34.2 +lon_0=39.15 +k=1.0 +x_0=28 +y_0=-42 +a=6378249.2 +b=6356515 +units=m +no_defs" -t_srs EPSG:4326 -a_srs EPSG:4326 -dsco SPATIALITE=YES -dim XY -nln 'entities' -dialect SQLITE -sql "SELECT e.layer, e.text, e.geometry FROM entities e WHERE ST_GeometryType(e.geometry) IN ('POINT', 'POINT Z', 'LINESTRING', 'LINESTRING Z', 'MULTILINESTRING', 'MULTILINESTRING Z', 'POLYGON', 'POLYGON Z', 'MULTIPOLYGON', 'MULTIPOLYGON Z');" --config OGR_ENABLE_PARTIAL_REPROJECTION TRUE --debug ON
# Add field mapfile to table, length of character optional (default = 1), details in http://www.gdal.org/ogr_sql.html
ogrinfo testspatialite.sqlite -sql "ALTER TABLE entities ADD COLUMN mapfile character" --debug ON
# Add data to rows in field mapfile
ogrinfo testspatialite.sqlite -dialect SQLite -sql "UPDATE 'entities' SET mapfile = 'testspatialite'" --debug ON
# Print all rows and fields in sqlite database, to verify data
ogrinfo testspatialite.sqlite -sql "SELECT * FROM entities" --debug ON
# Find all files of filetype .dxf, exclude paths with MERGE*, Extra, RESOLVE* and SEPARATE*
find . -type d \( -name "MERGE*" -o -name "Extra" -o -name "RESOLVE*" -o -name "SEPARATE*" \) -prune -o -iname "*.dxf" -print
# Find all files of filetype .dxf, exclude paths with MERGE*, Extra, RESOLVE* and SEPARATE*, using xargs to run a script on each file
find . -type d \( -name "MERGE*" -o -name "Extra" -o -name "RESOLVE*" -o -name "SEPARATE*" \) -prune -o -iname "*.dxf" -print0 | xargs -0 -I {} ./sh-addmapfile.sh -i "{}"
# Find all files of filetype .dxf, exclude paths with MERGE*, Extra, RESOLVE* and SEPARATE*, count number of newlines (number of files)
find . -type d \( -name "MERGE*" -o -name "Extra" -o -name "RESOLVE*" -o -name "SEPARATE*" \) -prune -o -iname "*.dxf" -print | wc -l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment