Skip to content

Commit 710d5e8

Browse files
committed
Upgrade postgres
1 parent 5ce0d8e commit 710d5e8

5 files changed

Lines changed: 150 additions & 42 deletions

File tree

Gemfile.lock

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
GIT
22
remote: https://github.com/teritorio/overpass_parser-rb.git
3-
revision: b0bba1cfafd5db05e39d1782f939bbcf0aa0cb1b
3+
revision: 7e2344f84fc4293bc5ce64d136b37bff0978978e
44
specs:
55
overpass_parser (1.0.0)
66
rice (~> 4.0)
@@ -43,13 +43,13 @@ GEM
4343
rack (2.2.9)
4444
rainbow (3.1.1)
4545
rake (13.2.1)
46-
rbi (0.1.10)
47-
prism (>= 0.18.0, < 0.25)
46+
rbi (0.1.11)
47+
prism (>= 0.18.0, < 0.27)
4848
sorbet-runtime (>= 0.5.9204)
4949
regexp_parser (2.9.0)
5050
rexml (3.2.6)
5151
rice (4.3.1)
52-
rubocop (1.63.1)
52+
rubocop (1.63.2)
5353
json (~> 2.3)
5454
language_server-protocol (>= 3.17.0)
5555
parallel (~> 1.10)
@@ -70,15 +70,15 @@ GEM
7070
sorbet-runtime (>= 0.5.10782)
7171
ruby-progressbar (1.13.0)
7272
ruby2_keywords (0.0.5)
73-
sorbet (0.5.11346)
74-
sorbet-static (= 0.5.11346)
75-
sorbet-runtime (0.5.11346)
76-
sorbet-static (0.5.11346-aarch64-linux)
77-
sorbet-static (0.5.11346-universal-darwin)
78-
sorbet-static (0.5.11346-x86_64-linux)
79-
sorbet-static-and-runtime (0.5.11346)
80-
sorbet (= 0.5.11346)
81-
sorbet-runtime (= 0.5.11346)
73+
sorbet (0.5.11352)
74+
sorbet-static (= 0.5.11352)
75+
sorbet-runtime (0.5.11352)
76+
sorbet-static (0.5.11352-aarch64-linux)
77+
sorbet-static (0.5.11352-universal-darwin)
78+
sorbet-static (0.5.11352-x86_64-linux)
79+
sorbet-static-and-runtime (0.5.11352)
80+
sorbet (= 0.5.11352)
81+
sorbet-runtime (= 0.5.11352)
8282
sorbet-struct-comparable (1.3.0)
8383
sorbet-runtime (>= 0.5)
8484
spoom (1.3.0)

backends/postgres_osmosis.rb

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,45 @@ def initialize
1212

1313
def exec(query)
1414
request = OverpassParser.parse(query)
15-
sql = request.to_sql(@dialect)
15+
sql = request.to_sql(
16+
@dialect,
17+
"
18+
SELECT
19+
results.id,
20+
results.version,
21+
results.tstamp::timestamp with time zone AS created,
22+
results.changeset_id AS changeset,
23+
users.name AS user,
24+
results.user_id AS uid,
25+
results.tags,
26+
results.nodes,
27+
nullif(jsonb_agg(jsonb_strip_nulls(jsonb_build_object(
28+
'type', CASE relation_members.member_type WHEN 'N' THEN 'node' WHEN 'W' THEN 'way' WHEN 'R' THEN 'relation' END,
29+
'ref', relation_members.member_id,
30+
'role', relation_members.member_role
31+
))), '[{}]'::jsonb) AS members,
32+
results.geom,
33+
results.osm_type
34+
FROM
35+
{{query}} AS results
36+
LEFT JOIN users ON
37+
users.id = results.user_id
38+
LEFT JOIN relation_members ON
39+
results.osm_type = 'r' AND
40+
relation_members.relation_id = results.id
41+
GROUP BY
42+
results.id,
43+
results.version,
44+
results.tstamp,
45+
results.changeset_id,
46+
users.name,
47+
results.user_id,
48+
results.tags,
49+
results.nodes,
50+
results.geom,
51+
results.osm_type"
52+
)
53+
puts sql
1654
result = @@con.exec(sql)
1755
[sql, result.collect { |row| row['j'].gsub('+00:00', 'Z') }]
1856
end

