예제 #1
0
def process_local_slope(dem=None,
                        slope=None,
                        max_slope=30,
                        mask=None,
                        return_type="polygon",
                        workspace=None):
    """

	:param dem: The DEM to process
	:param slope: If slope is already processed, use this instead.
	:param max_slope: The maximum slope in degrees that will be considered suitable for building
	:param mask: A polygon or raster mask to use as the processing area (arcpy.env.mask/Analysis Mask environment)
	:param return_type: whether to return a polygon feature class or a raster. Default is polygon, where raster will be processed to polygon automatically. Options are "polygon" or "raster"
	:return:
	"""

    if not dem and not slope:
        raise ValueError(
            "Must provide either a slope raster or a DEM raster. Either parameter 'dem' or parameter 'slope' must be defined."
        )

    arcpy.CheckOutExtension("Spatial")

    if not slope:
        arcpy.env.mask = mask
        logger.info("Processing raster to slope")
        mask_raster = arcpy.sa.ExtractByMask(
            dem, mask
        )  # mask environment variable hasn't been working - force extraction
        slope_raster = arcpy.sa.Slope(mask_raster, output_measurement="DEGREE")
    else:
        slope_raster = arcpy.sa.ExtractByMask(
            slope, mask
        )  # mask environment variable hasn't been working - force extraction

    logger.info("Thresholding raster")
    threshold_raster = slope_raster < max_slope

    raster_name = generate_gdb_filename("slope_raster", gdb=workspace)

    logger.info("Saving raster to disk")
    threshold_raster.save(raster_name)

    arcpy.CheckInExtension("Spatial")

    if return_type.lower() == "polygon":

        logger.info("Converting to polygons")
        new_name = convert_and_filter_by_code(raster_name, filter_value=1)

        poly_name = generate_gdb_filename("slope_polygon", gdb=workspace)
        arcpy.CopyFeatures_management(new_name, poly_name)

        return poly_name
    elif return_type.lower() == "raster":
        return raster_name
    else:
        raise ValueError(
            "Invalid parameter for return_type. Must be either \"raster\" or \"polygon\""
        )
예제 #2
0
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
예제 #3
0
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
예제 #4
0
def process_local_slope(dem=None, slope=None, max_slope=30, mask=None, return_type="polygon", workspace=None):
	"""

	:param dem: The DEM to process
	:param slope: If slope is already processed, use this instead.
	:param max_slope: The maximum slope in degrees that will be considered suitable for building
	:param mask: A polygon or raster mask to use as the processing area (arcpy.env.mask/Analysis Mask environment)
	:param return_type: whether to return a polygon feature class or a raster. Default is polygon, where raster will be processed to polygon automatically. Options are "polygon" or "raster"
	:return:
	"""

	if not dem and not slope:
		raise ValueError("Must provide either a slope raster or a DEM raster. Either parameter 'dem' or parameter 'slope' must be defined.")

	arcpy.CheckOutExtension("Spatial")

	if not slope:
		arcpy.env.mask = mask
		logger.info("Processing raster to slope")
		mask_raster = arcpy.sa.ExtractByMask(dem, mask)  # mask environment variable hasn't been working - force extraction
		slope_raster = arcpy.sa.Slope(mask_raster, output_measurement="DEGREE")
	else:
		slope_raster = arcpy.sa.ExtractByMask(slope, mask)  # mask environment variable hasn't been working - force extraction

	logger.info("Thresholding raster")
	threshold_raster = slope_raster < max_slope

	raster_name = generate_gdb_filename("slope_raster", gdb=workspace)

	logger.info("Saving raster to disk")
	threshold_raster.save(raster_name)

	arcpy.CheckInExtension("Spatial")

	if return_type.lower() == "polygon":

		logger.info("Converting to polygons")
		new_name = convert_and_filter_by_code(raster_name, filter_value=1)
		
		poly_name = generate_gdb_filename("slope_polygon", gdb=workspace)
		arcpy.CopyFeatures_management(new_name, poly_name)

		return poly_name
	elif return_type.lower() == "raster":
		return raster_name
	else:
		raise ValueError("Invalid parameter for return_type. Must be either \"raster\" or \"polygon\"")