Skip to content

vector

This module contains a set of functions to deal with OGR geometries

get_bbox_wgs84(vector_file)

Returns the bounding box in WGS84 CRS from a vector data

Parameters:

Name Type Description Default
vector_file str

vector data filename

required

Returns:

Type Description
BoundingBox

bounding box in WGS84 CRS (BoundingBox instance)

Source code in scenes/vector.py
def get_bbox_wgs84(vector_file: str) -> BoundingBox:
    """
    Returns the bounding box in WGS84 CRS from a vector data

    Args:
        vector_file: vector data filename

    Returns:
        bounding box in WGS84 CRS (BoundingBox instance)

    """
    poly_ds = ogr_open(vector_file=vector_file)
    poly_layer = poly_ds.GetLayer()
    extent = poly_layer.GetExtent()
    coords = ((extent[0], extent[2]), (extent[1], extent[3]))
    src_srs = poly_layer.GetSpatialRef()
    tgt_srs = epsg2srs(4326)
    ((xmin, ymin), (xmax, ymax)) = reproject_coords(  # pylint: disable=W0632
        coords=coords,
        src_srs=src_srs,
        tgt_srs=tgt_srs
    )
    return BoundingBox(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax)

ogr_open(vector_file)

Return the vector dataset from a vector file. If the vector is empty, None is returned.

Parameters:

Name Type Description Default
vector_file str

input vector file

required

Returns:

Type Description
DataSource

ogr ds, or None (if error)

Source code in scenes/vector.py
def ogr_open(vector_file: str) -> ogr.DataSource:
    """
    Return the vector dataset from a vector file. If the vector is empty, None
    is returned.

    Args:
        vector_file: input vector file

    Returns:
        ogr ds, or None (if error)

    """
    poly_ds = ogr.Open(vector_file)
    if poly_ds is None:
        return None
    return poly_ds

poly_union(layer)

Compute the union all the geometrical features of layer.

Parameters:

Name Type Description Default
layer Layer

The layer

required

Returns:

Type Description
Geometry

the union of the layer's polygons (as a geometry)

Source code in scenes/vector.py
def poly_union(layer: ogr.Layer) -> ogr.Geometry:
    """
    Compute the union all the geometrical features of layer.

    Args:
        layer: The layer

    Returns:
        the union of the layer's polygons (as a geometry)

    """
    union1 = None
    for feat in layer:
        geom = feat.GetGeometryRef()
        if union1 is None:
            union1 = geom.Clone()
        else:
            union1 = union1.Union(geom)

    return union1

reproject_ogr_layer(layer, epsg=4326)

Reproject the input polygon

Parameters:

Name Type Description Default
layer Layer

OGR layer

required
epsg int

EPSG number (int)

4326

Returns:

Type Description
Geometry

New polygon projected in the specified CRS

Source code in scenes/vector.py
def reproject_ogr_layer(layer: ogr.Layer, epsg: int = 4326) -> ogr.Geometry:
    """
    Reproject the input polygon

    Args:
        layer: OGR layer
        epsg: EPSG number (int)

    Returns:
        New polygon projected in the specified CRS

    """

    # set spatial reference and transformation
    sourceprj = layer.GetSpatialRef()
    targetprj = epsg2srs(epsg)
    transform = coordinates_transform(sourceprj, targetprj)

    # apply transformation
    poly = poly_union(layer)
    poly.Transform(transform)
    return poly