def make_extent_from_dem(dem, output_location): arcpy.CheckOutExtension("Spatial") environments = store_environments(["mask", "extent", "outputCoordinateSystem"]) try: temp_raster_filename = generate_gdb_filename(scratch=True) dem_properties = arcpy.Describe(dem) arcpy.env.outputCoordinateSystem = dem_properties.spatialReference # set the spatial reference environment variable so that the constant raster gets created properly geoprocessing_log.info("Creating Constant Raster") arcpy.env.mask = dem raster = arcpy.sa.CreateConstantRaster(constant_value=1, data_type="INTEGER", cell_size=10, extent=dem) geoprocessing_log.info("Saving to output filename") print(temp_raster_filename) raster.save(temp_raster_filename) geoprocessing_log.info("Converting Raster to Polygon") arcpy.RasterToPolygon_conversion(temp_raster_filename, output_location, simplify=False, raster_field="Value") #arcpy.Delete_management(temp_raster_filename) finally: arcpy.CheckInExtension("Spatial") reset_environments(environments)
def land_use(nlcd_layer, search_area, excluded_types, tiger_lines, census_places, crs, workspace): arcpy.CheckOutExtension("spatial") geoprocessing_log.info("Extracting NLCD raster to search area") nlcd_in_area = arcpy.sa.ExtractByMask(nlcd_layer, search_area) avoid_types = [exclude.value for exclude in excluded_types] thresholded_raster = nlcd_in_area for avoid in avoid_types: # this is inefficient, but I couldn't get it to work with a "not in" and I need something that works for n number of values thresholded_raster = arcpy.sa.Con(thresholded_raster != avoid, thresholded_raster, 0) scratch_raster = generate_gdb_filename("temp_raster", scratch=True) thresholded_raster.save( scratch_raster) # save it so we can use it for environment variables stored_environments = store_environments( ('cellSize', 'mask', 'extent', 'snapRaster') ) # cache the env vars so we can reset them at the end of this function arcpy.env.cellSize = scratch_raster arcpy.env.mask = scratch_raster arcpy.env.extent = scratch_raster arcpy.env.snapRaster = scratch_raster roads_mask = make_road_mask(tiger_lines, census_places=census_places, search_area=search_area) roads_raster = generate_gdb_filename("roads_raster") geoprocessing_log.info("Converting roads mask to raster") try: arcpy.PolygonToRaster_conversion(roads_mask, "OBJECTID", roads_raster) #arcpy.CalculateStatistics_management(roads_raster) # crashes for invalid statistics unless we run this after the conversion except: geoprocessing_log.error( "Error creating raster: {0:s} - from roads mask: {1:s}".format( roads_raster, roads_mask)) raise # Raster Calculations final_nlcd = arcpy.sa.Con(arcpy.sa.IsNull(arcpy.sa.Raster(roads_raster)), thresholded_raster, 1) intermediate_raster = generate_gdb_filename("intermediate_nlcd_mask") projected_raster = generate_gdb_filename("projected_nlcd_mask", gdb=workspace) final_nlcd.save(intermediate_raster) reset_environments(stored_environments) geoprocessing_log.info("Reprojecting final raster") arcpy.ProjectRaster_management(intermediate_raster, projected_raster, out_coor_system=crs) filtered_nlcd_poly = filter_patches.convert_and_filter_by_code( projected_raster, filter_value=0) return filtered_nlcd_poly
def land_use(nlcd_layer, search_area, excluded_types, tiger_lines, census_places, crs, workspace): arcpy.CheckOutExtension("spatial") geoprocessing_log.info("Extracting NLCD raster to search area") nlcd_in_area = arcpy.sa.ExtractByMask(nlcd_layer, search_area) avoid_types = [exclude.value for exclude in excluded_types] thresholded_raster = nlcd_in_area for ( avoid ) in ( avoid_types ): # this is inefficient, but I couldn't get it to work with a "not in" and I need something that works for n number of values thresholded_raster = arcpy.sa.Con(thresholded_raster != avoid, thresholded_raster, 0) scratch_raster = generate_gdb_filename("temp_raster", scratch=True) thresholded_raster.save(scratch_raster) # save it so we can use it for environment variables stored_environments = store_environments( ("cellSize", "mask", "extent", "snapRaster") ) # cache the env vars so we can reset them at the end of this function arcpy.env.cellSize = scratch_raster arcpy.env.mask = scratch_raster arcpy.env.extent = scratch_raster arcpy.env.snapRaster = scratch_raster roads_mask = make_road_mask(tiger_lines, census_places=census_places, search_area=search_area) roads_raster = generate_gdb_filename("roads_raster") geoprocessing_log.info("Converting roads mask to raster") try: arcpy.PolygonToRaster_conversion(roads_mask, "OBJECTID", roads_raster) # arcpy.CalculateStatistics_management(roads_raster) # crashes for invalid statistics unless we run this after the conversion except: geoprocessing_log.error( "Error creating raster: {0:s} - from roads mask: {1:s}".format(roads_raster, roads_mask) ) raise # Raster Calculations final_nlcd = arcpy.sa.Con(arcpy.sa.IsNull(arcpy.sa.Raster(roads_raster)), thresholded_raster, 1) intermediate_raster = generate_gdb_filename("intermediate_nlcd_mask") projected_raster = generate_gdb_filename("projected_nlcd_mask", gdb=workspace) final_nlcd.save(intermediate_raster) reset_environments(stored_environments) geoprocessing_log.info("Reprojecting final raster") arcpy.ProjectRaster_management(intermediate_raster, projected_raster, out_coor_system=crs) filtered_nlcd_poly = filter_patches.convert_and_filter_by_code(projected_raster, filter_value=0) return filtered_nlcd_poly