lib.iolib module

Functions for IO, mostly wrapped around GDAL

Note: This was all written before RasterIO existed, which might be a better choice.

pygeotools.lib.iolib.b_getma(b)[source]

Get masked array from input GDAL Band

Parameters:b (gdal.Band) – Input GDAL Band
Returns:Masked array containing raster values
Return type:np.ma.array
pygeotools.lib.iolib.cpu_count(logical=True)[source]

Return system CPU count

pygeotools.lib.iolib.ds_getma(ds, bnum=1)[source]

Get masked array from input GDAL Dataset

Parameters:
  • ds (gdal.Dataset) – Input GDAL Datset
  • bnum (int, optional) – Band number
Returns:

Masked array containing raster values

Return type:

np.ma.array

pygeotools.lib.iolib.ds_getma_sub(src_ds, bnum=1, scale=None, maxdim=1024.0, return_ds=False)[source]

Load a subsampled array, rather than full resolution

This is useful when working with large rasters

Uses buf_xsize and buf_ysize options from GDAL ReadAsArray method.

Parameters:
  • ds (gdal.Dataset) – Input GDAL Datset
  • bnum (int, optional) – Band number
  • scale (int, optional) – Scaling factor
  • maxdim (int, optional) – Maximum dimension along either axis, in pixels
Returns:

Masked array containing raster values

Return type:

np.ma.array

pygeotools.lib.iolib.fn_check(fn)[source]

Wrapper to check for file existence

Parameters:fn (str) – Input filename string.
Returns:True if file exists, False otherwise.
Return type:bool
pygeotools.lib.iolib.fn_check_full(fn)[source]

Check for file existence

Avoids race condition, but slower than os.path.exists.

Parameters:fn (str) – Input filename string.
Returns:True if file exists, False otherwise.
Return type:status
pygeotools.lib.iolib.fn_getds(fn)[source]

Wrapper around gdal.Open()

pygeotools.lib.iolib.fn_getma(fn, bnum=1, return_ds=False)[source]

Get masked array from input filename

Parameters:
  • fn (str) – Input filename string
  • bnum (int, optional) – Band number
Returns:

Masked array containing raster values

Return type:

np.ma.array

pygeotools.lib.iolib.fn_getma_sub(fn, bnum=1, scale=None, maxdim=1024.0, return_ds=False)[source]
pygeotools.lib.iolib.fn_list_check(fn_list)[source]
pygeotools.lib.iolib.fn_list_valid(fn_list)[source]
pygeotools.lib.iolib.gdal2np_dtype(b)[source]

Get NumPy datatype that corresponds with GDAL RasterBand datatype Input can be filename, GDAL Dataset, GDAL RasterBand, or GDAL integer dtype

pygeotools.lib.iolib.get_auth()[source]

Get authorization token for https

pygeotools.lib.iolib.get_datadir()[source]
pygeotools.lib.iolib.get_ndv_b(b)[source]

Get NoData value for GDAL band.

If NoDataValue is not set in the band, extract upper left and lower right pixel values. Otherwise assume NoDataValue is 0.

Parameters:b (GDALRasterBand object) – This is the input band.
Returns:b_ndv – NoData value
Return type:float
pygeotools.lib.iolib.get_ndv_ds(ds, bnum=1)[source]
pygeotools.lib.iolib.get_ndv_fn(fn)[source]
pygeotools.lib.iolib.get_sub_dim(src_ds, scale=None, maxdim=1024)[source]

Compute dimensions of subsampled dataset

Parameters:
  • ds (gdal.Dataset) – Input GDAL Datset
  • scale (int, optional) – Scaling factor
  • maxdim (int, optional) – Maximum dimension along either axis, in pixels
Returns:

  • ns – Numper of samples in subsampled output
  • nl – Numper of lines in subsampled output
  • scale – Final scaling factor

pygeotools.lib.iolib.getfile(url, outdir=None)[source]

Function to fetch files using urllib

Works with ftp

pygeotools.lib.iolib.getfile2(url, auth=None, outdir=None)[source]

Function to fetch files using requests

Works with https authentication

pygeotools.lib.iolib.image_check(fn)[source]
pygeotools.lib.iolib.np2gdal_dtype(d)[source]

Get GDAL RasterBand datatype that corresponds with NumPy datatype Input should be numpy array or numpy dtype

pygeotools.lib.iolib.readcsv(fn)[source]

Wrapper to read arbitrary csv, check for header

Needs some work to be more robust, quickly added for demcoreg sampling

pygeotools.lib.iolib.replace_ndv(b, new_ndv)[source]
pygeotools.lib.iolib.set_ndv(dst_fn, ndv)[source]
pygeotools.lib.iolib.setstripe(dir, threads=2)[source]
pygeotools.lib.iolib.writeGTiff(a, dst_fn, src_ds=None, bnum=1, ndv=None, gt=None, proj=None, create=False, sparse=False)[source]

Write input array to disk as GeoTiff

Parameters:
  • a (np.array or np.ma.array) – Input array
  • dst_fn (str) – Output filename
  • src_ds (GDAL Dataset, optional) – Source Dataset to use for creating copy
  • bnum (int, optional) – Output band
  • ndv (float, optional) – Output NoData Value
  • gt (list, optional) – Output GeoTransform
  • proj (str, optional) – Output Projection (OGC WKT or PROJ.4 format)
  • create (bool, optional) – Create new dataset
  • sparse (bool, optional) – Output should be created with sparse options
pygeotools.lib.iolib.write_recarray(outfn, ra)[source]
pygeotools.lib.iolib.writevrt(out_csv, srs='EPSG:4326', x='field_1', y='field_2')[source]

Write out a vrt to accompany a csv of points