Osmium/Data processing

From OpenStreetMap Wiki
Jump to navigation Jump to search

General example of data processing from overpass API -> osm xml file -> osmium -> pg COPY SQL sctipt -> PostGIS table

Example of data processing using overpass api site

#!/bin/bash
database='GIS for examples'; #example of name
import_name='Paris'; #example of description
bbox=' .... '; #a bbox for downloading
apiadr="http://overpass-api.de/api/interpreter?data=[out:xml];(++node($bbox);++%3C;);out+meta;";

s=$(date '+%s');
f="$import_name $s"; # filename

wget "$apiadr" -O "$f.osm";
osmium export --config='osmium.conf' -f pg "$f.osm" -o "$f.pg" && echo "osmium ✔";
echo "PostGIS geom: "$(wc -l "$f.pg"); #number of records for postGIS
echo "truncate table \"public\".\"OSM $import_name\";" | psql -e -d "$database";
echo "\\copy \"public\".\"OSM $import_name\" FROM '$f.pg';" | psql -e -d "$database";
r=$?;

#if there is materialized views depended of \"OSM $import_name\" table try here to refresh it
# echo " refresh materialized view \"$2\".\"∀\";" | psql -e -d "$database";
if [ $r == 0 ]; then
  echo "postgis ✔";
  xz -z -9 "$f.osm"; # make archived osm data
  rm -v "$f.osm"; # delete original osm data
  rm -v "$f.pg"; # delete PostGIS COPY sctipt by the OSM data
fi;


Example of osmium.conf (full export)

{
"attributes": {
   "type":      true,
   "id":        true,
   "version":   true,
   "changeset": true,
   "timestamp": true,
   "uid":       true,
   "user":      true,
   "way_nodes": true
   },
"linear_tags":  true,
"area_tags":    true,
"exclude_tags": [],
"include_tags": []
}


Example of table for import through pg COPY, determined by osmium.conf (for full export)

CREATE TABLE public."OSM example" (
	"Original geometry" geometry NULL,
	"Type of OSM object" varchar(8) NULL,
	"Id of OSM object" int8 NULL,
	"Version" int4 NULL,
	"Last changeset" int4 NULL,
	"Last uid" int4 NULL,
	"Last editor username" varchar(256) NULL,
	"Timestamp of version" timestamptz(0) NULL,
	"way_nodes" _int8 NULL,
	"All tags" jsonb NULL
);