2024-08-24 19:02:27 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import re
|
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
import os
|
2024-09-16 18:42:06 +00:00
|
|
|
import argparse
|
2024-08-24 19:02:27 +00:00
|
|
|
|
2024-09-16 18:42:06 +00:00
|
|
|
parser = argparse.ArgumentParser(description='Export Umap maps for use with streckenkarte')
|
|
|
|
parser.add_argument("URL", help="The map's URL")
|
|
|
|
parser.add_argument("output_dir", help="Output directory")
|
2024-08-24 19:02:27 +00:00
|
|
|
|
2024-09-16 18:42:06 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
url = args.URL
|
|
|
|
outdir = args.output_dir
|
2024-08-24 19:02:27 +00:00
|
|
|
|
|
|
|
base = url.split("/map/")[0]
|
|
|
|
r = requests.get(url)
|
|
|
|
|
|
|
|
regexp = re.compile(r'U.MAP = new U.Map[(]"map", (.+) }\)', re.DOTALL)
|
|
|
|
|
|
|
|
data = json.loads(regexp.findall(r.text, re.DOTALL)[0].replace("})","}"))
|
|
|
|
|
|
|
|
properties = data["properties"]
|
|
|
|
layers = properties["datalayers"]
|
|
|
|
map_id = properties["umap_id"]
|
|
|
|
|
|
|
|
config = {}
|
|
|
|
config["name"] = properties["name"]
|
|
|
|
colors = {}
|
|
|
|
config["layers"] = colors
|
|
|
|
|
2024-09-06 21:44:41 +00:00
|
|
|
if "tilelayer" in properties and properties['tilelayer'] != {} :
|
|
|
|
config["tilelayer"] = properties["tilelayer"]
|
|
|
|
config["tilelayer"]["attribution"] = re.sub(r'\[\[([^|]+)\|([^|]+)\]\]', r'<a href="\1" >\2</a>', config["tilelayer"]["attribution"])
|
|
|
|
else :
|
|
|
|
config["tilelayer"] = { "attribution" : 'Map data © <a href="http://osm.org/copyright" >OpenStreetMap contributors</a>', "url_template" : "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" }
|
2024-09-06 21:46:09 +00:00
|
|
|
|
2024-08-24 19:02:27 +00:00
|
|
|
def normalize_name(name) :
|
2024-08-24 20:41:47 +00:00
|
|
|
return name.replace("/", "_").replace("-","").replace(" ","").replace(".","")
|
2024-08-24 19:02:27 +00:00
|
|
|
|
|
|
|
datadir = os.path.join(outdir,"data")
|
2024-09-06 21:51:03 +00:00
|
|
|
if not os.path.exists(outdir) :
|
|
|
|
os.mkdir(outdir)
|
2024-08-24 19:02:27 +00:00
|
|
|
if not os.path.exists(datadir) :
|
|
|
|
os.mkdir(datadir)
|
|
|
|
|
|
|
|
for layer in layers :
|
|
|
|
layer_id = layer["id"]
|
|
|
|
req = requests.get(f"{base}/datalayer/{map_id}/{layer_id}/")
|
|
|
|
options = req.json()["_umap_options"]
|
|
|
|
nname = normalize_name(options['name'])
|
|
|
|
if not os.path.exists(os.path.join(datadir,nname)) :
|
|
|
|
os.mkdir(os.path.join(datadir,nname))
|
|
|
|
with open(os.path.join(datadir,nname,f"{nname}.json"), "w") as f :
|
|
|
|
f.write(req.text)
|
|
|
|
colors[nname] = { "color" : options["color"] if "color" in options else "DarkGreen" , "humanname" : options["name"] }
|
|
|
|
if "weight" in options :
|
|
|
|
colors[nname]["width"] = options["weight"]
|
|
|
|
else:
|
|
|
|
colors[nname]["width"] = 2
|
|
|
|
|
|
|
|
with open(os.path.join(outdir,"layers.json"), "w") as f :
|
|
|
|
json.dump(config, f, indent=4)
|