Polygons#

Polygons represents a set of polygon geometries — typically field boundaries (talhões). It subclasses geopandas.GeoDataFrame and shares the same basics as the other types (CRS conversions, table utilities, save() — see the Lines page for the list).

from shapelib import read_file

fields = read_file("fields.shp")

Areas and perimeters#

fields.calculate_area()        # geodesic area per polygon ("area (ha)" column)
fields.calculate_perimeter()   # geodesic perimeter ("perimeter (m)" column)

fields.get_total_area()        # sum over all polygons
fields.get_total_perimeter()

Measurements are geodesic (computed on the WGS 84 ellipsoid), so they are accurate regardless of the file’s CRS.

Filtering#

fields.filter_by_area(1.0)        # drop polygons below a minimum area
fields.filter_by_hole_area(0.5)   # remove small holes inside polygons

Fixing geometries#

Real-world polygon files often carry invalid rings (self-intersections, bad ring order). Invalid geometries are validated and repaired when the object is created, and you can force another pass any time:

fields.force_make_valid()

Simplifying#

fields.simplify(tolerance=0.001)   # fewer vertices, holes preserved

Rasters#

Polygons converts to and from raster masks:

fields = Polygons.from_mask(mask, dataset_reader=orto)
mask = fields.to_mask(dataset_reader=orto)

See Raster converters for details.