easyidp.geotiff.GeoTiff#

class easyidp.geotiff.GeoTiff(tif_path='')#

A GeoTiff class, consisted by header information and file path to raw file.

__init__(tif_path='')#

The method to initialize the GeoTiff class

Parameters:

tif_path (str | pathlib.Path, optional) – the path to geotiff file, by default “”

Example

>>> import easyidp as idp
>>> test_data = idp.data.TestData()
>>> dom = idp.GeoTiff(test_data.pix4d.lotus_dom)

Methods

__init__([tif_path])

The method to initialize the GeoTiff class

crop_polygon(polygon_hv[, is_geo, save_path])

Crop a given polygon from geotiff

crop_rectangle(left, top, w, h[, is_geo, ...])

Extract a rectangle regeion crop from a GeoTIFF image file.

crop_rois(roi[, is_geo, save_folder])

Crop several ROIs from the geotiff by given <ROI> object with several polygons and polygon names

geo2pixel(polygon_hv)

Convert geotiff pixel coordinate (horizontal, vertical) to point cloud xyz coordinate (x, y, z)

has_data()

Return True if current objects has geotiff infomation

pixel2geo(polygon_hv)

Convert geotiff pixel coordinate (horizontal, vertical) to point cloud xyz coordinate (x, y, z)

point_query(points_hv[, is_geo])

Get the pixel value of given point(s)

polygon_math(polygon_hv[, is_geo, kernel])

Calculate the valus inside given polygon

read_geotiff(tif_path)

Open and get the meta information (header) from geotiff

Attributes

crs

A quick access to self.header['crs'], please access the header dict to change value

dim

A quick access to self.header['dim'], please access the header dict to change value

height

A quick access to self.header['height'], please access the header dict to change value

nodata

A quick access to self.header['nodata'], please access the header dict to change value

scale

A quick access to self.header['scale'], please access the header dict to change value

tie_point

A quick access to self.header['tie_point'], please access the header dict to change value

width

A quick access to self.header['width'], please access the header dict to change value

file_path

The file path of current GeoTiff

header

The Geotiff meta infomation

imarray

The numpy ndarray of GeoTiff images

crop_polygon(polygon_hv, is_geo=True, save_path=None)#

Crop a given polygon from geotiff

Parameters:
  • polygon_hv (numpy nx2 array) – (horizontal, vertical) points

  • is_geo (bool, optional) – whether the given polygon is pixel coords on imarray or geo coords (default)

  • save_path (str, optional) – if given, will save the cropped as *.tif file to path, by default None

Returns:

The cropped numpy pixels imarray

Return type:

imarray_out

Example

Prepare data:

>>> import easyidp as idp
>>> test_data = idp.data.TestData()

# prepare geotiff
>>> dom = idp.GeoTiff(test_data.pix4d.lotus_dom)

# prepare polygon
>>> roi = idp.ROI(test_data.shp.lotus_shp, name_field=0)
>>> roi = roi[0]
>>> roi.change_crs(dom.crs)
>>> roi
array([[ 368017.7565143 , 3955511.08102276],
       [ 368019.70190232, 3955511.49811902],
       [ 368020.11263046, 3955509.54636219],
       [ 368018.15769062, 3955509.13563382],
       [ 368017.7565143 , 3955511.08102276]])

Use this function:

>>> imarray = dom.crop_polygon(roi, is_geo=True)
>>> imarray.shape
(320, 319, 4)

If you want to save the previous as new GeoTiff:

>>> save_tiff = "path/to/save/cropped.tif"
>>> imarray = obj.crop_polygon(polygon_hv, is_geo=True, save_path=save_tiff)
crop_rectangle(left, top, w, h, is_geo=True, save_path=None)#

Extract a rectangle regeion crop from a GeoTIFF image file.

(0,0)
o--------------------------
|           ^
|           | top
|           v
| <-------> o=============o  ^
|   left    |<---- w ---->|  |
|           |             |  h
|           |             |  |
|           o=============o  v
Parameters:
  • top (int | float) – Coordinates of

  • left (int | float) – Coordinates of the top left corner of the desired crop.

  • h (int | float) – Desired crop height.

  • w (int | float) – Desired crop width.

  • is_geo (bool, optional) – whether the given polygon is pixel coords on imarray or geo coords (default)

  • save_path (str, optional) – if given, will save the cropped as *.tif file to path

