|
- import json
- import sys
- import subprocess
- from tqdm import tqdm
-
- config = json.load(open(sys.argv[1]))
-
- if config["mode"] == "bounding-boxes":
- results = []
-
- for item in config["items"]:
- lon0 = item["corner0"][0]
- lat0 = item["corner0"][1]
- lon1 = item["corner1"][0]
- lat1 = item["corner1"][1]
-
- center_lat = (lat0 + lat1) / 2
- center_lon = (lon0 + lon1) / 2
-
- path = "../../" + config["directory"] + "/" + item["name"] + ".svg"
-
- CMD = """\
- mapshaper -i {7} \
- -rectangle bbox={0},{1},{2},{3} name=rect \
- -clip rect target={8} \
- -proj crs="+proj=ortho +lon_0={4} +lat_0={5}" target={8},rect \
- -info target={8} \
- -o "{6}" target={8}
- """
-
- try:
- prepared = CMD.format(lon0, lat0, lon1, lat1, center_lon, center_lat, path, config["shapefile"], config["layer"])
- result = subprocess.check_output(
- prepared,
- shell=True,
- stderr=subprocess.STDOUT
- )
-
- for line in result.decode("utf-8").split("\n"):
- if "Bounds:" in line:
- data = [float(x) for x in line[8:].strip().split(",")]
-
- height = data[3] - data[1]
-
- results.append([item["name"], height])
- except Exception as e:
- print(e)
- print(result.decode("utf-8"))
-
- print(json.dumps(results))
- elif config["mode"] == "filter":
- 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
- )
-
-
- for item in config["items"]:
- CMD = (
- f"""mapshaper -i temp.shp """
- f"""-filter "{config["base-filter"]}" """
- f"""-filter "{item["filter"]}" """
- 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 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"""-style stroke-width={config["stroke-width"]} """
- f"""-o "../../{config["directory"]}/{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
-
- print(result.decode("utf-8"))
- for line in result.decode("utf-8").split("\n"):
- if "Bounds:" in line:
- coords = list(map(float, line[7:].strip().split(",")))
- print(coords)
- height = coords[3] - coords[1]
- height *= 1000 / (1000 - config["stroke-width"]) # accounts for the margin
- 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)
|