Created
May 28, 2019 21:56
-
-
Save AlexArcPy/3ea09e3265a166c074ae3a6c760398e5 to your computer and use it in GitHub Desktop.
Using bash to run OGR command line tools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo "Working directory: $PWD"; | |
echo "List files in a given directory" | |
for file in C:/GISData/*; do | |
echo $file; | |
done | |
echo "Get basic info about all shapefiles in a given folder" | |
for file in C:/GISData/*; do | |
if [[ ${file: -4} == ".shp" ]] | |
then | |
ogrinfo -so -al $file; | |
fi | |
done | |
echo "Get basic info about all shapefiles in a given folder recursively" | |
cd C:/GISData | |
for file in C:/GISData/**/*; do | |
if [[ ${file: -4} == ".shp" ]] | |
then | |
ogrinfo -so $file; | |
fi | |
done | |
echo "Merge shapefiles recursively" | |
BASEPATH=C:/GISData; | |
POSTFIX=SRTM; | |
SHP=output.shp; | |
cd ${BASEPATH}; | |
for file in ${BASEPATH}/${POSTFIX}*/*; do | |
if [[ ${file: -4} == ".shp" ]] | |
then | |
echo ${file}; | |
if [[ ! -e ${SHP} ]]; then | |
ogr2ogr -f "esri shapefile" ${SHP} ${file} | |
else | |
ogr2ogr -f "esri shapefile" -update -append ${SHP} ${file} | |
fi | |
fi | |
done | |
echo "Get unique values in a given field recursively" | |
cd C:/GISData | |
field='Type' | |
for file in C:/GISData/Data*/*; do | |
if [[ ${file: -4} == ".shp" ]] | |
then | |
filename=$(basename -- "$file"); | |
ogrinfo -sql "SELECT DISTINCT ${field} FROM ${filename%%.*}" ${file} -nomd; | |
fi | |
done | |
echo "Load a shapefile into a PostGIS database table keeping only certain columns" | |
DB_NAME=gfm | |
DB_USER=postgres | |
SRC_SHAPEFILE="C:/GISData/CountriesEsri/ESRI_Online_World_Countries_Detailed_WGS84.shp" | |
DEST_NAME=countries | |
DEST_COLUMNS="COUNTRY,ISO_CC,COUNTRYAFF" | |
ogr2ogr -f "PostgreSQL" PG:"dbname=${DB_NAME} user=${DB_USER}" ${SRC_SHAPEFILE} -nln ${DEST_NAME} -nlt geometry -lco GEOMETRY_NAME=geom -select ${DEST_COLUMNS} | |
echo "Export a PostGIS database table into a shapefile" | |
DB_NAME=gisdata | |
DB_USER=postgres | |
DB_PASSWORD=postgres | |
PORT=5433 | |
cd C:/GISData/PostGISData | |
ogr2ogr -f "ESRI Shapefile" countries.shp PG:"dbname=${DB_NAME} user=${DB_USER} password=${DB_PASSWORD} port=${PORT}" "countries" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment