From b5052a4580ac96bfab68294ecd204072c3018b63 Mon Sep 17 00:00:00 2001 From: Jonathan Giddy Date: Fri, 20 Jan 2017 09:30:49 +0000 Subject: [PATCH 1/3] Refactor maxspeed calculation to separate speed extraction. --- map/tools/bfmap.py | 75 +++++++++++++++--------------------- map/tools/test/test_bfmap.py | 28 ++++++++++++-- 2 files changed, 56 insertions(+), 47 deletions(-) diff --git a/map/tools/bfmap.py b/map/tools/bfmap.py index f31583d9..937ccfe9 100755 --- a/map/tools/bfmap.py +++ b/map/tools/bfmap.py @@ -77,7 +77,7 @@ def ways2bfmap(src_host, src_port, src_database, src_table, src_user, src_passwo maxspeed_forward,maxspeed_backward,priority,geom) VALUES %s;""" % ( tgt_table, ",".join("""('%s','%s','%s','%s','%s','%s', %s, %s,'%s', ST_GeomFromText('%s',4326))""" % segment for segment in segments)) - if printonly == False: + if not printonly: tgt_cur.execute(query) print("%s segments from %s ways inserted." % (roadcount, rowcount)) except Exception, e: @@ -130,47 +130,34 @@ def is_oneway(tags): return False -def maxspeed(tags): - # maxspeed_forward = int(config[key][value][2]) - forward = "null" - if ("maxspeed" in tags.keys()): - try: - if "mph" in tags["maxspeed"]: - forward = int(tags["maxspeed"].split(" ")[0]) * 1.609 - else: - forward = int(tags["maxspeed"]) - except: - pass - if ("maxspeed:forward" in tags.keys()): +def extract_speed_limit(tag): + if tag is None: + limit = None + else: try: - if "mph" in tags["maxspeed:forward"]: - forward = int(tags["maxspeed:forward"].split(" ")[0]) * 1.609 + if "mph" in tag: + limit = int(tag.split("mph")[0]) * 1.609 else: - forward = int(tags["maxspeed:forward"]) + limit = int(tag) except: - pass + limit = None + return limit - # maxspeed_backward = maxspeed_forward - backward = "null" - if ("maxspeed" in tags.keys()): - try: - if "mph" in tags["maxspeed"]: - backward = int(tags["maxspeed"].split(" ")[0]) * 1.609 - else: - backward = int(tags["maxspeed"]) - except: - pass - if ("maxspeed:backward" in tags.keys()): - try: - if "mph" in tags["maxspeed:backward"]: - backward = int(tags["maxspeed:backward"].split(" ")[0]) * 1.609 - else: - backward = int(tags["maxspeed:backward"]) - except: - pass +def maxspeed(tags): + forward = ( + extract_speed_limit(tags.get("maxspeed:forward")) or + extract_speed_limit(tags.get("maxspeed")) or + "null" + ) + backward = ( + extract_speed_limit(tags.get("maxspeed:backward")) or + extract_speed_limit(tags.get("maxspeed")) or + "null" + ) return (forward, backward) + def segment(config, row): segments = [] @@ -180,7 +167,7 @@ def segment(config, row): (tags, way) = waysort(row) (key, value) = type(config, tags) - if key == None or value == None: + if key is None or value is None: return segments osm_id = row[0] @@ -232,7 +219,7 @@ def exists(host, port, database, table, user, password): try: cursor.execute( - """SELECT COUNT(tablename) FROM pg_tables + """SELECT COUNT(tablename) FROM pg_tables WHERE schemaname='public' AND tablename='%s';""" % table) dbcon.commit() except Exception, e: @@ -262,7 +249,7 @@ def remove(host, port, database, table, user, password, printonly): try: query = "DROP TABLE %s;" % table - if printonly == True: + if printonly: print(query) else: cursor.execute(query) @@ -285,7 +272,7 @@ def schema(host, port, database, table, user, password, printonly): except: print("Connection to database failed.") exit(1) - + try: query = """CREATE TABLE %s(gid bigserial, osm_id bigint NOT NULL, @@ -299,25 +286,25 @@ def schema(host, port, database, table, user, password, printonly): priority double precision NOT NULL); SELECT AddGeometryColumn('%s','geom',4326, 'LINESTRING',2);""" % (table, table) - if printonly == True: + if printonly: print(query) else: cursor.execute(query) dbcon.commit() except Exception, e: print("Database transaction failed. (%s)" % e.pgerror) - + try: query = "CREATE INDEX idx_%s_geom ON %s USING gist(geom);" % ( table, table) - if printonly == True: + if printonly: print(query) else: cursor.execute(query) dbcon.commit() except Exception, e: print("Database transaction failed. (%s)" % e.pgerror) - + cursor.close() dbcon.close() @@ -335,7 +322,7 @@ def index(host, port, database, table, user, password, printonly): try: query = "CREATE INDEX idx_%s_geom ON %s USING gist(geom);" % ( table, table) - if printonly == True: + if printonly: print(query) else: cursor.execute(query) diff --git a/map/tools/test/test_bfmap.py b/map/tools/test/test_bfmap.py index a14536b6..74a13d63 100644 --- a/map/tools/test/test_bfmap.py +++ b/map/tools/test/test_bfmap.py @@ -100,31 +100,53 @@ def test_segment2(self): segments = bfmap.segment(config, row) self.assertEquals(2, len(segments)) - def test_maxspeed(self): + def test_extract_limit_number(self): + self.assertEquals(60, bfmap.extract_speed_limit("60")) + + def test_extract_limit_space_mph(self): + self.assertEquals(60 * 1.609, bfmap.extract_speed_limit("60 mph")) + + def test_extract_limit_nospace_mph(self): + self.assertEquals(60 * 1.609, bfmap.extract_speed_limit("60mph")) + + def test_extract_limit_unrecognised(self): + self.assertEquals(None, bfmap.extract_speed_limit("national")) + + def test_maxspeed_space_mph(self): tags = {"maxspeed": "60 mph"} (fwd, bwd) = bfmap.maxspeed(tags) self.assertEquals(60 * 1.609, fwd) self.assertEquals(60 * 1.609, bwd) + def test_maxspeed_number(self): tags = {"maxspeed": "60"} (fwd, bwd) = bfmap.maxspeed(tags) self.assertEquals(60, fwd) self.assertEquals(60, bwd) + def test_maxspeed_fb_space_mph(self): tags = {"maxspeed:forward": "60 mph", "maxspeed:backward": "60 mph"} (fwd, bwd) = bfmap.maxspeed(tags) self.assertEquals(60 * 1.609, fwd) self.assertEquals(60 * 1.609, bwd) + def test_maxspeed_fb_kph(self): tags = {"maxspeed:forward": "60", "maxspeed:backward": "60"} (fwd, bwd) = bfmap.maxspeed(tags) self.assertEquals(60, fwd) self.assertEquals(60, bwd) + def test_maxspeed_nospace_mph(self): tags = {"maxspeed": "60mph"} (fwd, bwd) = bfmap.maxspeed(tags) - self.assertEquals("null", fwd) - self.assertEquals("null", bwd) + self.assertEquals(60 * 1.609, fwd) + self.assertEquals(60 * 1.609, bwd) + + def test_maxspeed_specific_overrides_general(self): + tags = {"maxspeed": "20", "maxspeed:backward": "30"} + (fwd, bwd) = bfmap.maxspeed(tags) + self.assertEquals(20, fwd) + self.assertEquals(30, bwd) def test_ways2bfmap(self): properties = dict(line.strip().split('=') From 1177cd834e79a1081638648b558500ff62eed4b5 Mon Sep 17 00:00:00 2001 From: Jonathan Giddy Date: Fri, 20 Jan 2017 10:18:29 +0000 Subject: [PATCH 2/3] Remove hard-wired pathname in tests --- map/tools/test/test_bfmap.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/map/tools/test/test_bfmap.py b/map/tools/test/test_bfmap.py index 74a13d63..b6cdf068 100644 --- a/map/tools/test/test_bfmap.py +++ b/map/tools/test/test_bfmap.py @@ -149,8 +149,9 @@ def test_maxspeed_specific_overrides_general(self): self.assertEquals(30, bwd) def test_ways2bfmap(self): - properties = dict(line.strip().split('=') - for line in open('/mnt/map/tools/test/test.properties')) + from os.path import join, dirname, abspath + prop_file = join(dirname(abspath(__file__)), 'test.properties') + properties = dict(line.strip().split('=') for line in open(prop_file)) bfmap.schema("localhost", 5432, properties[ "database"], "bfmap_ways", properties["user"], properties["password"], False) config = bfmap.config(properties["config"]) From 0e07d02e15422b92d624b0bc5a7ce77e0e69088b Mon Sep 17 00:00:00 2001 From: Jonathan Giddy Date: Fri, 20 Jan 2017 10:34:44 +0000 Subject: [PATCH 3/3] Add __init__.py to simplify testing from parent directory --- map/tools/test/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 map/tools/test/__init__.py diff --git a/map/tools/test/__init__.py b/map/tools/test/__init__.py new file mode 100644 index 00000000..e69de29b