Exemple #1
0
def create_snap_raster(self,
                       feature,
                       scratch,
                       output_file,
                       cellsize=10,
                       spacing=10):
    """ Create a Snap Raster based on input feature. Snap raster units are 
    based on the units of the input dataset."""
    import utilities.image as image  # @UnresolvedImport

    desc = arcpy.Describe(feature)
    gXMin = int(round(desc.extent.XMin, -1)) - cellsize * spacing
    gYMin = int(round(desc.extent.YMin, -1)) - cellsize * spacing
    gXMax = int(round(desc.extent.XMax, -1)) + cellsize * spacing
    gYMax = int(round(desc.extent.YMax, -1)) + cellsize * spacing
    arcpy.env.extent = arcpy.Extent(gXMin, gYMin, gXMax, gYMax)

    arcpy.env.cellSize = cellsize
    arcpy.env.outputCoordinateSystem = desc.spatialReference

    arcpy.CreateRandomRaster_management(scratch, 'RandomRaster', 'INTEGER 0 0',
                                        arcpy.Extent, str(cellsize))

    image = image.Single_Image(scratch + '\\RandomRaster')
    for row in range(0, image.get_rows(), spacing):
        for col in range(0, image.get_columns(), spacing):
            image.set_value(row, col, 1)
    image.save(output_file)

    arcpy.Delete_management(scratch + '\\RandomRaster')
    return output_file
 def onRectangle(self, rectangle_geometry):
     #save the parameters for the domain
     file_loc = pythonaddins.SaveDialog("Save your file","", "")
     file_loc = os.path.split(file_loc) 
     out_path = file_loc[0]
     out_name = file_loc[1]
     resolution = setRes.text
     #and actually captures the extent used (onRectangle saves rectangle_geometry as an extent object)
     extent = rectangle_geometry
     #create temporary raster, then get unique cell values
     raster = ap.CreateRandomRaster_management(out_path, out_name, "NORMAL", extent, resolution)
     return raster #these rasters should generally 
Exemple #3
0
    def dem_noise2(self, noise_factor=None):
        arcpy.env.overwriteOutput = True
        arcpy.env.extent = self._in_dem
        r_metadata = arcpy.Raster(self._in_dem)
        self._noise_order = self.noise_factor_dictionary(noise_factor)
        sufix = str(noise_factor)
        noise_raster = "noise{}.tif".format(sufix)
        noise_raster_path = os.path.join(self._temp_folder, noise_raster)
        neighborhood_smooth = arcpy.sa.NbrRectangle(width=self._noise_order, height=self._noise_order, units="CELL")

        arcpy.CreateRandomRaster_management(self._temp_folder, noise_raster, "NORMAL {} {}".format(self._mean, self._stdev), self._in_dem, r_metadata.meanCellHeight)
        noise_smooth = arcpy.sa.FocalStatistics(in_raster=noise_raster_path, neighborhood=neighborhood_smooth, statistics_type="MEAN", ignore_nodata="DATA")
        noise_smooth.save(self._temp_folder + "\\noise_smooth{}.tif".format(sufix))
        outRaster = (noise_smooth + arcpy.sa.Raster(self._in_dem)) * arcpy.Raster(self._mask)
        outRaster.save(self._out_dem)
        # arcpy.Delete_management(points_interpolation)
        # arcpy.Delete_management(os.path.join(self._temp_folder, random_points))
        # arcpy.Delete_management(noise)
        # del noise
        return self._out_dem
    def DemNoise(self, inDEM, outDEM, mean, stdev, mask, noise_order):
        arcpy.env.overwriteOutput = True
        arcpy.env.extent = inDEM
        # Process: Create Random Raster
        tempPath = tempfile.gettempdir()
        print(tempPath)
        rMetadata = arcpy.Raster(inDEM)
        # Process: Create Random Points
        arcpy.CreateRandomPoints_management(tempPath, "randomPoints.shp", None, mask, "1000", "10 Meters", "POINT", "0")
        arcpy.CreateRandomRaster_management(tempPath, "noise.tif", "NORMAL {} {}".format(mean, stdev), inDEM, rMetadata.meanCellHeight)
        # Process: Extract Values to Points
        arcpy.sa.ExtractValuesToPoints(os.path.join(tempPath, "randomPoints.shp"), os.path.join(tempPath, "noise.tif"), os.path.join(tempPath, "PointsInterpolation.shp"), "INTERPOLATE", "VALUE_ONLY")
        # Process: Trend
        noise = arcpy.sa.Trend(in_point_features=os.path.join(tempPath, "PointsInterpolation.shp"), z_field="RASTERVALU", cell_size=rMetadata.meanCellWidth, order=noise_order, regression_type="LINEAR", out_rms_file=None)
        # Process: Raster Calculator
        outRaster = (noise + arcpy.sa.Raster(inDEM)) * arcpy.Raster(mask)

        # arcpy.Clip_management(outRaster, in_template_dataset=mask, out_raster=outDEM, clipping_geometry="ClippingGeometry", maintain_clipping_extent="MAINTAIN_EXTENT")
        outRaster.save(outDEM)
        arcpy.Delete_management(os.path.join(tempPath, "PointsInterpolation.shp"))
        arcpy.Delete_management(os.path.join(tempPath, "randomPoints.shp"))
        # arcpy.Delete_management(noise)
        del noise
        return outDEM
 dem = arcpy.sa.Raster(
     'cley_lidar.asc'
 )  # Enters ARCGIS name of DEM file (it must in the same folder as this script)
 extent = dem.extent  # Sets study area to have the same extent as that of 'dem'
 cellSize = (
     dem.meanCellHeight + dem.meanCellWidth
 ) / 2  # Sets cell size of output raster layers to be the same as that of 'dem'
 random.seed()  # Initializes random number generator
 nsims = 100  # Sets number of simulations
 for i in range(
         nsims):  # Initializes for loop from 1 through 100 iterations
     print 'Simulating DEM.Simulation {0}'.format(i + 1)
     pre_rand = arcpy.CreateRandomRaster_management(
         out_path=arcpy.env.
         workspace,  # Location of the output the raster dataset
         out_name='pre_rand',  # Name of output random layer
         distribution=
         'UNIFORM 0 1',  # Random probability model (Uniform in the range 0 to 1)
         raster_extent=extent,  # Extent of the random layer
         cellsize=cellSize)  # Cell size of the random layer
     rand = 150.0 - (
         arcpy.sa.Raster(pre_rand) * 300.0
     )  # Creates random height values based on RMS error signature of the terrain model raster layer 'dem'
     rand.save('rand')  # Saves 'rand' raster layer to disk
     print 'rand saved'
     arcpy.Delete_management(pre_rand)  # Deletes 'pre_rand' raster layer
     dtm = dem + rand  # Creates a new terrain model raster layer with random values added to the terrain model raster layer 'dem'
     dtm.save('dtm')  # Saves 'dtm' raster layer to disk
     print 'dtm saved'
     inund = arcpy.sa.Con(
         dtm > 890, 1, 0
     )  # Reclassifies the randomised terrain model raster layer into above (1) and below (0) sea level
#Execute expand tool. 
outExpand = Expand(inRaster, numberCells, zoneValues)

#Save the output of the expand tool run.
outExpand.save("expandAllLandCover.img")

# Note that there is no expansion for potato/vegetable rotations because there is only one generalized class for them - value "500"
 
#STEP 3

# Create random raster of values 0 through 100 for apportioning continuous corn rotations into 50% dairy and 50% cash grain from the nearest expanded management schemes of those respective rotations types. Previous field-scale analysis in Pleasant Valley as welk as conversation with county/regional conservation staff showed that dairy was misclassified as continuous corn approximately 50% of the time and as cash grain 50% of the time.

# This step creates the random raster

arcpy.CreateRandomRaster_management(env.scratchFolder, "random", "UNIFORM 0.0 100.0", CropCodeRaster, env.cellSize)
arcpy.CopyRaster_management(env.scratchFolder + '/random', env.scratchGDB + '/random')
random = Raster(env.scratchGDB + '/random')

# This step splits the continuous corn CDL analysis pixels into 50% dairy pixels and 50% cash grain pixels using uniform random values
step1 = Con(BooleanAnd(inCDLRasterCLU == 1, random1 <= 50), 3, inCDLRasterCLU)
step2 = Con(step1 == 1, 2, step1)

step2.save(inCDLRasterCLUnoContCorn)

#STEP 4

#Merge individual land management raster files into their respective CDL defined rotation categories.

#Execute Con Function. Used "if, else" statements to create pixel-based land cover with land management incorporated.
Exemple #7
0
rastName="/testRastGrid.tif"
rastName2="/testRastGrid2.tif"
fcName="grid_mainland_afr.shp"

    
print "Getting extent from: {0}".format(aoi)
desc = arcpy.Describe(aoi)
extent = desc.extent
extent= str(extent.XMin)+" "+str(extent.YMin) +" "+str(extent.XMax)+" "+str(extent.YMax)

coordSys=desc.spatialReference


print "Creating random raster"
arcpy.CreateRandomRaster_management(out_path=tempFolder+"/",out_name=rastName,distribution="NORMAL 0.0 10000000.0",raster_extent=extent, cellsize=cellSize)

arcpy.env.workspace = tempFolder+"/"

print "Making integer"
arcpy.gp.RasterCalculator_sa("""Int(rastName)""",rastName2)

print "Converting raster to polygon"
arcpy.RasterToPolygon_conversion(in_raster=rastName2,out_polygon_features=outFolder+"/"+fcName,simplify="NO_SIMPLIFY",raster_field="Value")

arcpy.Delete_management(rastName)
arcpy.Delete_management(rastName2)

arcpy.env.workspace = outFolder+"/"
gridCellID="cell_id"
arcpy.AddField_management(fcName,gridCellID,"LONG")