[GIS] Importing OSM data into PostGIS and display into QGIS

osm2pgsqlpostgispostgresqlqgis

I am new to all things GIS! However, I need to set up a full GIS infrastructure for a project.

I have installed a Postgres server with PostGIS. That is on ancient hardware for now, 32bits processor with 1GB of RAM, Ubuntu 12.04.

I have imported OSM data from geofabrik.de into my server using osm2pgsql.

The first issue I had was a problem with the osm.pbf files ("Document is empty").
I was more lucky with bz2.
I "just" have an error message at the end but as it is at clean up time, I thought it was OK.

My command looks like this :

/usr/bin/osm2pgsql -d osm -s -K -S /usr/share/osm2pgsql/default.style languedoc-roussillon-latest.osm.bz2

The import log looks like this :

Committing transaction for planet_osm_polygon
Committing transaction for planet_osm_line
Committing transaction for planet_osm_roads
Sorting data and creating indexes for planet_osm_polygon
Sorting data and creating indexes for planet_osm_roads
Sorting data and creating indexes for planet_osm_line
Committing transaction for planet_osm_point
Sorting data and creating indexes for planet_osm_point
Completed planet_osm_point
Completed planet_osm_roads
Completed planet_osm_line
Completed planet_osm_polygon
Stopping table: planet_osm_nodes
Stopping table: planet_osm_ways
Stopping table: planet_osm_rels
COPY_END for COPY planet_osm_nodes FROM STDIN;
 failed: ERROR:  duplicate key value violates unique constraint "planet_osm_nodes_pkey"
DETAIL:  Key (id)=(2147483647) already exists.
CONTEXT:  COPY planet_osm_nodes, line 12179900

Error occurred, cleaning up
Stopped table: planet_osm_rels
Stopped table: planet_os_ways

Unfortunately, the data does not look good when I connect QGIS to my PosGIS DB.

It is very strange, but it seems that all polygons include one point at the center of the map.

I am not sure what is wrong or if it is related to the error that occurs at the end of the import.

Best Answer

This is a well known issue in osm2pgsql. See https://github.com/openstreetmap/osm2pgsql/issues/16 . This issue is raised when osm2pgsql slim mode is used and multiple geometries are imported. Don't use osm2pgsql slim mode, then the issue will probably go away.

edit: Or it might be a stale version of osm2pgsql: https://help.openstreetmap.org/questions/24070/failed-error-duplicate-key-value-violates-unique-constraint . osm2pgsql was switched from 32bit to 64bit ID space some time ago, not all releases of osm2pgsql can handle 64 bit osm ids. Note this has nothing to do with the "bittiness" of your processor or operating system.