Skip to content

Instantly share code, notes, and snippets.

@maning
Last active March 9, 2021 19:55
Show Gist options
  • Save maning/11998bb29eb96c4bf38d to your computer and use it in GitHub Desktop.
Save maning/11998bb29eb96c4bf38d to your computer and use it in GitHub Desktop.
geo-oneliners
# Some GEO oneliners that make my life easier.
# Dissolve Shapefiles using OGR from an attribute table
# http://darrencope.com/2015/01/23/dissolve-shapefiles-using-ogr/
ogr2ogr outputfile.shp inputfile.shp \
-dialect sqlite -sql “SELECT dissolvefield,ST_Union(geometry) \
as geometry FROM inputfile GROUP BY dissolvefield”
# Dissolve everything http://gis.stackexchange.com/questions/79850/merge-large-number-of-polygons-using-qgis
ogr2ogr -dialect SQLITE -sql "SELECT ST_Union(geometry) FROM input" output.shp input.shp
# loop convert to another format
# Base on http://gis.stackexchange.com/questions/107000/ogr2ogr-looping-through-files-in-a-folder-on-a-mac
for f in *.gml; do ogr2ogr -s_srs "EPSG:4326" -t_srs "EPSG:4326" dir/${f} ${f}; done
# merge shapefiles
# alternate bash script http://www.northrivergeographic.com/ogr2ogr-merge-shapefiles
ogr2ogr merged.shp input1.shp || \
for f in shp/*.shp; do ogr2ogr -update -append merged.shp $f -f "ESRI Shapefile"; done;
# reduce imagery/raster size to 1/10 of the original size without visible difference
# http://blog.cleverelephant.ca/2015/02/geotiff-compression-for-dummies.html
gdal_translate -co COMPRESS=JPEG -co PHOTOMETRIC=YCBCR -co TILED=YES original.tif modified.tif
# Point on surface instead of centroids
# gis.stackexchange.com/questions/72112/qgis-i-would-like-to-get-a-single-point-from-a-polygon-centroid-if-within-the-po
ogr2ogr PointonSurface.shp InputPolygons.shp -dialect sqlite -sql "SELECT ST_PointOnSurface(geometry) from InputPolygons"
# Count features in shapefile
ogrinfo data.shp -sql "SELECT COUNT(*) FROM data"
# Show summary info
ogrinfo -so -al data.shp
# Loop through several files filtering selected attributes and by specific values
for f in *.geojson; do ogr2ogr -f GeoJSON -sql "SELECT Var FROM ${f%%.*} WHERE Var> 0" test/${f} ${f}; done`
# Get total area using a defined srs, data is wgs84 (4326) to UTMz51n (32651)
ogrinfo -dialect SQLite -sql 'SELECT SUM(ST_Area(ST_transform(geometry,32651))) FROM data' data.shp
# Get total length using a defined srs, data is wgs84 (4326) to UTMz51n (32651)
ogrinfo -dialect SQLite -sql 'SELECT SUM(ST_Length(ST_Transform(geometry,32651))) FROM data' data.shp
# Choose specifc geometrey type from geojson, example for linestring
ogr2ogr -f "ESRI Shapefile" -where "OGR_GEOMETRY='LineString'" test_2.shp test2.geojson
# Individual shp files to s singel zip file within a given directory
for fname in *.*; do
prefix=${fname%.*}
[ ! -f "$fname" ] || [ -f "$prefix.zip" ] && continue
zip "$prefix" "$prefix".*
done
# See also
# https://github.com/dwtkns/gdal-cheat-sheet
# https://trac.osgeo.org/gdal/wiki/FAQVector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment