Diskussion:PostGIS Terminal

Aus Geometa Lab OST
Zur Navigation springen Zur Suche springen

Please note here your questions and feedback below. You can also send them directly to Stefan Keller.

Feature Requests

  • Markers mit Maus-über ("hover") - wären toll... nicht Popup und wegklicken müssen! -
  • Features klickbar machen mit Link zu OSM mit osm_id, z.B. http://www.openstreetmap.org/browse/way/82142220
  • Data... Next update scheduled on ....
  • DB-unabhängig machen! D.h. "Weitere Parameter" (aus config.php) in HTML/Javascript laden.
  • DB Zugangsparameter mit Port und Zugangsweise (Text, TSL, SSL) erweitern.
  • Besseres Verwalten von Layern, die aus Map und Marker Queries entstanden sind.
  • Wie geht das mit Wait-Cursor in OpenLayers???

More Queries...

Marker Queries

 --- marker query (must have exactly field names lon, lat, title and description):
 SELECT X(p2.way) AS lon, Y(p2.way) AS lat, 'Briefkasten' AS title, p2.ref AS description
 FROM planet_osm_polygon p1
 JOIN planet_osm_point p2 ON CONTAINS(p1.way, p2.way)
 WHERE p1.name = 'Uster'
 AND p2.amenity = 'post_box'
 --- Extension with user defined marker icon (attribute 'icon'):
 SELECT X(p2.way) AS lon, Y(p2.way) AS lat, 'Briefkasten' AS title, p2.ref AS description, 'http://myserver/marker.png' as icon 
 FROM planet_osm_polygon p1
 JOIN planet_osm_point p2 ON CONTAINS(p1.way, p2.way)
 WHERE p1.name = 'Uster'
 AND p2.amenity = 'post_box'

All Tag-Value-Pairs of OSM data

Kann anstelle mit osm_point auch mit osm_all durchgeführt werden.

 --
 -- Key-Value Statistics of OSM Data
 -- Return all key-value-pairs of type 'enum' without some 
 -- types numeric, date/time etc. chosen by hand:
 --
 SELECT tmp.key||'='||tmp.value as kvp, count(*)::integer as freq 
 FROM (
   SELECT (each(tags)).key as key, (each(tags)).value as value 
   FROM osm_point) as tmp
 WHERE (trim(value) !~ '^[-]*[0-9,.:\ ]+[m]*$')
 AND NOT (value ILIKE '%fixme%' OR key ILIKE '%fixme%')
 AND key NOT LIKE '%:%'
 AND key NOT LIKE '%description%'
 AND key NOT LIKE '%comment%'
 AND key NOT LIKE '%name'
 AND key NOT LIKE 'uic_%'
 AND key NOT LIKE '%ref'
 AND key NOT ILIKE '%fixme%'
 AND key NOT ILIKE '%todo%'
 AND key NOT IN ('name','operator','_picture_','_waypoint_','address','alt','is_in','url','website','wikipedia','email',
                 'converted_by','phone','information','opening_hours','date','time','collection_times','colour','fee',
                 'population','access','noexit','towards','bus_routes','busline','lines','type','denotation',
                 'CONTINUE','continue','copyright','stop')
 GROUP BY tmp.key, tmp.value
 HAVING COUNT(*) > 1
 ORDER by key, freq DESC

XAPI-to-Map

Die XAPI-to-Map-Funktion wandelt Anfragen in der Query-Sprache XAPI nach SQL um, fragt die OSM-Datenbank ab und stellt das Resultat direkt in der Webkarte dar. XAPI-to-Map ist - wie der Name sagt - kein Webservice (Maschine-Maschine) sondern eine Mensch-Maschine-Webapplikation. Die Syntax kennt häufige Requests, bestehend aus einem oder mehrerer Tag/Value-Paar sowie ggf. einer Bounding Box-Angabe. Nicht implementiert ist z.B. 'Union-Filter' (OR) im Key (node[amenity|leisure=golf_course]). Im Gegensatz zu anderen XAPI-Implementationn kann man dafür mehrere Filter-Prädikate angeben (z.B. node[amenity=golf_course]node[leisure=golf_course]).

Supported syntax elements:

    • Abbildung aller Tabellen sowie '*' (siehe Tabellen-Abbildungen unten)
    • Equality Filter (1x): way[highway=motorway]
    • Wildcard Filter: im Value way[highway=*]
    • BBox-Filter (1x): [bbox=left,bottom,right,top]
    • 'Union-Filter' (OR) im Value: way[highway=motorway|motorway_link|trunk|primary]
    • Mehrere Filter-Prädikate: node[amenity=golf_course]node[leisure=golf_course]

Notes:

  • Special query characters - except A-Z, a-z and " - must be "percent-encoded US-ASCII octet". There are online tools to help out like this one. (see [1]
  • It's not possible to do an OR query with nodes OR ways. This is a limitation of the XAPI syntax.

Some test examples:

 Use PostGIS Terminal for <postgisterminal_url>:
 <postgisterminal_url>?xapi=node[tourism=zoo]</nowiki> 
 <postgisterminal_url>/?xapi=node[amenity=*][bbox=5.943,45.935,10.612,47.731]</nowiki>
 <postgisterminal_url>/?xapi=node[highway=busstop]</nowiki>
 <postgisterminal_url>/?xapi=node[amenity=hospital][bbox=-6,50,2,61]?zoom=18&lat=47.223&lon=8.822&layers=B0T</nowiki>
 <postgisterminal_url>/?xapi=*[seamark:type=signal_station_warning]</nowiki>

Original XAPI-Dokumentation und andere XAPI-Server: Siehe XAPI

PostGIS-Terminal Documentation

Database

Das PostGIS-Terminal-Schema (siehe Osm2pgsql) wurde angepasst (wo überhaupt möglich) mittels der Konfigurations-Daten (.style). Die Attribute osm_id und osm_version (aus Oms2pgsql-Option "-extended-attributes") in der Tabellen-Gruppe 'Osm2pgsql' können nicht umbenannt werden (hardcoded).

Tabellen-Gruppe 'OSM-Original': Tabellen angelehnt an originale OSM-Planet-Struktur (jedoch mit Tags als Array):

  • osm_nodes => Node mit id, lat, lon, tags (Array) (gem. default.style)
  • osm_ways => Way mit Set of references to Node, d.h. ohne eigene Geometrie (gem. default.style).
  • osm_rels => Relation mit Referenz auf Nodes, Ways, Relations (gem. default.style).

Tabellen-Gruppe 'Osm2pgsql': Tabellen mit Geometrien (PostGIS) zusammengestellt aus aus osm_nodes und osm_ways (mit Tags als hstore durch osm2pgsql '-h'-Flag ):

  • osm_point => Punktobjekte mit den Attribten osm_id, versch. weitere (gem. default.style) plus osm_version. Enthält nur Nodes, die auch Tags enthalten (also keine, die nur Stützpunkte von Linien/Rändern sind).
  • osm_line => Linienobjekte aus Ways-Tabelle mit 'echtem' Geometrie-Attribut, sowie osm_id, versch. weitere (gem. default.style) plus osm_version. Enthält auch Ways, die eigentlich Flächen bilden. Enthält Objekte mit (möglicherweise identischen!) negativen osm_id's; das sind Ways, die in der OSM-DB über Relations zusammengesetzt sind.
  • osm_polygon => Flächenobjekte (gebildet aus bestimmten Ways und Relations), osm_id, versch. Attr. (gem. default.style) plus osm_version

Spezialtabellen:

  • osm_roads => linienhafte Strassenobjekte => fehlt, da nicht verwendet! (aus .style auskommentiert)
  • osm_all => View über osm_point, osm_line und osm_polygon über die Kolonnen osm_id, name, tags, gtype, way, osm_version (ACHTUNG: z.Zt. nicht-materialisiert, d.h. langsam!).
  • osm_poi => Tabelle als View über osm_point und osm_polygon mit ST_Centroid mit den Kolonnen osm_id, name, tags, gtype, way, osm_version (materialisierte View).

Duplikate:

  • Original OSM tables have separate ids and no duplicates in each table.
  • Table osm_point has no duplicates.
  • Table osm_lines has duplicates with neg. and pos. osm_ids. Line rows with duplicate neg. osm_ids got the (positive) osm_id from relations. Lines also have rows with duplicate pos. osm_ids. These belong somehow together since they have identical tags but different geometries. In original db there is only one http://www.openstreetmap.org/browse/way/47926394
  • Table osm_polygon have duplicates too but only with negative osm_ids. Like Lines, Polygon rows with duplicate neg. osm_ids got the (positive) osm_id from relations.

XAPI-to-Map

Tabellen-Abbildungen auf Basis osm2pgsql-Schema, die dann Grundlage der:

  • point, line, polygon => osm_point, osm_line, osm_polygon (zusätzlich zur XAPI-Syntax).
  • node => osm_point
  • way => osm_polygon
  • relation => osm_rels
  • '*' => osm_all

XAPI-zu-SQL-Abbildungen:

Implementations-Details:

  • Wegen '[', '*' sowie ':' (in Keys wie 'name:de') muss die URL kanonisiert werden?
  • Spaces sind in Values auch zugelassen, vgl. node[name=Knie's Kinderzoo]
  • das XAPI-to-Map funktioniert auch mit Permalink, z.B. node[amenity=hospital][bbox=-6,50,2,61]?zoom=18&lat=47.223&lon=8.822&layers=B0T
  • Für häufige Abrafen siehe auch die JXAPI-Logs (live!) sowie den Log-Backup von XAPI mit unbestätigter Herkunft.
  • Ev. Zusätzliche Views, Tabellen und Indexes anlegen.
  • Primary Keys prüfen! Bei mir hatten osm_point, osm_line, osm_polygon keine!!
  • Prüfen was schneller ist: DROP osm_node und all das CREATE... oder TRUNCATE TABLE osm_node und INSERT...
  • osm_roads => streichen aus default-style?

Beispiel (man beachte, dass nicht der '->'- sondern der '@>'-Operator verwendet wird wegen GiST-Index):

 .../node[tourism=zoo][bbox=6.2,46.1,10.0,47.6]
 wird abgebildet auf: 
 SELECT ST_AsText(way) geom, name||' '||osm_id label
 FROM osm_point
 WHERE tags @> hstore('tourism','zoo') -- nicht hstore(tags)->'tourism' = 'zoo'
 -- OR ... abhängig von XAPI-Syntax
 AND ST_Contains(ST_Transform(ST_SetSRID('BOX(6.2 46.1, 10.0 47.6)'::box2d, 4326),900913),way)
 .../way[xxx]
 wird abgebildet auf osm_line (oder osm_polygon?)

Security Features

GRANTS: Rechte entziehen, damit User Tables und Views nicht erzeugen oder löschen können!

 Schema nicht PUBLIC? Test: create table customer (id serial, name text); drop table customer; !!!
 Konfigurieren PostgreSQL Server (postgresql.conf parameters): Disabling autovacuum daemon, fsync=off etc.

DB-Server-Timeout: als Parameter der PostgreSQL-Verbindung in process-query.php (oder als "statement_timeout (60000)" in postgresql.conf); siehe [2]

 SET STATEMENT_TIMEOUT TO 60000;
 SET transaction_read_only TO TRUE;
 ALTER DATABASE x SET default_transaction_read_only = on;

Logging: Log von IP, Query-String und Response-Infos (success/error/etc. und Prozess-Zeit) speichert. Lösung über process-query.php oder Analyse (grep) Apache-Log?

Data Features

Schema configuration: Style-Datei anpassen, so dass Tabellennamen ohne "planet" und ohne Underscores erzeugt werden, also z.B. anstelle „planet_osm_point“ nur „osmpoint“.

Associative Array: hstore (-h Parameter bei osm2pgsql)

Additional Views and Tables:

 CREATE VIEW osm_all AS 
 SELECT osm_id, name, tags, 'pt' AS gtyp, way
 FROM osm_point
 UNION
 SELECT osm_id, name, tags, 'ln' AS gtyp, way
 FROM osm_line
 UNION
 SELECT osm_id, name, tags, 'po' AS gtyp, way
 FROM osm_polygon
 CREATE INDEX osm_point_name_idx
 ON osm_point(name) WITH (FILLFACTOR=100);
 CREATE INDEX osm_point_tags_idx
 ON osm_point USING gist(tags) WITH (FILLFACTOR=100);
 -- Kann "FEHLER:  invalid hstore value found" ergeben!
 
 ALTER TABLE osm_point CLUSTER ON osm_point_name_idx;
 VACUUM FREEZE ANALYZE;  -- das kann dauern...

Nightly Update: Einrichten eines Cron-Jobs, der einmal pro Nacht die Schweiz neu einspielt (Bitte Vorschlag).

Webservice indicatind database update time and a list of tables => about-db-query.php

Marker Query with attribute 'icon', mit dem man Icons von externen Quellen (url) darstellen kann.