Returns:

Extracted crop.

Return type:

ndarray

Example

>>> import easyidp as idp
>>> test_data = idp.data.TestData()

>>> obj = idp.GeoTiff(test_data.pix4d.lotus_dom)
>>> out = obj.crop_rectangle(left=434, top=918, w=320, h=321, is_geo=False)
>>> out.shape
(321, 320, 4)

Note

It is not recommended to use without specifying parameters like this:

crop_rectiange(434, 918, 320, 321)

It is hard to know the exactly order

Caution

subfunction easyidp.geotiff.tifffile_crop() has the order (top, left, h, w) which is too heavy to change it.

crop_rois(roi, is_geo=True, save_folder=None)#

Crop several ROIs from the geotiff by given <ROI> object with several polygons and polygon names

Parameters:
  • roi (easyidp.ROI | dict) – the <ROI> object created by easyidp.ROI(), or dictionary with multiple polygons. If you just need crop single polygon with ndarray coordinates, please use GeoTiff.crop_polygon() instead.

  • is_geo (bool, optional) – whether the given polygon is pixel coords on imarray or geo coords (default)

  • save_folder (str, optional) – the folder to save cropped images, use ROI indices as file_names, by default “”, means not save.

Returns:

The dictionary with key=id and value=ndarray data

Return type:

dict,

Example

Prepare data:

>>> import easyidp as idp
>>> test_data = idp.data.TestData()

# prepare dom geotiff
>>> dom = idp.GeoTiff(test_data.pix4d.lotus_dom)

# prepare several ROIs
>>> roi = idp.ROI(test_data.shp.lotus_shp, name_field=0)
>>> roi = roi[0:3]    # only use 3 for quick example
>>> roi.change_crs(obj.crs)   # transform to the same CRS like DOM
{0: array([[ 368017.7565143 , 3955511.08102276],
           [ 368019.70190232, 3955511.49811902],
           [ 368020.11263046, 3955509.54636219],
           [ 368018.15769062, 3955509.13563382],
           [ 368017.7565143 , 3955511.08102276]]),
 1: array([[ 368018.20042946, 3955508.96051697],
           [ 368020.14581791, 3955509.37761334],
           [ 368020.55654627, 3955507.42585654],
           [ 368018.601606  , 3955507.01512806],
           [ 368018.20042946, 3955508.96051697]]),
 2: array([[ 368018.64801755, 3955506.84956301],
           [ 368020.59340644, 3955507.26665948],
           [ 368021.00413502, 3955505.31490271],
           [ 368019.04919431, 3955504.90417413],
           [ 368018.64801755, 3955506.84956301]])}

Use this function:

>>> out_dict = obj.crop_rois(roi)
{"N1W1": array[...], "N1W3": array[...], ...}

>>> out_dict["N1W1"].shape
(320, 319, 4)

If you want automatically save geotiff results to specific folder:

>>> tif_out_folder = "./cropped_geotiff"
>>> os.mkdir(tif_out_folder)
>>> out_dict = obj.crop_rois(roi, save_folder=tif_out_folder)
property crs#

A quick access to self.header['crs'], please access the header dict to change value

property dim#

A quick access to self.header['dim'], please access the header dict to change value

file_path#

The file path of current GeoTiff

>>> dom.file_path
PosixPath('/Users/<user>/Library/Application Support/easyidp.data/data_for_tests/pix4d/lotus_tanashi_full/hasu_tanashi_20170525_Ins1RGB_30m_transparent_mosaic_group1.tif')
geo2pixel(polygon_hv)#

Convert geotiff pixel coordinate (horizontal, vertical) to point cloud xyz coordinate (x, y, z)

Parameters:

points_hv (numpy nx2 array) – [horizontal, vertical] points

Return type:

The ndarray pixel position of these points (horizontal, vertical)

Example

Prepare data:

>>> import easyidp as idp
>>> test_data = idp.data.TestData()

