GDALGrid

GDALGrid

A Python wrapper for the gdal.Dataset() with additionaly functonality.

class gazar.grid.GDALGrid(grid_file, prj_file=None)[source]

Wrapper for gdal.Dataset() with osr.SpatialReference() object.

Parameters:
  • grid_file – The grid file to be wrapped.
  • prj_file (str, optional) – Path to projection file.
bounds(as_geographic=False, as_utm=False, as_projection=None)[source]

Returns bounding coordinates for the dataset.

Parameters:
  • as_geographic (bool, optional) – If True, this will return the bounds in EPSG:4326. Default is False.
  • as_utm (bool, optional) – If True, it will attempt to find the UTM zone and will return bounds in that UTM zone.
  • as_projection (osr.SpatialReference(), optional) – Output projection for bounds.
Returns:

(x_min, x_max, y_min, y_max) Bounds for the grid in the format

Return type:

tuple

coord2pixel(x_coord, y_coord)[source]

Returns base-0 raster index using global coordinates to pixel center

Parameters:
  • x_coord (float) – The projected x coordinate of the cell center.
  • y_coord (float) – The projected y coordinate of the cell center.
Returns:

(col, row) - The 0-based column and row index of the pixel.

Return type:

tuple

epsg

str – EPSG code

geotransform

tuple – The geotransform for the dataset.

get_val(x_pixel, y_pixel, band=1)[source]

Returns value of raster

Parameters:
  • x_pixel (int) – X pixel location (0-based).
  • y_pixel (int) – Y pixel location (0-based).
  • band (int, optional) – Band number (1-based). Default is 1.
Returns:

Return type:

object dtype

get_val_coord(x_coord, y_coord, band=1)[source]

Returns value of raster from a projected coordinate point.

Parameters:
  • x_coord (float) – The projected x coordinate of the cell center.
  • y_coord (float) – The projected y coordinate of the cell center.
  • band (int, optional) – Band number (1-based). Default is 1.
Returns:

Return type:

object dtype

get_val_latlon(longitude, latitude, band=1)[source]

Returns value of raster from a latitude and longitude point.

Parameters:
  • longitude (float) – The longitude of the cell center.
  • latitude (float) – The latitude of the cell center.
  • band (int, optional) – Band number (1-based). Default is 1.
Returns:

Return type:

object dtype

latlon

Returns latitude and longitude arrays representing the grid.

Returns:
  • proj_lats (numpy.array()) – The latitude array.
  • proj_lons (numpy.array()) – The longitude array.
lonlat2pixel(longitude, latitude)[source]

Returns base-0 raster index using longitude and latitude of pixel center

Parameters:
  • longitude (float) – The longitude of the cell center.
  • latitude (float) – The latitude of the cell center.
Returns:

(col, row) - The 0-based column and row index of the pixel.

Return type:

tuple

np_array(band=1, masked=True)[source]

Returns the raster band as a numpy array.

Parameters:
  • band (obj:int, optional) – Band number (1-based). Default is 1. If ‘all’, it will return all of the data as a 3D array.
  • masked (bool, optional) – If True, will return the array masked with the NoData value. Default is True.
Returns:

Return type:

numpy.array() or numpy.ma.array()

num_bands

int – number of bands in raster

pixel2coord(col, row)[source]

Returns global coordinates to pixel center using base-0 raster index.

Parameters:
  • col (int) – The 0-based column index.
  • row (int) – The 0-based row index.
Returns:

(x_coord, y_coord) - The x, y coordinate of the pixel center in the dataset’s projection.

Return type:

tuple

pixel2lonlat(col, row)[source]

Returns latitude and longitude to pixel center using base-0 raster index

Parameters:
  • col (int) – The 0-based column index.
  • row (int) – The 0-based row index.
Returns:

(longitude, latitude) - The lat, lon of the pixel center in the dataset’s projection.

Return type:

tuple

proj

funcpyproj.Proj – Proj4 object

proj4

str – proj4 string

to_arc_ascii(file_path, band=1, print_nodata=True)[source]

Writes data to Arc ASCII file format.

Parameters:
  • file_path (str) – Path to output ascii file.
  • band (obj:int, optional) – Band number (1-based). Default is 1.
  • print_nodata (bool, optional) – If True, it will write out the NoData value for the raster band. Default is False.
to_grass_ascii(file_path, band=1, print_nodata=True)[source]

Writes data to GRASS ASCII file format.

Parameters:
  • file_path (str) – Path to output ascii file.
  • band (obj:int, optional) – Band number (1-based). Default is 1.
  • print_nodata (bool, optional) – If True, it will write out the NoData value for the raster band. Default is False.
to_polygon(out_shapefile, band=1, fieldname='DN', self_mask=None)[source]

Converts the raster to a polygon.

https://svn.osgeo.org/gdal/trunk/gdal/swig/python/scripts
/gdal_polygonize.py
https://stackoverflow.com/questions/25039565
/create-shapefile-from-tif-file-using-gdal
Parameters:
  • out_shapefile (str) – Output path for shapefile.
  • band (int, optional) – Band number (1-based). Default is 1.
  • fieldname (str, optional) – Name of the output field. Defailt is ‘DN’.
  • self_mask (bool, optional) – If True, will use self as mask. Default is None.
to_projection(dst_proj, resampling=<Mock id='140107904794704'>)[source]

Reproject dataset to new projection.

Parameters:dst_proj (osr.SpatialReference()) – Output projection.
Returns:
Return type:GDALGrid()
to_tif(file_path)[source]

Write out as geotiff.

Parameters:file_path (str) – Output path for file.
wkt

str – WKT projection string

write_prj(out_projection_file, esri_format=False)[source]

Writes projection file.

Parameters:
  • out_projection_file (str) – Output path for file.
  • esri_format (bool, optional) – If True, it will convert the projection string to the Esri format. Default is False.
x_coords

Returns x coordinate array representing the grid. Use method from: https://github.com/pydata/xarray/pull/1712

Returns:x_coords – The X coordinate array.
Return type:numpy.array()
x_size

int – size of x dimensions

y_coords

Returns y coordinate array representing the grid. Use method from: https://github.com/pydata/xarray/pull/1712

Returns:y_coords – The Y coordinate array.
Return type:numpy.array()
y_size

int – size of y dimensions

ArrayGrid

Class for constructing a GDALGrid from an array.

class gazar.grid.ArrayGrid(in_array, wkt_projection, geotransform, gdal_dtype=<Mock id='140107904795024'>, nodata_value=None)[source]

Bases: gazar.grid.GDALGrid

Loads numpy.array() into a GDALGrid().

Parameters:
  • in_array (numpy.array()) – 2D or 3D array of data.
  • wkt_projection (str) – WKT projection string.
  • geotransform (tuple) – Geotransform for array.
  • gdal_dtype (gdalconst(), optional) – The data type of the in_array for GDAL. Default is gdalconst.GDT_Float32.
  • nodata_value (int or float, optional) – The value used in the grid for NoData. Default is None.