如何从geoJSON文件计算几个MultiPolygon的面积?

时间:2019-03-03 19:37:14

标签: python python-3.x geojson

我正在尝试计算GeoJSON文件中每个要素(即社区)的面积。理想情况下,结果将被添加为文件中的字段或单独的数据框,但是目前我什至无法计算面积。我的代码如下:

import urllib
import json
from area import area

bdry_url = urllib.request.urlopen("https://data.edmonton.ca/resource/xu6q-xcmj.geojson")
with bdry_url as bdry_file:
    bdry = json.load(bdry_file)


for x in bdry['features']:
    print(area(x))

上面代码的结果是很多0。区域函数应该为'calculate the area inside any GeoJSON geometry'。因此,我不确定是不是只是将文件正确地传递给函数,还是函数不喜欢文件中的'MultiPolygons'。

当我在print(x)循环中仅使用for时,输出例如:

{'type': 'Feature', 'geometry': {'type': 'MultiPolygon', 'coordinates': [[[[-113.47970029870794, 53.6152271386194], [-113.47623664663439, 53.61561158319339], [-113.4744820422431, 53.6158162210957], [-113.4731808787134, 53.61590027235317], [-113.4674204545053, 53.61591375444001], [-113.46742798260262, 53.610538856185805], [-113.46743523571406, 53.60691755885339], [-113.46961818955656, 53.60690984975463], [-113.47022601929541, 53.60689581095175], [-113.47086485888067, 53.60685578200864], [-113.4715850828474, 53.606767279197136], [-113.47225008266139, 53.606645830070825], [-113.47373112695321, 53.6063652736457], [-113.47641979230873, 53.6058984811946], [-113.47672620998279, 53.60585027816153], [-113.47791436479508, 53.60576846613057], [-113.48101443948326, 53.605768565180774], [-113.48173969843924, 53.605811862524945], [-113.4824407929969, 53.605915397805376], [-113.48510468465751, 53.606306092276874], [-113.48850157059921, 53.60679389522115], [-113.488933001326, 53.606820031912164], [-113.49168085687263, 53.606784967555676], [-113.49170507787457, 53.60810574183392], [-113.49171395279058, 53.61275181411882], [-113.49179330220164, 53.614032553715944], [-113.48966991380574, 53.6140237367952], [-113.48906656552556, 53.61404155038072], [-113.48794288078807, 53.614190044322235], [-113.48600883911077, 53.61444001996334], [-113.47970029870794, 53.6152271386194]]]]}, 'properties': {'descriptio': 'Evansdale is named for H.M.E. Evans, Mayor of Edmonton in 1918. Mr. Evans was also president of the Edmonton Board of Trade in 1916. Although single detached homes account for 90% of the structures in Evansdale, almost 50% of the dwelling units are locat', 'descriptiv': 'Evansdale', 'name': 'EVANSDALE', 'neighbourh': '2260'}}

1 个答案:

答案 0 :(得分:2)

area库似乎只想要几何图形,而不是整个要素。因此,请尝试以下操作:

for x in bdry['features']:
    print(area(x['geometry']))