General Spatial Tools

cmap_mpl2gdal

convert a matplotlib color table to a GDAL representation.

coordinate_reproject

reproject a coordinate from one CRS to another

crsConvert

convert between different types of spatial reference representations

gdal_rasterize

a simple wrapper for osgeo.gdal.Rasterize()

gdal_translate

a simple wrapper for osgeo.gdal.Translate()

gdalbuildvrt

a simple wrapper for osgeo.gdal.BuildVRT()

gdalwarp

a simple wrapper for osgeo.gdal.Warp()

haversine

compute the distance in meters between two points in latlon

ogr2ogr

a simple wrapper for osgeo.gdal.VectorTranslate() aka ogr2ogr

utm_autodetect

get the UTM CRS for a spatial object

spatialist.auxil.cmap_mpl2gdal(mplcolor, values)[source]

convert a matplotlib color table to a GDAL representation.

Parameters:
  • mplcolor (str) – a color table code

  • values (list[int] or range) – the integer data values for which to retrieve colors

Returns:

the color table in GDAL format

Return type:

osgeo.gdal.ColorTable

Note

This function is currently only developed for handling discrete integer data values in an 8 Bit file. Colors are thus scaled between 0 and 255.

Examples

>>> from osgeo import gdal
>>> from spatialist.auxil import cmap_mpl2gdal
>>> values = list(range(0, 100))
>>> cmap = cmap_mpl2gdal(mplcolor='YlGnBu', values=values)
>>> print(isinstance(cmap, gdal.ColorTable))
True
spatialist.auxil.coordinate_reproject(x, y, s_crs, t_crs)[source]

reproject a coordinate from one CRS to another

Parameters:
Return type:

tuple

spatialist.auxil.crsConvert(crsIn, crsOut, wkt_format='DEFAULT')[source]

convert between different types of spatial reference representations

Parameters:
Returns:

the output CRS

Return type:

int or str or osgeo.osr.SpatialReference

Examples

convert an integer EPSG code to PROJ.4:

>>> crsConvert(4326, 'proj4')
'+proj=longlat +datum=WGS84 +no_defs '

convert the opengis URL back to EPSG:

>>> crsConvert('https://www.opengis.net/def/crs/EPSG/0/4326', 'epsg')
4326

convert an EPSG compound CRS (WGS84 horizontal + EGM96 vertical) to PROJ.4

>>> crsConvert('EPSG:4326+5773', 'proj4')
'+proj=longlat +datum=WGS84 +geoidgrids=us_nga_egm96_15.tif +vunits=m +no_defs'
spatialist.auxil.gdal_rasterize(src, dst, **kwargs)[source]

a simple wrapper for osgeo.gdal.Rasterize()

Parameters:
spatialist.auxil.gdal_translate(src, dst, **kwargs)[source]

a simple wrapper for osgeo.gdal.Translate()

Parameters:
spatialist.auxil.gdalbuildvrt(src, dst, void=True, **kwargs)[source]

a simple wrapper for osgeo.gdal.BuildVRT()

Parameters:
spatialist.auxil.gdalwarp(src, dst, pbar=False, **kwargs)[source]

a simple wrapper for osgeo.gdal.Warp()

Parameters:
spatialist.auxil.haversine(lat1, lon1, lat2, lon2)[source]

compute the distance in meters between two points in latlon

Parameters:
  • lat1 (int or float) – the latitude of point 1

  • lon1 (int or float) – the longitude of point 1

  • lat2 (int or float) – the latitude of point 2

  • lon2 (int or float) – the longitude of point 2

Returns:

the distance between point 1 and point 2 in meters

Return type:

float

spatialist.auxil.ogr2ogr(src, dst, **kwargs)[source]

a simple wrapper for osgeo.gdal.VectorTranslate() aka ogr2ogr

Parameters:
spatialist.auxil.utm_autodetect(spatial, crsOut)[source]

get the UTM CRS for a spatial object

The bounding box of the object is extracted, reprojected to EPSG:4326 and its center coordinate used for computing the best UTM zone fit.

Parameters:
  • spatial (Raster or Vector) – a spatial object in an arbitrary CRS

  • crsOut (str) – the output CRS type; see function crsConvert() for options

Returns:

the output CRS

Return type:

int or str or osgeo.osr.SpatialReference