# prepare the roi data
>>> roi = idp.ROI(test_data.shp.lotus_shp, name_field=0)
>>> dom = idp.GeoTiff(test_data.pix4d.lotus_dom)
>>> roi.change_crs(dom.crs)
>>> roi_test = roi[111]
array([[ 368051.75902187, 3955484.68169527],
       [ 368053.70441367, 3955485.09879908],
       [ 368054.11515079, 3955483.14704415],
       [ 368052.16020711, 3955482.73630818],
       [ 368051.75902187, 3955484.68169527]])

Use this function:

>>> roi_test_pixel = dom.geo2pixel(roi_test)
array([[5043.01515811, 4551.90714551],
       [5306.6183839 , 4495.38901391],
       [5362.27381938, 4759.85445164],
       [5097.37630191, 4815.50973136],
       [5043.01515811, 4551.90714551]])
has_data()#

Return True if current objects has geotiff infomation

Return type:

bool

Example

>>> import easyidp as idp
>>> test_data = idp.data.TestData()

>>> aaa = idp.GeoTiff()
>>> aaa.has_data()
False
>>> aaa.read_geotiff(test_data.pix4d.lotus_dom)
>>> aaa.has_data()
True

>>> bbb = idp.GeoTiff(test_data.pix4d.lotus_dom)
>>> bbb.has_data()
True
header#

The Geotiff meta infomation

>>> dom.header
{'height': 5752, 'width': 5490, 'dim': 4, 'nodata': 0, 'dtype': dtype('uint8'),
'tags': <tifffile.TiffTags @0x00007FB1E8C3FFD0>, 'photometric': <PHOTOMETRIC.RGB: 2>,
'planarconfig': <PLANARCONFIG.CONTIG: 1>, 'compress': <COMPRESSION.LZW: 5>,
'scale': [0.00738, 0.00738], 'tie_point': [368014.54157, 3955518.2747700005],
'crs': <Derived Projected CRS: EPSG:32654>
        Name: WGS 84 / UTM zone 54N
        Axis Info [cartesian]:
        - E[east]: Easting (metre)
        - N[north]: Northing (metre)
        Area of Use:
        - name: Between 138°E and 144°E, northern hemisphere between equator and 84°N, onshore and offshore. Japan. Russian Federation.
        - bounds: (138.0, 0.0, 144.0, 84.0)
        Coordinate Operation:
        - name: UTM zone 54N
        - method: Transverse Mercator
        Datum: World Geodetic System 1984 ensemble
        - Ellipsoid: WGS 84
        - Prime Meridian: Greenwich
}
>>> dom.header["height"]
5752
property height#

A quick access to self.header['height'], please access the header dict to change value

imarray#

The numpy ndarray of GeoTiff images

property nodata#

A quick access to self.header['nodata'], please access the header dict to change value

pixel2geo(polygon_hv)#

Convert geotiff pixel coordinate (horizontal, vertical) to point cloud xyz coordinate (x, y, z)

Parameters:

points_hv (numpy nx2 array) – [horizontal, vertical] points

Return type:

The ndarray pixel position of these points (horizontal, vertical)

Example

Prepare data:

>>> import easyidp as idp
>>> test_data = idp.data.TestData()

# prepare the roi data
>>> roi = idp.ROI(test_data.shp.lotus_shp, name_field=0)
>>> dom = idp.GeoTiff(test_data.pix4d.lotus_dom)
>>> roi.change_crs(dom.crs)
>>> roi_test = roi[111]
>>> roi_test_pixel = dom.geo2pixel(roi_test)
array([[5043.01515811, 4551.90714551],
       [5306.6183839 , 4495.38901391],
       [5362.27381938, 4759.85445164],
       [5097.37630191, 4815.50973136],
       [5043.01515811, 4551.90714551]])

Use this function:

>>> roi_test_back = dom.pixel2geo(roi_test_pixel)
array([[ 368051.75902187, 3955484.68169527],
       [ 368053.70441367, 3955485.09879908],
       [ 368054.11515079, 3955483.14704415],
       [ 368052.16020711, 3955482.73630818],
       [ 368051.75902187, 3955484.68169527]])
point_query(points_hv, is_geo=True)#

Get the pixel value of given point(s)

