ramesesjd
ramesesjd

Reputation: 303

How to calculate the area of several MultiPolygons from a geoJSON file?

I'm trying to calculate the area of each feature (i.e. neighbourhood) in a GeoJSON file. Ideally, the result would be either added as a field in file or in a separate dataframe, however I'm unable to even calculate the area at present. My code is as follows:

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))

The result from the code above is a lot of 0's. The area function is supposed to 'calculate the area inside any GeoJSON geometry'. So, I'm not sure if I'm just not passing the file correctly to the function or if the function doesn't like 'MultiPolygons' like those in the file.

When I use just print(x) in the for loop, the output is for example:

{'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'}}

Upvotes: 1

Views: 1617

Answers (1)

Nikolas Stevenson-Molnar
Nikolas Stevenson-Molnar

Reputation: 4700

It looks like the area library wants only the geometry, not the entire feature. So try this instead:

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

Upvotes: 5

Related Questions