def calc_stats_shapely()

in data/geospatial/geospatial-gen.py [0:0]


def calc_stats_shapely(wkts):
    geometries = shapely.from_wkt(wkts)

    # Calculate the list of iso type codes
    any_not_null = any(geom is not None for geom in geometries)
    type_codes = set(geometry_type_code(wkt) for wkt in wkts)

    # Calculate min/max ignoring nan values
    coords = shapely.get_coordinates(geometries, include_z=True, include_m=True)
    coord_mins = [
        None if np.isposinf(x) else float(x)
        for x in np.nanmin(coords, 0, initial=np.inf)
    ]
    coord_maxes = [
        None if np.isneginf(x) else float(x)
        for x in np.nanmax(coords, 0, initial=-np.inf)
    ]

    # Assemble stats in the same format as returned by geo_statistics.to_dict()
    stats = {}
    stats["geospatial_types"] = list(
        sorted(code for code in type_codes if code is not None)
    ) if any_not_null else None
    stats["xmin"], stats["ymin"], stats["zmin"], stats["mmin"] = coord_mins
    stats["xmax"], stats["ymax"], stats["zmax"], stats["mmax"] = coord_maxes

    return stats