Last active
August 11, 2020 00:13
-
-
Save linuxmalaysia/4dc2f741e9a16b558690f18b6ca51779 to your computer and use it in GitHub Desktop.
Bash script to load to postgresql shpfile epsg4742
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
#!/bin/bash | |
### Gunakan command shp2pgsql daripada projek PostGIS Versi 3.0 | |
### SHPNAME => shape file name tanpa shp (dalam command dimasukkan) | |
### MYSCHEMA => ikut nama folder kandungan setiap shp, pastikan schema dibuat dahulu dalam PosrgreSQL | |
### Database juga telah dibuat | |
### Malaysia menggunakan DATUM2000 EPSG:4742 | |
### Semua perlu huruf kecil nama table dan fail. Sebab command shp2pgsql akan kecilkan huruf yang besar. Nanti pening nak debug. | |
### Harisfazillah Jamel 4 Ogos 2020 | |
### FILENAME hanyalah shp file | |
### password URI ada isu dengan special carather dalam password | |
### shp2pgsql -s 4742 -I -a ${FILENAME} ${MYSCHEMA}.${FILENAME} | psql -h localhost -p 6432 -U namauser namadb | |
### use -a only for first time for append | |
### psql "postgresql://$PENGGUNA@$DBSERVER:$PGPORT/$DATABASEA" | |
### dnf install gdal | |
### dnf install postgis30_12 | |
### Port 6432 sebab gunakan pgbouncer, tukarkan kepada 5432 jika PosrgreSQL localhost. | |
FOLDERSHP=`pwd` | |
### tukar mengikut directory | |
### tukar ikut keperluan | |
### pastikan schema telah create tanpa error | |
MYSCHEMA="plan_myschemacontoh" | |
DATABASEA="developmentdb" | |
PENGGUNA="developer01" | |
PGPASSWORD="tukarkanpassword" | |
PGPORT="6432" | |
DBSERVER="localhost" | |
echo "Script ini akan break sekiranya nama fail atau directory ada space, ctrl dan c untuk stop" | |
echo "MYSCHEMA ikut nama directory kandungan setiap shp, pastikan schema dibuat dahulu dalam PosrgreSQL" | |
echo " Script ini bergantung kepada nama shp dengan dot shp huruf kecil (disengajakan)" | |
sleep 5 | |
# tukar nama fail yang ada space kepada underscore _ | |
for g in *\ *; do mv "$g" "${g// /_}"; done | |
# Buat schema | |
PGPASSWORD="tukarkanpassword" psql -h $DBSERVER -p $PGPORT -U $PENGGUNA $DATABASEA -c "CREATE SCHEMA ${MYSCHEMA}" | |
for f in $(ls "$FOLDERSHP"/*.shp) | |
do | |
FILEBNAME=`basename ${f%%.*}` | |
TABLENAME=`echo "$FILEBNAME" | tr ' ' '_' | tr '[:upper:]' '[:lower:]'` | |
DIRNAME=`dirname $f` | |
echo $f | |
echo ${FILEBNAME} | |
echo ${DIRNAME} | |
echo ${TABLENAME} | |
echo $MYSCHEMA | |
# convert to projection GDM2000 EPSG 4742 | |
ogr2ogr ${FILEBNAME}_4742.shp -t_srs "EPSG:4742" ${FILEBNAME}.shp | |
# upload to postgresql | |
shp2pgsql -s 4742 -I ${FILEBNAME}_4742.shp ${MYSCHEMA}.${TABLENAME} | PGPASSWORD="tukarkanpassword" psql "postgresql://$PENGGUNA@$DBSERVER:$PGPORT/$DATABASEA" | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment