pyeo.coordinate_manipulation

Contains a set of functions for transforming spatial coorinates between projections and pixel indicies.

Unless otherwise stated, all functions assume that any geometry, rasters and shapefiles are using the same projection. If they are not, there may be unexpected errors.

Some of these functions call for an AOI shapefile. This is a single-layer shapefile containing only the geometry of one polygon.

These functions all work on the objects provided by the ogr and gdal libraries. If you wish to use them in your own processing, a gdal.Image object is usually the output from gdal.Open() and an ogr.Geometry object can be obtained from a well-known text (wkt) string using the snipped `object=ogr.ImportFromWkt(“mywkt”). For more information on wkt, see https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry and the “QuickWKT” QGIS plugin.

pyeo.coordinate_manipulation.align_bounds_to_whole_number(bounding_box)

Creates a new bounding box with it’s height and width rounded to the nearest whole number.

Parameters

bounding_box – An ogr.Geometry object containing a raster’s bounding box as a polygon.

Returns

Return type

An ogr.Geometry object containing the aligned bounding box.

pyeo.coordinate_manipulation.check_overlap(raster, aoi)

A test to see if a raster and an AOI overlap. :param raster: A gdal.Image object :param aoi: A ogr.Dataset object containing a single polygon

Returns

Return type

True if the raster and the polygon overlap, oherwise False.

pyeo.coordinate_manipulation.floor_to_resolution(input, resolution)

Returns input rounded DOWN to the nearest multiple of resolution. Used to prevent float errors on pixel boarders.

Parameters
  • input – The value to be rounded

  • resolution – The resolution

Returns

Return type

The largest value between input and 0 that is divisible by resolution.

Notes

Uses the following formula: input-(input%resolution)

pyeo.coordinate_manipulation.get_aoi_bounds(aoi)

Returns a wkbPolygon geometry with the bounding rectangle of a single-polygon shapefile

Parameters

aoi – An ogr.Dataset object containing a single layer.

pyeo.coordinate_manipulation.get_aoi_intersection(raster, aoi)

Returns a wkbPolygon geometry with the intersection of a raster and a shpefile containing an area of interest

Parameters
  • raster – A raster containing image data

  • aoi – A shapefile with a single layer and feature

Returns

Return type

a ogr.Geometry object containing a single polygon with the area of intersection

pyeo.coordinate_manipulation.get_aoi_size(aoi)

Returns the width and height of the bounding box of an aoi.

Parameters

aoi – A shapefile containing a single layer with a single polygon

Returns

Return type

A tuple of (width, height)

pyeo.coordinate_manipulation.get_combined_polygon(rasters, geometry_mode='intersect')

Returns a polygon containing the combined boundary of each raster in rasters.

Parameters
  • rasters – A list of raster objects opened with gdal.Open()

  • geometry_mode – If ‘intersect’, returns the boundary of the area that all rasters cover. If ‘union’, returns the boundary of the area that any raster covers.

Returns

Return type

ogr.Geometry() containing a polygon.

pyeo.coordinate_manipulation.get_local_top_left(raster1, raster2)

Gets the top-left corner of raster1 in the array of raster 2. Assumes both rasters are in the same projection and units.

Parameters
  • raster1 – The raster to get the top-left corner of.

  • raster2 – The raster that raster1’s top-left corner is over.

Returns

Return type

A tuple of (x_pixel, y_pixel), containing the indicies of the point in the raster.

pyeo.coordinate_manipulation.get_poly_bounding_rect(poly)

Returns a polygon of the bounding rectangle of an input polygon. :param poly: An ogr.Geometry object containing a polygon

Returns

Return type

An ogr.Geometry object with a four-point polygon representing the bounding rectangle.

pyeo.coordinate_manipulation.get_poly_intersection(poly1, poly2)

A functional wrapper for ogr.Geometry.Intersection()

pyeo.coordinate_manipulation.get_poly_size(poly)

Returns the width and height of a bounding box of a polygon

Parameters

poly – A ogr.Geometry object containing the polygon.

Returns

Return type

A tuple of (width, height)

pyeo.coordinate_manipulation.get_raster_bounds(raster)

Returns a wkbPolygon geometry with the bounding rectangle of a raster calculated from its geotransform.

Parameters

raster – A gdal.Image object

Returns

  • An ogr.Geometry object containing a single wkbPolygon with four points defining the bounding rectangle of the

  • raster.

Notes

Bounding rectangle is obtained from raster.GetGeoTransform(), with the top left corners rounded down to the nearest multiple of of the resolution of the geotransform. This is to avoid rounding errors in reprojected geotransformations.

pyeo.coordinate_manipulation.get_raster_intersection(raster1, raster2)

Returns a wkbPolygon geometry with the intersection of two raster bounding boxes.

Parameters

raster2 (raster1,) – A gdal.Image() object

Returns

Return type

a ogr.Geometry object containing a single polygon

pyeo.coordinate_manipulation.get_raster_size(raster)

Return the width and height of a raster, in that raster’s units.

Parameters

raster – A gdal.Image object

Returns

Return type

A tuple containing (width, height)

pyeo.coordinate_manipulation.multiple_intersection(polygons)

Takes a list of polygons and returns a geometry representing the intersection of all of them

Parameters

polygons – A list of ogr.Geometry objects, each containing a single polygon.

Returns

Return type

An ogr.Geometry object containing a single polygon

pyeo.coordinate_manipulation.multiple_union(polygons)

Takes a list of polygons and returns a polygon of the union of their perimeter

Parameters

polygons – A list of ogr.Geometry objects, each containing a single polygon.

Returns

Return type

An ogr.Geometry object containing a single polygon

pyeo.coordinate_manipulation.pixel_bounds_from_polygon(raster, polygon)

Returns the bounding box of the overlap between a raster and a polygon in the raster

Parameters
  • raster – A gdal raster object

  • polygon – A ogr.Geometry object containing a single polygon

Returns

Return type

A tuple (x_min, x_max, y_min, y_max)

pyeo.coordinate_manipulation.pixel_to_point_coordinates(pixel, GT)

Given a pixel and a geotransformation, returns the picaltion of that pixel’s top left corner in the projection used by the geotransform. NOTE: At present, this takes input in the form of y,x! This is opposite to the output of point_to_pixel_coordinates!

Parameters
  • pixel – A tuple (y, x) of the coordinates of the pixel

  • GT – A six-element numpy array containing a geotransform

Returns

Return type

A tuple containing the geographic coordinates of the top-left corner of the pixel.

pyeo.coordinate_manipulation.point_to_pixel_coordinates(raster, point, oob_fail=False)
Returns a tuple (x_pixel, y_pixel) in a georaster raster corresponding to the geographic point in a projection.

Assumes raster is north-up non rotated.

Parameters
  • raster – A gdal raster object

  • point

    One of:

    A well-known text string of a single point An iterable of the form (x,y) An ogr.Geometry object containing a single point

Returns

Return type

A tuple of (x_pixel, y_pixel), containing the indicies of the point in the raster.

Notes

The equation is a rearrangement of the section on affinine geotransform in http://www.gdal.org/gdal_datamodel.html

pyeo.coordinate_manipulation.reproject_geotransform(in_gt, old_proj_wkt, new_proj_wkt)

Reprojects a geotransform from the old projection to a new projection. See [https://gdal.org/user/raster_data_model.html]

Parameters
  • in_gt – A six-element numpy array, usually an output from gdal_image.GetGeoTransform()

  • old_proj_wkt – The projection of the old geotransform in well-known text.

  • new_proj_wkt – The projection of the new geotrasform in well-known text.

Returns

The geotransform in the new projection

Return type

out_gt

pyeo.coordinate_manipulation.reproject_vector(in_path, out_path, dest_srs)

Reprojects a vector file to a new SRS. Simple wrapper for ogr2ogr. :param in_path: :param out_path: :param dest_srs:

pyeo.coordinate_manipulation.write_geometry(geometry, out_path, srs_id=4326)

Saves the geometry in an ogr.Geometry object to a shapefile.

Parameters
  • geometry – An ogr.Geometry object

  • out_path – The location to save the output shapefile

  • srs_id – The projection of the output shapefile. Can be an EPSG number or a WKT string.

Notes

The shapefile consists of one layer named ‘geometry’.