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.
 
 
 

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