less copy protection, more size visualization
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

88 行
2.6 KiB

  1. import requests
  2. import sys
  3. import urllib.parse
  4. import json
  5. import subprocess
  6. import os
  7. def get_polygon(name):
  8. if not os.path.isfile(name + ".json"):
  9. url = "https://nominatim.openstreetmap.org/search.php?q={0}&polygon_geojson=1&format=jsonv2".format(
  10. urllib.parse.quote(name)
  11. )
  12. r = requests.get(url)
  13. data = json.loads(r.text)
  14. if data is None:
  15. raise ValueError("Bogus results")
  16. osm_id = None
  17. for entry in data:
  18. if "boundary" in entry["category"] and entry["osm_type"] == "relation":
  19. osm_id = entry["osm_id"]
  20. break
  21. if not osm_id:
  22. raise ValueError("No id")
  23. url = "http://polygons.openstreetmap.fr/get_geojson.py?id={0}&params=0".format(osm_id)
  24. with open(name + ".json", "w") as file:
  25. file.write(requests.get(url).text)
  26. else:
  27. data = json.load(open(name + ".json"))
  28. info = subprocess.check_output(
  29. [
  30. "mapshaper",
  31. "-i",
  32. "{0}.json".format(name),
  33. "-each",
  34. "console.log(this.centroidX, this.centroidY, this.area)"],
  35. shell=True,
  36. stderr=subprocess.STDOUT,
  37. ).decode("utf-8")
  38. lon, lat, area = list(map(float, info.split(" ")))
  39. if area is not None:
  40. info = subprocess.check_output(
  41. [
  42. "mapshaper",
  43. "-i",
  44. "{0}.json".format(name),
  45. "-proj",
  46. "+proj=nsper",
  47. "+h=100000",
  48. "+lat_0={0}".format(lat),
  49. "+lon_0={0}".format(lon),
  50. "-simplify",
  51. "resolution=500x500",
  52. "-each",
  53. "console.log(\"HEIGHT:\" + this.height)",
  54. "-o",
  55. "{0}.svg".format(name),
  56. ],
  57. shell=True,
  58. stderr=subprocess.STDOUT,
  59. ).decode("utf-8")
  60. height = None
  61. for line in info.split("\n"):
  62. if "HEIGHT:" in line:
  63. height = float(line.split(":")[1].strip())
  64. print("[\"{0}\", {1}, {2}],".format(name, area, height))
  65. if __name__ == "__main__":
  66. if len(sys.argv) < 2:
  67. print("Usage: {0} list-of-cities".format(sys.argv[0]))
  68. else:
  69. city = None
  70. try:
  71. for city in open(sys.argv[1]).readlines():
  72. try:
  73. get_polygon(city.strip())
  74. except ValueError as e:
  75. print(city + " failed")
  76. print(e)
  77. except Exception as e:
  78. print(city + " failed")
  79. print(e)