从名称获取城市多边形

时间:2016-07-14 04:09:44

标签: python python-2.7

在python中。我有城市/邮政地址的名称,我希望找到它周围多边形的坐标。我确实在很多地方都读到了它,但无法得到具体的指导。

import json
import requests
from urllib.parse import urlencode
import pprint

base_url="https://maps.googleapis.com/maps/api/geocode/json?"

address=input("Enter location: ")

main_url=base_url + urlencode({"address":address})

r=requests.get(main_url)

json_obj=r.json()

if 'status' not in json_obj or json_obj["status"]!="OK":
    print("fail")
    print(json_obj)


lat=json_obj["results"][0]["geometry"]["location"]["lat"]
lng=json_obj["results"][0]["geometry"]["location"]["lng"]
print("Latitudes:" ,lat)
print("Longitudes:", lng)
location=json_obj["results"][0]["formatted_address"]
print("Location:", location).

此代码用于在输入位置名称时给出位置lat long,但它不起作用!另外,我想从城市名称获取多边形而不是位置。 我也找到了这个网站。 http://overpass-turbo.eu/我可以输入城市名称并下载多边形坐标,但我不希望每次都手动执行此操作。如果我有100个城市,我不想逐个输入它们并为所有这些城市下载地理位置文档,然后在我的python查询中重复使用它们。是否有一些API,一些更好的方法呢?

编辑:这是导出的格式。

{
  "type": "FeatureCollection",
  "generator": "overpass-turbo",
  "copyright": "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.",
  "timestamp": "2016-07-14T03:50:02Z",
  "features": [
    {
      "type": "Feature",
      "id": "relation/127864",
      "properties": {
        "@id": "relation/127864",
        "admin_level": "6",
        "alt_name": "Soton",
        "borough": "yes",
        "boundary": "administrative",
        "designation": "unitary_authority",
        "name": "Southampton",
        "name:ja": "サウサンプトン",
        "name:ru": "Саутгемптон",
        "name:sr": "Саутемптон",
        "ons_code": "00MS",
        "operator": "Southampton City Council",
        "place": "city",
        "population": "228600",
        "ref:gss": "E06000045",
        "ref:lau:1": "UKJ3200",
        "ref:nuts:3": "UKJ32",
        "source:ons_code": "OS_OpenData_CodePoint Codelist.txt",
        "source:ref:gss": "ONS_OpenData",
        "source:ref:lau:1": "ONS_OpenData",
        "source:ref:nuts:3": "ONS_OpenData",
        "type": "boundary",
        "website": "http://www.southampton.gov.uk/",
        "wikipedia": "en:Southampton",
        "@geometry": "bounds"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -1.479005,
              50.8715095
            ],
            [
              -1.3219862,
              50.8715095
            ],
            [
              -1.3219862,
              50.9561307
            ],
            [
              -1.479005,
              50.9561307
            ],
            [
              -1.479005,
              50.8715095
            ]
          ]
        ]
      }
    }
  ]
}  

geometry.coordinates是我正在寻找的!

1 个答案:

答案 0 :(得分:0)

我不确定你的问题是什么。但是考虑到你的导出,你可以通过以下方式获取坐标:

export = """{
  "type": "FeatureCollection",
  [..]
  }"""

import json
results = json.loads(export)
print results['features'][0]['geometry']['coordinates'][0]

在线试用:https://repl.it/CbtH/0