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
- 参数:
tif_path (str | pathlib.Path, optional) – the path to geotiff file, by default “”
示例
>>> 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
()如果含有Geotiff信息,则返回True
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)从Geotiff中读取头文件信息
Attributes
A quick access to
self.header['crs']
, please access theheader
dict to change valueA quick access to
self.header['dim']
, please access theheader
dict to change valueA quick access to
self.header['height']
, please access theheader
dict to change valueA quick access to
self.header['nodata']
, please access theheader
dict to change valueA quick access to
self.header['scale']
, please access theheader
dict to change valueA quick access to
self.header['tie_point']
, please access theheader
dict to change valueA quick access to
self.header['width']
, please access theheader
dict to change valueThe file path of current GeoTiff
The Geotiff meta infomation
The numpy ndarray of GeoTiff images
- crop_polygon(polygon_hv, is_geo=True, save_path=None)#
Crop a given polygon from geotiff
- 参数:
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
- 返回:
The cropped numpy pixels imarray
- 返回类型:
imarray_out
示例
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
- 参数:
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
- 返回:
Extracted crop.
- 返回类型:
ndarray
示例
>>> 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)
备注
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
小心
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
- 参数:
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.
- 返回:
The dictionary with key=id and value=ndarray data
- 返回类型:
dict,
示例
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 theheader
dict to change value
- property dim#
A quick access to
self.header['dim']
, please access theheader
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)
- 参数:
points_hv (numpy nx2 array) – [horizontal, vertical] points
- 返回类型:
The ndarray pixel position of these points (horizontal, vertical)
示例
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()#
如果含有Geotiff信息,则返回True
- 返回类型:
bool
示例
>>> 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 theheader
dict to change value
- imarray#
The numpy ndarray of GeoTiff images
- property nodata#
A quick access to
self.header['nodata']
, please access theheader
dict to change value
- pixel2geo(polygon_hv)#
Convert geotiff pixel coordinate (horizontal, vertical) to point cloud xyz coordinate (x, y, z)
- 参数:
points_hv (numpy nx2 array) – [horizontal, vertical] points
- 返回类型:
The ndarray pixel position of these points (horizontal, vertical)
示例
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)
- 参数:
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.
- 返回:
the obtained pixel value (RGB or height)
- 返回类型:
ndarray
示例
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
- 参数:
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.
备注
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
示例
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
小心
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)#
从Geotiff中读取头文件信息
- 参数:
tif_path (str | pathlib.Path) – Geotiff文件的路径
示例
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 theheader
dict to change value
- property tie_point#
A quick access to
self.header['tie_point']
, please access theheader
dict to change value
- property width#
A quick access to
self.header['width']
, please access theheader
dict to change value