예제 #1
0
# raster. Of course you could do this using Raster Calculator, but what if you want to do more complex statistics,
# model simulations and so on?

# Be aware that the size of the raster is crucial as the resulting numpy array will reside in your computer
# memory, so no 10gb files!

import arcpy

arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\Data\Course_ArcGIS_Python\Classes\10_Rasters\DataFolder\Step_2_Data"

inRas = arcpy.Raster("etopo10")
lowerLeft = arcpy.Point(inRas.extent.XMin, inRas.extent.YMin)
cellSize = inRas.meanCellWidth
arr = arcpy.RasterToNumPyArray(inRas, nodata_to_value=0)

# Print the resulting array
print(arr.shape)

# Now lets convert m to feet
arrFeet = arr * 3.28084

newRaster = arcpy.NumPyArrayToRaster(arrFeet,
                                     lowerLeft,
                                     cellSize,
                                     value_to_nodata=0)
newRaster.save("etopo10_ft.tif")

# Task: Using the etopo10 dataset and a NumPyArray, extract only land values, replacing them with null data and
# create the resulting tif file. Hint: arr[arr < 0] = -9999
예제 #2
0
## Output File creation
# Output raster file names and locations
dataCountOut = os.path.join(outputFolder, r"dataCount.tif")
if linRegBool == "true":
    linReg_slopeOut = os.path.join(outputFolder, r"linReg_slope.tif")
    linReg_pvalueOut = os.path.join(outputFolder, r"linReg_pvalue.tif")
    linReg_stderrOut = os.path.join(outputFolder, r"linReg_std_err.tif")
if theilSenBool == "true":
    theilSen_slopeOut = os.path.join(outputFolder, r"theilSen_slope.tif")
    theilSen_loSlopeOut = os.path.join(outputFolder, r"theilSen_loSlope.tif")
    theilSen_upSlopeOut = os.path.join(outputFolder, r"theilSen_upSlope.tif")

# Save outpu files
output_r = arcpy.NumPyArrayToRaster(dataCount_rast, lowerLeft,
                                    arcRast(inputRastDir, 1).meanCellWidth,
                                    arcRast(inputRastDir, 1).meanCellHeight,
                                    noData)
output_r.save(dataCountOut)
if linRegBool == "true":
    output_r = arcpy.NumPyArrayToRaster(
        linReg_slope, lowerLeft,
        arcRast(inputRastDir, 1).meanCellWidth,
        arcRast(inputRastDir, 1).meanCellHeight, noData)
    output_r.save(linReg_slopeOut)
    output_r = arcpy.NumPyArrayToRaster(
        linReg_pvalue, lowerLeft,
        arcRast(inputRastDir, 1).meanCellWidth,
        arcRast(inputRastDir, 1).meanCellHeight, noData)
    output_r.save(linReg_pvalueOut)
    output_r = arcpy.NumPyArrayToRaster(
        linReg_std_err, lowerLeft,
예제 #3
0
# --------------------------
# processing

r = Raster(rasterPath)  # open the raster

# calcuate
max = r.maximum  # get the maximum of this raster
min = r.minimum  # get the minimum of this raster
d = (max - min) / groupCnt  # the distance

array = arcpy.RasterToNumPyArray(r)  # the Raster to NumPy
rowNum, colNum = array.shape
for i in range(0, rowNum):
    for j in range(0, colNum):
        value = array[i][j]
        if value == max:  # the value of max is specific
            array[i][j] = groupCnt + startingNum - 1
            continue
        cnt = groupCnt - 1
        while cnt != -1:
            if value >= min + d * cnt:
                array[i][j] = cnt + 1
                break
            cnt = cnt - 1

# save raster from numpy
lowerLeft = arcpy.Point(r.extent.XMin, r.extent.YMin)
cellSize = r.meanCellWidth
newRaster = arcpy.NumPyArrayToRaster(array, lowerLeft, cellSize)
newRaster.save(outPath)
예제 #4
0
bandArray = npy.zeros(inArray.shape, dtype='int32')
stripeArray = bandArray.copy()
header = get_header(raster)
outDir, rasterName = os.path.split(raster)

bandRasterPath = os.path.join(
    outDir, resisRasterBase + '_bandsBlockSize' + str(blockSize) + '.tif')
stripeRasterPath = os.path.join(
    outDir, resisRasterBase + '_stripesBlockSize' + str(blockSize) + '.tif')
bandNum = 0
stripeNum = 0
print 'Processing', raster
for centerRow in range((blockSize - 1) / 2, header['nrows'], blockSize):
    bandNum += 1
    bandArray[centerRow - (blockSize - 1) / 2:centerRow + (blockSize - 1) / 2 +
              1, :] = bandNum
LLC = arcpy.Point(header['xllcorner'], header['yllcorner'])
bandRaster = arcpy.NumPyArrayToRaster(bandArray, LLC, header['cellsize'],
                                      header['cellsize'], -9999)
bandRaster.save(bandRasterPath)
print 'Saved', bandRasterPath

for centerCol in range((blockSize - 1) / 2, header['ncols'], blockSize):
    stripeNum += 1
    stripeArray[:, centerCol - (blockSize - 1) / 2:centerCol +
                (blockSize - 1) / 2 + 1] = stripeNum
stripeRaster = arcpy.NumPyArrayToRaster(stripeArray, LLC, header['cellsize'],
                                        header['cellsize'], -9999)
stripeRaster.save(stripeRasterPath)
print 'Saved', stripeRasterPath
예제 #5
0
    #Collect initial environment states
    initMask = arcpy.env.mask

    #Convert the raw bin file to an array (NSIDC ice vector data are 361x361x3 arrays)
    with open(moFile, 'rb') as inFile:
        arrMO = np.fromfile(inFile, np.int16).reshape(361, 361, 3)

    #Extract u/v arrays and reshape to 1D data vectors
    uValues = arrMO[:, :, 0].reshape(-1).astype(np.float)
    vValues = arrMO[:, :, 1].reshape(-1).astype(np.float)

    #Compute magnitudes (via pythagorean theorem)
    mValues = np.sqrt(np.square(uValues) + np.square(vValues))

    #Create a mask from the 3rd band
    maskRaster = arcpy.NumPyArrayToRaster(arrMO[:, :, 2], lowerleft, 25000,
                                          25000, 0)
    arcpy.env.mask = maskRaster

    #Save u/v/magnitude arrays as 3-band raster
    uRaster = arcpy.NumPyArrayToRaster(uValues.reshape(361, 361), lowerleft,
                                       cellSize, cellSize)
    vRaster = arcpy.NumPyArrayToRaster(vValues.reshape(361, 361), lowerleft,
                                       cellSize, cellSize)
    mRaster = arcpy.NumPyArrayToRaster(mValues.reshape(361, 361), lowerleft,
                                       cellSize, cellSize)
    arcpy.CompositeBands_management((uRaster, vRaster, mRaster), outUVRasterFN)
    arcpy.DefineProjection_management(outUVRasterFN, srEASE)

    #Compute the arctan of above to get the angle, in radians
    thetaRadians = np.arctan2(vValues, uValues)
예제 #6
0
def execute_task(args):
	in_extentDict, data, traj_list, noncroplist, croplist, cls, rws = args

	fc_count = in_extentDict[0]
	
	###get the extent from the SPECIFIC tile
	procExt = in_extentDict[1]
	XMin = procExt[0]
	YMin = procExt[1]
	XMax = procExt[2]
	YMax = procExt[3]

	#set environments
	arcpy.env.snapRaster = data['pre']['traj']['path']
	arcpy.env.cellsize = data['pre']['traj']['path']
	arcpy.env.outputCoordinateSystem = data['pre']['traj']['path']	

	###set the extent for this SPECIFIC tile 
	arcpy.env.extent = arcpy.Extent(XMin, YMin, XMax, YMax)


	print 'rws==================================',rws
	print 'cls==================================',cls
	

	##create an empty matrix that will be filled with values using the code below if conditions are met
	outData = np.zeros((rws, cls), dtype=np.uint16)
	print 'outdata', outData
    
	### create numpy arrays for input datasets cdls and traj
	cdls = {
			2008:arcpy.RasterToNumPyArray(in_raster='C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2008', lower_left_corner = arcpy.Point(XMin,YMin), nrows = rws, ncols = cls),
			2009:arcpy.RasterToNumPyArray(in_raster='C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2009', lower_left_corner = arcpy.Point(XMin,YMin), nrows = rws, ncols = cls),
			2010:arcpy.RasterToNumPyArray(in_raster='C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2010', lower_left_corner = arcpy.Point(XMin,YMin), nrows = rws, ncols = cls),
			2011:arcpy.RasterToNumPyArray(in_raster='C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2011', lower_left_corner = arcpy.Point(XMin,YMin), nrows = rws, ncols = cls),
			2012:arcpy.RasterToNumPyArray(in_raster='C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2012', lower_left_corner = arcpy.Point(XMin,YMin), nrows = rws, ncols = cls),
			2013:arcpy.RasterToNumPyArray(in_raster='C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2013', lower_left_corner = arcpy.Point(XMin,YMin), nrows = rws, ncols = cls),
			2014:arcpy.RasterToNumPyArray(in_raster='C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2014', lower_left_corner = arcpy.Point(XMin,YMin), nrows = rws, ncols = cls),
			2015:arcpy.RasterToNumPyArray(in_raster='C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2015', lower_left_corner = arcpy.Point(XMin,YMin), nrows = rws, ncols = cls),
			2016:arcpy.RasterToNumPyArray(in_raster='C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2016', lower_left_corner = arcpy.Point(XMin,YMin), nrows = rws, ncols = cls),
			2017:arcpy.RasterToNumPyArray(in_raster='C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2017', lower_left_corner = arcpy.Point(XMin,YMin), nrows = rws, ncols = cls)
	       }

	###this is the trajectory that is referenced with the specific trajectory values we are looking for from the sql statement above
	arr_traj = arcpy.RasterToNumPyArray(in_raster=data['pre']['traj']['path'], lower_left_corner = arcpy.Point(XMin,YMin), nrows = rws, ncols = cls)

	#### find the location of each pixel labeled with specific arbitray value in the rows list  
	#### note the traj_list is derived from the sql query above
	for traj in traj_list:
		###
		traj_value = traj[0]
		mtr = traj[1]
		traj_array = traj[2]

		#Return the indices of the pixels that have values of the ytc arbitray values of the traj.
		indices = (arr_traj == traj_value).nonzero()

		#stack the indices variable above so easier to work with
		stacked_indices=np.column_stack((indices[0],indices[1]))
        
        #####  get the x and y location of each pixel that has been selected from above
		for pixel_location in stacked_indices:

			row = pixel_location[0] 
			col = pixel_location[1]


			### attach the cdl values to each binary trajectory at each pixel location
			entirelist = []
			for year in data['global']['years']:
				entirelist.append(cdls[year][row][col])

			### if 61 in the entirelist AND the entirelist does not start with 61 then process the entirelist otherwise skip processing of pixel
			if(61 in entirelist):

				# print 'entirelist---', entirelist
				current_index=entirelist.index(61)

				beforelist=np.array(entirelist[:current_index])
				# print 'beforelist---', beforelist

				afterlist=np.array(entirelist[current_index:])
				# print 'afterlist---', afterlist

				### remove the first for elements from traj_array
				traj_array_before = traj_array[:current_index]

				##make sure that there are at least two elements in the beforelist(i.e 2 elements before the first 61)-----length of 2 means yfc is 2010
				if(beforelist.size >= 2):
					
					####  Conditions  ################################################
					## only uniques elements -1 and 0 in numpy diff list to make sure change is only in one direction (ie 1 to 0)
					cond1 =  np.isin(np.diff(traj_array_before), [-1,0]).all()
					
					## make sure that the first two elements in beforelist are crop ----- this works with cond1
					cond2 = traj_array_before[0] == 1 and traj_array_before[1] == 1
					
					## make sure that afterlist contains only noncrop and 61
					cond3 = np.isin(afterlist, noncroplist + [61]).all()

					## make sure that the afterlist length is greater than 1
					cond4 = afterlist.size > 1

					if(cond1 and cond2 and cond3 and cond4):
	
						#####  label the pixel with conversion year  ##############################################
						outData[row,col] = data['global']['years'][getIndex(traj_array_before)]



    

	arcpy.ClearEnvironment("extent")

	outname = "tile_" + str(fc_count) +'.tif'

	outpath = os.path.join("C:/Users/Bougie/Desktop/Gibbs/data/", r"tiles", outname)

	myRaster = arcpy.NumPyArrayToRaster(outData, lower_left_corner=arcpy.Point(XMin, YMin), x_cell_size=30, y_cell_size=30, value_to_nodata=0)

	##free memory from outdata array
	outData = None

	myRaster.save(outpath)

	myRaster = None
예제 #7
0
def execute_task(in_extentDict):

    fc_count = in_extentDict[0]

    procExt = in_extentDict[1]
    # print procExt
    XMin = procExt[0]
    YMin = procExt[1]
    XMax = procExt[2]
    YMax = procExt[3]

    #set environments
    arcpy.env.snapRaster = data['pre']['traj']['path']
    arcpy.env.cellsize = data['pre']['traj']['path']
    arcpy.env.outputCoordinateSystem = data['pre']['traj']['path']
    arcpy.env.extent = arcpy.Extent(XMin, YMin, XMax, YMax)

    cls = 21973
    rws = 13789

    # outData = numpy.zeros((rows,cols), numpy.int16)
    outData = np.zeros((13789, 21973), dtype=np.int)

    ### create numpy arrays for input datasets nlcds and traj
    # arcpy.RasterToNumPyArray(in_raster='C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\binaries.gdb\\cdl30_b_2007_resampled', lower_left_corner = arcpy.Point(XMin,YMin), nrows = 13789, ncols = 21973),
    cdls = {
        2007:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\binaries.gdb\\cdl30_b_2007_resampled',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=13789,
            ncols=21973,
            nodata_to_value=255)
    }

    arr_traj = arcpy.RasterToNumPyArray(in_raster=data['pre']['traj']['path'],
                                        lower_left_corner=arcpy.Point(
                                            XMin, YMin),
                                        nrows=13789,
                                        ncols=21973)

    #### find the location of each pixel labeled with specific arbitray value in the rows list
    #### note the traj_list is derived from the sql query above
    for row in traj_list:
        #trajectory value
        traj = row[0]
        #conversion year ytc
        ytc = row[1]
        # print 'ytc', ytc

        yfc = row[2]
        # print 'yfc', yfc

        #Return the indices of the pixels that have values of the ytc arbitray values of the traj.
        indices = (arr_traj == row[0]).nonzero()

        #stack the indices variable above so easier to work with
        stacked_indices = np.column_stack((indices[0], indices[1]))

        #get the x and y location of each pixel that has been selected from above
        for pixel_location in stacked_indices:
            row = pixel_location[0]
            col = pixel_location[1]

            if ytc == 2009 and cdls[2007][row][col] == 1:
                # print cdls[2007][row][col]
                outData[row, col] = data['refine']['arbitrary_crop']

            elif yfc == 2009 and cdls[2007][row][col] == 0:
                # print cdls[2007][row][col]
                outData[row, col] = data['refine']['arbitrary_noncrop']

    arcpy.ClearEnvironment("extent")

    outname = "tile_" + str(fc_count) + '.tif'

    # #create
    outpath = os.path.join("C:/Users/Bougie/Desktop/Gibbs/", r"tiles", outname)

    # NumPyArrayToRaster (in_array, {lower_left_corner}, {x_cell_size}, {y_cell_size}, {value_to_nodata})
    myRaster = arcpy.NumPyArrayToRaster(outData,
                                        lower_left_corner=arcpy.Point(
                                            XMin, YMin),
                                        x_cell_size=30,
                                        y_cell_size=30,
                                        value_to_nodata=0)

    myRaster.save(outpath)
예제 #8
0
    raster_ref = result_table.getRow(0)[1:-1]

    arcpy.AddMessage("\nRaster reference is " + str(raster_ref))

    spatial = arcpy.Describe(raster_ref).spatialReference

    cell_size_x = str(
        arcpy.GetRasterProperties_management(raster_ref, "CELLSIZEX"))
    cell_size_y = str(
        arcpy.GetRasterProperties_management(raster_ref, "CELLSIZEY"))
    x_min = str(arcpy.GetRasterProperties_management(raster_ref, "LEFT"))
    y_min = str(arcpy.GetRasterProperties_management(raster_ref, "BOTTOM"))

    outRaster = arcpy.NumPyArrayToRaster(outGrid,
                                         x_cell_size=float(cell_size_x),
                                         y_cell_size=float(cell_size_y),
                                         lower_left_corner=arcpy.Point(
                                             x_min, y_min),
                                         value_to_nodata=0)

    arcpy.DefineProjection_management(outRaster, spatial)
    outRaster.save(output)

except Exception as e:
    # If unsuccessful, end gracefully by indicating why
    arcpy.AddError('\n' + "Script failed because: \t\t" + e.message)
    # ... and where
    exceptionreport = sys.exc_info()[2]
    fullermessage = traceback.format_tb(exceptionreport)[0]
    arcpy.AddError("at this location: \n\n" + fullermessage + "\n")
def long_time_task(FID):
    '''
    处理一个FID,也就是一个2度网格的城市阈值计算,提取,和镶嵌。
    '''

    # arcpy的临时输出文件
    # --- 这段代码保证,不同的arcpy进程使用不同的临时空间,在多进程的环境下避免一些错误
    newTempDir = u"./tmp/tmp" + time.strftime('%Y%m%d%H%M%S') + str(FID)
    os.makedirs(newTempDir)
    os.environ["TEMP"] = newTempDir
    os.environ["TMP"] = newTempDir
    # ---

    for row in arcpy.SearchCursor(config['fc'], '"FID" = {0}'.format(FID)):
        begin_time = time.time()

        grid_code = int(row.getValue("grid_id"))  # 10度网格号
        mask = row.getValue("Shape")  # 2度网格shape
        row_code = row.getValue("code")  # 2度网格号

        print '**** PID: ' + str(os.getpid()) + ' FID: ' + str(
            FID) + " grid_code: " + str(grid_code)

        target_area = row.getValue(config['target_count_field'])  # 目标城市数目字段
        threshold_max = config['threshold_max']
        threshold_min = config['threshold_min']
        threshold_step = config['threshold_step']

        nuaci_path = '{0}/{1}{2}.tif'.format(config['in_folder_path'],
                                             config['input_prefix'], grid_code)

        # if not arcpy.Exists(nuaci_path):
        #     # arcpy.AddError(nuaci_path + ' not found ')

        print '####input', nuaci_path

        in_raster = Raster(nuaci_path)
        in_raster_mask = ExtractByMask(in_raster, mask)
        del in_raster

        lower_left = arcpy.Point(in_raster_mask.extent.XMin,
                                 in_raster_mask.extent.YMin)
        cell_size = in_raster_mask.meanCellWidth
        dsc = arcpy.Describe(in_raster_mask)
        sr = dsc.SpatialReference
        no_data_value = in_raster_mask.noDataValue

        in_raster_arr = arcpy.RasterToNumPyArray(in_raster_mask,
                                                 nodata_to_value=no_data_value)
        del in_raster_mask

        actual_area = 0
        threshold_value = threshold_max
        in_raster_arr_bi = in_raster_arr > threshold_value

        while (actual_area < target_area) and (threshold_value >=
                                               threshold_min):
            in_raster_arr_bi = in_raster_arr > threshold_value
            actual_area = in_raster_arr_bi.sum() * 0.0009
            threshold_value -= threshold_step

        print('------------final area = ' + str(actual_area) +
              '---------------' + str(row_code))
        print('final threshold = ' +
              str(min([threshold_max, threshold_value + threshold_step])))

        in_raster_arr_dtype = in_raster_arr.dtype
        del in_raster_arr
        in_raster_arr_bi = in_raster_arr_bi.astype(in_raster_arr_dtype)
        new_raster = arcpy.NumPyArrayToRaster(in_raster_arr_bi, lower_left,
                                              cell_size, cell_size,
                                              no_data_value)
        del in_raster_arr_bi

        arcpy.DefineProjection_management(new_raster, sr)
        out_raster_path = '{0}/{1}{2}.tif'.format(config['out_folder_path'],
                                                  config['output_prefix'],
                                                  grid_code)
        arcpy.Mosaic_management(new_raster, out_raster_path, "LAST", "FIRST",
                                "", "", "", "", "")
        del new_raster
        print 'Task %d - %d - %d runs %0.2f seconds.' % (
            os.getpid(), FID, grid_code, (time.time() - begin_time))
예제 #10
0
def connect_wet_segments(valley_raster, water_ele_raster, wet_raster, flowdir_raster, ele_raster, \
                         width_raster, order_raster, connected_wet_raster, max_gap, pix_per_m):
    corner = arcpy.Point(arcpy.Describe(valley_raster).Extent.XMin, arcpy.Describe(valley_raster).Extent.YMin)
    dx = arcpy.Describe(valley_raster).meanCellWidth

    valley = arcpy.RasterToNumPyArray(valley_raster, nodata_to_value=0).astype(np.uint8)
    dep_ele = arcpy.RasterToNumPyArray(water_ele_raster, nodata_to_value=0)
    wet = arcpy.RasterToNumPyArray(wet_raster, nodata_to_value=0).astype(np.uint8)
    ele = arcpy.RasterToNumPyArray(ele_raster, nodata_to_value=0)
    width = arcpy.RasterToNumPyArray(width_raster, nodata_to_value=0)
    width = np.where(wet == 1, width, 0)
    dep_ele = np.where(wet == 1, dep_ele, 0)

    Lab_wet, num_label_wet = ndimage.label(wet, structure=np.ones((3, 3)))

    # find dry and wet valley
    tran = disconnect_valley_segments(valley_raster, flowdir_raster, 3, order_raster)

    dry_valley = np.where((valley == 1) & (wet == 0), 1, 0).astype(np.uint8)
    wet_valley = np.where((valley == 1) & (wet == 1), 1, 0).astype(np.uint8)

    temp_dry_valley = ndimage.binary_dilation(dry_valley, iterations=1, structure=np.ones((3, 3))).astype(np.uint8)
    dry_valley = np.where(((wet_valley == 1) & (temp_dry_valley == 1)) | (dry_valley == 1), 1, 0).astype(np.uint8)
    dry_valley = np.where(tran == 1, 0, dry_valley).astype(np.uint8)
    del temp_dry_valley

    Lab_dry, num_label_dry = ndimage.label(dry_valley, structure=np.ones((3, 3)))
    labels = np.arange(1, num_label_dry + 1)

    # 1. connection of dry segments
    num_conn_label = ndimage.labeled_comprehension(Lab_wet, Lab_dry, labels, count_unique, int, 0)
    num_conn = np.zeros_like(wet)
    num_conn = num_conn_label[Lab_dry - 1]
    num_conn = np.where(Lab_dry == 0, 0, num_conn)
    conn_dry_valley = np.where(num_conn >= 3, 1, 0)
    del num_conn, num_conn_label

    # 2. elevation of dry segments
    Lab_dry, num_label_dry = ndimage.label(conn_dry_valley, structure=np.ones((3, 3)))
    labels = np.arange(1, num_label_dry + 1)
    ave_ele_label = ndimage.labeled_comprehension(dep_ele, Lab_dry, labels, np.max, float, 0)
    max_ele_label = ndimage.labeled_comprehension(ele, Lab_dry, labels, np.max, float, 0)
    diff_ele = np.zeros_like(ele)
    diff_ele = ave_ele_label[Lab_dry - 1] - max_ele_label[Lab_dry - 1]
    diff_ele = np.where(Lab_dry == 0, 0, diff_ele)
    conn_dry_valley = np.where(diff_ele < 0, 0, conn_dry_valley)

    del diff_ele, max_ele_label, ave_ele_label

    # 3. length of dry segment
    Lab_dry, num_label_dry = ndimage.label(conn_dry_valley, structure=np.ones((3, 3)))
    labels = np.arange(1, num_label_dry + 1)
    area_gap_label = ndimage.labeled_comprehension(conn_dry_valley, Lab_dry, labels, np.sum, float, 0) * pix_per_m ** 2
    area_dry = np.zeros_like(ele)
    area_dry = area_gap_label[Lab_dry - 1]
    area_dry = np.where((Lab_dry == 0), 0, area_dry)
    conn_dry_valley = np.where(area_dry > max_gap, 0, conn_dry_valley)
    del area_dry, ele

    # find width of added segments
    Lab_dry, num_label_dry = ndimage.label(conn_dry_valley, structure=np.ones((3, 3)))
    labels = np.arange(1, num_label_dry + 1)
    width_gap_label = ndimage.labeled_comprehension(width, Lab_dry, labels, np.sum, float, 0)
    num_conn_label = ndimage.labeled_comprehension(Lab_wet, Lab_dry, labels, count_unique, int, 0)
    width_conn = np.zeros_like(width)
    width_conn = (width_gap_label[Lab_dry - 1] / num_conn_label[Lab_dry - 1]).astype(np.float32)
    width_conn = np.where(Lab_dry == 0, 0, width_conn)

    add_wet = add_width(width_conn)
    wet = np.where((add_wet == 1) | (wet == 1), 1, 0)

    arcpy.NumPyArrayToRaster(wet, corner, dx, dx).save(connected_wet_raster)
예제 #11
0
def execute_task(args):
    in_extentDict, data, nc_list = args

    fc_count = in_extentDict[0]

    procExt = in_extentDict[1]
    # print procExt
    XMin = procExt[0]
    YMin = procExt[1]
    XMax = procExt[2]
    YMax = procExt[3]

    #set environments
    #The brilliant thing here is that using the extents with the full dataset!!!!!!   DONT EVEN NEED TO CLIP THE FULL RASTER TO THE FISHNET BECASUE
    arcpy.env.snapRaster = data['pre']['traj']['path']
    arcpy.env.cellsize = data['pre']['traj']['path']
    arcpy.env.outputCoordinateSystem = data['pre']['traj']['path']
    arcpy.env.extent = arcpy.Extent(XMin, YMin, XMax, YMax)

    cls = 21973
    rws = 13789

    # outData = numpy.zeros((rows,cols), numpy.int16)
    outData = np.zeros((13789, 21973), dtype=np.int)

    ### create numpy arrays for input datasets cdls and traj
    cdls = {
        2008:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2008',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=13789,
            ncols=21973),
        2009:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2009',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=13789,
            ncols=21973),
        2010:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2010',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=13789,
            ncols=21973),
        2011:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2011',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=13789,
            ncols=21973),
        2012:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2012',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=13789,
            ncols=21973),
        2013:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2013',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=13789,
            ncols=21973),
        2014:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2014',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=13789,
            ncols=21973),
        2015:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2015',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=13789,
            ncols=21973),
        2016:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2016',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=13789,
            ncols=21973),
        2017:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2017',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=13789,
            ncols=21973)
    }

    arr_traj = arcpy.RasterToNumPyArray(in_raster=data['pre']['traj']['path'],
                                        lower_left_corner=arcpy.Point(
                                            XMin, YMin),
                                        nrows=13789,
                                        ncols=21973)

    # find the location of each pixel labeled with specific arbitray value in the rows list
    for row in createReclassifyList(data):
        #year of conversion for either expansion or abandonment
        fc = data['global']['years_conv']
        print 'fc', fc

        #year before conversion for either expansion or abandonment
        bfc = data['global']['years_conv'] - 1
        print 'bfc', bfc

        #Return the indices of the pixels that have values of the fc arbitrsy values of the traj.
        indices = (arr_traj == row[0]).nonzero()

        #stack indices so easier to work with
        stacked_indices = np.column_stack((indices[0], indices[1]))

        #get the x and y location of each pixel that has been selected from above
        for pixel_location in stacked_indices:
            row = pixel_location[0]
            col = pixel_location[1]

            #get the pixel value for fc
            pixel_value_fc = cdls[fc][row][col]
            #get the pixel value for bfc
            pixel_value_bfc = cdls[bfc][row][col]

            #####  create dev mask  ##################################################################################
            if pixel_value_bfc in [122, 123, 124]:
                outData[row, col] = data['refine']['arbitrary_noncrop']

    #####  create 36_61 mask  ################################################################################
            if pixel_value_fc in [36, 61]:
                #find the years still left in the time series for this pixel location
                yearsleft = [i for i in data['global']['years'] if i > fc]
                #create templist to hold the rest of the cld values for the time series.  initiaite it with the first cdl value
                templist = [pixel_value_fc]
                for year in yearsleft:
                    templist.append(cdls[year][row][col])

                #if the templist values are esentailly all "noncrop" then realbel as noncrop
                if len(set(np.isin(templist, nc_list))) == 1:
                    outData[row, col] = data['refine']['arbitrary_noncrop']

    arcpy.ClearEnvironment("extent")

    outname = "tile_" + str(fc_count) + '.tif'

    # #create
    outpath = os.path.join("C:/Users/Bougie/Desktop/Gibbs/", r"tiles", outname)

    # NumPyArrayToRaster (in_array, {lower_left_corner}, {x_cell_size}, {y_cell_size}, {value_to_nodata})
    myRaster = arcpy.NumPyArrayToRaster(outData,
                                        lower_left_corner=arcpy.Point(
                                            XMin, YMin),
                                        x_cell_size=30,
                                        y_cell_size=30,
                                        value_to_nodata=0)

    myRaster.save(outpath)
예제 #12
0
def depth_cal(wet_raster, width_raster, ele_raster, pix_per_m, min_slope, max_slope, min_pos_slope,
              wet_pos_depth_raster, water_ele_raster):
    corner = arcpy.Point(arcpy.Describe(wet_raster).Extent.XMin, arcpy.Describe(wet_raster).Extent.YMin)
    dx = arcpy.Describe(wet_raster).meanCellWidth

    org_wet = arcpy.RasterToNumPyArray(wet_raster, nodata_to_value=0)
    wet = ndimage.morphology.binary_fill_holes(org_wet).astype(np.uint8)
    ele = arcpy.RasterToNumPyArray(ele_raster, nodata_to_value=0)

    common_border = find_sep_border(org_wet, 7, corner, dx)
    wet = ndimage.binary_dilation(org_wet, iterations=2).astype(np.uint8)
    wet = np.where(common_border == 1, 0, wet)
    common_border = find_sep_border(wet, 7, corner, dx)
    wet = ndimage.morphology.binary_fill_holes(wet).astype(np.uint8)
    wet = np.where(common_border == 1, 0, wet)
    wet = np.where(org_wet == 1, 1, wet)

    er_wet_1 = ndimage.binary_erosion(wet).astype(np.uint8)
    bound_1 = np.where((wet == 1) & (er_wet_1 == 0), 1, 0).astype(np.uint8)
    ele_1 = np.where(bound_1 == 1, ele, 0)
    temp_ele_1 = arcpy.NumPyArrayToRaster(ele_1, corner, dx, dx, value_to_nodata=0)
    ele_1_thick = FocalStatistics(temp_ele_1, NbrRectangle(3, 3, "CELL"), "MEAN", "DATA")

    del ele_1

    er_wet_2 = ndimage.binary_erosion(er_wet_1).astype(np.uint8)
    bound_2 = np.where((er_wet_1 == 1) & (er_wet_2 == 0), 1, 0).astype(np.uint8)
    ele_2 = np.where(bound_2 == 1, ele, 0)

    ele_1 = arcpy.RasterToNumPyArray(ele_1_thick, nodata_to_value=0)
    ele_1 = np.where(bound_2 == 0, 0, ele_1)

    slope = (ele_1 - ele_2) / pix_per_m
    slope_sign = np.where(slope > 0, 1, 0)

    Lab_wet, num_label = ndimage.label(wet, structure=np.ones((3, 3)))
    labels = np.arange(1, num_label + 1)
    slope_label = ndimage.labeled_comprehension(slope, Lab_wet, labels, np.sum, float, 0)
    slope_sign_label = ndimage.labeled_comprehension(slope_sign, Lab_wet, labels, np.sum, float, 0)
    count_2 = ndimage.labeled_comprehension(bound_2, Lab_wet, labels, np.sum, float, 0)
    water_ele_label = ndimage.labeled_comprehension(ele, Lab_wet, labels, np.min, float, 0)

    slope_seg = np.zeros_like(org_wet).astype(np.float32)
    slope_seg = slope_label[Lab_wet - 1].astype(np.float32) / count_2[Lab_wet - 1].astype(np.float32)
    slope_seg = np.where((Lab_wet == 0) | (org_wet == 0), 0, slope_seg)

    new_wet = np.where((slope_seg > min_slope) & (slope_seg < max_slope), 1, 0).astype(np.uint8)

    width = arcpy.RasterToNumPyArray(width_raster, nodata_to_value=0)
    depth = slope_seg * width / 2

    del slope_seg, slope_label, width

    slope_sign_seg = np.zeros_like(org_wet).astype(np.float32)
    slope_sign_seg = slope_sign_label[Lab_wet - 1].astype(np.float32) / count_2[Lab_wet - 1].astype(np.float32)
    slope_sign_seg = np.where((Lab_wet == 0) | (org_wet == 0), 0, slope_sign_seg)

    new_wet = np.where((slope_sign_seg <= min_pos_slope), 0, new_wet).astype(np.uint8)
    del slope_sign_seg, slope_sign_label, count_2

    arcpy.NumPyArrayToRaster(new_wet, corner, dx, dx, value_to_nodata=0).save(wet_pos_depth_raster)

    water_ele = water_ele_label[Lab_wet - 1].astype(np.float32) + depth
    water_ele = np.where((Lab_wet == 0) | (new_wet == 0), 0, water_ele)
    arcpy.NumPyArrayToRaster(water_ele, corner, dx, dx, value_to_nodata=0).save(water_ele_raster)

    del new_wet, water_ele, depth
