Geometry tools

When working with geometry data, the functions in this library can be practical to help you with some calculations and easier support for widely used Esri Shapefiles.

The foundation on which we mostly build is the shapely library, which supports a wide area of geometrical functions.

calculate_area_m2

With calculate_area_m2(poly_area, projection) you can calculate the area (in m2) of a polygon for a specified projection, where the standard projection is EPSG:4326.

calculate_area_box_m2

The calculate_area_box_m2(x1, y1, x2, y2, projection) is comparable to calculate_area_m2 where the input in this case is specified by the four corners of a box.

shapefile_reader

Using the shapefile reader you can read both the geometry and data provided by an Esri type shapefile, our example below reads all dutch provinces and shows the calculated area in km2.

The input files used are:

lib_geometry.py
1import valuea_framework.geometry
2
3in_shp = 'input/2018_Imergis_provinciegrenzen_met_water.shp'
4in_dbf = 'input/2018_Imergis_provinciegrenzen_met_water.dbf'
5
6for shape in valuea_framework.geometry.shapefile_reader(shp_file=in_shp, dbf_file=in_dbf):
7    m2 = valuea_framework.geometry.calculate_area_m2(shape.geom, 'EPSG:28992')
8    km2 = m2 / 1000000.0
9    print ("%-30s %.2f" % (shape.record['provincien'], km2))

This outputs a result comparable to:

Noord-Holland                  4093.39
Groningen                      2960.05
Overijssel                     3421.89
Zeeland                        2934.02
Friesland                      5750.22
Drenthe                        2681.07
Flevoland                      2413.25
Utrecht                        1449.73
Zuid-Holland                   3420.00
Limburg                        2210.41
Gelderland                     5138.30
Noord-Brabant                  5083.83