Home
%3CLINGO-SUB%20id%3D%22lingo-sub-1255421%22%20slang%3D%22en-US%22%3EImporting%20spatial%20data%20to%20PostGIS%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1255421%22%20slang%3D%22en-US%22%3E%3CP%3EIf%20you%E2%80%99re%20new%20to%20PostGIS%20and%20want%20to%20try%20it%20out%2C%20one%20of%20the%20first%20things%20you%E2%80%99ll%20want%20to%20do%20is%20to%20import%20some%20data%20into%20your%20database.%20I%20want%20to%20provide%20an%20overview%20of%20how%20to%20accomplish%20this.%20The%20post%20provides%20an%20introduction%20to%20some%20common%20data%20import%20terminology%2C%20details%20on%20where%20to%20find%20spatial%20data%20and%20tools%20for%20importing%20data%20to%20PostGIS%2C%20and%20specific%20instructions%20for%20how%20to%20use%20each%20tool%20to%20perform%20the%20import.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20an%20introduction%20to%20PostGIS%2C%20see%20my%20previous%20blog%20post%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-postgresql%2Ftake-your-spatial-data-analysis-to-the-next-level-with-postgis%2Fba-p%2F1124288%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Ehere%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1382948528%22%20id%3D%22toc-hId--1382948533%22%3E%3CSPAN%3EIntroduction%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3EIf%20you%E2%80%99ve%20worked%20with%20other%20data%20types%20before%2C%20then%20most%20of%20the%20process%20for%20importing%20spatial%20data%20will%20be%20self-explanatory.%20However%2C%20for%20someone%20working%20with%20spatial%20data%2C%20acronyms%20like%20CRS%20and%20SRID%20might%20be%20confusing.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20data%20import%2C%20you%20should%20know%20the%20coordinate%20reference%20system%20(%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSpatial_reference_system%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ECRS%3C%2FA%3E)%20your%20data%20is%20in%20and%20which%20spatial%20reference%20identifier%20(%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSpatial_reference_system%23Identifier%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESRID%3C%2FA%3E)%20number%20is%20used%20to%20reference%20that%20specific%20CRS.%20The%20SRID%20defines%20all%20the%20parameters%20of%20a%20data%20set%E2%80%99s%20geographic%20coordinate%20system%20and%20projection.%20Using%20an%20SRID%20is%20convenient%20because%20it%20packs%20all%20the%20information%20about%20a%20map%20projection%20into%20a%20single%20number.%20When%20creating%20spatial%20objects%20for%20insertion%20into%20the%20database%2C%20the%20SRID%20is%20required.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1104564305%22%20id%3D%22toc-hId-1104564300%22%3E%3CSPAN%3EWhere%20to%20find%20spatial%20data%3F%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3EBefore%20you%20can%20load%20any%20data%20to%20your%20database%2C%20you%20need%20to%20acquire%20it.%20I%20think%20that%20one%20of%20the%20most%20underrated%20skills%20for%20GIS%20experts%20is%20to%20know%20where%20to%20find%20suitable%20data.%20If%20you%20are%20using%20PostGIS%20to%20manage%20the%20data%20that%20you%20have%20in%20your%20organization%20already%2C%20you%20don%E2%80%99t%20have%20to%20start%20your%20process%20by%20searching%20new%20data%2C%20but%20rather%20working%20with%20your%20own.%20I%20have%20added%20here%20a%20few%20data%20sources%20that%20you%20can%20try%20out%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fwww.openstreetmap.org%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EOpenStreetMap%3C%2FA%3E%3A%20OpenStreetMap%20(OSM)%20is%20a%20collaborative%20project%20to%20create%20a%20free%20editable%20map%20of%20the%20world.%20Many%20people%20refer%20to%20OSM%20as%20the%20Wikipedia%20of%20maps.%20If%20you%20seek%20easy%20ways%20to%20get%20an%20extract%20of%20the%20data%2C%20you%20can%20check%20for%20%3CA%20href%3D%22https%3A%2F%2Fwww.geofabrik.de%2Fdata%2Fdownload.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EGeoFabrik%3C%2FA%3E%20for%20Shapefiles%20or%20%3CA%20href%3D%22https%3A%2F%2Fdownload.osmdata.xyz%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eosmdata.xyz%3C%2FA%3E%20for%20GeoPackages.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22tjukanov_0-1585224774798.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F179698i18B0311ED11F5B32%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22tjukanov_0-1585224774798.png%22%20alt%3D%22tjukanov_0-1585224774798.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CH6%20class%3D%22lia-align-center%22%20id%3D%22toc-hId-699238998%22%20id%3D%22toc-hId-699238993%22%3E%3CI%3E%3CSPAN%3EOpenStreetMap%20data%20from%20Africa.%20All%20the%20roads%20in%20the%20OSM%20database.%26nbsp%3B%3C%2FSPAN%3E%3C%2FI%3E%3CSPAN%3E%26nbsp%3B%C2%A9%20OpenStreetMap%20contributors.%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FH6%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fwww.naturalearthdata.com%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ENatural%20Earth%20Data%3C%2FA%3E%3A%20Natural%20Earth%20is%20a%20public%20domain%20map%20dataset%20available%20at%201%3A10m%2C%201%3A50m%2C%20and%201%3A110%20million%20scales.%20Featuring%20tightly%20integrated%20vector%20and%20raster%20data%2C%20with%20Natural%20Earth%20you%20can%20make%20a%20variety%20of%20visually%20pleasing%2C%20well-crafted%20maps%20with%20cartography%20or%20GIS%20software.%20So%2C%20if%20you%20need%20country%20boundaries%2C%20states%20or%20railroads%20in%20the%20world%20on%20a%20very%20general%20level%2C%20this%20is%20your%20data%20of%20choice.%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Ffreegisdata.rtwilson.com%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EFree%20GIS%20Data%3C%2FA%3E%3A%20The%20page%20contains%20a%20categorised%20list%20of%20links%20to%20over%20500%20sites%20providing%20freely%20available%20geographic%20datasets.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3ESo%2C%20after%20you%20have%20loaded%20some%20files%20to%20your%20local%20disk%2C%20you%20can%20start%20looking%20at%20different%20ways%20on%20how%20to%20import%20them%20to%20PostGIS.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1784622675%22%20id%3D%22toc-hId-1784622670%22%3E%3CSPAN%3ETools%20for%20importing%20data%20to%20PostGIS%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3EAlthough%20the%20official%20documentation%20states%20that%20there%20are%20%3CA%20href%3D%22https%3A%2F%2Fpostgis.net%2Fdocs%2Fusing_postgis_dbmanagement.html%23loading_geometry_data%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Etwo%20ways%20to%20get%20data%20into%20a%20PostGIS%2FPostgreSQL%20database%3C%2FA%3E%2C%20the%20ways%20to%20achieve%20this%20are%20much%20more%20diverse.%20The%20two%20recognized%20by%20the%20documentation%20are%20formatted%20SQL%20statements%20or%20using%20the%20Shape%20file%20loader%2Fdumper%20(shp2pgsql)%2C%20so%20we%20can%20start%20with%20those.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1819783147%22%20id%3D%22toc-hId--1819783152%22%3E%3CSPAN%3EFormatted%20SQL%3C%2FSPAN%3E%3C%2FH3%3E%0A%3CP%3ESo%20loading%20with%20plain%20SQL%20is%20not%20recommended%20and%20nobody%20probably%20does%20it%2C%20but%20going%20through%20this%20method%20first%2C%20gives%20you%20a%20good%20idea%20of%20what%20is%20happening%20behind%20the%20scenes.%20It%E2%80%99s%20important%20to%20know%20how%20PostGIS%20handles%20spatial%20data%20types%20and%20the%20content%20of%20the%20%3CA%20href%3D%22http%3A%2F%2Fpostgis.net%2Fworkshops%2Fpostgis-intro%2Fgeometries.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Egeometry%20column%3C%2FA%3E%2C%20which%20can%E2%80%99t%20store%20strings%20or%20integers.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20example%2C%20a%20valid%20insert%20statement%20to%20create%20and%20insert%20an%20OGC%20spatial%20object%20would%20be%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EINSERT%20INTO%20data.islands(geom%2C%20the_name%20)%0A%20%20VALUES%20(%20ST_GeomFromText('POINT(0%200)'%2C%204326)%2C%20'NULL%20ISLAND')%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnother%20example%20is%20a%20method%20where%20you%20already%20have%20latitude%20and%20longitude%20data%20as%20text%20or%20numbers%20in%20your%20table%20and%20you%20want%20to%20build%20a%20valid%20geometry%20object%20from%20those.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%20you%20need%20to%20add%20a%20geometry%20column%20for%20your%20data%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ESELECT%20AddGeometryColumn%20('data'%2C'islands'%2C'geom'%2C4326%2C'POINT'%2C2)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENext%20you%20could%20update%20the%20content%20of%20the%20geometry%20column%20based%20on%20the%20columns%20from%20the%20data%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EUPDATE%20data.islandsset%20geom%20%3D%20ST_SetSRID(ST_MakePoint(longitude%2C%20latitude)%2C4326)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20a%20nutshell%2C%20a%20simple%20insert%20works%2C%20but%20remember%20to%20build%20your%20geometry%20in%20a%20valid%20way!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20better%20performance%2C%20you%20should%20use%20COPY%20statements%20instead%20of%20INSERT%2C%20as%20it%20results%20in%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fwww.citusdata.com%2Fblog%2F2017%2F11%2F08%2Ffaster-bulk-loading-in-postgresql-with-copy%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Emuch%20better%20performance.%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-667729686%22%20id%3D%22toc-hId-667729681%22%3E%3CSPAN%3Eshp2pgsql%3C%2FSPAN%3E%3C%2FH3%3E%0A%3CP%3EThe%20most%20common%20data%20format%20for%20spatial%20data%20has%20traditionally%20been%20the%20ESRI%20shapefile.%20shp2pgsql%20is%20a%20command%20line%20tool%20to%20import%20ESRI%20shapefiles%20to%20the%20database.%20Under%20Unix%2C%20you%20can%20use%20the%20following%20command%20for%20importing%20a%20new%20PostGIS%20table%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Eshp2pgsql%20-s%20%3CSRID%3E%20-c%20-D%20-I%20%3CPATH%20to%3D%22%22%20shapefile%3D%22%22%3E%20%3CSCHEMA%3E.%3CTABLE%3E%20%7C%20%5C%0A%20%20psql%20-d%20%3CDATABASENAME%3E%20-h%20%3CHOSTNAME%3E%20-U%20%3CUSERNAME%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOn%20Windows%20it%E2%80%99s%20%3CA%20href%3D%22https%3A%2F%2Fwww.bostongis.com%2Fpgsql2shp_shp2pgsql_quickguide.bqg%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ejust%20as%20simple%3C%2FA%3E.%20Additionally%2C%20there%E2%80%99s%20the%20shp2pgsql-utility%2C%20which%20is%20a%20small%20GUI%20to%20import%20shapefiles%20to%20PostGIS.%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22tjukanov_1-1585224774796.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F179697iFC704F2934690DB8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22tjukanov_1-1585224774796.png%22%20alt%3D%22tjukanov_1-1585224774796.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1139724777%22%20id%3D%22toc-hId--1139724782%22%3E%3CSPAN%3Eogr2ogr%3C%2FSPAN%3E%3C%2FH3%3E%0A%3CP%3EThe%20tool%20I%20use%20most%20often%20for%20loading%20data%20to%20PostGIS%20is%20probably%20ogr2ogr.%20It%20is%20a%20very%20powerful%20tool%20to%20convert%20data%20into%20and%20from%20PostGIS%20%3CA%20href%3D%22https%3A%2F%2Fgdal.org%2Fdrivers%2Fvector%2Findex.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eto%20almost%20all%20possible%20vector%20data%20formats%3C%2FA%3E.%20GDAL%2FOGR%20is%20a%20software%20package%20that%20powers%20a%20great%20variety%20of%20different%20geospatial%20software%20tools%20and%20it%20comes%20with%20QGIS%20already%20installed%20on%20the%20server%20computer.%20Conveniently%20ogr2ogr%20comes%20with%20the%20QGIS%20installation%20and%20Windows%20users%20can%20access%20the%20commands%20directly%20through%20%3CA%20href%3D%22https%3A%2F%2Ftrac.osgeo.org%2Fosgeo4w%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EOSGeo4W%20Shell%3C%2FA%3E.%20So%20for%20a%20single%20import%20of%20a%20shapefile%2C%20you%20would%20run%20the%20following%20command%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Eogr2ogr%20-f%20%E2%80%9CPostgreSQL%E2%80%9D%20PG%3A%E2%80%9Dhost%3D%3CHOSTNAME%3E%20%20dbname%3D%3CDBNAME%3E%20user%3D%3CYOURUSERNAME%3E%20password%3D%3CYOURPASSWORD%3E%E2%80%9D%20%3CDIR%3E%5Cyourdatafile.shp%20-lco%20SCHEMA%3Dfoo%3C%2FDIR%3E%3C%2FYOURPASSWORD%3E%3C%2FYOURUSERNAME%3E%3C%2FDBNAME%3E%3C%2FHOSTNAME%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20parameters%20used%20to%20run%20the%20ogr2ogr-command%20here%20are%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E-f%20%20%20output%20file%20format%20name%0A-lco%20layer%20creation%20option%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECombining%20ogr2ogr%20commands%20with%20simple%20bash%20allow%20you%20to%20load%20a%20folder%20full%20of%20shapefiles!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Efor%20%25i%20in%20(*.shp)%20do%20ogr2ogr%20-update%20-append%20-f%20PostgreSQL%20PG%3A%22host%3D%3CHOSTNAME%3E%20port%3D5432%20dbname%3D%3CYOURDATABASENAME%3E%20user%3D%3CYOURUSER%3E%20password%3D%3CYOURPASSWORD%3E%20schemas%3Dmyshapefiles%22%20%25i%3C%2FYOURPASSWORD%3E%3C%2FYOURUSER%3E%3C%2FYOURDATABASENAME%3E%3C%2FHOSTNAME%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20small%20and%20simple%20script%20has%20been%20useful%20%3CSTRONG%3Emany%3C%2FSTRONG%3E%20times.%20This%20will%20create%20you%20a%20table%20automatically%20and%20append%20all%20the%20files%20to%20the%20correct%20tables.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1347788056%22%20id%3D%22toc-hId-1347788051%22%3E%3CSPAN%3ELoading%20data%20with%20QGIS%3C%2FSPAN%3E%3C%2FH3%3E%0A%3CP%3EQGIS%20allows%20a%20few%20different%20approaches%20when%20it%20comes%20to%20loading%20spatial%20data%20to%20PostGIS.%20In%20my%20previous%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-postgresql%2Fqgis-azure-database-for-postgresql-postgis%2Fba-p%2F1152249%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Eblog%20post%3C%2FA%3E%20you%20can%20find%20some%20basic%20information%20about%20QGIS%20and%20how%20to%20connect%20to%20PostGIS.%20Inside%20QGIS%20you%20can%20do%20data%20import%20through%20DB%20Manager%2C%20but%20I%20recommend%20using%20Export%20to%20PostgreSQL%2C%20as%20it%20is%20using%20COPY%20statements%20in%20the%20background%20rather%20than%20INSERTS%20and%20%3CA%20href%3D%22https%3A%2F%2Fgeosupportsystem.se%2F2018%2F09%2F03%2Fpostgis-uppfoljning-2%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eperformance%20is%20much%20better.%3C%2FA%3E%20You%20can%20load%20data%20layers%20from%20the%20project%20or%20from%20disk.%20Below%20you%20see%20a%20screenshot%20from%20the%20main%20dialog.%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22tjukanov_2-1585224774795.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F179696i587AFC5B74B964FC%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22tjukanov_2-1585224774795.png%22%20alt%3D%22tjukanov_2-1585224774795.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBesides%20ogr2ogr%20this%20is%20another%20tool%20for%20data%20imports%20that%20I%20use%20very%20often%2C%20as%20most%20of%20my%20workflows%20are%20strongly%20QGIS%20related.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--459666407%22%20id%3D%22toc-hId--459666412%22%3E%3CSPAN%3ELoading%20data%20with%20Python%20and%20psycopg2%3C%2FSPAN%3E%3C%2FH3%3E%0A%3CP%3EIf%20you%20are%20integrating%20PostGIS%20into%20an%20application%20and%20want%20to%20automate%20things%2C%20GUI-workflows%20are%20not%20viable%20options.%20Instead%20you%20might%20want%20to%20look%20into%20loading%20data%20with%20Python.%20The%20most%20obvious%20choice%20for%20making%20a%20connection%20from%20Python%20to%20PostGIS%20is%20%3CA%20href%3D%22https%3A%2F%2Fwiki.postgresql.org%2Fwiki%2FPsycopg2_Tutorial%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Epsycopg2%3C%2FA%3E.%20The%20most%20important%20thing%20to%20understand%20when%20working%20with%20psycopg2%20are%20the%20data%20types%20for%20geometries%20that%20was%20already%20mentioned%20earlier.%20Inserting%20latitude%20and%20longitude%20values%20as%20such%20to%20a%20geometry%20column%20%3CSTRONG%3Ewill%20not%20work%3C%2FSTRONG%3E%2C%20but%20instead%20you%20have%20to%20build%20your%20insert%20value%20with%20the%20%3CEM%3Elat%3C%2FEM%3E%20and%20%3CEM%3Elong%3C%2FEM%3E%20parameters%20as%20follows%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EST_SetSRID(ST_MakePoint(%25s%2C%20%25s)%2C%204326)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOther%20aspects%20of%20psycopg2%20workflows%20won%E2%80%99t%20differ%20much%20from%20normal%20Python-PostgreSQLdata%20pipelines.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-2027846426%22%20id%3D%22toc-hId-2027846421%22%3E%3CSPAN%3Eraster2pgsql%3C%2FSPAN%3E%3C%2FH3%3E%0A%3CP%3EIf%20you%20are%20working%20with%20raster%20data%2C%20the%20benefits%20of%20moving%20your%20data%20from%20a%20file%20to%20a%20database%20are%20not%20nearly%20as%20obvious%20as%20with%20vector%20data.%20However%20PostGIS%20offers%20%3CA%20href%3D%22https%3A%2F%2Fpostgis.net%2Fdocs%2FRT_reference.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Efunctionalities%3C%2FA%3E%20to%20store%20and%20analyze%20raster%20data%20and%20a%20wide%20range%20of%20raster%20functions.%20Check%20for%20example%20%3CA%20href%3D%22http%3A%2F%2Fblog.cleverelephant.ca%2F2019%2F08%2Fpostgis-3-raster.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ethis%20blog%20post%3C%2FA%3E%20about%20working%20with%20raster%20in%20PostGIS.%20For%20loading%20raster%20to%20PostGIS%2C%20two%20main%20options%20are%20%3CA%20href%3D%22https%3A%2F%2Fpostgis.net%2Fdocs%2Fusing_raster_dataman.html%23RT_Raster_Loader%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eraster2pgsql%3C%2FA%3E%20tool%20or%20GDAL%20with%20Python.%20Just%20like%20shp2pgqsl%2C%20raster2pgsql%20comes%20packaged%20with%20a%20PostGIS%20bundle%20installation.%3C%2FP%3E%0A%3CP%3EBasic%20structure%20of%20a%20raster2pgsql%20command%20is%20as%20follows%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Eraster2pgsql%20raster_options_go_here%20raster_file%20yourtable%20%26gt%3B%20out.sql%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EJust%20like%20shp2pgsql%2C%20this%20outputs%20a%20SQL%20file%20that%20you%20can%20then%20run%20in%20PostGIS.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--603854235%22%20id%3D%22toc-hId--603854240%22%3E%3CSPAN%3ETools%20for%20OpenStreetMap%20data%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3EOpenStreetMap%20is%20a%20slightly%20specific%20case%2C%20but%20worth%20going%20through%20here%20separately%2C%20as%20it%20is%20widely%20used.%20The%20OpenStreetMap%20data%20structure%20in%20a%20database%20is%20by%20default%20divided%20into%20lines%2C%20roads%2C%20points%20and%20polygons.%20Depending%20on%20the%20application%2C%20you%20might%20want%20to%20change%20the%20style%20the%20data%20is%20loaded.%20However%2C%20the%20default%20structure%20works%20well%20with%20many%20tools%20and%20services.%20Most%20Linux%20distributions%20include%20osm2pgsql%2C%20which%20is%20a%20good%20generic%20tool%20in%20importing%20a%20small%20piece%20of%20OSM%20data%20to%20PostGIS.%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fopenstreetmap%2Fosm2pgsql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eosm2pgsql%3C%2FA%3E%20is%20actively%20maintained%20and%20widely%20used.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20basic%20way%20to%20load%20the%20data%20into%20PostGIS%20for%20rendering%20would%20be%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Eosm2pgsql%20--create%20--database%20postgres%20data.osm.pbf%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20will%20load%20the%20data%20from%20%3CEM%3Edata.osm.pbf%3C%2FEM%3E%20into%20the%20%3CEM%3Eplanet_osm_point%3C%2FEM%3E%2C%20%3CEM%3Eplanet_osm_line%3C%2FEM%3E%2C%20%3CEM%3Eplanet_osm_roads%2C%3C%2FEM%3E%20and%20%3CEM%3Eplanet_osm_polygon%3C%2FEM%3E%20tables.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20thorough%20walkthrough%20on%20the%20osm2pgsql%20import%20process%20can%20be%20found%20at%20%3CA%20href%3D%22https%3A%2F%2Fwww.cybertec-postgresql.com%2Fen%2Fopen-street-map-to-postgis-the-basics%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ethis%20blog%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fomniscale%2Fimposm3%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EImposm3%3C%2FA%3E%2C%20written%20in%20Go%2C%20is%20designed%20to%20create%20databases%20that%20are%20optimized%20for%20rendering.%20You%20need%20a%20json%20mapping%20to%20define%20the%20data%20schema.%20For%20a%20simple%20import%20with%20imposm%20you%20could%20try%20the%20following%20command%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Eimposm%20import%20-connection%20postgis%3A%2F%2Fuser%3Apassword%40host%2Fdatabase%20%5C%0A%20%20%20%20-mapping%20mapping.json%20-read%20%2Fpath%2Fto%2Fosm.pbf%20-write%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYet%20another%20tool%20for%20OpenStreetMap%20data%20is%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fopenstreetmap%2Fosmosis%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EOsmosis%3C%2FA%3E.%20It%E2%80%99s%20a%20command%20line%20Java%20application%20for%20processing%20OSM%20data.%20Just%20like%20imposm3%2C%20Osmosis%20also%20creates%20a%20new%20data.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1883658598%22%20id%3D%22toc-hId-1883658593%22%3E%3CSPAN%3EBonus%3A%20Foreign%20data%20wrapper%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3EAs%20a%20bonus%2C%20if%20you%20don%E2%80%99t%20want%20to%20load%20data%20at%20all%2C%20I%20should%20also%20mention%20the%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fpramsey%2Fpgsql-ogr-fdw%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eogr_fdw%3C%2FA%3E.%20If%20you%20are%20familiar%20with%20traditional%20PostgreSQL%20foreign%20data%20wrappers%2C%20this%20is%20basically%20the%20same%20thing%20but%20for%20spatial%20data.%20Basically%2C%20the%20data%20can%20be%20in%20a%20ESRI%20shapefile%20or%20a%20shapefile%2C%20and%20stored%20to%20PostGIS%20as%20a%20foreign%20table.%3C%2FP%3E%0A%3CP%3EThe%20ogr_fdw%20is%20not%20yet%20available%20on%20Azure%20Database%20for%20PostgreSQL%2C%20but%20adding%20it%20to%20the%20selection%20of%20extensions%20has%20also%20been%20%3CA%20href%3D%22https%3A%2F%2Ffeedback.azure.com%2Fforums%2F597976-azure-database-for-postgresql%2Fsuggestions%2F19844545-add-ogr-fwd-support-extension%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Erequested%20as%20a%20feature%3C%2FA%3E%2C%20so%20throw%20the%20request%20an%20upvote%20if%20you%20would%20like%20to%20see%20it%20there%20too!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-76204135%22%20id%3D%22toc-hId-76204130%22%3E%3CSPAN%3EConclusions%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3ENow%20you%20might%20be%20overwhelmed%20with%20the%20number%20of%20tools%20available%20to%20achieve%20the%20same%20goal.%20But%20what%20is%20the%20%3CSTRONG%3Ebest%3C%2FSTRONG%3E%20way%20of%20loading%20data%20to%20PostGIS%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIt%20depends.%20Are%20you%20loading%20static%20files%20or%20something%20through%20a%20live%20API%3F%20Do%20you%20have%20PostGIS%20installed%20on%20your%20own%20server%20or%20do%20you%20have%20a%20hosted%20service%3F%20How%20will%20you%20be%20using%20the%20data%3F%20For%20example%2C%20with%20OpenStreetMap%20data%2C%20you%20must%20consider%20whether%20you%E2%80%99re%20updating%20the%20data%20or%20performing%20a%20one-time%20load.%20Note%20that%20also%20ETL%20tools%20such%20as%20%3CA%20href%3D%22https%3A%2F%2Fwww.safe.com%2Ffme%2Fkey-capabilities%2Fspatial-etl%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EFME%3C%2FA%3E%20and%20%3CA%20href%3D%22https%3A%2F%2Fwww.deegree.org%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EDeegree%3C%2FA%3E%20allow%20data%20loading%2C%20if%20those%20fit%20your%20purpose%20better%20than%20any%20of%20the%20tools%20I%E2%80%99ve%20presented%20here.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHopefully%2C%20this%20gives%20you%20a%20good%20overview%20about%20the%20different%20ways%20to%20get%20spatial%20data%20into%20your%20PostgreSQL%20tables.%20Happy%20exploring!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EAbout%20the%20Author%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EI%20train%20and%20consult%20on%20PostGIS%2C%20QGIS%2C%20and%20other%20FOSS4G%20(%3D%20Free%20Open%20Source%20Software%20for%20Geospatial)%20at%20%3CA%20href%3D%22https%3A%2F%2Fwww.gispo.fi%2Fen%2Fhome%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EGispo%20Ltd%3C%2FA%3E%2C%20in%20Finland.%20I%20have%20my%20%3CA%20href%3D%22https%3A%2F%2Ftjukanov.org%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Epersonal%20portfolio%20website%20with%20a%20gallery%3C%2FA%3E%20of%20geospatial%20visualizations.%3C%2FP%3E%3C%2FUSERNAME%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1255421%22%20slang%3D%22en-US%22%3E%3CP%3EIf%20you%E2%80%99re%20new%20to%20PostGIS%20and%20want%20to%20try%20it%20out%2C%20one%20of%20the%20first%20things%20you%E2%80%99ll%20want%20to%20do%20is%20to%20import%20some%20data%20into%20your%20database.%20I%20want%20to%20provide%20an%20overview%20of%20how%20to%20accomplish%20this.%20The%20post%20provides%20an%20introduction%20to%20some%20common%20data%20import%20terminology%2C%20details%20on%20where%20to%20find%20spatial%20data%20and%20tools%20for%20importing%20data%20to%20PostGIS%2C%20and%20specific%20instructions%20for%20how%20to%20use%20each%20tool%20to%20perform%20the%20import.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1255421%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Edata%20loading%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Egeospatial%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Egis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eogr%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epostgis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eqgis%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3C%2FHOSTNAME%3E%3C%2FDATABASENAME%3E%3C%2FTABLE%3E%3C%2FSCHEMA%3E%3C%2FPATH%3E%3C%2FSRID%3E%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
Regular Visitor

If you’re new to PostGIS and want to try it out, one of the first things you’ll want to do is to import some data into your database. I want to provide an overview of how to accomplish this. The post provides an introduction to some common data import terminology, details on where to find spatial data and tools for importing data to PostGIS, and specific instructions for how to use each tool to perform the import.

 

For an introduction to PostGIS, see my previous blog post here.

 

Introduction

If you’ve worked with other data types before, then most of the process for importing spatial data will be self-explanatory. However, for someone working with spatial data, acronyms like CRS and SRID might be confusing.

 

For data import, you should know the coordinate reference system (CRS) your data is in and which spatial reference identifier (SRID) number is used to reference that specific CRS. The SRID defines all the parameters of a data set’s geographic coordinate system and projection. Using an SRID is convenient because it packs all the information about a map projection into a single number. When creating spatial objects for insertion into the database, the SRID is required.

 

Where to find spatial data?

Before you can load any data to your database, you need to acquire it. I think that one of the most underrated skills for GIS experts is to know where to find suitable data. If you are using PostGIS to manage the data that you have in your organization already, you don’t have to start your process by searching new data, but rather working with your own. I have added here a few data sources that you can try out:

  • OpenStreetMap: OpenStreetMap (OSM) is a collaborative project to create a free editable map of the world. Many people refer to OSM as the Wikipedia of maps. If you seek easy ways to get an extract of the data, you can check for GeoFabrik for Shapefiles or osmdata.xyz for GeoPackages.

