Skip to content

Instantly share code, notes, and snippets.

@jjelosua
Created September 24, 2015 15:32
Show Gist options
  • Save jjelosua/1960e816982f9661a293 to your computer and use it in GitHub Desktop.
Save jjelosua/1960e816982f9661a293 to your computer and use it in GitHub Desktop.
OGR2OGR: Using VRT to load csv into Postgis
<OGRVRTDataSource>
<OGRVRTLayer name="test">
<SrcDataSource relativeToVRT="1">test.csv</SrcDataSource>
<GeometryType>wkbPoint</GeometryType>
<LayerSRS>WGS84</LayerSRS>
<GeometryField encoding="PointFromColumns" x="longitude" y="latitude"/>
</OGRVRTLayer>
</OGRVRTDataSource>
#!/bin/bash
SCRIPTPATH=$( cd $(dirname $0) ; pwd -P )
# read configuration from env vars or default values
OGR_PATH=${GDAL_PATH:-'/usr/local/bin'}
DB=${DB_CONNECTION:-'dbname="dbname" host="localhost" port="5432" user="user" password="pass"'}
# Needs the starting slash for relative path generation
VRT_PATH=$SCRIPTPATH'/../data/csv'
VRT_FILE='vrtfile.vrt'
# table name to create in postgis
TABLE_NAME='tbname'
echo "****************************************"
echo "set GDAL_PATH env var or change default to point to the ogr2ogr binary"
echo "set DB_CONNECTION env var or change default to point to the target DB"
echo "****************************************"
echo "loading csv through $VRT_FILE into postgis"
$OGR_PATH/ogr2ogr -nlt PROMOTE_TO_MULTI -progress -nln $TABLE_NAME -skipfailures -lco PRECISION=no -f PostgreSQL PG:"$DB" $VRT_PATH/$VRT_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment