chm_zd_bckup.py
def chm_calculate(dtm_fn, dsm_fn, chm_fn):
    """Calculating CHM from DSM and DTM"""
    start = time.time()
    print("CHM calculating...")

    # LOADING DRIVER
    driver_tiff = gdal.GetDriverByName("GTiff")

    # OPEN DATASET & READ DATA
    dtm_ds = gdal.Open(dtm_fn)
    dtm_data = dtm_ds.GetRasterBand(1).ReadAsArray()
    dsm_ds = gdal.Open(dsm_fn)
    dsm_data = dsm_ds.GetRasterBand(1).ReadAsArray()

    # CALCULATE CHM
    chm_data = dsm_data - dtm_data
    chm_data[chm_data == 0] = np.NaN

    # CREATE FILTERED RASTER AND SAVE DATA
    chm_ds = driver_tiff.Create(chm_fn,
                                dtm_ds.RasterXSize,
                                dtm_ds.RasterYSize,
                                1,
                                gdal.GDT_Float32)

    chm_ds.SetProjection(dtm_ds.GetProjection())
    chm_ds.SetGeoTransform(dtm_ds.GetGeoTransform())
    chm_ds.GetRasterBand(1).WriteArray(chm_data)

    # CLOSING DATASETS
    dtm_ds = None
    dsm_ds = None
    chm_ds = None
    end = time.time()
    print("CHM calculated successfully in {:.4f} sec\n".format(end - start))
Calculating CHM from DSM and DTM
Calculating CHM from DSM and DTM
Calculating CHM from DSM and DTM
def chm_calculate(dtm_fn, dsm_fn, chm_fn):
    start = time.time()
    print("CHM calculating...")
    driver_tiff = gdal.GetDriverByName("GTiff")
    dtm_ds = gdal.Open(dtm_fn)
    dtm_data = dtm_ds.GetRasterBand(1).ReadAsArray()
    dsm_ds = gdal.Open(dsm_fn)
    dsm_data = dsm_ds.GetRasterBand(1).ReadAsArray()
    chm_data = dsm_data - dtm_data
    chm_data[chm_data == 0] = np.NaN
    chm_ds = driver_tiff.Create(chm_fn,
                                dtm_ds.RasterXSize,
                                dtm_ds.RasterYSize,
                                1,
                                gdal.GDT_Float32)
    chm_ds.SetProjection(dtm_ds.GetProjection())
    chm_ds.SetGeoTransform(dtm_ds.GetGeoTransform())
    chm_ds.GetRasterBand(1).WriteArray(chm_data)
    dtm_ds = None
    dsm_ds = None
    chm_ds = None
    end = time.time()
    print("CHM calculated successfully in {:.4f} sec\n".format(end - start))
def chm_calculate ( dtm_fn , dsm_fn , chm_fn ) : start = time . time ( ) print ( "CHM calculating..." ) driver_tiff = gdal . GetDriverByName ( "GTiff" ) dtm_ds = gdal . Open ( dtm_fn ) dtm_data = dtm_ds . GetRasterBand ( 1 ) . ReadAsArray ( ) dsm_ds = gdal . Open ( dsm_fn ) dsm_data = dsm_ds . GetRasterBand ( 1 ) . ReadAsArray ( ) chm_data = dsm_data - dtm_data chm_data [ chm_data == 0 ] = np . NaN chm_ds = driver_tiff . Create ( chm_fn , dtm_ds . RasterXSize , dtm_ds . RasterYSize , 1 , gdal . GDT_Float32 ) chm_ds . SetProjection ( dtm_ds . GetProjection ( ) ) chm_ds . SetGeoTransform ( dtm_ds . GetGeoTransform ( ) ) chm_ds . GetRasterBand ( 1 ) . WriteArray ( chm_data ) dtm_ds = None dsm_ds = None chm_ds = None end = time . time ( ) print ( "CHM calculated successfully in {:.4f} sec\n" . format ( end - start ) )
Calculating CHM from DSM and DTM
Calculating CHM from DSM and DTM
"""Calculating CHM from DSM and DTM""" # LOADING DRIVER
 # OPEN DATASET & READ DATA
 # CALCULATE CHM
 # CREATE FILTERED RASTER AND SAVE DATA
 # CLOSING DATASETS

dtm_fn
dsm_fn
chm_fn