less copy protection, more size visualization
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

223 lignes
6.7 KiB

  1. import json
  2. import sys
  3. import subprocess
  4. from tqdm import tqdm
  5. config = json.load(open(sys.argv[1]))
  6. if config["mode"] == "bounding-boxes":
  7. results = []
  8. for item in config["items"]:
  9. lon0 = item["corner0"][0]
  10. lat0 = item["corner0"][1]
  11. lon1 = item["corner1"][0]
  12. lat1 = item["corner1"][1]
  13. center_lat = (lat0 + lat1) / 2
  14. center_lon = (lon0 + lon1) / 2
  15. path = "../../" + config["directory"] + "/" + item["name"] + ".svg"
  16. CMD = """\
  17. mapshaper -i {7} \
  18. -rectangle bbox={0},{1},{2},{3} name=rect \
  19. -clip rect target={8} \
  20. -proj crs="+proj=ortho +lon_0={4} +lat_0={5}" target={8},rect \
  21. -info target={8} \
  22. -o "{6}" target={8}
  23. """
  24. try:
  25. prepared = CMD.format(lon0, lat0, lon1, lat1, center_lon, center_lat, path, config["shapefile"], config["layer"])
  26. result = subprocess.check_output(
  27. prepared,
  28. shell=True,
  29. stderr=subprocess.STDOUT
  30. )
  31. for line in result.decode("utf-8").split("\n"):
  32. if "Bounds:" in line:
  33. data = [float(x) for x in line[8:].strip().split(",")]
  34. height = data[3] - data[1]
  35. results.append([item["name"], height])
  36. except Exception as e:
  37. print(e)
  38. print(result.decode("utf-8"))
  39. print(json.dumps(results))
  40. elif config["mode"] == "filter":
  41. results = []
  42. CMD = (
  43. f"""mapshaper -i {config["shapefile"]} """
  44. f"""-filter "{config["base-filter"]}" """
  45. f"""-o temp.shp"""
  46. )
  47. result = subprocess.check_output(
  48. CMD,
  49. shell=True,
  50. stderr=subprocess.STDOUT
  51. )
  52. for item in config["items"]:
  53. CMD = (
  54. f"""mapshaper -i temp.shp """
  55. f"""-filter "{config["base-filter"]}" """
  56. f"""-filter "{item["filter"]}" """
  57. f"""-simplify interval=1000 """
  58. f"""-info """
  59. )
  60. result = subprocess.check_output(
  61. CMD,
  62. shell=True,
  63. stderr=subprocess.STDOUT
  64. )
  65. found = False
  66. for line in result.decode("utf-8").split("\n"):
  67. if "Bounds:" in line:
  68. coords = list(map(float, line[7:].strip().split(",")))
  69. lat = coords[1] + coords[3]
  70. lat /= 2
  71. lon = coords[0] + coords[2]
  72. lon /= 2
  73. found = True
  74. break
  75. if not found:
  76. print("Did not find ", item["name"])
  77. continue
  78. CMD = (
  79. f"""mapshaper -i temp.shp """
  80. f"""-filter "{config["base-filter"]}" """
  81. f"""-filter "{item["filter"]}" """
  82. f"""-simplify interval={config["simplify-size"]} """
  83. f"""-proj "+proj=ortho +lat_0={lat} +lon_0={lon}" """
  84. f"""-info """
  85. f"""-style stroke-width={config["stroke-width"]} """
  86. f"""-o "../../{config["directory"]}/{item["name"]}.svg" margin={config["stroke-width"]/2} height=1000 """
  87. )
  88. try:
  89. result = subprocess.check_output(
  90. CMD,
  91. shell=True,
  92. stderr=subprocess.STDOUT
  93. )
  94. except subprocess.CalledProcessError as exc:
  95. print(exc.output.decode("utf-8"))
  96. continue
  97. print(result.decode("utf-8"))
  98. for line in result.decode("utf-8").split("\n"):
  99. if "Bounds:" in line:
  100. coords = list(map(float, line[7:].strip().split(",")))
  101. print(coords)
  102. height = coords[3] - coords[1]
  103. height *= 1000 / (1000 - config["stroke-width"]) # accounts for the margin
  104. results.append([item["name"], height])
  105. print(results)
  106. elif config["mode"] == "forms":
  107. results = []
  108. CMD = (
  109. f"""mapshaper -i {config["shapefile"]} """
  110. f"""-filter "{config["base-filter"]}" """
  111. f"""-o temp.shp"""
  112. )
  113. result = subprocess.check_output(
  114. CMD,
  115. shell=True,
  116. stderr=subprocess.STDOUT
  117. )
  118. CMD = (
  119. f"""mapshaper -i temp.shp """
  120. f"""encoding=utf-8 """
  121. f"""-each "console.log(JSON.stringify({{id: this.id, name: {config["name-key"]}, key: {config["form-key"]} }}))" """
  122. f"""-split """
  123. f"""-o tmp/ format=topojson singles target=* """
  124. )
  125. try:
  126. result = subprocess.check_output(
  127. CMD,
  128. shell=True
  129. )
  130. except subprocess.CalledProcessError as e:
  131. print(e.output)
  132. entities = []
  133. for line in result.decode("utf-8").split("\n"):
  134. if len(line) > 0:
  135. print(line)
  136. entities.append(json.loads(line))
  137. for item in tqdm(entities):
  138. CMD = (
  139. f"""mapshaper -i tmp/temp-{item["id"] + 1}.json """
  140. f"""encoding=utf-8 """
  141. f"""-simplify interval=1000 """
  142. f"""-info """
  143. )
  144. result = subprocess.check_output(
  145. CMD,
  146. shell=True,
  147. stderr=subprocess.STDOUT
  148. )
  149. found = False
  150. for line in result.decode("utf-8").split("\n"):
  151. if "Bounds:" in line:
  152. coords = list(map(float, line[7:].strip().split(",")))
  153. lat = coords[1] + coords[3]
  154. lat /= 2
  155. lon = coords[0] + coords[2]
  156. lon /= 2
  157. found = True
  158. break
  159. if not found:
  160. print("Did not find ", item["name"])
  161. continue
  162. CMD = (
  163. f"""mapshaper -i tmp/temp-{item["id"] + 1}.json """
  164. f"""encoding=utf-8 """
  165. f"""-simplify interval={config["simplify-size"]} """
  166. f"""-proj "+proj=ortho +lat_0={lat} +lon_0={lon}" """
  167. f"""-info """
  168. f"""-style stroke-width={config["stroke-width"]} """
  169. f"""-o "../../{config["directory"]}/{item["key"]}/{item["name"]}.svg" margin={config["stroke-width"]/2} height=1000 """
  170. )
  171. try:
  172. result = subprocess.check_output(
  173. CMD,
  174. shell=True,
  175. stderr=subprocess.STDOUT
  176. )
  177. except subprocess.CalledProcessError as exc:
  178. print(exc.output.decode("utf-8"))
  179. continue
  180. for line in result.decode("utf-8").split("\n"):
  181. if "Bounds:" in line:
  182. coords = list(map(float, line[7:].strip().split(",")))
  183. height = coords[3] - coords[1]
  184. height *= 1000 / (1000 - config["stroke-width"]) # accounts for the margin
  185. results.append({"name": item["name"], "form": item["key"], "height": round(height)})
  186. print(results)