less copy protection, more size visualization
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

123 lines
3.6 KiB

  1. import json
  2. import sys
  3. import subprocess
  4. config = json.load(open(sys.argv[1]))
  5. if config["mode"] == "bounding-boxes":
  6. results = []
  7. for item in config["items"]:
  8. lon0 = item["lon0"]
  9. lat0 = item["lat0"]
  10. lon1 = item["lon1"]
  11. lat1 = item["lat1"]
  12. center_lat = (lat0 + lat1) / 2
  13. center_lon = (lon0 + lon1) / 2
  14. path = "../../" + config["directory"] + "/" + item["name"] + ".svg"
  15. CMD = """\
  16. mapshaper -i {7} \
  17. -rectangle bbox={0},{1},{2},{3} name=rect \
  18. -clip rect target={8} \
  19. -proj crs="+proj=nsper +h=10000000 +lon_0={4} +lat_0={5}" target={8},rect \
  20. -each 'console.log(this.bounds.concat([this.area]))' target=rect \
  21. -o "{6}" target={8}
  22. """
  23. try:
  24. prepared = CMD.format(lon0, lat0, lon1, lat1, center_lon, center_lat, path, config["shapefile"], config["layer"])
  25. result = subprocess.check_output(
  26. prepared,
  27. shell=True
  28. )
  29. data = json.loads(result.decode("utf-8"))
  30. height = data[3] - data[1]
  31. area = data[4]
  32. results.append([item["name"], area, height])
  33. except:
  34. print(result.decode("utf-8"))
  35. print(json.dumps(results))
  36. elif config["mode"] == "filter":
  37. results = []
  38. CMD = (
  39. f"""mapshaper -i {config["shapefile"]} """
  40. f"""-filter "{config["base-filter"]}" """
  41. f"""-o temp.shp"""
  42. )
  43. result = subprocess.check_output(
  44. CMD,
  45. shell=True,
  46. stderr=subprocess.STDOUT
  47. )
  48. for item in config["items"]:
  49. CMD = (
  50. f"""mapshaper -i temp.shp """
  51. f"""-filter "{config["base-filter"]}" """
  52. f"""-filter "{item["filter"]}" """
  53. f"""-simplify interval=1000 """
  54. f"""-info """
  55. )
  56. result = subprocess.check_output(
  57. CMD,
  58. shell=True,
  59. stderr=subprocess.STDOUT
  60. )
  61. found = False
  62. for line in result.decode("utf-8").split("\n"):
  63. if "Bounds:" in line:
  64. coords = list(map(float, line[7:].strip().split(",")))
  65. lat = coords[1] + coords[3]
  66. lat /= 2
  67. lon = coords[0] + coords[2]
  68. lon /= 2
  69. found = True
  70. break
  71. if not found:
  72. print("Did not find ", item["name"])
  73. continue
  74. CMD = (
  75. f"""mapshaper -i temp.shp """
  76. f"""-filter "{config["base-filter"]}" """
  77. f"""-filter "{item["filter"]}" """
  78. f"""-simplify interval={config["simplify-size"]} """
  79. f"""-proj "+proj=ortho +lat_0={lat} +lon_0={lon}" """
  80. f"""-info """ +
  81. f"""-style stroke-width={config["stroke-width"]} """
  82. f"""-o "../../{config["directory"]}/{item["name"]}.svg" margin={config["stroke-width"]/2} height=1000 """
  83. )
  84. try:
  85. result = subprocess.check_output(
  86. CMD,
  87. shell=True,
  88. stderr=subprocess.STDOUT
  89. )
  90. except subprocess.CalledProcessError as exc:
  91. print(exc.output.decode("utf-8"))
  92. continue
  93. print(result.decode("utf-8"))
  94. for line in result.decode("utf-8").split("\n"):
  95. if "Bounds:" in line:
  96. coords = list(map(float, line[7:].strip().split(",")))
  97. print(coords)
  98. height = coords[3] - coords[1]
  99. height *= 1000 / (1000 - config["stroke-width"]) # accounts for the margin
  100. results.append([item["name"], height])
  101. print(results)