knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
GDAL commands for importing and exporting spatial layers into SQL Server, PostGres and Spatialite
library(knitr) eval = FALSE opts_chunk$set(echo = TRUE, eval = FALSE, warning = FALSE, error = FALSE, comment = NA)
Batch import shape files by supplying folder path containing shape files rather than individual shape file path
ogr2ogr -progress -lco geometry_name=geom -lco schema=public -f "PostgreSQL" PG:"host=IPADDRESS dbname=DATABASENAME user=USERNAME password=PASSWORD" SHAPEFILEPATH.shp -nln TABLENAME --config PG_USE_COPY YES
There are two drivers using in File GeoDatabases
Issue with importing some polygons files producing errors - Error occurred in filegdbtable.cpp at line 2375, ERROR 1: Error occurred in filegdbtable.cpp at line 2703. This appears to result from GDAL not supporting M routing values. Either
ogr2ogr -progress -lco geometry_name=geom -lco schema=public -f "PostgreSQL" PG:"host=IPADDRESS dbname=DATABASENAME user=USERNAME password=PASSWORD" GEODATABASEFILEPATH.gdb "TABLENAME"
Unable to rename multiple tables using -nln
ogr2ogr -progress -lco geometry_name=geom -lco schema=public -f "PostgreSQL" PG:"host=IPADDRESS dbname=DATABASENAME user=USERNAME password=PASSWORD" SPATIALITEFILEPATH.sqlite "TABLENAME1" "TABLENAME2"
ogr2ogr -progress -f "ESRI Shapefile" OUTPUTFOLDERNAME PG:"host=HOSTNAME dbname=DATABASENAME user=USERNAME password=PASSWORD" TABLENAME
ogr2ogr -progress -f SQLite -dsco SPATIALITE=yes SPATIALITEPATH PG:"host=HOSTNAME dbname=DATABASENAME user=USERNAME password=PASSWORD" TABLENAME1 TABLENAME2
ogr2ogr -progress -f SQLite -dsco SPATIALITE=NO SPATIALITEFILEPATH.sqlite SHAPEFILEPATH.shp
ogr2ogr -progress -f SQLite -dsco SPATIALITE=NO SPATIALITEFILEPATH.sqlite GEODATABASEFILEPATH.gdb "TABLENAME"
ogr2ogr -progress -f "ESRI Shapefile" SHAPEFILEPATH.shp SPATIALITEPATH.sqlite -dsco SPATIALITE=yes
ogr2ogr -progress -f "ESRI Shapefile" OUTPUTFOLDERNAME GEODATABASEFILEPATH.gdb "TABLENAME"
ogr2ogr -f "ESRI Shapefile" ORIGINAL.shp REPROJECTED.shp -s_srs EPSG:27700 -t_srs EPSG:4326
ogr2ogr -progress -f "MSSQLSpatial" "MSSQL:Server=INSTANCENAME;Database=DATABASENAME; uid=USERNAME;pwd=PASSWORD;Integrated Security=True;GeometryFormat=native;Launder=YES; precision=NO;geom_type=Geometry" SHAPEFILEPATH.shp
ogr2ogr -progress -f "ESRI Shapefile" SHAPEFILEPATH.shp "MSSQL:Server=INSTANCENAME; Database=DATABASENAME;uid=USERNAME;pwd=PASSWORD;Integrated Security=True; GeometryFormat=native" -sql "SELECT QUERY" -lco "SHPT=POLYGON" -a_srs "EPSG:27700"
| command | Description | |:-------------------|:------------------------------------------------| | -nln TABLENAME | Name table on import | | -overwrite | Overwrite existing table | | -append | Append to existing table | | -nlt MULTIPOLYGON | Define polygon type if not Polygon | | -lco precision=NO | Turn off guessing at precision for column data types as GDAL can incorrectly guess precision for columns leading to obscure errors | | -lco geometry_name=NAME | Name given to the geometry field | | -lco schemna=NAME | define schema to use | | -s_srs ESPG:xxxx | override the source spatial reference system | | -t_srs EPSG:xxxx | Reproject / transform to spatial reference system | | -a_srs ESPG:xxxx | Assign spatial reference system on output | | -sql SQLQUERY | sql statement to execute | | --config PG_USE_COPY YES | Improve speed importing into PostGres |
| command | Description | |:-------------------|:------------------------------------------------| | ogrinfo FILEPATH TABLENAME -so | Summary of layer | | ogrinfo --formats | lists drivers installed |
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.