예제 #1
0
def subset (feature, raster, workspace, buffer_scale = 2):
    """Subset a raster based on an input features boundaries plus a buffer
    which should be greater then the size of the pixels in the given raster.
    This is to ensure there are no gaps between where the raster ends and the
    input feature begins. Any excess raster will be clipped later after it is
    converted to a feature class."""
    subset = workspace + '\\' + 'raster_subset_' + str(feature.GLIMSID) + '.img'
    try:
        # Buffer the input features geometry
        cellsize = float(get_properties(raster, 'CELLSIZEX')) * buffer_scale
        mask = ARCPY.Buffer_analysis(feature.shape, ARCPY.Geometry(), cellsize)
        
        # Extract by mask using the buffered feature geometry
        extract = spatial.ExtractByMask (raster, mask[0])
        extract.save(subset) # Save extracted mask as subset
        
        return subset, False # Return path to subset location in the workspace
    except:
        return subset, True
    def clip_raster(self, in_raster, boundary_raster, out_raster):
        """
        Clip raster

        Parameters
        ----------
        in_raster : str
            name of input raster to clip
        boundary_raster : str
            name of raster to use as clipping boundary
        out_raster : str
            name of clipped raster
        """
        print('Clipping raster ' + in_raster)
        try:
            scratch = sa.ExtractByMask(in_raster, boundary_raster)
            scratch.save(out_raster)
        except:
            raise Exception(arcpy.GetMessages())
예제 #3
0
def subset (feature, raster, scratch, name = None, buffer_scale = 2):
    """Subset a raster based on an input features boundaries plus a buffer
    which should be greater then the size of the pixels in the given raster.
    This is to ensure there are no gaps between where the raster ends and the
    input feature begins. Any excess raster will be clipped later after it is
    converted to a feature class."""
    # Build Export Name. This option is largely included in case there is unexpected 
    # naming conflicts with other functions
    if name == '' or name == None: subset = os.path.join(scratch,'Subset.img')
    else: subset = os.path.join(scratch, name)
    
    # Buffer the input features geometry
    cellsize = float(get_properties(raster, 'CELLSIZEX')) * buffer_scale
    mask = arcpy.Buffer_analysis(feature, arcpy.Geometry(), cellsize)
    
    # Extract by mask using the buffered feature geometry
    extract = spatial.ExtractByMask (raster, mask[0])
    extract.save(subset) # Save extracted mask as subset
    
    arcpy.Delete_management(mask)
    del cellsize
    
    return subset
예제 #4
0
arcpy.env.outputCoordinateSystem = arcpy.Describe(elevInput).SpatialReference
msg("Setting spatial reference to %s" % elevInput)

#Create a maskRaster from the maskPoly
msg("Creating mask raster")
fld = arcpy.ListFields(maskPoly)[0].name
arcpy.FeatureToRaster_conversion(maskPoly, fld, maskRaster, cellSize)

#Set the mask to the mask raster
arcpy.env.mask = maskRaster

#Extract datasets by mask
if nlcdOutput <> "#":
    msg("Extracting NLCD to %s" % nlcdOutput)
    rasterLyr = arcpy.MakeRasterLayer_management(nlcdInput)
    outputRaster = sa.ExtractByMask(rasterLyr, maskRaster)
    outputRaster.save(nlcdOutput)
if elevOutput <> "#":
    msg("Extracting elevation to %s" % elevOutput)
    rasterLyr = arcpy.MakeRasterLayer_management(elevInput)
    outputRaster = sa.ExtractByMask(rasterLyr, maskRaster)
    outputRaster.save(elevOutput)
if fdirOutput <> "#":
    msg("Extracting flow direction to %s" % fdirOutput)
    rasterLyr = arcpy.MakeRasterLayer_management(fdirInput)
    outputRaster = sa.ExtractByMask(rasterLyr, maskRaster)
    outputRaster.save(fdirOutput)
if faccOutput <> "#":
    msg("Extracting flow accumulation to %s" % faccOutput)
    rasterLyr = arcpy.MakeRasterLayer_management(faccInput)
    outputRaster = sa.ExtractByMask(rasterLyr, maskRaster)
