From e65667b1b06d219300f6d731be98f8ac108e7fd7 Mon Sep 17 00:00:00 2001 From: Fen Dweller Date: Wed, 6 Oct 2021 21:20:57 -0400 Subject: [PATCH] Add some changes to the scripts --- scripts/mapshaper/map-coords.py | 101 +++++++++++++++++++++++++++++++- scripts/mapshaper/states.json | 10 ++++ 2 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 scripts/mapshaper/states.json diff --git a/scripts/mapshaper/map-coords.py b/scripts/mapshaper/map-coords.py index f5659995..6f2d3a30 100644 --- a/scripts/mapshaper/map-coords.py +++ b/scripts/mapshaper/map-coords.py @@ -1,6 +1,7 @@ import json import sys import subprocess +from tqdm import tqdm config = json.load(open(sys.argv[1])) @@ -61,6 +62,7 @@ elif config["mode"] == "filter": shell=True, stderr=subprocess.STDOUT ) + for item in config["items"]: CMD = ( @@ -91,14 +93,15 @@ elif config["mode"] == "filter": if not found: print("Did not find ", item["name"]) continue - + + CMD = ( f"""mapshaper -i temp.shp """ f"""-filter "{config["base-filter"]}" """ f"""-filter "{item["filter"]}" """ f"""-simplify interval={config["simplify-size"]} """ f"""-proj "+proj=ortho +lat_0={lat} +lon_0={lon}" """ - f"""-info """ + + f"""-info """ f"""-style stroke-width={config["stroke-width"]} """ f"""-o "../../{config["directory"]}/{item["name"]}.svg" margin={config["stroke-width"]/2} height=1000 """ ) @@ -123,3 +126,97 @@ elif config["mode"] == "filter": results.append([item["name"], height]) print(results) +elif config["mode"] == "forms": + results = [] + + CMD = ( + f"""mapshaper -i {config["shapefile"]} """ + f"""-filter "{config["base-filter"]}" """ + f"""-o temp.shp""" + ) + + result = subprocess.check_output( + CMD, + shell=True, + stderr=subprocess.STDOUT + ) + + CMD = ( + f"""mapshaper -i temp.shp """ + f"""encoding=utf-8 """ + f"""-each "console.log(JSON.stringify({{id: this.id, name: name, key: {config["form-key"]} }}))" """ + f"""-split """ + f"""-o tmp/ format=topojson singles target=* """ + ) + + try: + result = subprocess.check_output( + CMD, + shell=True + ) + except subprocess.CalledProcessError as e: + print(e.output) + + entities = [] + + for line in result.decode("utf-8").split("\n"): + if len(line) > 0: + print(line) + entities.append(json.loads(line)) + + for item in tqdm(entities): + CMD = ( + f"""mapshaper -i tmp/temp-{item["id"] + 1}.json """ + f"""encoding=utf-8 """ + f"""-simplify interval=1000 """ + f"""-info """ + ) + + result = subprocess.check_output( + CMD, + shell=True, + stderr=subprocess.STDOUT + ) + + found = False + for line in result.decode("utf-8").split("\n"): + if "Bounds:" in line: + coords = list(map(float, line[7:].strip().split(","))) + lat = coords[1] + coords[3] + lat /= 2 + lon = coords[0] + coords[2] + lon /= 2 + found = True + break + + if not found: + print("Did not find ", item["name"]) + continue + + CMD = ( + f"""mapshaper -i tmp/temp-{item["id"] + 1}.json """ + f"""encoding=utf-8 """ + f"""-simplify interval={config["simplify-size"]} """ + f"""-proj "+proj=ortho +lat_0={lat} +lon_0={lon}" """ + f"""-info """ + f"""-style stroke-width={config["stroke-width"]} """ + f"""-o "../../{config["directory"]}/{item["key"]}/{item["name"]}.svg" margin={config["stroke-width"]/2} height=1000 """ + ) + + try: + result = subprocess.check_output( + CMD, + shell=True, + stderr=subprocess.STDOUT + ) + except subprocess.CalledProcessError as exc: + print(exc.output.decode("utf-8")) + continue + + for line in result.decode("utf-8").split("\n"): + if "Bounds:" in line: + coords = list(map(float, line[7:].strip().split(","))) + height = coords[3] - coords[1] + height *= 1000 / (1000 - config["stroke-width"]) # accounts for the margin + results.append({"name": item["name"], "form": item["key"], "height": round(height)}) + print(results) diff --git a/scripts/mapshaper/states.json b/scripts/mapshaper/states.json new file mode 100644 index 00000000..2ec3ecdc --- /dev/null +++ b/scripts/mapshaper/states.json @@ -0,0 +1,10 @@ +{ + "name": "Straits", + "directory": "/media/naturals/States/", + "shapefile": "E:/macrovision/mapshaper/states/ne_10m_admin_1_states_provinces_lakes.shp", + "mode": "forms", + "base-filter": "true", + "form-key": "admin", + "stroke-width": 1, + "simplify-size": 100 +} \ No newline at end of file