예제 #13
0
    def allocate(self,random_effect = 0.5,error_num = 10,max_interation = 100):
        nodataValue = arcpy.Raster(self.land_map).noDataValue
        x0 = float(arcpy.GetRasterProperties_management(self.land_map,'LEFT').getOutput(0))
        y0 = float(arcpy.GetRasterProperties_management(self.land_map,'BOTTOM').getOutput(0))
        size_land = float(arcpy.GetRasterProperties_management(self.land_map,'CELLSIZEX').getOutput(0))

        

        land_array = arcpy.RasterToNumPyArray(self.land_map)
        land_index = list(np.unique(land_array))
        try:
            land_index.remove(nodataValue)
        except:
            None
##        print land_index
## add prt_zone to probabilities
        for i in range(len(land_index)):
            try:
                self.probabilities[i] = arcpy.sa.Con(self.prt_zone,arcpy.sa.Con(arcpy.sa.EqualTo(self.land_map,land_index[i]),1,0),self.probabilities[i])
            except:
                None
##            
        or_area = []
        for a in range(len(land_index)):
            area = np.sum(land_array==land_index[a])
            or_area.append(area)
            self.matrix[a] = self.matrix[a]*area

        print self.matrix
        arcpy.AddMessage('allocate matrix:')
        arcpy.AddMessage(self.matrix)
##        print or_area,self.matrix
        
            
        probability_array = [arcpy.RasterToNumPyArray(x) for x in self.probabilities]
### add random factor
        for pp in probability_array:
            for i in range(len(pp)):
                for j in range(len(pp[i])):
                    pp[i,j] = (pp[i,j]+random_effect*random.random())/(1+random_effect)
## start processing            
        out_array = np.int8(np.zeros((len(land_array),len(land_array[0]))))


##        print trans_area,or_area,trans_array
        
## iteration for every categories
        for i in range(len(land_index)):
            land_index1 = [x for x in land_index]
            land_index1.remove(land_index[i])
            land_index1 = [land_index[i]]+land_index1
            for j in range(len(land_index1)-1):
                print i,j,self.matrix[i,j]
                list_error,list_break = [],[]
                break_value = 0.5
                interation_num = 1
                while True:
                    list_break.append(break_value)
                    num = np.sum(((land_array==land_index[i])*(out_array==0)*(probability_array[j]>break_value)))
                    error = (num-self.matrix[i,j])
                    list_error.append(error)
                    if interation_num > max_interation:
                        plt.plot(list_error,label = 'iteration error')
                        plt.plot(list_break,label = 'break_value')
                        plt.legend()
                        plt.title('allocation {0} to {1}, error ={2}'.format(land_index[i],land_index1[j],error))
                        print "unsuccesfully allocate category {0} to {1} with error_rate of {2}, pleast close the figure to continue".format(land_index[i],land_index1[j],error)
                        out_array = out_array+(((land_array==land_index[i])*(out_array==0)*(probability_array[j]>break_value))*land_index[j])
                        plt.savefig(os.path.join(self.path,str(land_index[i])+'to'+str(land_index1[j])))
                        plt.close()
                        break
                    try:
                        if error > error_num:
                            interation_num += 1
                            print break_value,num,self.matrix[i,j]
                            break_value += 1.0/(2**interation_num)                                                                        
                        elif error < (-error_num):
                            interation_num += 1
                            print break_value,num,self.matrix[i,j]
                            break_value -= 1.0/(2**interation_num)                        
                        else:
                            plt.plot(list_error,label = 'iteration error')
                            plt.plot(list_break,label = 'break_value')
                            plt.legend()
                            plt.title('allocation {0} to {1}, error ={2}'.format(land_index[i],land_index1[j],error))
                            print "succesfully allocate category {0} to {1} with error_rate of {2}, pleast close the figure to continue".format(land_index[i],land_index1[j],error)
                            plt.savefig(os.path.join(self.path,str(land_index[i])+'to'+str(land_index1[j])))
                            out_array = out_array+(((land_array==land_index[i])*(out_array==0)*(probability_array[j]>break_value))*land_index[j])
                            plt.close()
                            break
                    except:
                        plt.plot(list_error,label = 'iteration error')
                        plt.plot(list_break,label = 'break_value')
                        plt.legend()
                        plt.title('allocation {0} to {1}, error ={2}'.format(land_index[i],land_index1[j],error))
                        print "unsuccesfully allocate category {0} to {1} with error_rate of {2}, pleast close the figure to continue".format(land_index[i],land_index1[j],error)
                        out_array = out_array+(((land_array==land_index[i])*(out_array==0)*(probability_array[j]>break_value))*land_index[j])
                        plt.savefig(os.path.join(self.path,str(land_index[i])+'to'+str(land_index1[j])))
                        plt.close()
                        break
            j += 1
            num = np.sum(((land_array==land_index[i])*(out_array==0)*(probability_array[j]>0)))
            error = (num-self.matrix[i,j])/np.sum(self.matrix[i])
            print "succesfully allocate category {0} to {1} with error_rate of {2}".format(land_index[i],land_index1[j],error)
            out_array = out_array+(((land_array==land_index[i])*(out_array==0)*(probability_array[j]>0))*land_index[j])

        out_raster = arcpy.NumPyArrayToRaster(out_array,arcpy.Point(x0,y0),size_land,size_land,nodataValue)
        out_raster.save(self.simulate_map)
        arcpy.DefineProjection_management(self.simulate_map,self.land_map)
        del probability_array[:]
        return self.simulate_map
예제 #14
0
    print 'done creating an array of the shapefiles'
    print 'converting to rasters'
    rasters=[]
    for x in range(len(shapefiles)):
        print 'converting',shapefiles[x]
        raster=arcpy.PolygonToRaster_conversion(shapefiles[x], 'value', 'storm'+str(stormfolder)+'/raster'+str(x), 'CELL_CENTER', 'NONE',0.00012196015)
        rasters.append(raster)
    print 'completed raster conversion'
    print 'calculating cell statistics'
    maxreflect=CellStatistics (rasters, 'MAXIMUM', 'DATA')
    maxreflect.save('storm'+str(stormfolder)+'/reflect'+str(stormfolder)+'.tif')
    lowerLeft = arcpy.Point(maxreflect.extent.XMin,maxreflect.extent.YMin)
    cellSize = maxreflect.meanCellWidth
    reflectence=arcpy.RasterToNumPyArray(maxreflect)
    rows=len(reflectence)
    cols=len(reflectence[0])
    rainfallraster=numpy.zeros((rows,cols))
    for row in range(rows):
        for col in range(cols):
            if reflectence[row][col]<0:
                rainfallraster[row][col]=0
            rainfallraster[row][col]=(reflectence[row][col]/300)**(1/1.4)
    where_are_NaNs = numpy.isnan(rainfallraster)
    rainfallraster[where_are_NaNs]=0
    newraster=arcpy.NumPyArrayToRaster(rainfallraster,lowerLeft,cellSize)
    newraster.save('storm'+str(stormfolder)+'/rainfall'+str(stormfolder)+'.tif')
    stormfolder=stormfolder+1
    print 'completed rainfall calc'
    print 'complete with folder',stormfolder
print 'finished making max reflectance rasters'
예제 #15
0
def tail():
    tupDateNow = datetime.now()
    while(1):
        # buka file csv untuk mengetahui scene yang telah selesai diproses
        #arcpy.env.workspace = config.gdbPath

        log = pd.read_csv("logComplete.csv")
        liScene = log["scene"].tolist()
        liDate = log["dateComplete"].tolist()

        msg = str(datetime.now()) + '\t' + "Importing Library ... \n"
        redis.rpush(config.MESSAGES_KEY, msg)
        redis.publish(config.CHANNEL_NAME, msg)
        
        arcpy.CheckOutExtension("spatial")
        # pass list yang telah selesai ke ftp download
        filenameNow, scene, boolScene, year, month = ft.downloadFile(liScene)

        del log
        del liScene
        del liDate

        if(boolScene == False):
            print "Data hari ini selesai diproses"
            tupDateLoop = datetime.now()
            while (tupDateNow.day == tupDateLoop.day):
                print "menunggu hari berganti :)"
                time.sleep(10)
                tupDateLoop = datetime.now()
                
            tupDateNow = tupDateLoop
            print "hari telah berganti"

        #definisikan nama file yang akan diproses
        filename = filenameNow
        # definisikan nama file keluaran hasil klasifikasi yang masih mentah
        filenameOut = filenameNow + "_classified.TIF"
        # definisikan letak file ers yang telah didownload dalam workstation
        dataPath =  config.dataPath + scene + "/" + filename
        # definisikan letak model .pkl hasil training data sampel
        modelPath = config.modelPath
        # definisikan shp file indonesia untuk cropping batas administrasi
        shpPath = config.shpPath

        # definisikan folder keluaran hasil proses
        outFolder = config.outputPath + filename.split(".")[0]
        # jika folder ada maka hapus 
        if(os.path.exists(outFolder)):
            shutil.rmtree(outFolder)
        # buat folder yang telah didefinisikan
        os.makedirs(outFolder)
        # definisikan path file keluaran
        outputPath = outFolder + "/" + filenameOut

        ##################### KONVERSI DATA ERS KE TIAP BAND ######################################
        print ("converting b3")
        if(os.path.exists(dataPath + "TOA_B3" + ".TIF")):
            os.remove(dataPath + "TOA_B3" + ".TIF")
        # Ambil hanya band 3 dan jadikan raster
        try:
            b_green = arcpy.Raster( dataPath  + "/B3" ) * 1.0
        except :
            b_green = arcpy.Raster( dataPath  + "/Band_3" ) * 1.0
        
        print ("saving b3")
        msg = str(datetime.now()) + '\t' + "saving b3 \n"
        redis.rpush(config.MESSAGES_KEY, msg)
        redis.publish(config.CHANNEL_NAME, msg)
        # save raster band 3 ke folder data input
        b_green.save(dataPath + "TOA_B3" + ".TIF" )
        del b_green

        print ("converting b5")
        if(os.path.exists(dataPath + "TOA_B5" + ".TIF")):
            os.remove(dataPath + "TOA_B5" + ".TIF")
        # Ambil hanya band 5 dan jadikan raster
        try:
            b_nir = arcpy.Raster( dataPath  + "/B5" ) * 1.0
        except:
            b_nir = arcpy.Raster( dataPath  + "/Band_5" ) * 1.0
        
        print ("saving b5")
        msg = str(datetime.now()) + '\t' + "saving b5 \n"
        redis.rpush(config.MESSAGES_KEY, msg)
        redis.publish(config.CHANNEL_NAME, msg)
        # save raster band 5 ke folder data input
        b_nir.save( dataPath +  "TOA_B5" + ".TIF" )
        del b_nir

        print ("converting b6")
        if(os.path.exists(dataPath + "TOA_B6" + ".TIF")):
           os.remove(dataPath + "TOA_B6" + ".TIF")
        # Ambil hanya band 6 dan jadikan raster
        try:
            b_swir1 = arcpy.Raster( dataPath + "/B6") * 1.0
        except:
            b_swir1 = arcpy.Raster( dataPath + "/Band_6") * 1.0

        msg = str(datetime.now()) + '\t' + "saving b6 \n"
        redis.rpush(config.MESSAGES_KEY, msg)
        redis.publish(config.CHANNEL_NAME, msg)
        print ("saving b6")
        # save raster band 6 ke folder data input
        b_swir1.save( dataPath + "TOA_B6" + ".TIF" )
        del b_swir1

        ####################### SELESAI KONVERSI DATA #######################################
        
        #################### UBAH RASTER KE FORMAT DATAFRAME ###############################
        msg = str(datetime.now()) + '\t' + "Processing file "+filename+"\n"
        redis.rpush(config.MESSAGES_KEY, msg)
        redis.publish(config.CHANNEL_NAME, msg)

        # load semua raster yang telah dikonversi diawal
        rasterarrayband6 = arcpy.RasterToNumPyArray(dataPath + "TOA_B3.TIF")
        rasterarrayband6 = np.array(rasterarrayband6, dtype=np.uint32)  
        rasterarrayband5 = arcpy.RasterToNumPyArray(dataPath + "TOA_B5.TIF")
        rasterarrayband5 = np.array(rasterarrayband5, dtype=np.uint32)
        rasterarrayband3 = arcpy.RasterToNumPyArray(dataPath + "TOA_B6.TIF")
        rasterarrayband3 = np.array(rasterarrayband3, dtype=np.uint32)

        print rasterarrayband6.dtype
        print("Change raster format to numpy array")
        # gabungkan 3 array data secara horizontal
        data = np.array([rasterarrayband6.ravel(), rasterarrayband5.ravel(), rasterarrayband3.ravel()], dtype=np.int16)
        # ubah menjadi vertikal untuk kebutuhan prediksi .pkl
        data = data.transpose()

        # langsung hapus variabel yang tidak digunakan lagi
        del rasterarrayband6
        del rasterarrayband5
        del rasterarrayband3

        print("Change to dataframe format")
        msg = str(datetime.now()) + '\t' + "Change to dataframe format \n"
        redis.rpush(config.MESSAGES_KEY, msg)
        redis.publish(config.CHANNEL_NAME, msg)
        #time.sleep(1)

        # definisikan nama kolom dataframe
        columns = ['band3','band5', 'band6']
        # ubah array vertical menjadi dataframe
        df = pd.DataFrame(data, columns=columns)
        # hapus array vertikal
        del data
        ###################### SELESAI ####################################################
        print("Split data to 20 chunks ")
        msg = str(datetime.now()) + '\t' + "Split data to 20 chunks \n"
        redis.rpush(config.MESSAGES_KEY, msg)
        redis.publish(config.CHANNEL_NAME, msg)
        #time.sleep(1)

        # bagi data menjadi 20 bagian karena program tidak kuat prediksi sekaligus
        df_arr = np.array_split(df, 20)
        # hapus dataframe
        del df
        # load classifier (model pkl) yang telah di train
        clf = joblib.load(modelPath) 

        # definisikan array untuk menampung nilai integer hasil prediksi
        kelasAll = []
        # ulangi untuk setiap bagian data
        for i in range(len(df_arr)):
            
            print ("predicting data chunk-%s\n" % i)
            msg = str(datetime.now()) + '\t' + "predicting data chunk-%s\n" % i
            redis.rpush(config.MESSAGES_KEY, msg)
            redis.publish(config.CHANNEL_NAME, msg)

            msg2 = i
            redis.rpush(config.MESSAGES_KEY_2, msg2)
            redis.publish(config.CHANNEL_NAME_2, msg2)
            #time.sleep(1)
            # fungi untuk prediksi data baru dengan data ke i
            kelas = clf.predict(df_arr[i])

            # buat dataframe kosong
            dat = pd.DataFrame()
            # masukkan hasil prediksi data ke i ke kolom kelas
            dat['kel'] = kelas
            print ("mapping to integer class")
            msg = str(datetime.now()) + '\t' + "mapping to integer class \n"
            redis.rpush(config.MESSAGES_KEY, msg)
            redis.publish(config.CHANNEL_NAME, msg)
            #time.sleep(1)
            # definisikan dictionary untuk ubah string kelas ke integer kelas prediksi
            mymap = {'awan':1, 'air':2, 'tanah':3, 'vegetasi':4}
            # fungsi map dengan parameter dictionary
            dat['kel'] = dat['kel'].map(mymap)

            # ubah kolom dataframe ke array 
            band1Array = dat['kel'].values
            # ubah array ke numpy array dengan tipe unsigned 8 untuk menghindari memory error
            band1Array = np.array(band1Array, dtype = np.uint8)
            print ("extend to list")
            msg = str(datetime.now()) + '\t' + "extend to list \n"
            redis.rpush(config.MESSAGES_KEY, msg)
            redis.publish(config.CHANNEL_NAME, msg)
            #time.sleep(1)
            #kelasAllZeros[] = band1Array
            # masukkan numpy aray ke list prediksi
            kelasAll.extend(band1Array.tolist())
            # mencoba cek array hasil prediksi
            print(kelasAll[1:10])
            
        # hapus semua variabel yang tidak digunakan lagi
        del df_arr
        del clf
        del kelas
        del dat
        del band1Array

        print ("change list to np array")
        msg = str(datetime.now()) + '\t' + "change list to np array \n"
        redis.rpush(config.MESSAGES_KEY, msg)
        redis.publish(config.CHANNEL_NAME, msg)

        # ubah list prediksi ke numpy array
        kelasAllArray = np.array(kelasAll, dtype=np.uint8)
        # hapus list prediksi
        del kelasAll
        print ("reshaping np array")
        msg = str(datetime.now()) + '\t' + "reshaping np array \n"
        redis.rpush(config.MESSAGES_KEY, msg)
        redis.publish(config.CHANNEL_NAME, msg)

        rasterarrayband6 = arcpy.RasterToNumPyArray(dataPath + "TOA_B3.TIF")
        # reshape numpy array 1 dimensi ke dua dimensi sesuai format raster
        band1 = np.reshape(kelasAllArray, (-1, rasterarrayband6[0].size))
        # ubah tipe data ke unsigned integer
        band1 = band1.astype(np.uint8)
        del rasterarrayband6

        # load raster band6 untuk kebutuhan projeksi dan batas batas raster
        raster = arcpy.Raster(dataPath + "TOA_B6.TIF")
        inputRaster = dataPath + "TOA_B6.TIF"

        # ambil referensi spatial
        spatialref = arcpy.Describe(inputRaster).spatialReference
        # ambil tinggi dan lebar raster
        cellsize1  = raster.meanCellHeight
        cellsize2  = raster.meanCellWidth
        # definisikan extent dari raster dan point dari extent
        extent     = arcpy.Describe(inputRaster).Extent
        pnt        = arcpy.Point(extent.XMin,extent.YMin)

        # hapus yang tidak dipakai lagi
        del raster
        del kelasAllArray

        # save the raster
        print ("numpy array to raster ..")
        msg = str(datetime.now()) + '\t' + "numpy array to raster .. \n"
        redis.rpush(config.MESSAGES_KEY, msg)
        redis.publish(config.CHANNEL_NAME, msg)

        # ubah numpy array ke raster dengan atribut yang telah didefinisikan
        out_ras = arcpy.NumPyArrayToRaster(band1, pnt, cellsize1, cellsize2)

        arcpy.CheckOutExtension("Spatial")
        print ("define projection ..")
        msg = str(datetime.now()) + '\t' + "define projection ..\n"
        redis.rpush(config.MESSAGES_KEY, msg)
        redis.publish(config.CHANNEL_NAME, msg)

        # simpan raster hasil konversi ke path yang telah didefinisikan
        arcpy.CopyRaster_management(out_ras, outputPath)
        # definisikan projeksi dengan referensi spatial
        arcpy.DefineProjection_management(outputPath, spatialref)
                
        print ("Majority Filter..")
        msg = str(datetime.now()) + '\t' + "majority filter..\n"
        redis.rpush(config.MESSAGES_KEY, msg)
        redis.publish(config.CHANNEL_NAME, msg)
        
        #overwriteoutputPath outputPath enable  
        arcpy.env.workspace = config.outputPath     
        arcpy.env.overwriteOutput = True        
        
        #majority filter
        arcpy.CheckOutExtension("Spatial")
        outMajFilt = MajorityFilter(outputPath, "FOUR", "MAJORITY")

        #Save the output 
        outMajFilt.save(outputPath)

        # hapus yang tidak digunakan lagi
        del out_ras
        del band1
        del spatialref
        del cellsize1
        del cellsize2
        del extent
        del pnt


        ########################### MASKING CLOUD AND BORDER #########################
        print("Masking Cloud")
        # load file cm hasil download yang disediakan
        mask = Raster(os.path.dirname(dataPath) + "/" + filename.split(".")[0] + "_cm.ers")
        # load raster hasil klasifikasi mentah 
        inRas = Raster(outputPath)
        # jika file cm bernilai 1 = cloud, 2 = shadow, 11 = border
        # ubah nilai tersebut menjadi 1 dan lainnya menjadi 0
        #inRas_mask = Con((mask == 1), 1, Con((mask == 2), 1, Con((mask == 11), 1, 0)))
        inRas_mask = Con((mask == 1), 1, Con((mask == 2), 1, Con((mask == 11), 1, Con((mask == 3), 1, Con((mask == 4), 1, Con((mask == 5), 1, Con((mask == 6), 1, Con((mask == 7), 1, 0))))))))

        # buat raster yang merupakan nilai no data dari hasil kondisi diatas, hasilnya nodata = 1
        # saya juga tidak mengerti yang bukan cloud jadi no data
        mask2 = IsNull(inRas_mask)
        # jika raster bernilai 1 maka ubah jadi 0, jika tidak tetap nilai raster hasil kondisi
        inRas2 = Con((mask2 == 1), 0, inRas_mask)
        # simpan raster pure dimana semua nilai 1 akan dihilangkan dari hasil klasifikasi
        inRas2.save(os.path.dirname(outputPath) + "/" + filenameOut.split(".")[0] + "_mask.TIF")
        # jika raster bernilai 1 maka jadi no data, jika tidak maka tetap si raster hasil awal
        inRas_mask2 = SetNull(inRas2 == 1, inRas)
        # simpan raster yang telah bersih dari cloud dan border yang jelek
        inRas_mask2.save(os.path.dirname(outputPath) + "/" + filenameOut.split(".")[0] + "_maskCloud.TIF")

        # hapus variabel conditional yang tidak digunakan lagi
        del mask
        del mask2
        del inRas
        del inRas2
        del inRas_mask
        del inRas_mask2
        ############################## SELESAI ###########################################

        ####################### MASKING DENGAN SHP INDONESIA ##############################
        print("Masking with shp indonesia")
        arcpy.CheckOutExtension("Spatial")
        # buka file shp indonesia
        inMaskData = os.path.join(shpPath, "INDONESIA_PROP.shp")
        # buka raster hasil masking cloud dan border
        inRasData = Raster(os.path.dirname(outputPath) + "/" + filenameOut.split(".")[0] + "_maskCloud.TIF")
        # terapkan fungsi masking dengan shapefile
        try:
            outExtractByMask = ExtractByMask(inRasData, inMaskData)
            print("Saving in: " + str(os.path.dirname(outputPath) + "/" + filenameOut.split(".")[0] + "_maskShp.TIF"))
            # simpan hasil masking
            
            outExtractByMask.save(os.path.dirname(outputPath) + "/" + filenameOut.split(".")[0] + "_maskShp.TIF")
            finalPath = config.finalOutputPath + year + "/" + month + "/" + filenameNow.split(".")[0]
            print finalPath
            if( os.path.exists(finalPath) ):
                shutil.rmtree(finalPath)
            os.makedirs(finalPath)
            arcpy.CopyRaster_management( outExtractByMask, finalPath + "/" + filenameOut)
            # hapus lagi dan lagi variabel yang tidak digunakan
            del inMaskData
            del inRasData
            del outExtractByMask        
        except:
            print "diluar indonesia shp"
            finalPath = config.finalOutputPath + year + "/" + month + "/" + filenameNow.split(".")[0]
            print finalPath
            if( os.path.exists(finalPath) ):
                shutil.rmtree(finalPath)
            os.makedirs(finalPath)
            arcpy.CopyRaster_management( inRasData, finalPath + "/" + filenameOut)
            pass
        
        ########################## SELESAI ################################################


        ####################### SAVE LOG DATA YANG TELAH SELESAI DIPROSES ########################################
        log = pd.read_csv("logComplete.csv")
        liScene = log["scene"].tolist()
        liDate = log["dateComplete"].tolist()

        liScene.append(scene)
        liDate.append(str(datetime.now()))

        print(liScene)
        print(liDate)

        serScene = pd.Series(liScene)
        serDate = pd.Series(liDate)

        print(serScene)
        print(serDate)
        log2 = pd.DataFrame()
        log2["scene"] = serScene
        log2["dateComplete"] = serDate

        print(log2.head(5))
        log2.to_csv("logComplete.csv", index=False)

        del liScene
        del liDate
        del serScene
        del serDate
        del log
        del log2

        ##########################################################################################################
        # delete downloaded data in workstation
        dataFolder = os.listdir(config.dataPath)
        print dataFolder
        if(len(dataFolder) > 1):    
            print config.dataPath + dataFolder[0]
            shutil.rmtree(config.dataPath + dataFolder[0])

        hasilFolder = os.listdir(config.outputPath)
        print hasilFolder
        if(len(hasilFolder) > 1):
            print config.outputPath + hasilFolder[0]
            shutil.rmtree(config.outputPath + hasilFolder[0])
        print ("Finished ..")
        msg = str(datetime.now()) + '\t' + "Finished ... \n"
        redis.rpush(config.MESSAGES_KEY, msg)
        redis.publish(config.CHANNEL_NAME, msg)

        redis.delete(config.MESSAGES_KEY)
        redis.delete(config.MESSAGES_KEY_2)

        # local variable to list
        dictLocal = locals()
        # delete all local variable, hope will free some space
        for key in dictLocal.keys():
            del key
        clear_all()
        # bersih bersih lainnya
        gc.collect()
        #shutil.rmtree(config.gdbPath)
        arcpy.Delete_management(config.gdbPathDefault)
        arcpy.Delete_management("in_memory")
        arcpy.env.overwriteOutput = True
예제 #16
0
def from_numpy(numpy_rast, metadata, outpath, NoData_Value=None):
    """
    Wrapper for arcpy.NumPyArrayToRaster function with better metadata handling

    this is just a wrapper for the NumPyArrayToRaster function within arcpy. It is used in
    conjunction with to_numpy to streamline reading image files in and out of numpy
    arrays. It also ensures that all spatial referencing and projection info is preserved
    between input and outputs of numpy manipulations.

    :param numpy_rast:      The numpy array version of the input raster
    :param metadata:        The variable exactly as output from "to_numpy"
    :param outpath:         Output filepath of the individual raster
    :param NoData_Value:    The no data value of the output raster

    :return outpath:        Same as input outpath, filepath to created file.

    Usage example
    call to_numpy with  "rast,metadata = to_numpy(Raster)"
    perform numpy manipulations as you please
    then save the array with "raster.from_numpy(rast, metadata, output)"
    """

    numpy_rast = numpy_rast.astype(metadata.numpy_datatype)

    if NoData_Value is None:
        NoData_Value = metadata.NoData_Value

    llcorner = arcpy.Point(metadata.Xmin, metadata.Ymin)

    # save the output.
    if isinstance(numpy_rast, numpy.ma.core.MaskedArray):
        mask = numpy_rast.mask
        data = numpy_rast.data
        data[mask] = metadata.NoData_Value

        OUT = arcpy.NumPyArrayToRaster(data, llcorner, metadata.cellWidth,
                                       metadata.cellHeight)
        OUT.save(outpath)

    elif isinstance(numpy_rast, numpy.ndarray):
        OUT = arcpy.NumPyArrayToRaster(numpy_rast, llcorner,
                                       metadata.cellWidth, metadata.cellHeight)
        OUT.save(outpath)

    # define its projection
    try:
        arcpy.DefineProjection_management(outpath, metadata.projection)
    except:
        Warning("Unable to define the projection on {0}".format(outpath))

    # reset the NoData_Values
    try:
        arcpy.SetRasterProperties_management(outpath,
                                             data_type="#",
                                             statistics="#",
                                             stats_file="#",
                                             nodata="1 " + str(NoData_Value))

    except:
        Warning("Unable to establish NoData profile on {0}".format(outpath))

    # calculate statistics and pyramids
    arcpy.CalculateStatistics_management(outpath)
    arcpy.BuildPyramids_management(outpath)

    print("Saved output file as {0}".format(outpath))

    return outpath
예제 #17
0
    def run_year(self):
        logging.info('starting garden disturbance for year: %s' % self.year)
        logging.info('garden {} is using ecocom: {} {}'.format(
            self.year, self.ecocommunities, type(self.ecocommunities)))

        self.calculate_suitability()
        self.points_to_coordinates()
        self.set_populations()

        logging.info('checking for existing gardens')
        for population, coordinates in zip(self.site_populations,
                                           self.coordinate_list):
            logging.info('garden coordinates: {} {}'.format(
                coordinates[0], coordinates[1]))
            self.site_center = arcpy.Point(coordinates[0], coordinates[1])
            self.population = population
            self.population_to_garden_area()

            self.set_local_extent()
            # check for gardens in area buffered around site (from set_local_extent)
            # if garden area of garden is 0 create new garden
            if self.garden_area == 0:
                arcpy.env.extent = self.temp_buffer
                self.set_garden_center()
                # Continue only if garden center has been assigned
                if self.garden is not None:
                    self.create_garden()
                    arcpy.env.extent = self.ecocommunities

                    # Return garden cells from self.garden else return existing communities
                    self.ecocommunities = arcpy.sa.Con(
                        arcpy.sa.IsNull(self.garden) == 0, self.garden,
                        self.ecocommunities)

                    thisisabsurd = random.randint(0, 1000000)
                    fn = 'ecocommunities_{}_{}.tif'.format(
                        self.year, thisisabsurd)
                    self.ecocommunities.save((os.path.join(s.TEMP_DIR, fn)))
                    logging.info('ecocommunities: {}'.format(
                        self.ecocommunities))
                    e = arcpy.RasterToNumPyArray(self.ecocommunities)
                    self.canopy[e == s.GARDEN_ID] = 0
                    self.forest_age[e == s.GARDEN_ID] = 0
                    self.dbh[e == s.GARDEN_ID] = 0
                    # self.dbh[(e == s.SUCCESSIONAL_OLD_FIELD_ID) & (self.forest_age == 0)] = 0.5

            arcpy.env.extent = self.ecocommunities

        self.ecocommunities.save(
            (os.path.join(s.OUTPUT_DIR, 'ecocommunities_%s.tif' % self.year)))
        logging.info('ecocom after garden disturbance: {}'.format(
            self.ecocommunities))

        self.time_since_disturbance = arcpy.sa.Con(
            self.ecocommunities == s.GARDEN_ID, 0,
            self.time_since_disturbance + 1)
        self.time_since_disturbance.save(
            os.path.join(self.OUTPUT_DIR,
                         'time_since_disturbance_%s.tif' % self.year))

        canopy = arcpy.NumPyArrayToRaster(self.canopy,
                                          arcpy.Point(arcpy.env.extent.XMin,
                                                      arcpy.env.extent.YMin),
                                          x_cell_size=s.CELL_SIZE,
                                          y_cell_size=s.CELL_SIZE)
        canopy.save(s.CANOPY)
        forestage = arcpy.NumPyArrayToRaster(self.forest_age,
                                             arcpy.Point(
                                                 arcpy.env.extent.XMin,
                                                 arcpy.env.extent.YMin),
                                             x_cell_size=s.CELL_SIZE,
                                             y_cell_size=s.CELL_SIZE)
        forestage.save(s.FOREST_AGE)
        dbh = arcpy.NumPyArrayToRaster(self.dbh,
                                       arcpy.Point(arcpy.env.extent.XMin,
                                                   arcpy.env.extent.YMin),
                                       x_cell_size=s.CELL_SIZE,
                                       y_cell_size=s.CELL_SIZE)
        dbh.save(s.DBH)

        logging.info('garden area: %s' % self.garden_area)
        self.ecocommunities = None
        del self.ecocommunities
예제 #18
0
    def execute(self, parameters, messages):
        """The source code of the tool."""

        arcpy.env.overwriteOutput = True

        scratchWorkspace = arcpy.env.scratchWorkspace
        if not scratchWorkspace:
            scratchWorkspace = arcpy.env.scratchGDB

        in_nc = parameters[0].valueAsText
        in_rapid_connect_file = parameters[1].valueAsText
        in_catchment = parameters[2].valueAsText
        streamID = parameters[3].valueAsText
        out_WeightTable = parameters[4].valueAsText
        out_CGPolygon = parameters[5].valueAsText
        out_CGPoint = parameters[6].valueAsText

        # validate the netcdf dataset
        self.dataValidation(in_nc, messages)

        # open netcdf dataset
        data_nc = NET.Dataset(in_nc)
        '''Obtain west-east, south-north dimension information'''
        name_xdim = 'west_east'
        name_ydim = 'south_north'
        size_xdim = len(data_nc.dimensions['west_east'])
        size_ydim = len(data_nc.dimensions['south_north'])

        #####################Start of script adaption###############################################################
        '''Obtain projection information'''
        map_proj = data_nc.__dict__['MAP_PROJ']

        # read projection information from global attributes
        corner_lats = data_nc.__dict__['corner_lats']
        corner_lons = data_nc.__dict__['corner_lons']
        DX = data_nc.__dict__['DX']
        DY = data_nc.__dict__['DY']
        standard_parallel_1 = data_nc.__dict__['TRUELAT1']
        standard_parallel_2 = data_nc.__dict__['TRUELAT2']
        central_meridian = data_nc.__dict__['STAND_LON']
        pole_latitude = data_nc.__dict__['POLE_LAT']
        pole_longitude = data_nc.__dict__['POLE_LON']
        latitude_of_origin = data_nc.__dict__['CEN_LAT']

        # Create Projection file with information from NetCDF global attributes
        sr2 = arcpy.SpatialReference()
        if map_proj == 1:
            # Lambert Conformal Conic
            if 'standard_parallel_2' in locals():
                arcpy.AddMessage(
                    '    Using Standard Parallel 2 in Lambert Conformal Conic map projection.'
                )
            else:
                # According to http://webhelp.esri.com/arcgisdesktop/9.2/index.cfm?TopicName=Lambert_Conformal_Conic
                standard_parallel_2 = standard_parallel_1
                latitude_of_origin = standard_parallel_1
            Projection_String = (
                'PROJCS["North_America_Lambert_Conformal_Conic",'
                'GEOGCS["GCS_Sphere",'
                'DATUM["D_Sphere",SPHEROID["Sphere",6370000.0,0.0]],'
                'PRIMEM["Greenwich",0.0],'
                'UNIT["Degree",0.0174532925199433]],'
                'PROJECTION["Lambert_Conformal_Conic"],'
                'PARAMETER["false_easting",0.0],'
                'PARAMETER["false_northing",0.0],'
                'PARAMETER["central_meridian",' + str(central_meridian) + '],'
                'PARAMETER["standard_parallel_1",' + str(standard_parallel_1) +
                '],'
                'PARAMETER["standard_parallel_2",' + str(standard_parallel_2) +
                '],'
                'PARAMETER["latitude_of_origin",' + str(latitude_of_origin) +
                '],'
                'UNIT["Meter",1.0]]')

        elif map_proj == 2:
            # Polar Stereographic

            # Set up pole latitude
            phi1 = float(standard_parallel_1)

            ### Back out the central_scale_factor (minimum scale factor?) using formula below using Snyder 1987 p.157 (USGS Paper 1395)
            ##phi = math.copysign(float(pole_latitude), float(latitude_of_origin))    # Get the sign right for the pole using sign of CEN_LAT (latitude_of_origin)
            ##central_scale_factor = (1 + (math.sin(math.radians(phi1))*math.sin(math.radians(phi))) + (math.cos(math.radians(float(phi1)))*math.cos(math.radians(phi))))/2

            # Method where central scale factor is k0, Derivation from C. Rollins 2011, equation 1: http://earth-info.nga.mil/GandG/coordsys/polar_stereographic/Polar_Stereo_phi1_from_k0_memo.pdf
            # Using Rollins 2011 to perform central scale factor calculations. For a sphere, the equation collapses to be much  more compact (e=0, k90=1)
            central_scale_factor = (
                1 + math.sin(math.radians(abs(phi1)))
            ) / 2  # Equation for k0, assumes k90 = 1, e=0. This is a sphere, so no flattening

            loglines.append('      Central Scale Factor: %s' %
                            central_scale_factor)
            arcpy.AddMessage(loglines[-1])
            Projection_String = ('PROJCS["Sphere_Stereographic",'
                                 'GEOGCS["GCS_Sphere",'
                                 'DATUM["D_Sphere",'
                                 'SPHEROID["Sphere",6370000.0,0.0]],'
                                 'PRIMEM["Greenwich",0.0],'
                                 'UNIT["Degree",0.0174532925199433]],'
                                 'PROJECTION["Stereographic"],'
                                 'PARAMETER["False_Easting",0.0],'
                                 'PARAMETER["False_Northing",0.0],'
                                 'PARAMETER["Central_Meridian",' +
                                 str(central_meridian) + '],'
                                 'PARAMETER["Scale_Factor",' +
                                 str(central_scale_factor) + '],'
                                 'PARAMETER["Latitude_Of_Origin",' +
                                 str(standard_parallel_1) + '],'
                                 'UNIT["Meter",1.0]]')

        elif map_proj == 3:
            # Mercator Projection
            Projection_String = ('PROJCS["Sphere_Mercator",'
                                 'GEOGCS["GCS_Sphere",'
                                 'DATUM["D_Sphere",'
                                 'SPHEROID["Sphere",6370000.0,0.0]],'
                                 'PRIMEM["Greenwich",0.0],'
                                 'UNIT["Degree",0.0174532925199433]],'
                                 'PROJECTION["Mercator"],'
                                 'PARAMETER["False_Easting",0.0],'
                                 'PARAMETER["False_Northing",0.0],'
                                 'PARAMETER["Central_Meridian",' +
                                 str(central_meridian) + '],'
                                 'PARAMETER["Standard_Parallel_1",' +
                                 str(standard_parallel_1) + '],'
                                 'UNIT["Meter",1.0],AUTHORITY["ESRI",53004]]')

        elif map_proj == 6:
            # Cylindrical Equidistant (or Rotated Pole)
            # Check units (linear unit not used in this projection).  GCS?
            Projection_String = ('PROJCS["Sphere_Equidistant_Cylindrical",'
                                 'GEOGCS["GCS_Sphere",'
                                 'DATUM["D_Sphere",'
                                 'SPHEROID["Sphere",6370000.0,0.0]],'
                                 'PRIMEM["Greenwich",0.0],'
                                 'UNIT["Degree",0.0174532925199433]],'
                                 'PROJECTION["Equidistant_Cylindrical"],'
                                 'PARAMETER["False_Easting",0.0],'
                                 'PARAMETER["False_Northing",0.0],'
                                 'PARAMETER["Central_Meridian",' +
                                 str(central_meridian) + '],'
                                 'PARAMETER["Standard_Parallel_1",' +
                                 str(standard_parallel_1) + '],'
                                 'UNIT["Meter",1.0],AUTHORITY["ESRI",53002]]')

        sr2.loadFromString(Projection_String)
        '''Create a projected ones raster'''
        arr_ones = NUM.ones((size_ydim, size_xdim), dtype=NUM.uint16)

        #####################Start of script adaption###############################################################
        # Create a point geometry object from gathered corner point data
        sr1 = arcpy.SpatialReference(104128)  # Using EMEP Sphere (6370000m)
        point = arcpy.Point()
        point.X = float(corner_lons[0])
        point.Y = float(corner_lats[0])
        pointGeometry = arcpy.PointGeometry(point, sr1)
        projpoint = pointGeometry.projectAs(sr2)

        # Get projected X and Y of the point geometry and adjust so lower left corner becomes lower left center
        point2 = arcpy.Point((projpoint.firstPoint.X - (float(DX) / 2)),
                             (projpoint.firstPoint.Y -
                              (float(DY) / 2)))  # Adjust by half a grid cell

        # Process: Numpy Array to Raster
        ras_ones = arcpy.NumPyArrayToRaster(arr_ones, point2, float(DX),
                                            float(DY))

        # Process: Define Projection
        arcpy.DefineProjection_management(ras_ones, sr2)

        ras_ones.save(os.path.join(scratchWorkspace, "ras_ones"))
        ################End of script adaption######################################################################
        '''Create CG Polygon for the buffered catchment'''
        arcpy.AddMessage("Creating computation grid polygons...")
        # Obtain a minimum bounding envelope of the catchment with the same spatial reference as the WRF-Hydro data
        envelope = os.path.join(scratchWorkspace, "envelope")
        arcpy.env.outputCoordinateSystem = sr2
        result0 = arcpy.MinimumBoundingGeometry_management(
            in_catchment, envelope, "ENVELOPE", "ALL")
        arcpy.ResetEnvironments()
        envelope = result0.getOutput(0)
        # Determine whether the input catchment is within the WRF-Hydro data extent
        ext_ras_ones = arcpy.Describe(ras_ones).extent
        ext_envelope = arcpy.Describe(envelope).extent
        if ext_envelope.XMin < ext_ras_ones.XMin \
            or ext_envelope.XMax > ext_ras_ones.XMax \
            or ext_envelope.YMin < ext_ras_ones.YMin \
            or ext_envelope.YMax > ext_ras_ones.YMax:
            # Input Catchments exceed the WRF data extent
            messages.addErrorMessage(self.errorMessages[2])
            raise arcpy.ExecuteError

        # Buffer the minimum bounding envelope
        envelope_b = os.path.join(scratchWorkspace, "envelope_b")
        dist_buffer = str(int(DX) * 3) + " Meters"
        result1 = arcpy.Buffer_analysis(envelope, envelope_b, dist_buffer,
                                        "FULL", "#", "ALL")
        envelope_b = result1.getOutput(0)

        # Convert the buffered envelope into raster with cellsize, and snapraster the same as ras_ones
        arcpy.env.snapRaster = ras_ones
        arcpy.env.cellSize = ras_ones
        name_OID = arcpy.Describe(envelope_b).OIDFieldName
        ras_env_b = os.path.join(scratchWorkspace, "ras_env_b")
        arcpy.PolygonToRaster_conversion(envelope_b, name_OID, ras_env_b,
                                         'CELL_CENTER', 'NONE', ras_ones)
        arcpy.ResetEnvironments()

        # Create fishnet based on the clipped raster
        # Get the extent of the raster
        ext_ras = arcpy.Describe(ras_env_b).extent
        xmin_ras = ext_ras.XMin
        ymin_ras = ext_ras.YMin

        # Set parameters of the output fishnet
        originCoordinate = str(xmin_ras) + ' ' + str(ymin_ras)  # the origin
        yAxiCoordinate = str(xmin_ras) + ' ' + str(
            ymin_ras + 10)  # the orientation
        cellSizeWidth = str(DX)
        cellSizeHeight = str(DY)
        ras_temp = arcpy.sa.Raster(ras_env_b)
        numRows = str(ras_temp.height)
        numCols = str(ras_temp.width)
        labels = "NO_LABELS"
        if out_CGPoint is not None:
            labels = "LABELS"
        oppositeCorner = ""
        templateExtent = ras_env_b
        geometryType = "POLYGON"

        if out_CGPolygon is None:
            out_CGPolygon = os.path.join(scratchWorkspace, "out_CGPolygon")

        # Create fishnet (computation grid polygon) (and point if specified)
        result2 = arcpy.CreateFishnet_management(
            out_CGPolygon, originCoordinate, yAxiCoordinate, cellSizeWidth,
            cellSizeHeight, numRows, numCols, oppositeCorner, labels,
            templateExtent, geometryType)
        out_CGPolygon = result2.getOutput(0)

        # Add and calculate fields of CENTROID_X and CENTROD_Y for fishnet (computation grid polygon)
        arcpy.AddGeometryAttributes_management(out_CGPolygon, "CENTROID", "#",
                                               "#", "#")
        temp_CGPoint = None
        (dirnm_out_CGPolygon,
         basenm_out_CGPolygon) = os.path.split(out_CGPolygon)
        if not basenm_out_CGPolygon.endswith(".shp"):
            temp_CGPoint = out_CGPolygon + "_label"
        else:
            temp_CGPoint = os.path.join(
                dirnm_out_CGPolygon,
                "{}_label.shp".format(basenm_out_CGPolygon[:-4]))

        if arcpy.Exists(temp_CGPoint) and out_CGPoint is not None and (
                temp_CGPoint) != out_CGPoint:
            if not arcpy.Exists(out_CGPoint):
                arcpy.CopyFeatures_management(temp_CGPoint, out_CGPoint)
                arcpy.Delete_management(temp_CGPoint)

        # Get latitude and longitude
        lat_arr = data_nc.variables["XLAT_M"][:]
        lon_arr = data_nc.variables["XLONG_M"][:]
        data_nc.close()
        '''Create weight table'''
        # Obtain the minimum X and minimum Y for the lower left pixel center of the ones raster
        minX = projpoint.firstPoint.X
        minY = projpoint.firstPoint.Y

        # Intersect the catchment polygons with the computation grid polygons
        arcpy.AddMessage(
            "Intersecting computation grid polygons with catchment...")
        intersected = os.path.join(scratchWorkspace, "intersected")
        result3 = arcpy.Intersect_analysis([out_CGPolygon, in_catchment],
                                           intersected, "ALL", "#", "INPUT")
        intersected = result3.getOutput(0)

        # Calculate the geodesic area in square meters for each intersected polygon
        arcpy.AddMessage("Calculating geodesic areas...")
        arcpy.AddGeometryAttributes_management(intersected, "AREA_GEODESIC",
                                               "", "SQUARE_METERS", "")

        # Calculate the total geodesic area of each catchment based on the contributing areas of points
        fields = [streamID, "CENTROID_X", "CENTROID_Y", "AREA_GEO"]
        area_arr = arcpy.da.FeatureClassToNumPyArray(intersected, fields)

        arcpy.AddMessage("Writing the weight table...")
        # Get list of COMIDs in rapid connect file so only those area included in computations
        connectivity_table = self.csvToList(in_rapid_connect_file)
        streamID_unique_list = [int(row[0]) for row in connectivity_table]

        # Get some dummy data from the first row
        centroidX_dummy = area_arr["CENTROID_X"][0]
        centroidY_dummy = area_arr["CENTROID_Y"][0]
        indexX_dummy = long(round((centroidX_dummy - minX) / float(DX)))
        indexY_dummy = long(round((centroidY_dummy - minY) / float(DY)))
        lon_dummy = lon_arr[0, indexY_dummy, indexX_dummy]
        lat_dummy = lat_arr[0, indexY_dummy, indexX_dummy]

        with open(out_WeightTable, 'wb') as csvfile:
            connectwriter = csv.writer(csvfile, dialect='excel')
            #header
            connectwriter.writerow([
                streamID, "area_sqm", "west_east", "south_north", "npoints",
                "weight", "Lon", "Lat", "x", "y"
            ])

            for streamID_unique in streamID_unique_list:
                ind_points = NUM.where(
                    area_arr[streamID] == streamID_unique)[0]
                num_ind_points = len(ind_points)
                # Get the total area
                area_geo_total = 0
                for ind_point in ind_points:
                    area_geo_total += float(area_arr["AREA_GEO"][ind_point])

                if num_ind_points <= 0:
                    # if point not in array, append dummy data for one point of data
                    # streamID, "area_sqm", "west_east", "south_north", "npoints", "weight", "Lon", "Lat", "x", "y"
                    row_dummy = [
                        streamID_unique, 0, indexX_dummy, indexY_dummy, 1, 1.0,
                        lon_dummy, lat_dummy, centroidX_dummy, centroidY_dummy
                    ]
                    connectwriter.writerow(row_dummy)
                else:
                    for ind_point in ind_points:
                        area_geo_each = float(area_arr['AREA_GEO'][ind_point])
                        weight_each = area_geo_each / area_geo_total
                        centroidX = area_arr["CENTROID_X"][ind_point]
                        centroidY = area_arr["CENTROID_Y"][ind_point]
                        indexX = long(round((centroidX - minX) / float(DX)))
                        indexY = long(round((centroidY - minY) / float(DY)))
                        lon = lon_arr[0, indexY, indexX]
                        lat = lat_arr[0, indexY, indexX]
                        row = [
                            streamID_unique, area_geo_each, indexX, indexY,
                            num_ind_points, weight_each, lon, lat, centroidX,
                            centroidY
                        ]
                        connectwriter.writerow(row)

        return
예제 #19
0
sr= arcpy.GetParameterAsText(2)

#get lower left corner of first raster layer for projection later on
desc = arcpy.Describe(fr)
x = desc.extent.XMin
y = desc.extent.YMin

#convert first and second raster to numpy array
fr_array= arcpy.RasterToNumPyArray(fr)
sr_array= arcpy.RasterToNumPyArray(sr)

#get difference between second raster and first raster
diff_array=sr_array-fr_array

#convert the difference raster to array
diff_raster=arcpy.NumPyArrayToRaster(diff_array,arcpy.Point(x,y),'','',-9999)

final_ws= ws + '\\'
diff_raster.save(final_ws + 'diff_raster')
 
#get spatial reference of original first raster
coor_system = arcpy.Describe(fr).spatialReference

#project the difference raster to match the original raster 
projected_raster=arcpy.management.ProjectRaster(diff_raster,final_ws+'projected_raster',coor_system,'','','','',coor_system)
#projected_raster.save(final_ws +'projected_raster')

#add differnece layer to map
p=arcpy.mp.ArcGISProject("CURRENT")
m=p.listMaps()[0]
#make a feature layer from feature class
예제 #20
0
inputdata = arcpy.RasterToNumPyArray(InputRaster)

OutputArray = np.memmap(TemporaryFile(), "uint8", "w+", shape=inputdata.shape)

descData = arcpy.Describe(InputRaster + "\\Band_1")
cellSize = descData.meanCellHeight
sr = descData.spatialReference
extent = descData.Extent
pnt = arcpy.Point(extent.XMin, extent.YMin)
rows = inputdata.shape[0]
cols = inputdata.shape[1]

for i in range(0, rows, blockSize):
    if (i + blockSize) < rows:
        numRows = blockSize
    else:
        numRows = rows - i
    for j in range(0, cols, blockSize):
        if (j + blockSize) < cols:
            numCols = blockSize
        else:
            numCols = cols - j
        OutputArray[i:i + numRows,
                    j:j + numCols] = vbitreader(inputdata[i:i + numRows,
                                                          j:j + numCols])
        arcpy.AddMessage("row {0} of {1}  column {2} of {3}".format(
            (i / blockSize + 1), int(rows / blockSize + 1),
            (j / blockSize + 1), int(cols / blockSize + 1)))

outraster = arcpy.NumPyArrayToRaster(OutputArray, pnt, cellSize, cellSize, 0)
outraster.save(OutputFolder + "\\" + OutputFileName)
예제 #21
0
def create_response_raster(classifier, rasters, output, scale):
    """
        create_response_raster
            Uses a classifier in a multiband raster to obtain the response raster
            
        :param classifier: Classifier object previously trained
        :param rasters: Multiband raster with the information to be put in the classifier 
        :param output: Name of the file for the response raster
        :param scale: Object with the transformation to be done to normalize the data. If None, then no transformation 
            is made
        :return: None 
    """
    # Obtain spatial information from the raster
    spatial_reference = arcpy.Describe(rasters).spatialReference

    raster = arcpy.Raster(rasters)

    lower_left_corner = arcpy.Point(raster.extent.XMin, raster.extent.YMin)
    x_cell_size = raster.meanCellWidth
    y_cell_size = raster.meanCellHeight
    # Import the multiband raster to numpy array if the raster is of type integer it will throw an error because of the
    # NaN values, then is imported wit NaNs as maximum integers, casted to float and then the NaNs applied
    try:
        raster_array = arcpy.RasterToNumPyArray(rasters,
                                                nodata_to_value=np.NaN)
    except ValueError:
        _verbose_print("Integer type raster, changed to float")
        raster_array = 1.0 * arcpy.RasterToNumPyArray(
            rasters, nodata_to_value=sys.maxint)
        raster_array[raster_array == sys.maxint] = np.NaN

    MESSAGES.AddMessage("Creating response raster...")

    # If it is a single band, then the raster must be reshaped as a tri-dimensional numpy array
    if raster_array.ndim == 2:
        raster_array = np.reshape(raster_array, [1] + list(raster_array.shape))

    n_regr = raster_array.shape[0]
    n_rows = raster_array.shape[1]
    n_cols = raster_array.shape[2]
    _verbose_print("{} regressors of shape {} x {}".format(
        n_regr, n_rows, n_cols))

    # The raster looping must be done in the last dimension
    raster_array2 = np.empty([n_rows, n_cols, n_regr])
    for raster_index in range(n_regr):
        raster_array2[:, :, raster_index] = raster_array[raster_index, :, :]

    # The matrix is reshaped from 3D to 2D
    raster_array2 = np.reshape(raster_array2, [n_rows * n_cols, n_regr])

    # Create a mask where the values of all regressors are finite. The calculations will be made just there
    finite_mask = np.all(np.isfinite(raster_array2), axis=1)
    nan_mask = np.logical_not(finite_mask)
    _verbose_print("{} elements will be calculated and {} let as NaN".format(
        sum(finite_mask), sum(nan_mask)))
    # Make the transformation if available
    if scale is None:
        finite_array = raster_array2[finite_mask]
        _verbose_print("Data not normalized")
    else:
        finite_array = scale.transform(raster_array2[finite_mask])
        MESSAGES.AddMessage("Data normalized")
    # Calculate decision_function for the elements with finite values. If not available use  predict_proba
    # TODO: Measure time
    if getattr(classifier, "decision_function", None) is None:
        _verbose_print(
            "Decision function not available, probabilities used instead")
        responses = classifier.predict_proba(
            finite_array)[:, classifier.classes_ == 1]
    else:
        _verbose_print("Decision function used")
        responses = classifier.decision_function(finite_array)

    # Reshape the matrix with the response values
    response_vector = np.empty(n_rows * n_cols)
    response_vector[finite_mask] = responses
    response_vector[nan_mask] = -9
    response_array = np.reshape(response_vector, [n_rows, n_cols])
    # Known bug: There is a minimal displacement between the input layers and the response
    response_raster = arcpy.NumPyArrayToRaster(
        response_array,
        lower_left_corner=lower_left_corner,
        x_cell_size=x_cell_size,
        y_cell_size=y_cell_size,
        value_to_nodata=-9)
    response_raster.save(output)
    # Known bug: if the output name is too long (the name not the path) then arcpy shows "unexpected error"
    MESSAGES.AddMessage("Raster file created in " + output)
    arcpy.DefineProjection_management(output, spatial_reference)

    return
예제 #22
0
# Output         = arcpy.getParameterAsText(2)
Flow_Direction = arcpy.Raster("flowdir.tif")
BMP_Points     = arcpy.Raster("C:/Users/csomerlot/Desktop/Lab05Data/Lab05Geodatabase.gdb/BMP_Points_PointToRaster")
Output         = "C:/Users/csomerlot/Desktop/Lab05Data/Lab05Geodatabase.gdb/output"

# set environment 

# create variables to hold input and output datasets
flowdirData = arcpy.RasterToNumPyArray(Flow_Direction)
lowerLeft = arcpy.Point(Flow_Direction.extent.XMin,Flow_Direction.extent.YMin)
cellSize  = Flow_Direction.meanCellWidth
height = len(flowdirData)
width = len(flowdirData[0])

bmppointData = arcpy.RasterToNumPyArray(BMP_Points)
if BMP_Points.extent.XMin != Flow_Direction.extent.XMin:
    print BMP_Points.extent.XMin, Flow_Direction.extent.XMin
    raise Exception("Xmin of extents not the same")
if BMP_Points.extent.YMin != Flow_Direction.extent.YMin: raise Exception("YMin of extents are not the same") 
if BMP_Points.meanCellWidth != Flow_Direction.meanCellWidth: raise Exception("Cell sizes are not the same")
if len(bmppointData) != height:
    print len(bmppointData[0]), height, width
    raise Exception("Heights are not the same")
if len(bmppointData[0]) != width: raise Exception("Widths are not the same")

outputData = flowAccumulate(flowdirData)

# save outputs
outputRaster = arcpy.NumPyArrayToRaster(outputData,lowerLeft,cellSize)
outputRaster.save(Output)
예제 #23
0
파일: temp.py 프로젝트: mbougie/gibbs
def execute_task(in_extentDict):

    fc_count = in_extentDict[0]

    procExt = in_extentDict[1]
    # print procExt
    XMin = procExt[0]
    YMin = procExt[1]
    XMax = procExt[2]
    YMax = procExt[3]

    #set environments
    #The brilliant thing here is that using the extents with the full dataset!!!!!!   DONT EVEN NEED TO CLIP THE FULL RASTER TO THE FISHNET BECASUE
    arcpy.env.snapRaster = data['pre']['traj']['path']
    arcpy.env.cellsize = data['pre']['traj']['path']
    arcpy.env.outputCoordinateSystem = data['pre']['traj']['path']
    arcpy.env.extent = arcpy.Extent(XMin, YMin, XMax, YMax)

    cls = 21973
    rws = 13789

    # outData = numpy.zeros((rows,cols), numpy.int16)
    outData = np.zeros((13789, 21973), dtype=np.int)

    ### create numpy arrays for input datasets cdls and traj

    change = arcpy.RasterToNumPyArray(
        in_raster=
        'D:\\projects\\usxp\\series\\s14\\core\\core.gdb\\s14_traj_cdl30_b_2008to2016_rfnd_n8h_mtr_8w_mmu5',
        lower_left_corner=arcpy.Point(XMin, YMin),
        nrows=13789,
        ncols=21973),
    conf = arcpy.RasterToNumPyArray(
        in_raster=
        'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\conf.gdb\\National_average_cdl_30m_r_2008to2016_albers_confidence',
        lower_left_corner=arcpy.Point(XMin, YMin),
        nrows=13789,
        ncols=21973)

    # np.nonzero(change)
    # # arr_traj = arcpy.RasterToNumPyArray(in_raster=data['pre']['traj']['path'], lower_left_corner = arcpy.Point(XMin,YMin), nrows = 13789, ncols = 21973)

    # # find the location of each pixel labeled with specific arbitray value in the rows list
    # for row in location_list:
    # 	#year of conversion for either expansion or abandonment
    # 	ytx = row[1]
    # 	print 'ytx', ytx

    # 	#year before conversion for either expansion or abandonment
    # 	ybx = row[1]-1
    # 	print 'ybx', ybx

    #Return the indices of the pixels that have values of the ytc arbitrsy values of the traj.
    indices = np.nonzero(change)
    print indices

    # #stack indices so easier to work with
    # stacked_indices=np.column_stack((indices[0],indices[1]))

    #        #get the x and y location of each pixel that has been selected from above
    # for pixel_location in stacked_indices:
    # 	row = pixel_location[0]
    # 	col = pixel_location[1]
    #        print 'row', row
    #        print 'col', col

    #       #get the pixel value for ytx
    # pixel_value_ytx =  cdls[ytx][row][col]
    # #get the pixel value for ybx
    # pixel_value_ybx =  cdls[ybx][row][col]
    #       print row
    # #####  create dev mask componet
    # # cdls
    # if np.isnan(change[row,col]):
    # 	print 'null'
    # 	break
    # else:
    # 	outData[row,col] = conf[row,col]

    #       #####  create 36_61 mask componet
    # if pixel_value_ytx in [36,61]:
    # 	#find the years stil left in the time series for this pixel location
    # 	yearsleft = [i for i in data['global']['years'] if i > ytx]

    #           #only focus on the extended series ---dont care about 2012
    # 	if len(yearsleft) > 1:
    # 		#create templist to hold the rest of the cld values for the time series.  initiaite it with the first cdl value
    # 		templist = [pixel_value_ytx]
    # 		for year in yearsleft:
    # 			# print 'year', year
    # 			# print 'cdls[year][row][col] :', cdls[year][row][col]
    # 			templist.append(cdls[year][row][col])

    # 		#check if all elements in array are the same
    # 		if len(set(templist)) == 1:
    # 			outData[row,col] = data['refine']['mask_dev_alfalfa_fallow']['arbitrary']

    arcpy.ClearEnvironment("extent")

    outname = "tile_" + str(fc_count) + '.tif'

    # #create
    outpath = os.path.join("C:/Users/Bougie/Desktop/Gibbs/", r"tiles", outname)

    # NumPyArrayToRaster (in_array, {lower_left_corner}, {x_cell_size}, {y_cell_size}, {value_to_nodata})
    myRaster = arcpy.NumPyArrayToRaster(outData,
                                        lower_left_corner=arcpy.Point(
                                            XMin, YMin),
                                        x_cell_size=30,
                                        y_cell_size=30,
                                        value_to_nodata=0)

    myRaster.save(outpath)
    n = 0
    for l in range(lie - 2 * r):
        print("The progress rate is %3f" % (float(l) / (lie - 2 * r)))
        for h in range(hang - 2 * r):
            n += 1
            # print ("No. is "+ str(n))
            dfg1clp = dfraster.iloc[h:h + 2 * r + 1, l:l + 2 * r + 1]
            dfg2clp = dflower.iloc[h:h + 2 * r + 1, l:l + 2 * r + 1]
            pig1=piWindow(dfg1clp)
            pig2=piWindow(dfg2clp)
            sumg1=0
            nums=0
            for g1,num in zip(g1class,numberlist):
                nums+=num
                sumg2=0
                if pig1[g1]==0:
                    continue
                elif num==1:
                    sumg1+=-pig1[g1]*math.log(pig1[g1])
                else:
                    for g2 in g2class[nums-num:nums]:
                        if pig2[g2]!=0:
                            sumg2+=pig2[g2]*math.log(pig2[g2])
                    sumg1+=-pig1[g1]*math.log(pig1[g1])*(1-pig1[g1]*sumg2/math.log(num))
            dfzero.iat[h + r, l + r] = sumg1
    arrayzero = np.array(dfzero)
    fusionRaster = arcpy.NumPyArrayToRaster(arrayzero, lowerLeft, cellSize)
    arcpy.DefineProjection_management(fusionRaster, sourceSR)
    fusionRaster.save(outpath)
    print ("FLI-SHDI is ok")
예제 #25
0
        # aggregate the image pixels within the new, larger pixel
        for row in range(samplesize):
            for col in range(samplesize):
                # if the subpixel falls within the image:
                if (0 <= pixpos[0] + col < cols) and (0 <= pixpos[1] + row < rows):
                    pixVal = image[pixpos[1]+row][pixpos[0]+col]
                    pixelNew[row][col] = pixVal
                    if pixVal == nodatavalue:
                        weights[row][col] = 0
                else:
                    weights[row][col] = 0

        # find out if the new pixel has a valid value:
        weightSum = weights.sum()
        if weightSum != 0:
            weighted = weights * pixelNew
            imageNew[rowNew][colNew] = weighted.sum() / weightSum
        else:
            imageNew[rowNew][colNew] = nodatavalue



# Convert Array to raster (keep the origin and cellsize the same as the input)
newRaster = arcpy.NumPyArrayToRaster(imageNew,lowerLeftNew,cellSizeSAT, value_to_nodata=nodatavalue)
arcpy.DefineProjection_management(newRaster, prjSAT)
newRaster.save(outMap)


