Skip to content

Instantly share code, notes, and snippets.

@rhysallister
Last active October 30, 2018 14:13
Show Gist options
  • Save rhysallister/c5b89c897918b8b07c6eba0af448af93 to your computer and use it in GitHub Desktop.
Save rhysallister/c5b89c897918b8b07c6eba0af448af93 to your computer and use it in GitHub Desktop.
push an entire dataset to a given schema in a postgresql database using ogr2ogr
#!/bin/bash
# copies a geopackage or ESRI FileGDB to a given schema on a postgresql database
# ./push2db schema_name "PG:dbname=dbname host=host port=port user=user password=password" filename.gpkg
#
#./push2db cartwheel "PG:dbname=jpsgis host=localhost port=5432 user=rhys password=rhys" ~/Public/electricgis_nkredsqlentclu.gdb.zip
SCHEMA=$1
DB_URL=$2
FILENM=$3
SRID=$4
function pushtodb {
SCHEMA=$1
DB_URL="$2"
FILENM=$3
INTBL=$4
ogr2ogr -PROGRESS \
--config PG_USE_COPY YES \
-f "Postgresql" "$DB_URL" \
$FILENM \
-lco UNLOGGED=YES \
-lco SCHEMA=$SCHEMA \
-lco GEOMETRY_NAME=g \
-lco LAUNDER=NO \
$INTBL
}
echo "***************** Creating schema $1;"
ogrinfo "$2" -sql "CREATE SCHEMA $1;"
for i in $( ogrinfo $FILENM \
| grep '^[0-9]' \
| sed s/[0-9]//g \
| sed s/://g \
| sed s/\\s// \
| sed s/\\s.*// \
| while read litmus; do echo "$litmus"; done ) ;
do
echo item: $i;
pushtodb $1 "$2" $3 $i
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment