Skip to content

Commit a7e1cfc

Browse files
committed
update and document view.sql
to take into account several different osm2pgsql options when creating the database
1 parent cae682e commit a7e1cfc

1 file changed

Lines changed: 101 additions & 34 deletions

File tree

Lines changed: 101 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,110 @@
1+
/************** ABOUT TABLES *****************/
2+
/*
3+
- without the --slim option, only tables _point _line and _polygon are created, with filtered data. These tables also contain the geometry.
4+
- with --slim, 3 other tables containing the raw OSM elements are created : _nodes, _ways, _rels
5+
*/
6+
7+
/************** ABOUT TAGS COLUMNS *****************/
8+
/*
9+
- tags in tables _nodes, _ways, _rels are identical to the ones in OSM (they are "raw data" tables)
10+
- but tags in tables _point _line and _polygon are always (slightly) different depending on the hstore options:
11+
# With --hstore any tags without a column will be added to the hstore column.
12+
# With --hstore-all all tags are added to the hstore column unless they appear in the style file with a delete flag
13+
*/
14+
15+
/************** ABOUT METADATA *****************/
16+
/* adding metadata to the DB requires -x option */
17+
/* without it, all the CAST(tags->...) will return NULL */
18+
19+
20+
/************** NODES *****************/
21+
/* read data only in table planet_osm_point since planet_osm_nodes may be absent if you used flat nodes mode or did not use --slim */
122
CREATE OR REPLACE TEMP VIEW node AS
223
SELECT
324
osm_id AS id,
4-
/* CAST(tags->'osm_version AS integer)' */ NULL::integer AS version,
5-
/* CAST(tags->'osm_timestamp' AS timestamp without time zone) */ NULL::timestamp without time zone AS created,
6-
/* CAST(tags->'osm_changeset' AS bigint) */ NULL::bigint AS changeset,
7-
/* CAST(tags->'osm_uid' AS integer) */ NULL::integer AS uid,
8-
/* CAST(tags->'osm_user' AS text) */ NULL::text AS user,
25+
CAST(tags->'osm_version' AS integer) AS version,
26+
CAST(tags->'osm_timestamp' AS timestamp without time zone) AS created,
27+
CAST(tags->'osm_changeset' AS bigint) AS changeset,
28+
CAST(tags->'osm_uid' AS integer) AS uid,
29+
CAST(tags->'osm_user' AS text) AS user,
930
to_jsonb(tags) AS tags,
1031
NULL::bigint[] AS nodes,
1132
NULL::jsonb AS members,
1233
way AS geom,
1334
'n' AS osm_type
1435
FROM planet_osm_point;
1536

