Skip to content

Commit

Permalink
Speed up osmosis_highway_deadend sql20
Browse files Browse the repository at this point in the history
Checking for bicycle parking ways is a very slow part of the sql, but this is only relevant for cycleways. Skip it for car roads.

Use indices for ferry and bicycle parkings
  • Loading branch information
Famlam committed Dec 9, 2023
1 parent 4aa14a4 commit b2452a9
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 18 deletions.
96 changes: 78 additions & 18 deletions analysers/analyser_osmosis_highway_deadend.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,42 @@
"""

sql20 = """
CREATE TEMP TABLE ferry AS
SELECT
linestring,
nodes
FROM
ways
WHERE
tags != ''::hstore AND
tags?'route' AND
tags->'route' = 'ferry'
"""

sql21 = """
CREATE TEMP TABLE bicycle_parking AS
SELECT
linestring,
nodes
FROM
ways
WHERE
tags != ''::hstore AND
tags?'amenity' AND
tags->'amenity' = 'bicycle_parking'
"""

sql22 = """
CREATE INDEX idx_ferry_linestring ON ferry USING GIST(linestring);
CREATE INDEX idx_bicycle_parking_linestring ON bicycle_parking USING GIST(linestring);
"""

sql23 = """
CREATE TEMP TABLE unconnected_highways AS
SELECT
wid,
nid,
ST_AsText(geom),
geom,
highway
FROM (
SELECT
Expand All @@ -66,21 +98,37 @@
HAVING
COUNT(*) = 1
) AS t
LEFT JOIN ways AS ferry ON
LEFT JOIN ferry ON
ferry.linestring && t.geom AND
t.nid = ANY(ferry.nodes) AND
ferry.tags != ''::hstore AND
ferry.tags?'route' AND
ferry.tags->'route' = 'ferry'
LEFT JOIN ways AS bicycle_parking ON
bicycle_parking.linestring && t.geom AND
t.nid = ANY(bicycle_parking.nodes) AND
bicycle_parking.tags != ''::hstore AND
bicycle_parking.tags?'amenity' AND
bicycle_parking.tags->'amenity' = 'bicycle_parking'
t.nid = ANY(ferry.nodes)
WHERE
ferry IS NULL
"""

sql24 = """
SELECT
wid,
nid,
ST_AsText(geom)
FROM
unconnected_highways
LEFT JOIN bicycle_parking ON
bicycle_parking.linestring && unconnected_highways.geom AND
unconnected_highways.nid = ANY(bicycle_parking.nodes)
WHERE
highway = 'cycleway' AND
bicycle_parking IS NULL
"""

sql25 = """
SELECT
wid,
nid,
ST_AsText(geom)
FROM
unconnected_highways
WHERE
ferry.id IS NULL AND
bicycle_parking.id IS NULL
highway != 'cycleway'
"""


Expand Down Expand Up @@ -421,7 +469,8 @@ def __init__(self, config, logger = None):
'''Review the type of the service road or draw the local road network.'''),
resource = 'https://wiki.openstreetmap.org/wiki/Tag:service%3Ddrive-through')

self.callback20 = lambda res: {"class":1 if res[3] == 'cycleway' else 2, "data":[self.way_full, self.node_full, self.positionAsText]}
self.callback21 = lambda res: {"class": 1, "data": [self.way_full, self.node_full, self.positionAsText]}
self.callback22 = lambda res: {"class": 2, "data": [self.way_full, self.node_full, self.positionAsText]}

def analyser_osmosis_common(self):
boundary_relation = self.config.polygon_id # Either a number, None or (number, number, ...)
Expand All @@ -448,10 +497,20 @@ def analyser_osmosis_common(self):
self.run(sql50, lambda res: {"class":5, "data":[self.way_full, self.node, self.positionAsText]})

def analyser_osmosis_full(self):
self.run(sql20.format(''), self.callback20)
self.run(sql20)
self.run(sql21)
self.run(sql22)
self.run(sql23.format(''))
self.run(sql24, self.callback21)
self.run(sql25, self.callback22)

def analyser_osmosis_diff(self):
self.run(sql20.format('touched_'), self.callback20)
self.run(sql20)
self.run(sql21)
self.run(sql22)
self.run(sql23.format('touched_'))
self.run(sql24, self.callback21)
self.run(sql25, self.callback22)


###########################################################################
Expand All @@ -472,6 +531,7 @@ def test_classes(self):
a.analyser()

self.root_err = self.load_errors()
self.check_err(cl="1", elems=[("node", "161"), ("way", "1087")])
self.check_err(cl="2", elems=[("node", "59"), ("way", "1026")])
self.check_err(cl="2", elems=[("node", "55"), ("way", "1024")])

Expand Down Expand Up @@ -499,4 +559,4 @@ def test_classes(self):

self.check_err(cl="5", elems=[("node", "73"), ("way", "1031")])

self.check_num_err(21)
self.check_num_err(22)
37 changes: 37 additions & 0 deletions tests/osmosis_highway_deadend.osm
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@
<node id='156' timestamp='2014-03-31T22:00:00Z' version='1' lat='51.84913023756' lon='5.83769227449'>
<tag k='entrance' v='garage' />
</node>
<node id='157' timestamp='2014-03-31T22:00:00Z' version='1' lat='51.85110620921' lon='5.83892209608'>
<tag k='noexit' v='yes' />
</node>
<node id='158' timestamp='2014-03-31T22:00:00Z' version='1' lat='51.85093105188' lon='5.83892209608' />
<node id='159' timestamp='2014-03-31T22:00:00Z' version='1' lat='51.85092317963' lon='5.83920884205' />
<node id='160' timestamp='2014-03-31T22:00:00Z' version='1' lat='51.85110030505' lon='5.83919928385' />
<node id='161' timestamp='2014-03-31T22:00:00Z' version='1' lat='51.85082280825' lon='5.83932035437' />
<node id='162' timestamp='2014-03-31T22:00:00Z' version='1' lat='51.85114753837' lon='5.83916742319' />
<node id='163' timestamp='2014-03-31T22:00:00Z' version='1' lat='51.85114753837' lon='5.83923114451' />
<way id='1000' timestamp='2014-03-31T22:00:00Z' version='1'>
<nd ref='1' />
<nd ref='2' />
Expand Down Expand Up @@ -740,4 +749,32 @@
<tag k='highway' v='residential' />
<tag k='oneway' v='yes' />
</way>
<way id='1084' timestamp='2014-03-31T22:00:00Z' version='1'>
<nd ref='157' />
<nd ref='158' />
<tag k='highway' v='tertiary' />
</way>
<way id='1085' timestamp='2014-03-31T22:00:00Z' version='1'>
<nd ref='159' />
<nd ref='158' />
<tag k='route' v='ferry' />
</way>
<way id='1086' timestamp='2014-03-31T22:00:00Z' version='1'>
<nd ref='160' />
<nd ref='159' />
<tag k='highway' v='cycleway' />
</way>
<way id='1087' timestamp='2014-03-31T22:00:00Z' version='1'>
<nd ref='161' />
<nd ref='159' />
<tag k='highway' v='cycleway' />
<tag k='name' v='Class 1 bad' />
</way>
<way id='1088' timestamp='2014-03-31T22:00:00Z' version='1'>
<nd ref='162' />
<nd ref='160' />
<nd ref='163' />
<nd ref='162' />
<tag k='amenity' v='bicycle_parking' />
</way>
</osm>

0 comments on commit b2452a9

Please sign in to comment.