backends/postgres_osmosis.sql

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,11 @@
11
CREATE OR REPLACE TEMP VIEW node AS
2-
SELECT id, version, tstamp::timestamp with time zone AS created, tags, NULL::bigint[] AS nodes, NULL::jsonb AS members, geom, 'n' AS osm_type FROM nodes;
2+
SELECT id, version, tstamp, changeset_id, user_id, tags, NULL::bigint[] AS nodes, NULL::jsonb AS members, geom, 'n' AS osm_type FROM nodes;
33

44
CREATE OR REPLACE TEMP VIEW way AS
5-
SELECT id, version, tstamp::timestamp with time zone AS created, tags, nodes, NULL::jsonb AS members, linestring AS geom, 'w' AS osm_type FROM ways;
5+
SELECT id, version, tstamp, changeset_id, user_id, tags, nodes, NULL::jsonb AS members, linestring AS geom, 'w' AS osm_type FROM ways;
66

77
CREATE OR REPLACE TEMP VIEW relation AS
8-
SELECT
9-
id,
10-
version,
11-
tstamp::timestamp with time zone AS created,
12-
tags,
13-
NULL::bigint[] AS nodes,
14-
jsonb_agg(jsonb_build_object(
15-
'type', CASE relation_members.member_type WHEN 'N' THEN 'node' WHEN 'W' THEN 'way' WHEN 'R' THEN 'relation' END,
16-
'ref', relation_members.member_id,
17-
'role', relation_members.member_role
18-
)) AS members,
19-
NULL::geometry AS geom,
20-
'r' AS osm_type
21-
FROM relations
22-
JOIN relation_members ON
23-
relation_members.relation_id = relations.id
24-
GROUP BY
25-
relations.id,
26-
relations.version,
27-
relations.tstamp,
28-
relations.tags
29-
;
8+
SELECT id, version, tstamp, changeset_id, user_id, tags, NULL::bigint[] AS nodes, NULL::jsonb AS members, NULL::geometry AS geom, 'r' AS osm_type FROM relations;
309

3110
CREATE OR REPLACE TEMP VIEW nwr AS
3211
SELECT * FROM node
@@ -37,7 +16,7 @@ SELECT * FROM relation
3716
;
3817

3918
CREATE OR REPLACE TEMP VIEW area AS
40-
SELECT id, NULL::integer AS version, NULL::timestamp with time zone AS created, tags, NULL::bigint[] AS nodes, NULL::jsonb AS members, poly AS geom, 'a' AS osm_type FROM multipolygons WHERE id > 3600000000
19+
SELECT id, NULL::integer AS version, NULL::timestamp without time zone AS tstamp, NULL::bigint AS changeset_id, NULL::integer AS user_id, tags, NULL::bigint[] AS nodes, NULL::jsonb AS members, poly AS geom, 'a' AS osm_type FROM multipolygons WHERE id > 3600000000
4120
UNION ALL
42-
SELECT id, version, tstamp::timestamp with time zone AS created, tags, nodes AS nodes, NULL::jsonb AS members, ST_MakePolygon(linestring)::geometry(Geometry,4326) AS geom, 'w' AS osm_type FROM ways WHERE id < 3600000000 AND ST_IsClosed(linestring) AND ST_Dimension(ST_MakePolygon(linestring)) = 2
21+
SELECT id, version, tstamp::timestamp with time zone AS created, changeset_id, user_id, tags, nodes AS nodes, NULL::jsonb AS members, ST_MakePolygon(linestring)::geometry(Geometry,4326) AS geom, 'w' AS osm_type FROM ways WHERE id < 3600000000 AND ST_IsClosed(linestring) AND ST_Dimension(ST_MakePolygon(linestring)) = 2
4322
;

backends/postgres_osmosis_init.sql

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
CREATE INDEX nodes_idx_tags ON nodes USING gist(tags) WHERE tags != ''::hstore;
2+
CREATE INDEX ways_idx_tags ON ways USING gist(tags) WHERE tags != ''::hstore;
3+
CREATE INDEX relations_idx_tags ON relations USING gist(tags) WHERE tags != ''::hstore;
4+
5+
CREATE INDEX ways_idx_geom ON nodes USING gist(geom);
6+
CREATE INDEX ways_idx_linestring ON ways USING gist(linestring);
7+
8+
DO $$
9+
DECLARE
10+
mp RECORD;
11+
BEGIN
12+
DROP TABLE IF EXISTS multipolygons CASCADE;
13+
CREATE UNLOGGED TABLE multipolygons (
14+
id bigint,
15+
tags hstore,
16+
poly geometry(Geometry, 4326) NOT NULL,
17+
is_valid boolean NOT NULL
18+
);
19+
20+
FOR mp in (
21+
SELECT
22+
relations.id,
23+
relations.tags,
24+
ST_LineMerge(ST_Collect(ways.linestring)) AS linestrings
25+
FROM
26+
relations
27+
JOIN relation_members ON
28+
relation_members.relation_id = relations.id AND
29+
relation_members.member_type = 'W' AND
30+
relation_members.member_role IN ('outer', 'inner', '')
31+
LEFT JOIN ways ON
32+
ways.id = relation_members.member_id
33+
WHERE
34+
relations.tags->'type' = 'multipolygon'
35+
GROUP BY
36+
relations.id
37+
HAVING
38+
-- Ensure all ways are downloaded; may be false at extract borders
39+
-- If false, calculations like ST_Area would give invalid results
40+
bool_and(ways.id IS NOT NULL) AND
41+
-- Avoid dealing with very large multi-polygons
42+
ST_NPoints(ST_Collect(ways.linestring)) < 100000
43+
) LOOP
44+
BEGIN
45+
IF ST_BuildArea(mp.linestrings) IS NOT NULL AND NOT ST_IsEmpty(ST_BuildArea(mp.linestrings))
46+
-- Ensure no linestrings are dropped by ST_BuildArea, which would result in e.g. missing inners (see #2169)
47+
AND ST_CoveredBy(ST_Points(mp.linestrings), ST_Points(ST_BuildArea(mp.linestrings))) THEN
48+
WITH
49+
unary AS (
50+
SELECT
51+
id + 3600000000 AS id, tags,
52+
(ST_Dump(poly)).geom AS poly
53+
FROM (VALUES (
54+
mp.id,
55+
mp.tags,
56+
ST_BuildArea(mp.linestrings)
57+
)) AS t(id, tags, poly)
58+
),
59+
simplified AS (
60+
SELECT
61+
id, tags,
62+
ST_BuildArea(ST_Collect(
63+
ST_ExteriorRing(poly),
64+
(SELECT ST_Union(ST_MakePolygon(ST_InteriorRingN(poly, n))) FROM generate_series(1, ST_NumInteriorRings(poly)) AS t(n)) -- ST_MakePolygon is needed to union touching inner rings #2169
65+
)) AS poly
66+
FROM
67+
unary
68+
),
69+
multi AS (
70+
SELECT
71+
id, tags,
72+
ST_CollectionHomogenize(ST_Collect(poly)) AS poly
73+
FROM
74+
simplified
75+
GROUP BY
76+
id, tags
77+
)
78+
INSERT INTO
79+
multipolygons
80+
SELECT
81+
*,
82+
ST_IsValid(poly) AS is_valid -- see #2058 sometimes poly is considered valid but poly_proj not
83+
FROM
84+
multi;
85+
END IF;
86+
EXCEPTION WHEN OTHERS THEN
87+
RAISE NOTICE 'multipolygon fails: %', mp.id;
88+
END;
89+
END LOOP;
90+
END;
91+
$$ LANGUAGE 'plpgsql';

test/output.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ def test_output_format
142142
response = JSON.parse(Net::HTTP.get(uri))['elements']
143143
# puts JSON.dump(response)
144144
response = response.collect do |e|
145-
e = e.compact.except('changeset', 'user', 'uid', 'members', 'bounds')
145+
e = e.compact.except('members', 'bounds')
146146
e['center'] = nil if e.key?('center')
147147
e
148148
end
149149
except = JSON.parse(except).collect do |e|
150-
e = e.compact.except('changeset', 'user', 'uid', 'members', 'bounds')
150+
e = e.compact.except('members', 'bounds')
151151
e['center'] = nil if e.key?('center')
152152
e
153153
end

0 commit comments

Comments
 (0)