tjukanov_0-1585224774798.png

 

OpenStreetMap data from Africa. All the roads in the OSM database.  © OpenStreetMap contributors. 
  • Natural Earth Data: Natural Earth is a public domain map dataset available at 1:10m, 1:50m, and 1:110 million scales. Featuring tightly integrated vector and raster data, with Natural Earth you can make a variety of visually pleasing, well-crafted maps with cartography or GIS software. So, if you need country boundaries, states or railroads in the world on a very general level, this is your data of choice.
  • Free GIS Data: The page contains a categorised list of links to over 500 sites providing freely available geographic datasets.

So, after you have loaded some files to your local disk, you can start looking at different ways on how to import them to PostGIS.

 

Tools for importing data to PostGIS

Although the official documentation states that there are two ways to get data into a PostGIS/PostgreSQL database, the ways to achieve this are much more diverse. The two recognized by the documentation are formatted SQL statements or using the Shape file loader/dumper (shp2pgsql), so we can start with those.

 

Formatted SQL

So loading with plain SQL is not recommended and nobody probably does it, but going through this method first, gives you a good idea of what is happening behind the scenes. It’s important to know how PostGIS handles spatial data types and the content of the geometry column, which can’t store strings or integers.

 

For example, a valid insert statement to create and insert an OGC spatial object would be:

 

