MapIO

Submodule that implements several classes for reading maps (image type and NetCDF type).

class ArrayReader(array, xmin, ymin, xmax, ymax, transform)[source]
get_source_gt()[source]

Get geotransformation of the source file.

Returns:

gt – Geotransformation of source file.

Return type:

tuple[float, float, float, float, float, float]

property source_shape: tuple[int, int]

Get shape of source file.

Returns:
  • height – height of array.

  • width – width of array.

class MapReader(fp, xmin, ymin, xmax, ymax)[source]

This class can be used to read image type data from files, and then to easily sample data from it.

Parameters:
  • fp (str) – Filepath of file.

  • bounds – tuple of xmin, xmax, ymin, ymax of study_area.

get_source_gt()[source]

Get geotransformation of the source file.

Returns:

gt – Geotransformation of source file.

Return type:

tuple[float, float, float, float, float, float]

sample_geom(geom, all_touched=False, nodata=-1)[source]

Sample geometry from array.

Parameters:
  • geom (dict) – GeoJSON dictionary of geometry.

  • nodata (Union[float, int]) – Value to fill nodata.

  • all_touched – Bool to include a pixel in the mask if it touches any of the shapes.

Returns:

data – Values for geometry.

Return type:

ndarray

sample_geoms(geoms, nodata=-1)[source]

Sample multiple geometries from data array.

Parameters:
  • geoms (list[dict]) – List of GeoJSON dictionary of geometries.

  • nodata (int) – Value to fill nodata.

Yields:

data – Values for geometry.

property source_shape: tuple[int, int]

Get shape of source file.

Returns:
  • height – height of array.

  • width – width of array.

class NetCDFReader(fp, varname, xmin, ymin, xmax, ymax, latname='lat', lonname='lon', timename='time')[source]

This class can be used to read data from NetCDF files, and then to easily sample data from it.

Parameters:
  • fp (str) – Filepath of file.

  • varname (str) – netcdf variable to read

  • bounds – tuple of xmin, xmax, ymin, ymax of study_area.

  • latname (str) – name of latitude variable

  • lonname (str) – name of longitude variable

  • timename (str) – name of the time variable

close()[source]

Close NetCDF file.

Return type:

None

get_current_time_index()[source]

Read the time indices from NetCDF file in Python datetime format. If time index does not exist, it is not set.

Return type:

None

get_source_gt()[source]

Construct a GDAL-style Geotransformation from the source NetCDF using the longitude and latitude data.

Returns:

gt – Geotransformation of source file.

property source_shape

Get shape of source file.

Returns:
  • height – height of array.

  • width – width of array.

property title: str

Title of NetCDF file.

class Reader(xmin, ymin, xmax, ymax)[source]

This is a base class to read mapdata from files. The class takes as input the bounds of the relevant area. Any input maps are automatically cut to only read the required area. The class contains several functions to make reading data per agent easy and fast.

Parameters:

bounds – tuple of xmin, xmax, ymin, ymax of study_area.

delete_get_data_array_cache()[source]

If results from get_data_array were cached, these can be deleted using this function.

Return type:

None

get_data_array(*args, cache=False, **kwargs)[source]

Read data array from map.

Parameters:
  • cache (bool) – cache output from previous call to function, check parameters (default: False)

  • args – passed to child _get_data_array

  • kwargs – passed to child _get_data_array

Returns:

array – requested array

get_source_gt()[source]

Get Geotransformation of the source file. Must be implemented in the child class.

Return type:

None

sample_coords(coords, *args, **kwargs)[source]
Parameters:
  • coords (ndarray) – 2D NumPy array of coordinates to sample. First dimension is a list of coordinates, second dimension are the x and y.

  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

Returns:

data – values as read from map for given coordinates.

Return type:

ndarray

set_window_and_gt()[source]

This function gets the geotransformation (gt) of the cut map, and the rows to read from the original data (rowslice, colslice).

Return type:

None