import json import sys import subprocess config = json.load(open(sys.argv[1])) if config["mode"] == "bounding-boxes": results = [] for item in config["items"]: lon0 = item["lon0"] lat0 = item["lat0"] lon1 = item["lon1"] lat1 = item["lat1"] 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=ne_10m_land \ -proj crs="+proj=nsper +h=10000000 +lon_0={4} +lat_0={5}" target=ne_10m_land,rect \ -each 'console.log(this.bounds.concat([this.area]))' target=rect \ -o "{6}" target=ne_10m_land """ prepared = CMD.format(lon0, lat0, lon1, lat1, center_lon, center_lat, path, config["shapefile"]) result = subprocess.check_output( prepared, shell=True ) data = json.loads(result.decode("utf-8")) height = data[3] - data[1] area = data[4] results.append([item["name"], area, height]) 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)