예제 #5
0
        weight = float(vehicleRow[3])
        maxkph = float(vehicleRow[4])
        onslope = float(vehicleRow[5])
        offslope = float(vehicleRow[6])
        vehicleTable[name] = [classname,name,weight,maxkph,onslope,offslope]
    del vehicleRows
    if debug == True: arcpy.AddMessage("vehicleTable: " + str(vehicleTable))
    vehicleParams = vehicleTable[inputVehicleType]
    if debug == True: arcpy.AddMessage("vehicleParams: " + str(vehicleParams))

    # Clip slope service layers
    arcpy.AddMessage("Clipping slope...")
    slopeClip = os.path.join(scratch,"slopeClip")
    #arcpy.MakeRasterLayer_management(inputSlope,"SlopeLayer")
    #arcpy.CopyRaster_management("SlopeLayer",slopeClip)
    outSlope = sa.ExtractByMask(inputSlope,inputAOI)
    outSlope.save(slopeClip)
    deleteme.append(slopeClip)
    
    # Set all Slope values greater than the vehicle's off road max to that value
    arcpy.AddMessage("Reclassifying Slope ...")
    reclassSlope = os.path.join(os.path.dirname(scratch),"reclassSlope.tif")
    if debug == True: arcpy.AddMessage("reclassSlope: " + str(reclassSlope))
    #float(vehicleParams[5])
    if debug == True: arcpy.AddMessage(str(time.strftime("Con: %m/%d/%Y  %H:%M:%S", time.localtime())))
    outCon = sa.Con(sa.Raster(slopeClip) > float(vehicleParams[5]),float(vehicleParams[5]),sa.Raster(slopeClip))    
    # FAILS HERE:
    outCon.save(reclassSlope)
    # ERROR 010240: Could not save raster dataset to C:\Workspace\MAoT for A4W\A4W\test.gdb\reclassSlope with output format FGDBR.
    #
    # 010240 : Could not save raster dataset to <value> with output format <value>.
예제 #6
0
def get_centerline (feature, dem, workspace, power = 5, eu_cell_size = 10):
    """Returns a center line feature of the given polygon feature based on
    cost over an euclidean distance raster and cost path. points are seeded
    using minimum and maximum elevation."""    
    centerline = workspace + '\\centerline.shp'
    center_length = 0
    center_slope = 0
    smoothing = 4
    trim_distance = "100 Meters"

    try: 
        # Setup extents / environments for the current feature
        ARCPY.env.extent = feature.shape.extent
        desc = ARCPY.Describe(feature)
        XMin_new = desc.extent.XMin - 200
        YMin_new = desc.extent.YMin - 200
        XMax_new = desc.extent.XMax + 200
        YMax_new = desc.extent.YMax + 200
        ARCPY.env.extent = ARCPY.Extent(XMin_new, YMin_new, XMax_new, YMax_new)
    
        ARCPY.env.overwriteOutput = True
        ARCPY.env.cellSize = eu_cell_size
        ARCPY.env.snapRaster = dem
        
        
        # Get minimum and maximum points
        resample = ARCPY.Resample_management (dem, 'in_memory\\sample', eu_cell_size)
        masked_dem = spatial.ExtractByMask (resample, feature.shape)
    
    
        # Find the maximum elevation value in the feature, convert them to
        # points and then remove all but one.
        maximum = get_properties (masked_dem, 'MAXIMUM') 
        maximum_raster = spatial.SetNull(masked_dem, masked_dem, 'VALUE <> ' + maximum)
        maximum_point = ARCPY.RasterToPoint_conversion(maximum_raster, 'in_memory\\max_point')
        rows = ARCPY.UpdateCursor (maximum_point)
        for row in rows:
            if row.pointid <> 1:
                rows.deleteRow(row)
        del row, rows
        
        # Find the minimum elevation value in the feature, convert them to
        # points and then remove all but one.
        minimum = get_properties (masked_dem, 'MINIMUM')
        minimum_raster = spatial.SetNull(masked_dem, masked_dem, 'VALUE <> ' + minimum)
        minimum_point = ARCPY.RasterToPoint_conversion(minimum_raster, 'in_memory\\min_point')
        rows = ARCPY.UpdateCursor (minimum_point)
        for row in rows:
            if row.pointid <> 1:
                rows.deleteRow(row)
        del row, rows
        
        # Calculate euclidean Distance to boundary line for input DEM cells.
        polyline = ARCPY.PolygonToLine_management(feature.shape, 'in_memory\\polyline')
        eucdist =spatial.EucDistance(polyline, "", eu_cell_size, '')
         
        masked_eucdist = spatial.ExtractByMask (eucdist, feature.shape)
        
        # Calculate the cost raster by inverting the euclidean distance results,
        # and raising it to the power of x to exaggerate the least expensive route.
        cost_raster = (-1 * masked_eucdist + float(maximum))**power
            
        # Run the cost distance and cost path function to find the path of least
        # resistance between the minimum and maximum values. The results are set
        # so all values equal 1 (different path segments have different values)
        # and convert the raster line to a poly-line.
        backlink = 'in_memory\\backlink'
        cost_distance = spatial.CostDistance(minimum_point, cost_raster, '', backlink) 
        cost_path = spatial.CostPath(maximum_point, cost_distance, backlink, 'EACH_CELL', '')
        cost_path_ones = spatial.Con(cost_path, 1, '', 'VALUE > ' + str(-1)) # Set all resulting pixels to 1
        r_to_p = ARCPY.RasterToPolyline_conversion (cost_path_ones, 'in_memory\\raster_to_polygon')
        
        
        del ARCPY.env.extent # Delete current extents (need here but do not know why)
        
        # Removes small line segments from the centerline shape. These segments are
        # a byproduct of cost analysis.
        lines = str(ARCPY.GetCount_management(r_to_p)) #check whether we have more than one line segment
        if float(lines) > 1: # If there is more then one line
            rows = ARCPY.UpdateCursor(r_to_p)
            for row in rows:
                if row.shape.length == eu_cell_size: # delete all the short 10 m lines
                    rows.deleteRow(row)
            del row, rows
            lines = str(ARCPY.GetCount_management(r_to_p))
            if float(lines) > 1:
                ARCPY.Snap_edit(r_to_p, [[r_to_p, "END", "50 Meters"]]) # make sure that the ends of the lines are connected
                r_to_p = ARCPY.Dissolve_management(r_to_p, 'in_memory\\raster_to_polygon_dissolve')
    
    
        # Smooth the resulting line. Currently smoothing is determined by minimum
        # and maximum distance. The greater change the greater the smoothing.
        smooth_tolerance = (float(maximum) - float(minimum)) / smoothing
        ARCPY.SmoothLine_cartography(r_to_p, centerline, 'PAEK', smooth_tolerance, 'FIXED_CLOSED_ENDPOINT', 'NO_CHECK')
    
        field_names = [] # List of field names in the file that will be deleted.
        fields_list = ARCPY.ListFields(centerline)
        for field in fields_list: # Loop through the field names
            if not field.required: # If they are not required append them to the list of field names.
                field_names.append(field.name)
        # Add new fields to the center line feature
        ARCPY.AddField_management(centerline, 'GLIMSID', 'TEXT', '', '', '25')
        ARCPY.AddField_management(centerline, 'LENGTH', 'FLOAT')
        ARCPY.AddField_management(centerline, 'SLOPE', 'FLOAT')
        ARCPY.DeleteField_management(centerline, field_names) # Remove the old fields.
        
        
        # Calculate the length of the line segment and populate segment data.
        ARCPY.CalculateField_management(centerline, 'LENGTH', 'float(!shape.length@meters!)', 'PYTHON')
        rows = ARCPY.UpdateCursor (centerline)
        for row in rows:
            row.GLIMSID = feature.GLIMSID # Get GLIMS ID and add it to segment
            center_length = row.LENGTH # Get the length of the center line
            # Calculate slope of the line based on change in elevation over length of line
            center_slope = round(math.degrees(math.atan((float(maximum) - float(minimum)) / row.LENGTH)), 2)
            row.SLOPE = center_slope # Write slope to Segment
            rows.updateRow(row) # Update the new entry
        del row, rows #Delete cursors and remove locks    
        
        
        # Flip Line if needed - Turn min point and end point into a line segment if
        # the length of this line is greater then the threshold set, flip the line.
        end_point = ARCPY.FeatureVerticesToPoints_management(centerline, 'in_memory\\end_point', 'END')
        merged_points = ARCPY.Merge_management ([end_point, minimum_point], 'in_memory\\merged_points')
        merged_line = ARCPY.PointsToLine_management (merged_points, 'in_memory\\merged_line')
        
        merged_line_length = 0 # Get the line Length
        rows = ARCPY.SearchCursor (merged_line)
        for row in rows:
            merged_line_length += row.shape.length
        del row, rows
            
        # if the line length is greater then a quarter the entire feature length, flip
        if merged_line_length > (center_length/4):
            ARCPY.FlipLine_edit(centerline)
    
    
        # This function attempts to extend the line and clip it back to the 
        # feature extents in order to create a line that runs from edge to edge
        #trimmed_line = ARCPY.Merge_management([polyline, centerline], 'in_memory\\line_merge')
        trimmed_line = ARCPY.Append_management (polyline, centerline, 'NO_TEST')
        ARCPY.TrimLine_edit (trimmed_line, trim_distance, "DELETE_SHORT")
        ARCPY.ExtendLine_edit(trimmed_line, trim_distance, "EXTENSION")
        
        rows = ARCPY.UpdateCursor (trimmed_line)
        for row in rows:
            if row.LENGTH == 0.0:
                rows.deleteRow(row)
        del row, rows
        # Recalculate length. Must be after 0.0 lengths are deleted or they will
        # not be removed above.
        ARCPY.CalculateField_management(centerline, 'LENGTH', 'float(!shape.length@meters!)', 'PYTHON')
    
    
        ARCPY.env.overwriteOutput = False
        return centerline, center_length, center_slope, False
    except:
        ARCPY.env.overwriteOutput = False
        return centerline, '', '', True
예제 #7
0
    os.makedirs(OUT_PATH)

for site in idList:
    #selStmt = "OBJECTID = " + str(tup[0])  #first value of tuple is objectid
    selStmt = "site_ID = '" + site + "'"
    arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", selStmt)
    #siteID = tup[1]  #second value of tuple is siteid. Needs to be unique.
    outname = OUT_PATH + "\\" + site + ".tif"
    extent = lyr.getSelectedExtent()
    XMIN = str(extent.XMin)
    YMIN = str(extent.YMin)
    XMAX = str(extent.XMax)
    YMAX = str(extent.YMax)
    ENV.extent = XMIN + " " + YMIN + " " + XMAX + " " + YMAX
    print "clipping " + site
    outExtractByMask = SA.ExtractByMask(IN_RAS, lyr)
    # if the result is all no data (e.g. no dem under the poly), don't save
    # careful: this keeps partial disks
    if arcpy.GetRasterProperties_management(outExtractByMask,
                                            "ALLNODATA").getOutput(0) == '0':
        print " ... saving " + site
        outExtractByMask.save(outname)
    else:
        print " ... " + site + " dem is all null"

arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")

del lyr, selStmt
#%%
# reduce vertical resolution of DEM to remove micro-topography
# TRIAL!!
예제 #8
0
# Set processing extent environments
arcpy.env.snapRaster = newRast

outMask = "N:/MSP/Projects/Rockweed/Zones/ClippingMask.gdb/MARClippingMask"
#
# # using the clipping Mask remove all values that fall under the mask
print("Erasing all vegs patches in open ocean using mask")
outCon = arcpy.sa.Con(arcpy.sa.IsNull(outMask), newRast,
                      0)  #trying to add sa to the Con Command
outName = "NDVI_Final"
outCon.save(outName)

## using the extent of the Maritimes Bioregion, clip the final mosaic raster
mask = "N:/MSP/Data/Boundaries/MaritimesRegionBound/MaritimesRegionPolygon_UpdatedSept2015.shp"
outExtractByMask = sa.ExtractByMask(outName, mask)
outFinal = "NDVI_FinalClip"
outExtractByMask.save(outFinal)

print(str(time.ctime(int(time.time()))))
# set all values >= 0.4 to 1, everything else to NULL
outCon2 = arcpy.sa.SetNull(outFinal < 0.4, 1)

Poly1 = "NDVI_Poly"
# #---------------------------------------------------------------------------------#
# # Convert the raster to a polygon layer
print("Converted raster to polygon layer")
arcpy.RasterToPolygon_conversion(outCon2, Poly1, "NO_SIMPLIFY", "VALUE")

delFlds = "gridcode"
arcpy.DeleteField_management(Poly1, delFlds)