print 'done'
arcpy.AddMessage('donzo')
예제 #26
0
    def start_precipition(
            self, river_catchment, DTM, region, precipitation_textfile,
            baseflow_provided, day_pcp_yr, years_of_sim,
            total_day_month_precip, total_avg_month_precip,
            max_30min_rainfall_list, mannings_n, CULSE, orgC,
            precipitation_gauge_elevation, CN2_d, GS_list, active_layer,
            inactive_layer, active_layer_GS_P_temp, active_layer_V_temp,
            inactive_layer_GS_P_temp, inactive_layer_V_temp,
            numpy_array_location, output_file_dict, output_format,
            output_excel_discharge, output_excel_sediment,
            output_averages_temp, DTM_temp, slope_sub_path, slope_temp,
            extent_xmin, extent_ymin, workspace):

        # Check to see if the user wants to output discharge / sediment loss from the system
        discharge_spamwriter, sediment_spamwriter = rasterstonumpys.save_discharge_or_sediment_csv(
            output_excel_discharge, output_excel_sediment)

        # Open the precipitation file
        precipitation_read = open(precipitation_textfile)

        ### Set to default for the first loop ###
        recalculate_slope_flow = True

        ##### Daily loop start #####
        arcpy.AddMessage("Starting Model...")
        for precipitation in precipitation_read:

            # Ensure amount of availiable memory is at the optimum
            collected = gc.collect()
            arcpy.AddMessage("Garbage collector: collected %d objects." %
                             (collected))

            start = time.time()
            arcpy.AddMessage("Today's date is " + str(self.current_date))
            self.day_of_year = int(self.current_date.strftime('%j'))

            ### CHECK TO SEE IF BASEFLOW NEEDS TO BE SEPERATED ###
            precipitation, baseflow = hydrology.SCSCNQsurf(
                self.bottom_left_corner,
                self.cell_size).check_baseflow(precipitation,
                                               baseflow_provided)

            ### CHECK TO SEE IF THE SLOPE NEEDS TO BE CALCULATED ###
            if recalculate_slope_flow == True and self.first_loop == False:
                arcpy.AddMessage(
                    "Recalculating variables due to degree of elevation change"
                )
                DTM = arcpy.NumPyArrayToRaster(DTM, self.bottom_left_corner,
                                               self.cell_size, self.cell_size,
                                               -9999)
                slope, DTM, flow_direction_np, flow_direction_raster, flow_accumulation, CN2s_d, CN1s_d, CN3s_d, ang = hydrology.SCSCNQsurf(
                    self.bottom_left_corner,
                    self.cell_size).check_slope_flow_directions(
                        self.first_loop, self.use_dinfinity, self.day_of_year,
                        CN2_d, DTM, baseflow_provided, numpy_array_location)

            if self.first_loop == True:
                arcpy.AddMessage("Calculating variables for the first loop")

                slope, DTM, flow_direction_np, flow_direction_raster, flow_accumulation, CN2s_d, CN1s_d, CN3s_d, ang = hydrology.SCSCNQsurf(
                    self.bottom_left_corner,
                    self.cell_size).check_slope_flow_directions(
                        self.first_loop, self.use_dinfinity, self.day_of_year,
                        CN2_d, DTM, baseflow_provided, numpy_array_location)

                DTM_MINUS_AL_IAL = elevation_adjustment.get_DTM_AL_IAL(
                    DTM, active_layer, inactive_layer, self.cell_size)
                # change the inactive layer to m3
                inactive_layer *= (self.cell_size * self.cell_size)

            ##### HYDROLOGY SECTION OF LOOP #####
            # Calculate the daily precipitation in each grid cell
            precipitation = hydrology.SCSCNQsurf(
                self.bottom_left_corner,
                self.cell_size).spatial_uniform_spatial_precip(
                    precipitation, DTM, day_pcp_yr,
                    precipitation_gauge_elevation)

            # Calculate the surface runoff in each grid cell (Not fatoring in antecedent conditions
            Q_surf = hydrology.SCSCNQsurf(self.bottom_left_corner,
                                          self.cell_size).OldQsurf(
                                              precipitation, CN2s_d)

            # Calculate the mean, max and min temperatures. The latitude and feed those into the evapotranspiration calculation ~~~~~~##### GOT HERE #####~~~~~~#
            mean_temp, max_temp, min_temp = evapotranspiration.Evapotranspiration(
            ).MinMaxMeanTemp(region, self.current_date)
            latitude = evapotranspiration.Evapotranspiration(
            ).UKlatituderadians(region, river_catchment)
            ETo = evapotranspiration.Evapotranspiration(
            ).ReferenceEvapotranspiration(latitude, self.day_of_year, max_temp,
                                          min_temp,
                                          mean_temp)  # need to define lat

            # Check if this is the first loop of the models operation
            if self.first_loop == True:

                # Set a 0 value for Sprev
                Sprev = np.zeros_like(DTM)

            # Calculate the retention parameter (Antecedent Conditions and Evapotranspiration)
            Scurr = hydrology.SCSCNQsurf(self.bottom_left_corner,
                                         self.cell_size).RententionParameter(
                                             precipitation, CN1s_d, CN2_d,
                                             CN2s_d, ETo, Sprev, Q_surf,
                                             self.first_loop)

            # Calculate surface runoff and then convert to raster
            Q_surf_np = hydrology.SCSCNQsurf(self.bottom_left_corner,
                                             self.cell_size).SurfSCS(
                                                 precipitation, Scurr, CN2s_d)

            Q_surf = arcpy.NumPyArrayToRaster(Q_surf_np,
                                              self.bottom_left_corner,
                                              self.cell_size, self.cell_size,
                                              -9999)

            # Execute Flow accumulation to work out the discharge.
            Q_dis = ((Q_surf / 1000) / 86400) * (
                self.cell_size * self.cell_size
            )  # convert to metres (by dividing by 1000) and then to seconds by dividing by 86400 and finally to the area of the cell by multiplying by the area of the cell.

            if self.use_dinfinity == True:
                Q_dis = hydrology.SCSCNQsurf(
                    self.bottom_left_corner,
                    self.cell_size).FlowAccumulationDinf(
                        ang, Q_dis, numpy_array_location)
            else:
                Q_dis = FlowAccumulation(flow_direction_raster, Q_dis)

            arcpy.AddMessage("Calculated discharge")

            if baseflow_provided == True:
                baseflow_raster = hydrology.SCSCNQsurf(
                    self.bottom_left_corner,
                    self.cell_size).BaseflowCalculation(
                        baseflow, flow_accumulation)
                Q_dis += baseflow_raster
                arcpy.Delete_management(baseflow_raster)

            Q_dis = arcpy.RasterToNumPyArray(Q_dis, '#', '#', '#', -9999)

            Q_max = np.amax(Q_dis)
            arcpy.AddMessage("Discharge at the outlet for today is " +
                             str(Q_max))
            arcpy.AddMessage(" ")

            # If the user has selected to output the daily discharge value at the bottom of the catchment write that value to the excel
            rasterstonumpys.output_discharge_csv(self.current_date,
                                                 discharge_spamwriter, Q_max)

            # Scurr becomes Sprev
            Sprev = Scurr

            ### HYDROLOGY GARBAGE COLLECTION ###
            collected = gc.collect()
            arcpy.AddMessage("Garbage collector: collected %d objects." %
                             (collected))

            ###SEDIMENT TRANSPORT SECTION OF LOOP###
            if self.calculate_sediment_transport == True:
                arcpy.AddMessage("Starting to calculate sediment transport...")

                # Get the erosion values on the first loop
                if self.first_loop == True:
                    self.depth_recking_threshold, self.discharge_erosion_threshold = sediment.sedimenttransport(
                    ).get_erosion_threshold_values(self.cell_size)

                sufficient_discharge_calculate_erosion = np.any(
                    Q_dis > self.discharge_erosion_threshold)

                if sufficient_discharge_calculate_erosion == True:
                    arcpy.AddMessage(
                        "Sufficient Discharge for Erosion Calculation to Begin"
                    )

                    calculate_sediment_transport_based_on_depth = True
                    daily_save_date = str(
                        self.current_date.strftime('%d_%m_%Y'))

                    if calculate_sediment_transport_based_on_depth == True:
                        arcpy.AddMessage(
                            "Sufficient depth to calculate sediment transport")
                        # Calculate sediment transport for each timestep based on the above calculation
                        inactive_layer, DTM, DTM_MINUS_AL_IAL, recalculate_slope_flow, net_sediment, depth_recking = sediment.sedimenttransport(
                        ).sediment_loop(
                            GS_list, Q_dis, slope, self.cell_size,
                            flow_direction_np, self.bottom_left_corner,
                            daily_save_date, active_layer_GS_P_temp,
                            active_layer_V_temp, inactive_layer_GS_P_temp,
                            inactive_layer_V_temp, inactive_layer, DTM,
                            DTM_MINUS_AL_IAL, self.depth_recking_threshold,
                            DTM_temp, slope_sub_path, slope_temp, extent_xmin,
                            extent_ymin, workspace)

                        sediment_depth = DTM - DTM_MINUS_AL_IAL
                        sediment_depth[Q_surf_np == -9999] = -9999
                        Sed_max = " "

                    elif calculate_sediment_transport_based_on_depth == False:
                        recalculate_slope_flow = False
                        sediment_depth = np.zeros_like(Q_surf_np)
                        net_sediment = np.zeros_like(Q_surf_np)
                        Sed_max = 0
                        sediment_depth[Q_surf_np == -9999] = -9999
                        net_sediment[Q_surf_np == -9999] = -9999
                        arcpy.AddMessage("-------------------------")
                        arcpy.AddMessage(
                            "Sediment transport will not be calculated for this timestep due to insufficient river depth"
                        )
                        arcpy.AddMessage("-------------------------")

                    # Check if the user would like to save sediment transport at the outlet
                    rasterstonumpys.output_sediment_csv(
                        self.current_date, sediment_spamwriter, Sed_max)

                else:
                    recalculate_slope_flow = False
                    sediment_depth = 0
                    net_sediment = 0
                    depth_recking = 0

            else:
                recalculate_slope_flow = False
                sediment_depth = 0
                net_sediment = 0
                depth_recking = 0

            # Section of the loop to calculate peak runoff for the day
            if self.calculate_sediment_erosion_hillslope == True:

                calculate_sediment_erosion_hillslope_based_on_surface_runoff = np.any(
                    Q_surf_np > 0.00001)

                if calculate_sediment_erosion_hillslope_based_on_surface_runoff == True:
                    # Adjustment factor is something that could be worked in at a later date.
                    adjustment_factor = 1

                    # Calculate the number of rainfall days per month and the average precipitation in each day over the month
                    day_pcp_month, day_avg_pcp = self.days_pcp_month(
                        total_day_month_precip, total_avg_month_precip)

                    # Calculate the monthly average half hour fraction
                    average_half_hour_fraction = hydrology.SCSCNQsurf(
                    ).average_half_hour_rainfall(years_of_sim, day_pcp_month,
                                                 day_avg_pcp,
                                                 max_30min_rainfall_list,
                                                 adjustment_factor, self.index)

                    # Calculate Concetration Overland Flow
                    concentration_overland_flow = hydrology.SCSCNQsurf(
                    ).time_concentration(depth_recking, flow_direction_raster,
                                         slope, mannings_n, self.cell_size)

                    # Calculate Q peak and hru area
                    q_peak, hru_area = hydrology.SCSCNQsurf().peak_flow(
                        depth_recking, Q_surf_np, concentration_overland_flow,
                        flow_accumulation, average_half_hour_fraction,
                        self.cell_size)

                    # Calculate sediment erosion using MULSE
                    hillslope_sediment_erosion = MUSLE.hillslope_erosion_MUSLE(
                        slope, self.cell_size, GS_list,
                        active_layer_GS_P_temp).calculate_MUSLE(
                            Q_surf_np, q_peak, orgC, CULSE)

                else:
                    hillslope_sediment_erosion = 0
                    arcpy.AddMessage("-------------------------")
                    arcpy.AddMessage(
                        "Insufficient surface runoff hillslope erosion will not be calculated"
                    )
                    arcpy.AddMessage("-------------------------")

            else:
                hillslope_sediment_erosion = 0

            ### Check  what needs to be output from the model ###
            self.week_day, self.month_day, self.year_day = rasterstonumpys.raster_outputs(
                self.week_day, self.month_day, self.year_day,
                self.current_date, self.first_loop, output_file_dict,
                output_format, output_averages_temp, self.bottom_left_corner,
                self.cell_size, Q_surf_np, Q_dis, depth_recking, precipitation,
                hillslope_sediment_erosion, net_sediment)

            ### VARIABLES / PARAMETERS THAT CHANGE AT END OF LOOP ###
            ### Check if the DTM is a raster ###
            self.first_loop = False

            # Test using arcpy delete function
            #arcpy.Delete_management(Q_dis)
            #arcpy.Delete_management(Q_surf)

            #arcpy.Delete_management(precipitation)

            # Increment the date and day by 1
            self.current_date = self.current_date + datetime.timedelta(days=1)
            tomorrow = self.current_date + datetime.timedelta(days=1)
            self.tomorrow_day = int(tomorrow.strftime('%d'))
            self.day_of_year += 1
            arcpy.AddMessage(
                "Time to complete today is " +
                str(round(time.time() - start, 2)) +
                "s. Note that on day 1 and every 30 days the timestep will take longer."
            )
            arcpy.AddMessage("-------------------------")
            del Q_dis, Q_surf_np, precipitation, Scurr
            gc.collect()

            ### UNUSED CODE TO READD LATER ###
            # Set up sediment loop save location
            '''if discharge_file_location and discharge_file_location != "#":
예제 #27
0
        for j in range(0, colNum):
            if array[0][i][j] == BORDER_VALUE:  #找到边界
                for w in range(0, bandNum):
                    array[w][i][j] = noDataValue  #赋值
                print "替换:%d %d" % (i, j)
                continue
            elif array[0][i][j] == noDataValue:  #是无值
                continue
            else:  #是普通像素
                break  #退出该行
    # 右
    for i in range(0, rowNum):
        for z in range(0, colNum):
            j = colNum - 1 - z
            if array[0][i][j] == BORDER_VALUE:  #找到边界
                for w in range(0, bandNum):
                    array[w][i][j] = noDataValue  # 赋值
                print "替换:%d %d" % (i, j)
                continue
            elif array[0][i][j] == noDataValue:  #是无值
                continue
            else:  #是普通像素
                break  #退出该行

    #保存栅格
    lowerLeft = arcpy.Point(r.extent.XMin, r.extent.YMin)  #左下角点坐标
    cellWidth = r.meanCellWidth  #栅格宽度
    cellHeight = r.meanCellHeight
    newRaster = arcpy.NumPyArrayToRaster(array, lowerLeft, cellWidth,
                                         cellHeight, noDataValue)  #转换成栅格
    newRaster.save(outPath)  #保存
예제 #28
0
125.	                                    e[i,j] = 4500              
126.	                                  if a[i,j] == -1:
127.	                                    if math.fabs(d17[i,j]* 0.05) >= math.fabs(d17[i,j] - d15[i,j]):
128.	                                      a[i,j] = d15[i,j]
129.	                                      e[i,j] = 4750            
130.	                                    if a[i,j] == -1:
131.	                                      if math.fabs(d18[i,j]* 0.05) >= math.fabs(d18[i,j] - d16[i,j]):
132.	                                        a[i,j] = d16[i,j]
133.	                                        e[i,j] = 5000       
134.	                                      if a[i,j] == -1:
135.	                                        if math.fabs(d19[i,j]* 0.05) >= math.fabs(d19[i,j] - d17[i,j]):
136.	                                          a[i,j] = d17[i,j]
137.	                                          e[i,j] = 5250
138.	                                        if a[i,j] == -1:
139.	                                          if math.fabs(d20[i,j]* 0.05) >= math.fabs(d20[i,j] - d18[i,j]):
140.	                                            a[i,j] = d18[i,j]
141.	                                            e[i,j] = 5500      
142.	                                          if a[i,j] == -1:
143.	                                            if math.fabs(d20[i,j]* 0.05) >= math.fabs(d20[i,j] – d19[i,j]):
144.	                                               a[i,j] = d19[i,j]
145.	                                               e[i,j] = 5750      
146.	                                             if a[i,j] == -1:
147.	                                              a[i,j] = d20[i,j]
148.	                                              e[i,j] = 6000
149.	raster = arcpy.NumPyArrayToRaster (a, lower_left, dsc.meanCellWidth, dsc.meanCellHeight,noDataValue) 
150.	raster.save("amplituda")
151.	raster2 = arcpy.NumPyArrayToRaster(e, lower_left, dsc.meanCellWidth, dsc.meanCellHeight,noDataValue)
152.	raster2.save("velkost_okna")
153.	del raster
154.	del raster2
예제 #29
0
def execute_task(args):
    in_extentDict, data, traj_list, noncroplist, croplist, cls, rws = args

    fc_count = in_extentDict[0]

    procExt = in_extentDict[1]
    # ##print procExt
    XMin = procExt[0]
    YMin = procExt[1]
    XMax = procExt[2]
    YMax = procExt[3]

    #set environments
    #The brilliant thing here is that using the extents with the full dataset!!!!!!   DONT EVEN NEED TO CLIP THE FULL RASTER TO THE FISHNET BECASUE
    arcpy.env.snapRaster = data['pre']['traj']['path']
    arcpy.env.cellsize = data['pre']['traj']['path']
    arcpy.env.outputCoordinateSystem = data['pre']['traj']['path']
    arcpy.env.extent = arcpy.Extent(XMin, YMin, XMax, YMax)

    print 'rws==================================', rws
    print 'cls==================================', cls

    # outData = numpy.zeros((rows,cols), numpy.int16)
    outData = np.zeros((rws, cls), dtype=np.uint8)

    ### create numpy arrays for input datasets cdls and traj
    cdls = {
        2008:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2008',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=rws,
            ncols=cls),
        2009:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2009',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=rws,
            ncols=cls),
        2010:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2010',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=rws,
            ncols=cls),
        2011:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2011',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=rws,
            ncols=cls),
        2012:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2012',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=rws,
            ncols=cls),
        2013:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2013',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=rws,
            ncols=cls),
        2014:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2014',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=rws,
            ncols=cls),
        2015:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2015',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=rws,
            ncols=cls),
        2016:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2016',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=rws,
            ncols=cls),
        2017:
        arcpy.RasterToNumPyArray(
            in_raster=
            'C:\\Users\\Bougie\\Desktop\\Gibbs\\data\\usxp\\ancillary\\raster\\cdl.gdb\\cdl30_2017',
            lower_left_corner=arcpy.Point(XMin, YMin),
            nrows=rws,
            ncols=cls)
    }

    arr_traj = arcpy.RasterToNumPyArray(
        in_raster=data['pre']['traj_yfc']['path'],
        lower_left_corner=arcpy.Point(XMin, YMin),
        nrows=rws,
        ncols=cls)

    # find the location of each pixel labeled with specific arbitray value in the rows list

    for row in traj_list:
        traj_value = row[0]
        #year of conversion for either expansion or abandonmen
        ytx = row[1]
        ##print 'ytx', ytx

        #year before conversion for either expansion or abandonment
        ybx = row[1] - 1
        ##print 'ybx', ybx

        #Return the indices of the pixels that have values of the ytc arbitrsy values of the traj.
        indices = (arr_traj == row[0]).nonzero()

        #stack indices so easier to work with
        stacked_indices = np.column_stack((indices[0], indices[1]))

        #get the x and y location of each pixel that has been selected from above
        for pixel_location in stacked_indices:
            row = pixel_location[0]
            col = pixel_location[1]

            ##print '---------- new pixel to analyze -----------------------------------'
            #get the pixel value for ybx
            pixel_value_ybx = cdls[ybx][row][col]
            #print 'pixel_value_ybx', pixel_value_ybx

            #get the pixel value for ytx
            pixel_value_ytx = cdls[ytx][row][col]
            #print 'pixel_value_ytx', pixel_value_ytx

            #### find the years stil before in the time series for this pixel location
            years_before_list = [i for i in data['global']['years'] if i < ytx]
            #print 'years_before_list', years_before_list
            list_before = []
            for year in years_before_list:
                list_before.append(cdls[year][row][col])
            #print 'list_before', list_before

            #### find the alue of the years after cy!?!
            years_after_list = [i for i in data['global']['years'] if i >= ytx]
            #print 'years_after_list', years_after_list
            list_after = []
            for year in years_after_list:
                list_after.append(cdls[year][row][col])
            #print 'list_after', list_after

            list_entire = list_before + list_after
            # print 'list_entire------------------------------------------->', list_entire
            # print 'list_entire length', len(list_entire)

            fuzzylist = [36, 37, 61, 152, 176]
            fuzzycroplist = [58, 59, 60]
            fruitlist = [
                66, 67, 68, 69, 71, 72, 74, 75, 76, 77, 204, 210, 211, 212,
                218, 220, 223
            ]

            #### dev/fallow ######################################################
            if (np.isin(list_before, noncroplist + [61]).all()) and ((np.isin(
                    list_after, [121, 122, 123, 124])).any() == False):
                outData[row, col] = 101

            #######  fuzzylist  ##############################
            # if(np.isin(list_entire, fuzzylist).all()):
            # 	outData[row,col] = 102

            ###########  fuzzycroplist  ############################
            if (pixel_value_ybx in fuzzycroplist):
                outData[row, col] = 105

            #### fruit mask ##############################################################################################
            if (np.isin(list_before, fruitlist).any()
                    and np.isin(list_after, fruitlist).any()):
                outData[row, col] = 201

            #####  removed from mask for now ###################################################
            # if traj_value == 238:
            # 	# print 'bad traj'
            # 	# outData[row,col] = data['refine']['arbitrary_crop']
            # 	outData[row,col] = 202

            ### rice  ####################################
            #####needs to have rice before AND after to be considered false conversion
            if (np.isin(list_before, [3]).any()) and (np.isin(list_after,
                                                              [3]).any()):
                outData[row, col] = 204

### non-alfalfa  ########################################
            # if(np.isin(list_after, croplist + [37]).all()):
            # 	outData[row,col] = 205

            ### 36_to_37  ###########################################
            if (np.isin(list_before, [36, 37]).all()) and (np.isin(
                    list_after, croplist + [37]).all()):
                outData[row, col] = 206

    arcpy.ClearEnvironment("extent")

    outname = "tile_" + str(fc_count) + '.tif'

    # #create
    outpath = os.path.join("C:/Users/Bougie/Desktop/Gibbs/data/", r"tiles",
                           outname)

    # NumPyArrayToRaster (in_array, {lower_left_corner}, {x_cell_size}, {y_cell_size}, {value_to_nodata})
    myRaster = arcpy.NumPyArrayToRaster(outData,
                                        lower_left_corner=arcpy.Point(
                                            XMin, YMin),
                                        x_cell_size=30,
                                        y_cell_size=30,
                                        value_to_nodata=0)

    ##free memory from outdata array!!
    outData = None

    myRaster.save(outpath)

    myRaster = None
예제 #30
0
def calculateMonteCarloIterationRaster(
        dictactivities, matrixexcelfile, inputfolder, iteration_folder,
        lower_left_corner, dsc, spatial_ref, iteration_outputname,
        oceanrastername_with_path, iterations, starttime, starttimetobeupdated,
        countScoreRasterIsTrue, changeScoreInputs, changeRankingMethod):
    # preset parameters:
    outputrasterlist = []
    outputrasternamelist = []
    outputextrarasterlist = []
    outputextrarasternamelist = []
    dfdesc = pd.read_excel(matrixexcelfile)
    listOfLongActivityNames = dictactivities.keys()
    firstmontecarloraster = True
    # iterate through the Monte Carlo rounds:
    for iteration in range(iterations):
        starttimetobeupdated = gf.resetTime(starttimetobeupdated)
        # create random factors for test 2 (change scoring scale):
        if changeRankingMethod == "true":
            # factor for 2.75:
            factor_2_75 = gf.getRandomValueBetween0And1()
            # factor for 2.50:
            factor_2_50 = gf.getRandomValueBetween0And1()
            # factor for 2.00:
            factor_2_00 = gf.getRandomValueBetween0And1()
            # factor for 1.75:
            factor_1_75 = gf.getRandomValueBetween0And1()
            # factor for 1.50:
            factor_1_50 = gf.getRandomValueBetween0And1()
            # factor for 1.25:
            factor_1_25 = gf.getRandomValueBetween0And1()
            # factor for 1.00:
            factor_1_00 = gf.getRandomValueBetween0And1()
        firstpairwiserasterinround = True
        iterator1 = 0
        iterator2 = 1
        # get pairwise scores and turn them iteratively into total scores for the specific Monte Carlo round:
        for column in listOfLongActivityNames:
            inputrastername1 = dictactivities.get(
                column)  # get marine use raster 1
            while iterator2 in range((iterator1 + 1),
                                     len(listOfLongActivityNames)):
                alldesc_nparray = dfdesc.loc[
                    dfdesc['Idcolumn'] == listOfLongActivityNames[iterator2],
                    column].values
                # get scores:
                if len(alldesc_nparray) > 0:
                    alldesc = alldesc_nparray.item(0)
                    if len(str(alldesc)) >= 4:
                        if alldesc[:1] == "-":
                            spec_score = float(alldesc[:5].replace(",", "."))
                        else:
                            spec_score = float(alldesc[:4].replace(",", "."))
                        inputrastername2 = dictactivities.get(
                            listOfLongActivityNames[iterator2]
                        )  #get marine use raster 2
                        # create baseline total score raster where the score inputs stay unchanged and create an array that tracks the presence of conflicts/synergies:
                        if (firstmontecarloraster
                                == True) and (firstpairwiserasterinround
                                              == True):
                            score_baseline = scoresToRasters3(
                                inputfolder, inputrastername1,
                                inputrastername2, spec_score)
                            cells_with_score_array = np.copy(score_baseline)
                            cells_with_score_array[
                                cells_with_score_array >
                                0.] = 1  #1 is to count the presence of a conflict or synergy
                            cells_with_score_array[
                                cells_with_score_array <
                                0.] = 1  #1 is to count the presence of a conflict or synergy
                            cells_with_score_array = cells_with_score_array.astype(
                                int)
                        elif (firstmontecarloraster == True):
                            score_baseline += scoresToRasters3(
                                inputfolder, inputrastername1,
                                inputrastername2, spec_score)
                            cells_with_score_array = gf.updateScoreCellCount(
                                score_baseline, cells_with_score_array,
                                "montecarlo")
                        # test 1: test score category input variability:
                        if changeScoreInputs == "true":
                            if spec_score == -3.00:
                                spec_score = (spec_score +
                                              np.random.choice([0.00, 1.00]))
                            elif spec_score == -2.00:
                                spec_score = (
                                    spec_score +
                                    np.random.choice([-1.00, 0.00, 1.00]))
                            elif spec_score == -1.00:
                                spec_score = (
                                    spec_score +
                                    np.random.choice([-1.00, 0.00, 2.00]))
                            elif spec_score == 1.00:
                                spec_score = (spec_score + np.random.choice(
                                    [-2.00, 0.00, 0.25, 0.50, 0.75, 1.00]))
                            elif spec_score == 1.25:
                                spec_score = (spec_score + np.random.choice(
                                    [-2.25, -0.25, 0.00, 0.25, 0.50, 0.75]))
                            elif spec_score == 1.50:
                                spec_score = (spec_score + np.random.choice([
                                    -0.50, -0.25, 0.00, 0.25, 0.50, 1.00, 1.25,
                                    1.50
                                ]))
                            elif spec_score == 1.75:
                                spec_score = (spec_score + np.random.choice([
                                    -0.75, -0.50, -0.25, 0.00, 0.25, 0.75,
                                    1.00, 1.25
                                ]))
                            elif spec_score == 2.00:
                                spec_score = (spec_score + np.random.choice([
                                    -1.00, -0.75, -0.50, -0.25, 0.00, 0.50,
                                    0.75, 1.00
                                ]))
                            elif spec_score == 2.50:
                                spec_score = (spec_score + np.random.choice([
                                    -1.50, -1.25, -1.00, -0.75, -0.50, 0.00,
                                    0.25, 0.50
                                ]))
                            elif spec_score == 2.75:
                                spec_score = (spec_score + np.random.choice(
                                    [-0.75, -0.25, 0.00, 0.25]))
                            elif spec_score == 3.00:
                                spec_score = (spec_score + np.random.choice(
                                    [-1.00, -0.50, -0.25, 0.00]))
                        # test 2: test relative difference of scores (scoring scale):
                        if changeRankingMethod == "true":
                            if (spec_score == 2.75):
                                spec_score = 3.0 * factor_2_75
                            elif (spec_score == 2.50):
                                spec_score = (3.0 * factor_2_75) * factor_2_50
                            elif (spec_score == 2.00):
                                spec_score = ((3.0 * factor_2_75) *
                                              factor_2_50) * factor_2_00
                            elif (spec_score == 1.75):
                                spec_score = ((
                                    (3.0 * factor_2_75) * factor_2_50) *
                                              factor_2_00) * factor_1_75
                            elif (spec_score == 1.50):
                                spec_score = (
                                    (((3.0 * factor_2_75) * factor_2_50) *
                                     factor_2_00) * factor_1_75) * factor_1_50
                            elif (spec_score == 1.25):
                                spec_score = ((((
                                    (3.0 * factor_2_75) * factor_2_50) *
                                                factor_2_00) * factor_1_75) *
                                              factor_1_50) * factor_1_25
                            elif (spec_score == 1.00):
                                spec_score = (((
                                    (((3.0 * factor_2_75) * factor_2_50) *
                                     factor_2_00) * factor_1_75) * factor_1_50)
                                              * factor_1_25) * factor_1_00
                            elif (spec_score == -2.00):
                                spec_score = ((-3.0 * factor_2_75) *
                                              factor_2_50) * factor_2_00
                            elif (spec_score == -1.00):
                                spec_score = (((
                                    (((-3.0 * factor_2_75) * factor_2_50) *
                                     factor_2_00) * factor_1_75) * factor_1_50)
                                              * factor_1_25) * factor_1_00
                        # create/update total score raster (score_outras_array) where the score inputs have changed based on the selected test/tests:
                        if firstpairwiserasterinround == True:
                            score_outras_array = scoresToRasters3(
                                inputfolder, inputrastername1,
                                inputrastername2, spec_score)
                            firstpairwiserasterinround = False
                        else:
                            score_outras_array += scoresToRasters3(
                                inputfolder, inputrastername1,
                                inputrastername2, spec_score)
                iterator2 += 1  #iterate through all rasters that are after raster1 (to iteratively be raster2)
            iterator1 += 1  #new raster1
            iterator2 = iterator1 + 1  #start again by iterating through rasters to be raster2 (now the number of following rasters has decreased by 1)
        # a Monte Carlo array is created if not existing already:
        if firstmontecarloraster == True:
            montecarlo_array = np.zeros_like(score_outras_array)
            montecarlo_array = montecarlo_array.astype(float)
            firstmontecarloraster = False
        # the total score raster where the inputs have changed is updated to track the presence of conflicts (value=-1) and the presence of synergies (value=1) in the specific Monte Carlo round:
        score_outras_array[score_outras_array > 0.] = 1
        score_outras_array[score_outras_array < 0.] = -1
        # The Monte Carlo array is counting the number of times in each Monte Carlo round that a raster returns with respectively synergies (value=1), conflicts (value=-1), or not any of the two (value=0):
        montecarlo_array = montecarlo_array + score_outras_array
        (printline,
         starttimetobeupdated) = gf.printTime(starttime, starttimetobeupdated)
        arcpy.AddMessage("iteration {} out of {} iterations, {}\n".format(
            iteration + 1, iterations, printline))
    # Some statistics numpy arrays are produced to adjust the final Monte Carlo raster:
    ocean_array = arcpy.RasterToNumPyArray(
        oceanrastername_with_path)  # read ocean raster
    ocean_array = ocean_array.astype(float)  # convert ocean raster to float
    if_land_in_ocean_array = ocean_array == 0  # get all zero values corresponding to land in the ocean raster
    if_no_score_exist_array = cells_with_score_array == 0  # get all raster cells that have no pairwise scores
    if_positive_baseline = score_baseline > 0.
    if_negative_baseline = score_baseline < 0.
    if_neutral_baseline = score_baseline == 0.
    if_mostly_positive_iterations = montecarlo_array > 0.
    if_average_neutral_iterations = montecarlo_array == 0.
    if_mostly_negative_iterations = montecarlo_array < 0.
    # Output raster 1: Make Monte Carlo main output raster counting the number of times each raster cell returns positive minus negative:
    np.place(
        montecarlo_array, if_land_in_ocean_array & if_no_score_exist_array,
        np.nan
    )  # replace all zero values that are both land and zero in the total binary output of all iterations with NoData
    scoreRasterOption1A = arcpy.NumPyArrayToRaster(montecarlo_array,
                                                   lower_left_corner,
                                                   dsc.meanCellWidth,
                                                   dsc.meanCellHeight)
    (outputrasterlist, outputrasternamelist) = gf.createRaster(
        iteration_folder, iteration_outputname, "_montecarlo_all",
        scoreRasterOption1A, spatial_ref, outputrasterlist,
        outputrasternamelist)
    # Output raster 2 (optional): Make basis score raster where inputs are unchanged
    if (countScoreRasterIsTrue == "true"):
        np.place(score_baseline,
                 if_land_in_ocean_array & if_no_score_exist_array, np.nan)
        basisScoreRaster = arcpy.NumPyArrayToRaster(score_baseline,
                                                    lower_left_corner,
                                                    dsc.meanCellWidth,
                                                    dsc.meanCellHeight)
        gf.createNewPath(iteration_folder)
        (outputextrarasterlist, outputextrarasternamelist) = gf.createRaster(
            iteration_folder, iteration_outputname, "_basis_score",
            basisScoreRaster, spatial_ref, outputextrarasterlist,
            outputextrarasternamelist)
    # Output raster 3: Make Monte Carlo main output raster with robust ("correct") positive/negative/neutral trends:
    option1B_array = np.copy(montecarlo_array)
    np.place(option1B_array,
             ((if_mostly_positive_iterations & if_negative_baseline) |
              (if_mostly_negative_iterations & if_positive_baseline) |
              (if_average_neutral_iterations & if_positive_baseline) |
              (if_average_neutral_iterations & if_negative_baseline) |
              (if_mostly_positive_iterations & if_neutral_baseline) |
              (if_mostly_negative_iterations & if_neutral_baseline)),
             np.nan)  # set wrong values to NoData
    scoreRasterOption1B = arcpy.NumPyArrayToRaster(option1B_array,
                                                   lower_left_corner,
                                                   dsc.meanCellWidth,
                                                   dsc.meanCellHeight)
    (outputrasterlist, outputrasternamelist) = gf.createRaster(
        iteration_folder, iteration_outputname, "_montecarlo_correct",
        scoreRasterOption1B, spatial_ref, outputrasterlist,
        outputrasternamelist)
    # Output raster 4 (optional): Make basis score raster with robust ("correct") positive/negative/neutral trends:
    if (countScoreRasterIsTrue == "true"):
        option1Bscore_array = np.copy(score_baseline)
        np.place(option1Bscore_array,
                 ((if_mostly_positive_iterations & if_negative_baseline) |
                  (if_mostly_negative_iterations & if_positive_baseline) |
                  (if_average_neutral_iterations & if_positive_baseline) |
                  (if_average_neutral_iterations & if_negative_baseline) |
                  (if_mostly_positive_iterations & if_neutral_baseline) |
                  (if_mostly_negative_iterations & if_neutral_baseline)),
                 np.nan)  # set wrong values to NoData
        scoreRasterOption1Bscore = arcpy.NumPyArrayToRaster(
            option1Bscore_array, lower_left_corner, dsc.meanCellWidth,
            dsc.meanCellHeight)
        (outputextrarasterlist, outputextrarasternamelist) = gf.createRaster(
            iteration_folder, iteration_outputname, "_score_correct",
            scoreRasterOption1Bscore, spatial_ref, outputextrarasterlist,
            outputextrarasternamelist)
    # Output raster 5: Make Monte Carlo main output raster with sensitive ("wrong") positive/negative/neutral trends:
    option1C_array = np.copy(montecarlo_array)
    np.place(option1C_array,
             ((if_mostly_positive_iterations & if_positive_baseline) |
              (if_mostly_negative_iterations & if_negative_baseline) |
              (if_average_neutral_iterations & if_neutral_baseline)),
             np.nan)  # set correct values to NoData
    scoreRasterOption1C = arcpy.NumPyArrayToRaster(option1C_array,
                                                   lower_left_corner,
                                                   dsc.meanCellWidth,
                                                   dsc.meanCellHeight)
    (outputrasterlist, outputrasternamelist) = gf.createRaster(
        iteration_folder, iteration_outputname, "_montecarlo_wrong",
        scoreRasterOption1C, spatial_ref, outputrasterlist,
        outputrasternamelist)
    # Output raster 6(optional): Make basis score raster with sensitive ("wrong") positive/negative/neutral trends:
    if (countScoreRasterIsTrue == "true"):
        option1Cscore_array = np.copy(score_baseline)
        np.place(option1Cscore_array,
                 ((if_mostly_positive_iterations & if_positive_baseline) |
                  (if_mostly_negative_iterations & if_negative_baseline) |
                  (if_average_neutral_iterations & if_neutral_baseline)),
                 np.nan)  # set correct values to NoData
        scoreRasterOption1Cscore = arcpy.NumPyArrayToRaster(
            option1Cscore_array, lower_left_corner, dsc.meanCellWidth,
            dsc.meanCellHeight)
        (outputextrarasterlist, outputextrarasternamelist) = gf.createRaster(
            iteration_folder, iteration_outputname, "_score_wrong",
            scoreRasterOption1Cscore, spatial_ref, outputextrarasterlist,
            outputextrarasternamelist)
    return (outputrasterlist, outputrasternamelist, outputextrarasterlist,
            outputextrarasternamelist)