less copy protection, more size visualization
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

126 řádky
3.8 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["corner0"][0]
  9. lat0 = item["corner0"][1]
  10. lon1 = item["corner1"][0]
  11. lat1 = item["corner1"][1]
  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=ortho +lon_0={4} +lat_0={5}" target={8},rect \
  20. -info target={8} \
  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. stderr=subprocess.STDOUT
  29. )
  30. for line in result.decode("utf-8").split("\n"):
  31. if "Bounds:" in line:
  32. data = [float(x) for x in line[8:].strip().split(",")]
  33. height = data[3] - data[1]
  34. results.append([item["name"], height])
  35. except Exception as e:
  36. print(e)
  37. print(result.decode("utf-8"))
  38. print(json.dumps(results))
  39. elif config["mode"] == "filter":
  40. results = []
  41. CMD = (
  42. f"""mapshaper -i {config["shapefile"]} """
  43. f"""-filter "{config["base-filter"]}" """
  44. f"""-o temp.shp"""
  45. )
  46. result = subprocess.check_output(
  47. CMD,
  48. shell=True,
  49. stderr=subprocess.STDOUT
  50. )
  51. for item in config["items"]:
  52. CMD = (
  53. f"""mapshaper -i temp.shp """
  54. f"""-filter "{config["base-filter"]}" """
  55. f"""-filter "{item["filter"]}" """
  56. f"""-simplify interval=1000 """
  57. f"""-info """
  58. )
  59. result = subprocess.check_output(
  60. CMD,
  61. shell=True,
  62. stderr=subprocess.STDOUT
  63. )
  64. found = False
  65. for line in result.decode("utf-8").split("\n"):
  66. if "Bounds:" in line:
  67. coords = list(map(float, line[7:].strip().split(",")))
  68. lat = coords[1] + coords[3]
  69. lat /= 2
  70. lon = coords[0] + coords[2]
  71. lon /= 2
  72. found = True
  73. break
  74. if not found:
  75. print("Did not find ", item["name"])
  76. continue
  77. CMD = (
  78. f"""mapshaper -i temp.shp """
  79. f"""-filter "{config["base-filter"]}" """
  80. f"""-filter "{item["filter"]}" """
  81. f"""-simplify interval={config["simplify-size"]} """
  82. f"""-proj "+proj=ortho +lat_0={lat} +lon_0={lon}" """
  83. f"""-info """ +
  84. f"""-style stroke-width={config["stroke-width"]} """
  85. f"""-o "../../{config["directory"]}/{item["name"]}.svg" margin={config["stroke-width"]/2} height=1000 """
  86. )
  87. try:
  88. result = subprocess.check_output(
  89. CMD,
  90. shell=True,
  91. stderr=subprocess.STDOUT
  92. )
  93. except subprocess.CalledProcessError as exc:
  94. print(exc.output.decode("utf-8"))
  95. continue
  96. print(result.decode("utf-8"))
  97. for line in result.decode("utf-8").split("\n"):
  98. if "Bounds:" in line:
  99. coords = list(map(float, line[7:].strip().split(",")))
  100. print(coords)
  101. height = coords[3] - coords[1]
  102. height *= 1000 / (1000 - config["stroke-width"]) # accounts for the margin
  103. results.append([item["name"], height])
  104. print(results)