37+
/************** WAYS *****************/
1638
CREATE OR REPLACE TEMP VIEW way AS
1739
SELECT
1840
l.osm_id AS id,
19-
/* CAST(l.tags->'osm_version AS integer)' */ NULL::integer AS version,
20-
/* CAST(l.tags->'osm_timestamp' AS timestamp without time zone) */ NULL::timestamp without time zone AS created,
21-
/* CAST(l.tags->'osm_changeset' AS bigint) */ NULL::bigint AS changeset,
22-
/* CAST(l.tags->'osm_uid' AS integer) */ NULL::integer AS uid,
23-
/* CAST(l.tags->'osm_user' AS text) */ NULL::text AS user,
41+
CAST(l.tags->'osm_version' AS integer) AS version,
42+
CAST(l.tags->'osm_timestamp' AS timestamp without time zone) AS created,
43+
CAST(l.tags->'osm_changeset' AS bigint) AS changeset,
44+
CAST(l.tags->'osm_uid' AS integer) AS uid,
45+
CAST(l.tags->'osm_user' AS text) AS user,
2446
to_jsonb(l.tags) AS tags,
25-
w.nodes AS nodes, /* get nodes from table planet_osm_ways, using JOIN */
47+
w.nodes AS nodes, /* replace by `NULL::bigint[] AS nodes` if did not use --slim */
2648
NULL::jsonb AS members,
2749
l.way AS geom,
2850
'w' AS osm_type
2951
FROM planet_osm_line AS l
30-
LEFT JOIN planet_osm_ways AS w ON osm_id=id
52+
LEFT JOIN planet_osm_ways AS w ON osm_id = id /* remove if you did not use --slim */
3153
UNION ALL
3254
SELECT
3355
p.osm_id AS id,
34-
/* CAST(p.tags->'osm_version AS integer)' */ NULL::integer AS version,
35-
/* CAST(p.tags->'osm_timestamp' AS timestamp without time zone) */ NULL::timestamp without time zone AS created,
36-
/* CAST(p.tags->'osm_changeset' AS bigint) */ NULL::bigint AS changeset,
37-
/* CAST(p.tags->'osm_uid' AS integer) */ NULL::integer AS uid,
38-
/* CAST(p.tags->'osm_user' AS text) */ NULL::text AS user,
56+
CAST(p.tags->'osm_version' AS integer) AS version,
57+
CAST(p.tags->'osm_timestamp' AS timestamp without time zone) AS created,
58+
CAST(p.tags->'osm_changeset' AS bigint) AS changeset,
59+
CAST(p.tags->'osm_uid' AS integer) AS uid,
60+
CAST(p.tags->'osm_user' AS text) AS user,
3961
to_jsonb(p.tags) as tags,
40-
w.nodes AS nodes, /* get nodes from table planet_osm_ways, using JOIN */
62+
w.nodes AS nodes, /* replace by `NULL::bigint[] AS nodes` if did not use --slim */
4163
NULL::jsonb AS members,
4264
p.way AS geom,
4365
'w' AS osm_type
4466
FROM planet_osm_polygon AS p
45-
LEFT JOIN planet_osm_ways AS w ON osm_id=id
46-
WHERE id > 0; /* keep ways and exclude (multipolygon) relations */
67+
LEFT JOIN planet_osm_ways AS w ON osm_id = id /* remove if you did not use --slim */
68+
;
4769

70+
/************** RELATIONS *****************/
71+
/* complete version if you used --slim */
4872
CREATE OR REPLACE TEMP VIEW relation AS
4973
SELECT
50-
r.id,
51-
/* CAST(hstore(r.tags)->'osm_version AS integer)' */ NULL::integer AS version,
52-
/* CAST(hstore(r.tags)->'osm_timestamp' AS timestamp without time zone) */ NULL::timestamp without time zone AS created,
53-
/* CAST(hstore(r.tags)->'osm_changeset' AS bigint) */ NULL::bigint AS changeset,
54-
/* CAST(hstore(r.tags)->'osm_uid' AS integer) */ NULL::integer AS uid,
55-
/* CAST(hstore(r.tags)->'osm_user' AS text) */ NULL::text AS user,
74+
r.id AS id,
75+
CAST(r.tags->>'osm_version' AS integer) AS version,
76+
CAST(r.tags->>'osm_timestamp' AS timestamp without time zone) AS created,
77+
CAST(r.tags->>'osm_changeset' AS bigint) AS changeset,
78+
CAST(r.tags->>'osm_uid' AS integer) AS uid,
79+
CAST(r.tags->>'osm_user' AS text) AS user,
5680
r.tags as tags,
5781
NULL::bigint[] AS nodes,
5882
r.members AS members,
5983
p.way AS geom, /* get geom from table planet_osm_polygon with negative osm_id values, using JOIN */
6084
'r' AS osm_type
6185
FROM planet_osm_rels AS r
62-
LEFT JOIN planet_osm_polygon AS p ON id=-osm_id
63-
;
86+
LEFT JOIN planet_osm_polygon AS p ON id = -osm_id;
87+
88+
/* simple version if you did not used --slim */
89+
/* returns only some relations : multipolygon, boundary, routes */
90+
/*
91+
CREATE OR REPLACE TEMP VIEW relation AS
92+
SELECT
93+
-osm_id as id, /* relation ids are stored as negative values in planet_osm_polygon */
94+
CAST(tags->'osm_version' AS integer) AS version,
95+
CAST(tags->'osm_timestamp' AS timestamp without time zone) AS created,
96+
CAST(tags->'osm_changeset' AS bigint) AS changeset,
97+
CAST(tags->'osm_uid' AS integer) AS uid,
98+
CAST(tags->'osm_user' AS text) AS user,
99+
to_jsonb(tags) as tags,
100+
NULL::bigint[] AS nodes,
101+
NULL::jsonb AS members,
102+
way AS geom,
103+
'r' AS osm_type
104+
FROM planet_osm_polygon;
105+
*/
64106