INSERT INTO data.islands(geom, the_name )
  VALUES ( ST_GeomFromText('POINT(0 0)', 4326), 'NULL ISLAND');

 

Another example is a method where you already have latitude and longitude data as text or numbers in your table and you want to build a valid geometry object from those.

 

First you need to add a geometry column for your data:

 

SELECT AddGeometryColumn ('data','islands','geom',4326,'POINT',2);

 

Next you could update the content of the geometry column based on the columns from the data:

 

UPDATE data.islandsset geom = ST_SetSRID(ST_MakePoint(longitude, latitude),4326);

 

In a nutshell, a simple insert works, but remember to build your geometry in a valid way!

 

For better performance, you should use COPY statements instead of INSERT, as it results in much better performance.

 

shp2pgsql

The most common data format for spatial data has traditionally been the ESRI shapefile. shp2pgsql is a command line tool to import ESRI shapefiles to the database. Under Unix, you can use the following command for importing a new PostGIS table:

 

shp2pgsql -s <SRID> -c -D -I <path to shapefile> <schema>.<table> | \
  psql -d <databasename> -h <hostname> -U <username>

 

On Windows it’s just as simple. Additionally, there’s the shp2pgsql-utility, which is a small GUI to import shapefiles to PostGIS.

 

tjukanov_1-1585224774796.png

 

ogr2ogr

The tool I use most often for loading data to PostGIS is probably ogr2ogr. It is a very powerful tool to convert data into and from PostGIS to almost all possible vector data formats. GDAL/OGR is a software package that powers a great variety of different geospatial software tools and it comes with QGIS already installed on the server computer. Conveniently ogr2ogr comes with the QGIS installation and Windows users can access the commands directly through OSGeo4W Shell. So for a single import of a shapefile, you would run the following command:

 

ogr2ogr -f “PostgreSQL” PG:”host=<hostname>  dbname=<dbname> user=<yourusername> password=<yourpassword>” <dir>\yourdatafile.shp -lco SCHEMA=foo

 

The parameters used to run the ogr2ogr-command here are:

 

-f   output file format name
-lco layer creation option

 

Combining ogr2ogr commands with simple bash allow you to load a folder full of shapefiles!

 

for %i in (*.shp) do ogr2ogr -update -append -f PostgreSQL PG:"host=<hostname> port=5432 dbname=<yourdatabasename> user=<youruser> password=<yourpassword> schemas=myshapefiles" %i

 

This small and simple script has been useful many times. This will create you a table automatically and append all the files to the correct tables.

 

Loading data with QGIS

QGIS allows a few different approaches when it comes to loading spatial data to PostGIS. In my previous blog post you can find some basic information about QGIS and how to connect to PostGIS. Inside QGIS you can do data import through DB Manager, but I recommend using Export to PostgreSQL, as it is using COPY statements in the background rather than INSERTS and performance is much better. You can load data layers from the project or from disk. Below you see a screenshot from the main dialog.

 

tjukanov_2-1585224774795.png

 

Besides ogr2ogr this is another tool for data imports that I use very often, as most of my workflows are strongly QGIS related.

 

Loading data with Python and psycopg2

If you are integrating PostGIS into an application and want to automate things, GUI-workflows are not viable options. Instead you might want to look into loading data with Python. The most obvious choice for making a connection from Python to PostGIS is psycopg2. The most important thing to understand when working with psycopg2 are the data types for geometries that was already mentioned earlier. Inserting latitude and longitude values as such to a geometry column will not work, but instead you have to build your insert value with the lat and long parameters as follows:

 

ST_SetSRID(ST_MakePoint(%s, %s), 4326)

 

Other aspects of psycopg2 workflows won’t differ much from normal Python-PostgreSQLdata pipelines.

 

raster2pgsql

If you are working with raster data, the benefits of moving your data from a file to a database are not nearly as obvious as with vector data. However PostGIS offers functionalities to store and analyze raster data and a wide range of raster functions. Check for example this blog post about working with raster in PostGIS. For loading raster to PostGIS, two main options are raster2pgsql tool or GDAL with Python. Just like shp2pgqsl, raster2pgsql comes packaged with a PostGIS bundle installation.

Basic structure of a raster2pgsql command is as follows:

 

raster2pgsql raster_options_go_here raster_file yourtable > out.sql

 