Parameters:
  • points_hv (tuple | list | nx2 ndarray) –

    The coordinates of qurey points, in order (horizontal, vertical)

  • is_geo (bool, optional) –

    The given polygon is geo coords ( True , default) or pixel coords ( False ) on imarray.

Returns:

the obtained pixel value (RGB or height)

Return type:

ndarray

Example

Prequirements

>>> import easyidp as idp
>>> dom = idp.GeoTiff(test_data.pix4d.lotus_dom)

Query one point by tuple

>>> # one point tuple
>>> pts = (368023.004, 3955500.669)
>>> dom.point_query(pts, is_geo=True)
array([97.45558])

Query one point by list

>>> # one point list
>>> pts = [368023.004, 3955500.669]
>>> dom.point_query(pts, is_geo=True)
array([97.45558])

Query several points by list

>>> pts = [
...    [368022.581, 3955501.054],
...    [368024.032, 3955500.465]
... ]
>>> dom.point_query(pts, is_geo=True)
array([97.624344, 97.59617])

Query several points by numpy

>>> pts = np.array([
...    [368022.581, 3955501.054],
...    [368024.032, 3955500.465]
... ])
>>> dom.point_query(pts, is_geo=True)
array([97.624344, 97.59617])
polygon_math(polygon_hv, is_geo=True, kernel='mean')#

Calculate the valus inside given polygon

Parameters:
  • polygon_hv (numpy nx2 array | 'all') – (horizontal, vertical) points

  • is_geo (bool, optional) – whether the given polygon is pixel coords on imarray or geo coords (default)

  • kernel (str, optional) – The method to calculate polygon summary, options are: [“mean”, “min”, “max”, “pmin5”, “pmin10”, “pmax5”, “pmax10”], please check notes section for more details.

Notes

Option details for kernel parameter:

  • “mean”: the mean value inside polygon

  • “min”: the minimum value inside polygon

  • “max”: the maximum value inside polygon

  • “pmin5”: 5th [percentile mean]_ inside polygon

  • “pmin10”: 10th [percentile mean]_ inside polygon

  • “pmax5”: 95th [percentile mean]_ inside polygon

  • “pmax10”: 90th [percentile mean]_ inside polygon

Example

Prepare data:

>>> import easyidp as idp
>>> test_data = idp.data.TestData()

# prepare the roi data
>>> roi = idp.ROI(test_data.shp.lotus_shp, name_field=0)
>>> dsm = idp.GeoTiff(test_data.pix4d.lotus_dsm)
>>> roi.change_crs(dsm.crs)
>>> roi_test = roi[111]
array([[ 368051.75902187, 3955484.68169527],
       [ 368053.70441367, 3955485.09879908],
       [ 368054.11515079, 3955483.14704415],
       [ 368052.16020711, 3955482.73630818],
       [ 368051.75902187, 3955484.68169527]])

Use this function:

>>> dsm.polygon_math(roi_test, is_geo=True, kernel="mean")
97.20491

>>> dsm.polygon_math(roi_test, is_geo=True, kernel="pmax10")
97.311844

Caution

This function is initially designed for doing some simple calculations one band (layer) geotiff.

If you applying this function on RGB color geotiff, it will return the calculated results of each layer

>>> dom.polygon_math(roi_test, is_geo=True, kernel="pmax10")
array([139.97428808, 161.36439038, 122.30964888, 255.        ])

The four values are RGBA four color channels.

read_geotiff(tif_path)#

Open and get the meta information (header) from geotiff

Parameters:

tif_path (str | pathlib.Path) – the path to geotiff file

Example

Though this function can be used by:

>>> import easyidp as idp
>>> test_data = idp.data.TestData()
>>> dom = idp.GeoTiff()
>>> dom.read_geotiff(test_data.pix4d.lotus_dom)

It is highly recommended to specify the geotiff path when initializing the geotiff object:

>>> dom = idp.GeoTiff(test_data.pix4d.lotus_dom)
property scale#

A quick access to self.header['scale'], please access the header dict to change value

property tie_point#

A quick access to self.header['tie_point'], please access the header dict to change value

property width#

A quick access to self.header['width'], please access the header dict to change value