def process_DTM(context): dtm_tiles = get_dtm_tiles([ context.wgs84_extent[0][0], context.wgs84_extent[0][1], context.wgs84_extent[2][0], context.wgs84_extent[2][1] ]) dtm_tiles = [ os.path.join(context.srtm_directory, tile) for tile in dtm_tiles ] missing_tiles = [] for tile in dtm_tiles: if not os.path.exists(tile): missing_tiles.append(tile) if missing_tiles: print( "The following SRTM tiles are missing: {}. Please provide them in the SRTM directory ({}). Will try to continue, but unreliable results" .format([os.path.basename(tile) for tile in missing_tiles], context.srtm_directory)) # return False run_command(["gdalbuildvrt", "-q", context.dem_vrt] + dtm_tiles) run_command([ "otbcli_BandMath", "-il", context.dem_vrt, "-out", context.dem_nodata, "-exp", "im1b1 == -32768 ? 0 : im1b1", "-progress", "false" ]) run_command([ "gdalwarp", "-q", "-overwrite", "-multi", "-r", "cubic", "-t_srs", "EPSG:" + str(context.epsg_code), "-tr", str(context.spacing_x), str(context.spacing_y), "-te", str(context.extent[1][0]), str(context.extent[1][1]), str(context.extent[3][0]), str(context.extent[3][1]), context.dem_nodata, context.dem_r1 ]) if context.dem_r2: # run_command(["gdal_translate", # "-outsize", str(int(round(context.size_x / 2.0))), str(int(round(context.size_y # / 2.0))), # context.dem_r1, # context.dem_r2]) resample_dataset(context.dem_r1, context.dem_r2, 20, -20) # run_command(["otbcli_RigidTransformResample", # "-in", context.dem_r1, # "-out", context.dem_r2, # "-transform.type.id.scalex", "0.5", # "-transform.type.id.scaley", "0.5"]) if context.mode == 'L8': scale = 1.0 / 8 inv_scale = 8.0 else: # scale = 1.0 / 23.9737991266 # almost 1/24 scale = 1.0 / 24 inv_scale = 24.0 # run_command(["gdal_translate", # "-outsize", str(int(round(context.size_x / inv_scale))), str(int(round(context.size_y / # inv_scale))), # context.dem_r1, # context.dem_coarse]) resample_dataset(context.dem_r1, context.dem_coarse, 240, -240) # run_command(["otbcli_RigidTransformResample", # "-in", context.dem_r1, # "-out", context.dem_coarse, # "-transform.type.id.scalex", str(scale), # "-transform.type.id.scaley", str(scale)]) run_command([ "gdaldem", "slope", "-q", # "-s", "111120", "-compute_edges", context.dem_r1, context.slope_degrees ]) run_command([ "gdaldem", "aspect", "-q", # "-s", "111120", "-compute_edges", context.dem_r1, context.aspect_degrees ]) run_command([ "gdal_translate", "-q", "-ot", "Int16", "-scale", "0", "90", "0", "157", context.slope_degrees, context.slope_r1 ]) run_command([ "gdal_translate", "-q", "-ot", "Int16", "-scale", "0", "368", "0", "628", context.aspect_degrees, context.aspect_r1 ]) if context.slope_r2: run_command([ "gdalwarp", "-q", "-r", "cubic", "-tr", "20", "20", context.slope_r1, context.slope_r2 ]) if context.aspect_r2: run_command([ "gdalwarp", "-q", "-r", "cubic", "-tr", "20", "20", context.aspect_r1, context.aspect_r2 ]) run_command([ "gdalwarp", "-q", "-r", "cubic", "-tr", "240", "240", context.slope_r1, context.slope_coarse ]) run_command([ "gdalwarp", "-q", "-r", "cubic", "-tr", "240", "240", context.aspect_r1, context.aspect_coarse ]) return True
def process_WB(context): run_command([ "otbcli", "DownloadSWBDTiles", "-il", context.dem_r1, "-mode", "list", "-mode.list.indir", context.swbd_directory, "-mode.list.outlist", context.swbd_list, "-progress", "false" ]) with open(context.swbd_list) as f: swbd_tiles = f.read().splitlines() if len(swbd_tiles) == 0: empty_shp = os.path.join(context.swbd_directory, "empty.shp") run_command(["ogr2ogr", context.wb, empty_shp]) elif len(swbd_tiles) == 1: run_command(["ogr2ogr", context.wb, swbd_tiles[0]]) else: run_command([ "otbcli_ConcatenateVectorData", "-progress", "false", "-out", context.wb, "-vd" ] + swbd_tiles) run_command([ "ogr2ogr", "-s_srs", "EPSG:4326", "-t_srs", "EPSG:" + context.epsg_code, context.wb_reprojected, context.wb ]) run_command([ "otbcli_Rasterization", "-in", context.wb_reprojected, "-out", context.water_mask, "uint8", "-im", context.dem_coarse, "-mode.binary.foreground", "1", "-progress", "false" ])
def process_DTM(context): dem = get_dem_type(context.dem_directory) points = [ context.wgs84_extent[0][0], context.wgs84_extent[0][1], context.wgs84_extent[2][0], context.wgs84_extent[2][1], ] dtm_tiles = dem.get_dem_tiles(points) dem_nodata_value = dem.get_nodata_value() missing_tiles = [] dtm_tiles_nodata = [] for tile in dtm_tiles: tile_path = dem.get_tile_path(tile) if not tile_path: missing_tiles.append(tile) continue if (not dem.needs_nodata_translation() or get_raster_nodata_value(tile_path) == 0): dtm_tiles_nodata.append(tile_path) continue tile_name = "{}_nodata.vrt".format(os.path.splitext(tile)[0]) tile_nodata = os.path.join(context.working_directory, tile_name) command = [ "gdalbuildvrt", "-q", "-overwrite", "-vrtnodata", "0", ] if dem_nodata_value: command += ["-srcnodata", str(dem_nodata_value)] command += [ tile_nodata, tile_path, ] run_command(command) dtm_tiles_nodata.append(tile_nodata) if missing_tiles: print("The following DEM tiles are missing: {}".format(missing_tiles)) run_command([ "/usr/local/bin/gdalbuildvrt", "-q", "-overwrite", "-r", "cubic", context.dem_vrt, ] + dtm_tiles_nodata) run_command([ "gdalwarp", "-q", "-overwrite", "-multi", "-r", "cubic", "-t_srs", "EPSG:" + str(context.epsg_code), "-et", "0", "-tr", str(context.spacing_x), str(context.spacing_y), "-te", str(context.extent[1][0]), str(context.extent[1][1]), str(context.extent[3][0]), str(context.extent[3][1]), context.dem_vrt, context.dem_r1, ]) if context.dem_r2: # run_command(["gdal_translate", # "-outsize", str(int(round(context.size_x / 2.0))), str(int(round(context.size_y # / 2.0))), # context.dem_r1, # context.dem_r2]) resample_dataset(context.dem_r1, context.dem_r2, 20, -20) # run_command(["otbcli_RigidTransformResample", # "-in", context.dem_r1, # "-out", context.dem_r2, # "-transform.type.id.scalex", "0.5", # "-transform.type.id.scaley", "0.5"]) if context.mode == "L8": scale = 1.0 / 8 inv_scale = 8.0 else: # scale = 1.0 / 23.9737991266 # almost 1/24 scale = 1.0 / 24 inv_scale = 24.0 # run_command(["gdal_translate", # "-outsize", str(int(round(context.size_x / inv_scale))), str(int(round(context.size_y / # inv_scale))), # context.dem_r1, # context.dem_coarse]) resample_dataset(context.dem_r1, context.dem_coarse, 240, -240) # run_command(["otbcli_RigidTransformResample", # "-in", context.dem_r1, # "-out", context.dem_coarse, # "-transform.type.id.scalex", str(scale), # "-transform.type.id.scaley", str(scale)]) run_command([ "gdaldem", "slope", "-q", "-compute_edges", context.dem_r1, context.slope_degrees, ]) run_command([ "gdaldem", "aspect", "-q", "-compute_edges", context.dem_r1, context.aspect_degrees, ]) run_command([ "gdal_translate", "-q", "-ot", "Int16", "-scale", "0", "90", "0", "157", context.slope_degrees, context.slope_r1, ]) run_command([ "gdal_translate", "-q", "-ot", "Int16", "-scale", "0", "368", "0", "628", context.aspect_degrees, context.aspect_r1, ]) if context.slope_r2: run_command([ "gdalwarp", "-q", "-r", "cubic", "-tr", "20", "20", context.slope_r1, context.slope_r2, ]) if context.aspect_r2: run_command([ "gdalwarp", "-q", "-r", "cubic", "-tr", "20", "20", context.aspect_r1, context.aspect_r2, ]) run_command([ "gdalwarp", "-q", "-r", "cubic", "-tr", "240", "240", context.slope_r1, context.slope_coarse, ]) run_command([ "gdalwarp", "-q", "-r", "cubic", "-tr", "240", "240", context.aspect_r1, context.aspect_coarse, ]) return True