Just like shp2pgsql, this outputs a SQL file that you can then run in PostGIS.

 

Tools for OpenStreetMap data

OpenStreetMap is a slightly specific case, but worth going through here separately, as it is widely used. The OpenStreetMap data structure in a database is by default divided into lines, roads, points and polygons. Depending on the application, you might want to change the style the data is loaded. However, the default structure works well with many tools and services. Most Linux distributions include osm2pgsql, which is a good generic tool in importing a small piece of OSM data to PostGIS. osm2pgsql is actively maintained and widely used.

 

A basic way to load the data into PostGIS for rendering would be:

 

osm2pgsql --create --database postgres data.osm.pbf

 

This will load the data from data.osm.pbf into the planet_osm_point, planet_osm_line, planet_osm_roads, and planet_osm_polygon tables.

 

A thorough walkthrough on the osm2pgsql import process can be found at this blog.

Imposm3, written in Go, is designed to create databases that are optimized for rendering. You need a json mapping to define the data schema. For a simple import with imposm you could try the following command:

 

imposm import -connection postgis://user:password@host/database \
    -mapping mapping.json -read /path/to/osm.pbf -write

 

Yet another tool for OpenStreetMap data is Osmosis. It’s a command line Java application for processing OSM data. Just like imposm3, Osmosis also creates a new data.

 

