Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support modification of speed limit extraction #40

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 31 additions & 44 deletions map/tools/bfmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 = []

Expand All @@ -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]
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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()

Expand All @@ -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)
Expand Down
Empty file added map/tools/test/__init__.py
Empty file.
33 changes: 28 additions & 5 deletions map/tools/test/test_bfmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,35 +100,58 @@ 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('=')
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"])
Expand Down