示例#1
0
def log(message):
    common_functions.log(message)
    return
def prepare_canopy_data(input_tile_folder, tile_dimension, ndvi_raster_folder,
                        start_step, scratch_workspace, output_fc):
    try:
        arcpy.env.overwriteOutput = True
        arcpy.env.parallelProcessingFactor = "100%"
        arcpy.env.scratchWorkspace = os.path.normpath(scratch_workspace)
        arcpy.CheckOutExtension("Spatial")

        step_start = int(start_step)
        step_count = 1
        step_total = 14

        common_functions.log("Logging to " + arcpy.env.scratchFolder)
        common_functions.step_header(0, step_total, 'Input parameters', [
            input_tile_folder, tile_dimension, ndvi_raster_folder, start_step,
            scratch_workspace, output_fc
        ], [])

        input_tile_folder = os.path.normpath(input_tile_folder)
        ndvi_raster_folder = os.path.normpath(ndvi_raster_folder)
        output_fc = os.path.normpath(output_fc)

        tile_file_name_table = os.path.join(arcpy.env.scratchGDB,
                                            'tile_file_names')
        merged_tiles_unclumped = os.path.join(arcpy.env.scratchGDB,
                                              'merged_tiles_unclumped')
        fence_lines = os.path.join(arcpy.env.scratchGDB, 'fence_lines')
        fence_sitter_clumps_undissolved = os.path.join(
            arcpy.env.scratchGDB, 'fence_sitter_clumps_undissolved')
        fence_sitter_clumps_dissolved = os.path.join(
            arcpy.env.scratchGDB, 'fence_sitter_clumps_dissolved')
        merged_tiles_clumped = os.path.join(arcpy.env.scratchGDB,
                                            'merged_tiles_clumped')
        merged_ndvi_rasters = os.path.join(arcpy.env.scratchGDB,
                                           'merged_ndvi_rasters')
        zonal_ndvi = os.path.join(arcpy.env.scratchGDB, 'zonal_ndvi')
        #        canopies_without_ndvi = os.path.join(arcpy.env.scratchGDB, 'canopies_without_ndvi')
        canopies_without_ndvi = output_fc

        if step_count >= step_start:
            common_functions.step_header(step_count, step_total,
                                         'Collecting tile file names',
                                         [input_tile_folder],
                                         [tile_file_name_table])
            tile_file_names.create_table(input_tile_folder,
                                         tile_file_name_table)
        step_count += 1

        if step_count >= step_start:
            common_functions.step_header(step_count, step_total,
                                         'Populating TileId in all tiles',
                                         [input_tile_folder],
                                         [input_tile_folder])
            populate_field.populate([
                l + (
                    TILE_ID_COLUMN_NAME,
                    'Integer',
                )
                for l in tile_file_names.read_file_names(tile_file_name_table)
            ])
        step_count += 1

        if step_count >= step_start:
            common_functions.step_header(
                step_count, step_total,
                'Merging tiles into a single feature class',
                [tile_file_name_table], [merged_tiles_unclumped])
            merge_tiles.merge(tile_file_name_table, merged_tiles_unclumped)
        step_count += 1

        if step_count >= step_start:
            common_functions.step_header(
                step_count, step_total,
                'Adding and populating PolygonId field',
                [merged_tiles_unclumped], [merged_tiles_unclumped])
            populate_field.populate([(merged_tiles_unclumped, 'UNIQUE_ID',
                                      POLYGON_ID_COLUMN_NAME, 'Integer')])
        step_count += 1

        if step_count >= step_start:
            common_functions.step_header(step_count, step_total,
                                         'Creating fence lines',
                                         [tile_file_name_table], [fence_lines])
            interpolate_tile_extents.main_process_fc_files(
                tile_file_name_table, tile_dimension, fence_lines)
        step_count += 1

        if step_count >= step_start:
            common_functions.step_header(
                step_count, step_total,
                'Creating fence sitters feature class (all polygons that intersect a fence line)',
                [merged_tiles_unclumped, fence_lines],
                [fence_sitter_clumps_undissolved])
            arcpy.SpatialJoin_analysis(merged_tiles_unclumped, fence_lines,
                                       fence_sitter_clumps_undissolved,
                                       'JOIN_ONE_TO_ONE', 'KEEP_COMMON', '',
                                       'INTERSECT')
        step_count += 1

        if step_count >= step_start:
            common_functions.step_header(step_count, step_total,
                                         'Dissolve fence sitters into clumps',
                                         [fence_sitter_clumps_undissolved],
                                         [fence_sitter_clumps_dissolved])
            arcpy.Dissolve_management(fence_sitter_clumps_undissolved,
                                      fence_sitter_clumps_dissolved, "", "",
                                      "SINGLE_PART", "DISSOLVE_LINES")
        step_count += 1

        if step_count >= step_start:
            common_functions.step_header(step_count, step_total,
                                         'Adding and populating ClumpId',
                                         [fence_sitter_clumps_dissolved],
                                         [fence_sitter_clumps_dissolved])
            populate_field.populate([
                (fence_sitter_clumps_dissolved, 'UNIQUE_ID',
                 CLUMP_ID_COLUMN_NAME, 'Integer')
            ])
        step_count += 1

        if step_count >= step_start:
            common_functions.step_header(
                step_count, step_total, 'Append ClumpId to merged tiles',
                [merged_tiles_unclumped, fence_sitter_clumps_dissolved],
                [merged_tiles_clumped])
            arcpy.SpatialJoin_analysis(merged_tiles_unclumped,
                                       fence_sitter_clumps_dissolved,
                                       merged_tiles_clumped, "JOIN_ONE_TO_ONE",
                                       "KEEP_ALL", '', 'WITHIN')
        step_count += 1

        if step_count >= step_start:
            common_functions.step_header(
                step_count, step_total,
                'Stitching adjacent fence sitters together',
                [merged_tiles_clumped], [canopies_without_ndvi])
            merge_fence_sitters.merge(merged_tiles_clumped,
                                      canopies_without_ndvi)
        step_count += 1

        if step_count >= step_start:
            common_functions.step_header(step_count, step_total,
                                         'Merge NDVI rasters',
                                         [ndvi_raster_folder],
                                         [merged_ndvi_rasters])
            arcpy.Delete_management(merged_ndvi_rasters)
            arcpy.CreateMosaicDataset_management(
                os.path.dirname(merged_ndvi_rasters),
                os.path.basename(merged_ndvi_rasters),
                RASTER_COORDINATE_SYSTEM, 1, '32_BIT_FLOAT', 'NONE')
            arcpy.AddRastersToMosaicDataset_management(
                merged_ndvi_rasters, 'Raster Dataset', ndvi_raster_folder, '',
                '', '', '', '', '', '', '*.tif', False, 'OVERWRITE_DUPLICATES')
        step_count += 1

        if step_count >= step_start:
            common_functions.step_header(
                step_count, step_total, 'Computing NDVI zonal statistics',
                [canopies_without_ndvi, merged_ndvi_rasters], [zonal_ndvi])
            compute_zonal_stats.compute(canopies_without_ndvi,
                                        POLYGON_ID_COLUMN_NAME,
                                        merged_ndvi_rasters, zonal_ndvi)
            compute_zonal_stats
        step_count += 1

        if step_count >= step_start:
            common_functions.step_header(step_count, step_total,
                                         'Appending NDVI statistics',
                                         [canopies_without_ndvi, zonal_ndvi],
                                         [output_fc])
            join_files.join(output_fc, POLYGON_ID_COLUMN_NAME, zonal_ndvi,
                            POLYGON_ID_COLUMN_NAME, 'MAX;MEAN;STD')
        step_count += 1

        if step_count >= step_start:
            common_functions.step_header(step_count, step_total, 'Cleaning up',
                                         [], [])
            arcpy.DeleteField_management(output_fc, [
                'Join_Count', 'Shape_Length_1', 'Shape_Area_1', 'PolygonId_1',
                'COUNT', 'AREA_1'
            ])
        step_count += 1

    finally:
        arcpy.CheckInExtension("Spatial")