Bonus: Foreign data wrapper

As a bonus, if you don’t want to load data at all, I should also mention the ogr_fdw. If you are familiar with traditional PostgreSQL foreign data wrappers, this is basically the same thing but for spatial data. Basically, the data can be in a ESRI shapefile or a shapefile, and stored to PostGIS as a foreign table.

The ogr_fdw is not yet available on Azure Database for PostgreSQL, but adding it to the selection of extensions has also been requested as a feature, so throw the request an upvote if you would like to see it there too!

 

Conclusions

Now you might be overwhelmed with the number of tools available to achieve the same goal. But what is the best way of loading data to PostGIS?

 

It depends. Are you loading static files or something through a live API? Do you have PostGIS installed on your own server or do you have a hosted service? How will you be using the data? For example, with OpenStreetMap data, you must consider whether you’re updating the data or performing a one-time load. Note that also ETL tools such as FME and Deegree allow data loading, if those fit your purpose better than any of the tools I’ve presented here.

 

Hopefully, this gives you a good overview about the different ways to get spatial data into your PostgreSQL tables. Happy exploring!

 

About the Author

I train and consult on PostGIS, QGIS, and other FOSS4G (= Free Open Source Software for Geospatial) at Gispo Ltd, in Finland. I have my personal portfolio website with a gallery of geospatial visualizations.