WS30: Improved road generation

This commit is contained in:
Stuart Buchanan
2021-12-05 20:38:19 +00:00
parent f46f25dbaf
commit d7abbaa10f
4 changed files with 287 additions and 34 deletions
+74 -33
View File
@@ -17,7 +17,7 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# Simple generate of line feature - roads, railways, rivers
# You will need Python Overpass API - "pip install overpy"
import xml.etree.ElementTree as etree
@@ -32,11 +32,13 @@ import calc_tile
import overpy
nodes = {}
curr_road_count = 0
curr_river_count = 0
road_count = 0
river_count = 0
if (len(sys.argv) != 6):
print("Simple generation of ROAD_LIST files")
print("Simple generation of LINEAR_FEATURE_LIST files")
print("")
print("Usage: " + sys.argv[0] + " <scenery_dir> <lon1> <lat1> <lon2> <lat2>")
print(" <scenery_dir> \tScenery directory to write to")
@@ -63,14 +65,14 @@ def add_to_stg(lat, lon, type):
with open(stg, 'a') as f:
f.write("LINE_FEATURE_LIST " + feature_file(lat, lon, type) + " " + type + "\n")
def write_feature(lon, lat, road, type, width):
def write_feature(lon, lat, road, type, width, lit):
index = calc_tile.calc_tile_index((lon,lat))
dirname = os.path.join(scenery_prefix, calc_tile.directory_name((lon, lat)))
os.makedirs(dirname, exist_ok=True)
txt = os.path.join(scenery_prefix, calc_tile.directory_name((lon, lat)), feature_file(lat, lon, type))
#print("Writing " + txt)
with open(txt, 'a') as f:
f.write(str(width) + " 0 1 1 1 1") # Width plus currently unused generic attributes.
f.write(str(width) + " " + str(lit) + " 1 1 1 1") # Width plus currently unused generic attributes.
for pt in road :
f.write(" " + str(pt.lon) + " " + str(pt.lat))
f.write("\n")
@@ -90,34 +92,46 @@ def write_feature(lon, lat, road, type, width):
def parse_way(way) :
global road_count, river_count, lat1, lon1, lat2, lon2
global curr_road_count, curr_river_count, lat1, lon1, lat2, lon2
pts = []
width = 6.0
road = 0
lit = 0
road = 0
river = 0
rail = 0
highway = way.tags.get("highway")
waterway = way.tags.get("waterway")
railway = way.tags.get("railway")
feature_type = "None"
if (highway=="motorway_junction") or (highway=="motorway") or (highway=="motorway_link"):
width = 15.0
feature_type = "Road"
road_count = road_count + 1
feature_type = "ws30Freeway"
curr_road_count = curr_road_count + 1
if (highway=="secondary") or (highway=="primary") or (highway=="trunk") or (highway=="trunk_link") or (highway=="primary_link") or (highway=="secondary_link") :
if (highway=="primary") or (highway=="trunk") or (highway=="trunk_link") or (highway=="primary_link") :
width = 12.0
feature_type = "Road"
road_count = road_count + 1
feature_type = "ws30Freeway"
curr_road_count = curr_road_count + 1
if (highway=="secondary") or (highway=="secondary_link") :
width = 12.0
feature_type = "ws30Road"
curr_road_count = curr_road_count + 1
if (highway=="unclassified") or (highway=="tertiary") or (highway=="tertiary_link") or (highway=="service") or (highway=="residential"):
width = 6.0
feature_type = "Road"
road_count = road_count + 1
feature_type = "ws30Road"
curr_road_count = curr_road_count + 1
if (waterway=="river") or (waterway=="canal") :
width = 10.0
feature_type = "Watercourse"
river_count = river_count + 1
feature_type = "ws30River"
curr_river_count = curr_river_count + 1
if (railway=="rail") or (railway=="preserved") or (railway=="disused") :
width = 4.2 # Standard guage ~ 1.4m, with twice the space either side
feature_type = "ws30Railway"
# Use the width if defined and parseable
if (way.tags.get("width") != None) :
@@ -130,6 +144,16 @@ def parse_way(way) :
except ValueError :
print("Unable to parse width " + width_str)
# Use the lit tag if defined and parseable
if (way.tags.get("lit") != None) :
lit_str = way.tags.get("lit")
if ((lit_str == "no") or (lit == "disused")) :
# Specific tags for unlit ways
lit = 0
else :
# Everything else indicates some form of lighting
lit = 1
if (feature_type != "None") :
# It's a road or river. Add it to appropriate tile entries.
tileids = set()
@@ -140,35 +164,52 @@ def parse_way(way) :
idx = calc_tile.calc_tile_index([lon, lat])
if ((float(lon1) <= lon <= float(lon2)) and (float(lat1) <= lat <= float(lat2)) and (idx not in tileids)) :
# Write the feature to a bucket provided it's within the lat/lon bounds and if we've not already written it there
write_feature(lon, lat, way.nodes, feature_type, width)
write_feature(lon, lat, way.nodes, feature_type, width, lit)
tileids.add(idx)
def writeOSM(result):
for child in result.ways:
parse_way(child)
# Get River data
for lat in range(int(lat1), int(lat2)):
for lon in range(int(lon1), int(lon2)):
curr_road_count = 0
curr_river_count = 0
osm_bbox = ",".join([str(lat), str(lon), str(lat+1), str(lon+1)])
#api = overpy.Overpass(url="https://lz4.overpass-api.de/api/interpreter")
api = overpy.Overpass(url="https://overpass.kumi.systems/api/interpreter")
osm_bbox = ",".join([lat1, lon1, lat2, lon2])
api = overpy.Overpass(url="https://lz4.overpass-api.de/api/interpreter")
# Get River data
river_query = "(way[\"waterway\"=\"river\"](" + osm_bbox + "); way[\"waterway\"=\"canal\"](" + osm_bbox + "););(._;>;);out;"
result = api.query(river_query)
writeOSM(result)
river_query = "(way[\"waterway\"=\"river\"](" + osm_bbox + "); way[\"waterway\"=\"canal\"](" + osm_bbox + "););(._;>;);out;"
result = api.query(river_query)
writeOSM(result)
railway_query = "("
railway_types = ["rail", "preserved", "disused"]
for r in railway_types :
railway_query = railway_query + "way[\"railway\"=\"" + r + "\"](" + osm_bbox + ");"
road_query = "("
#road_types = ["unclassified", "tertiary", "service", "secondary", "primary", "motorway_junction", "motorway"]
#road_types = ["tertiary", "secondary", "primary", "motorway_junction", "motorway"]
road_types = ["motorway", "trunk", "primary", "secondary", "tertiary", "unclassified", "residential", "motorway_link", "trunk_link", "primary_link", "secondary_link", "tertiary_link"]
for r in road_types :
road_query = road_query + "way[\"highway\"=\"" + r + "\"](" + osm_bbox + ");"
railway_query = railway_query + ");(._;>;);out;"
result = api.query(railway_query)
writeOSM(result)
road_query = road_query + ");(._;>;);out;"
result = api.query(road_query)
writeOSM(result)
road_query = "("
#road_types = ["unclassified", "tertiary", "service", "secondary", "primary", "motorway_junction", "motorway"]
#road_types = ["tertiary", "secondary", "primary", "motorway_junction", "motorway"]
road_types = ["motorway", "trunk", "primary", "secondary", "tertiary", "unclassified", "residential", "motorway_link", "trunk_link", "primary_link", "secondary_link", "tertiary_link"]
for r in road_types :
road_query = road_query + "way[\"highway\"=\"" + r + "\"](" + osm_bbox + ");"
print("Wrote total of " + str(road_count) + " roads")
print("Wrote total of " + str(river_count) + " rivers")
road_query = road_query + ");(._;>;);out;"
result = api.query(road_query)
writeOSM(result)
print(str(lat) + "," + str(lon) + ": " + str(curr_road_count) + " roads " + str(curr_river_count) + " rivers")
road_count += curr_road_count
river_count += curr_river_count
print(str(lat1) + "," + str(lon1) + " " + str(lat2) + "," + str(lon2))
print("Wrote total of " + str(road_count) + " roads " + str(river_count) + " rivers")
#