Calculate Polygon Area

I managed to calculate a single polygon form a Json file, but when i want to calculate the total surface of the Json file i don’t get the expected results.
This code works for a single Polygon, but it cant run the total file:
f = open(‘wijken.json’)
data2 = json.load(f)
color = Color(85, 191, 19)
list_features1 = []
list_x = []
list_y = []
list_coordinates_1 = []
for j in range(len(data2[‘features’][0][‘geometry’][‘coordinates’][0])):
coordinates_rd = tuple(data2[“features”][0][“geometry”][“coordinates”][0][j])
lat, lon = RDWGSConverter.from_rd_to_wgs(coordinates_rd)
list_x.append(coordinates_rd[0])
list_y.append(coordinates_rd[1])
list_coordinates_1.append(MapPoint(lat, lon))
list_features1.append(MapPolygon(list_coordinates_1, color=color))

optellen = 0
for k in range(len(list_x)):
if k < (len(list_x) - 1):
values_x = list_x[k] * list_y[k+1]
optellen += values_x
values_y = list_y[k] * list_x[k+1]
optellen -= values_y

shoelace = abs(optellen/2)
print(shoelace)

image

This is the code for the total file with all the polygons but this is not the right answer:

f = open(‘wijken.json’)
data = json.load(f)
color = Color(85, 191, 19)
list_features2 = []
list_x = []
list_y = []
for i in range(len(data[“features”])):
list_coordinates = []
for j in range(len(data[“features”][i][“geometry”][“coordinates”][0])):
coordinates_rd = tuple(data[“features”][i][“geometry”][“coordinates”][0][j])
lat, lon = RDWGSConverter.from_rd_to_wgs(coordinates_rd)
list_x.append(coordinates_rd[0])
list_y.append(coordinates_rd[1])
list_coordinates.append(MapPoint(lat, lon))
list_features2.append(MapPolygon(list_coordinates, color=color))

optellen = 0
for k in range(len(list_x)):
if k < (len(list_x) - 1):
values_x = list_x[k] * list_y[k+1]
optellen += values_x
values_y = list_y[k] * list_x[k+1]
optellen -= values_y

shoelace = abs(optellen/2)
print(shoelace)


Json file: https://denhaag.dataplatform.nl/#/data/c1059cef-be66-4a7a-9657-2f38f55794ed
Can somebody help me?

Hi @Tiesvandam1 , I guess you would like to sum the area of all polygons. I believe the shoelace method only works when applying it to one polygon at a time. The first part you have written works for a single polygon. I would suggest to turn this into a function, as follows (by the way, you can make code blocks here in the forum as explained in this link):

def polygon_area_from_coordinates(coordinates):
    list_x = []
    list_y = []
    for coordinate in coordinates:
        list_x.append(coordinate[0])
        list_y.append(coordinate[1])

    optellen = 0
    for k in range(len(list_x)):
        if k < (len(list_x) - 1):
            values_x = list_x[k] * list_y[k + 1]
            optellen += values_x
            values_y = list_y[k] * list_x[k + 1]
            optellen -= values_y
    return abs(optellen / 2)

Then, you can loop over all the different polygons, and use your newly-crafted function:

total_polygon_area = 0
for feature in data["features"]:
    coordinates = feature["geometry"]["coordinates"][0]
    polygon_area = polygon_area_from_coordinates(coordinates)
    total_polygon_area += polygon_area

print(total_polygon_area)
1 Like