107+
/************** NWR *****************/
65108
CREATE OR REPLACE TEMP VIEW nwr AS
66109
SELECT * FROM node
67110
UNION ALL
@@ -70,17 +113,19 @@ UNION ALL
70113
SELECT * FROM relation
71114
;
72115

116+
/************** AREA *****************/
117+
/* complete version if you used --slim */
73118
CREATE OR REPLACE TEMP VIEW area AS
74119
SELECT
75120
CASE
76121
WHEN p.osm_id<0 THEN 3600000000-p.osm_id /* transform the negative values used here for relations to the id format used for areas in overpass */
77122
ELSE p.osm_id
78123
END AS id,
79-
/* CAST(p.tags->'osm_version AS integer)' */ NULL::integer AS version,
80-
/* CAST(p.tags->'osm_timestamp' AS timestamp without time zone) */ NULL::timestamp without time zone AS created,
81-
/* CAST(p.tags->'osm_changeset' AS bigint) */ NULL::bigint AS changeset,
82-
/* CAST(p.tags->'osm_uid' AS integer) */ NULL::integer AS uid,
83-
/* CAST(p.tags->'osm_user' AS text) */ NULL::text AS user,
124+
CAST(p.tags->'osm_version' AS integer) AS version,
125+
CAST(p.tags->'osm_timestamp' AS timestamp without time zone) AS created,
126+
CAST(p.tags->'osm_changeset' AS bigint) AS changeset,
127+
CAST(p.tags->'osm_uid' AS integer) AS uid,
128+
CAST(p.tags->'osm_user' AS text) AS user,
84129
to_jsonb(p.tags) as tags,
85130
w.nodes AS nodes, /* For ways only : get nodes from table planet_osm_ways, using JOIN */
86131
r.members AS members, /* For relations only : get members from table planet_osm_rels, using JOIN */
@@ -89,5 +134,27 @@ SELECT
89134
FROM planet_osm_polygon AS p
90135
LEFT JOIN planet_osm_ways AS w ON osm_id=w.id
91136
LEFT JOIN planet_osm_rels AS r ON osm_id=-r.id
92-
/* I don't know if JOIN to get nodes and members are useful for areas. Maybe geom is enough */
137+
/* I don't know if JOIN to get nodes and members are useful for areas. Maybe geom is enough? */
138+
;
139+
140+
/* simple version if you did not used --slim */
141+
/*
142+
CREATE OR REPLACE TEMP VIEW area AS
143+
SELECT
144+
CASE
145+
WHEN p.osm_id<0 THEN 3600000000-p.osm_id /* transform the negative values used here for relations to the id format used for areas in overpass */
146+
ELSE p.osm_id
147+
END AS id,
148+
CAST(p.tags->'osm_version' AS integer) AS version,
149+
CAST(p.tags->'osm_timestamp' AS timestamp without time zone) AS created,
150+
CAST(p.tags->'osm_changeset' AS bigint) AS changeset,
151+
CAST(p.tags->'osm_uid' AS integer) AS uid,
152+
CAST(p.tags->'osm_user' AS text) AS user,
153+
to_jsonb(p.tags) as tags,
154+
NULL::bigint[] AS nodes,
155+
NULL::jsonb AS members,
156+
p.way AS geom,
157+
'a' AS osm_type
158+
FROM planet_osm_polygon AS p
93159
;
160+
*/

0 commit comments

Comments
 (0)