def set_raster_background(input_raster, remove_color): desc = arcpy.Describe(input_raster) if remove_color == 'black': for i in range(desc.bandCount): arcpy.SetRasterProperties_management(input_raster, nodata=str(i + 1) + ' 0') elif remove_color == 'white': for i in range(desc.bandCount): arcpy.SetRasterProperties_management(input_raster, nodata=str(i + 1) + ' 255') elif remove_color == 'both': for i in range(desc.bandCount): arcpy.SetRasterProperties_management(input_raster, nodata=str(i + 1) + ' 255;' + str(i + 1) + ' 0')
def null_define(rastlist, NoData_Value): """ Simple batch NoData setting function. Makes raster data more arcmap viewing friendly Function inputs a list of raster (usually tifs) files and sets no data values. This function does not actually change the raster values in any way, and simply defines which numerical values to be considered NoData in metadata. inputs: rastlist list of files for which to set NoData values. easily created with "core.list_files" function NoData_Value Value to declare as NoData (usually 0 or -9999) Quiet Set Quiet to 'True' if you don't want anything printed to screen. Defaults to 'False' if left blank. """ rastlist = enf_rastlist(rastlist) # iterate through each file in the filelist and set nodata values for rastname in rastlist: arcpy.SetRasterProperties_management(rastname,data_type="#",statistics="#", stats_file="#",nodata="1 "+str(NoData_Value)) print("Set nulls in {0}".format(rastname)) return
def null_define(rastlist, NoData_Value): """ Simple batch NoData setting function. Makes raster data more arcmap viewing friendly Function inputs a list of raster (usually tifs) files and sets no data values. This function does not actually change the raster values in any way, and simply defines which numerical values to be considered NoData in metadata. :param rastlist: list of rasters for which to set nodata value :param NoData_Value: Value to declare as NoData (usually 0 or -9999) :return rastlist: returns list of modified files """ rastlist = enf_rastlist(rastlist) # iterate through each file in the filelist and set nodata values for rastname in rastlist: arcpy.SetRasterProperties_management(rastname, data_type="#", statistics="#", stats_file="#", nodata="1 " + str(NoData_Value)) print("Set nulls in {0}".format(rastname)) return rastlist
def Landslides2Raster(self, landslides_feature, reference_raster, mask): landslidesOUT = "in_memory\\lSimR" arcpy.env.snapRaster = reference_raster arcpy.env.extent = reference_raster.extent # arcpy.FeatureToRaster_conversion(landslides, "tobia", landslidesOUT, cell_size=referenceRaster.meanCellWidth) arcpy.PolygonToRaster_conversion(landslides_feature, "SIM", out_rasterdataset=landslidesOUT, cellsize=reference_raster.meanCellWidth) arcpy.SetRasterProperties_management(landslidesOUT, nodata=[[1, 0]]) return arcpy.Raster(landslidesOUT) * mask
def landslide2raster(self, in_landslides, out_landslide, reference_raster, mask): temp_landslides = os.path.join(self._temp_folder, "lsimr.tif") arcpy.env.snapRaster = reference_raster arcpy.env.extent = reference_raster.extent # arcpy.FeatureToRaster_conversion(landslides, "tobia", landslidesOUT, cell_size=referenceRaster.meanCellWidth) arcpy.PolygonToRaster_conversion( in_features=in_landslides, value_field="SIM", out_rasterdataset=temp_landslides, cellsize=reference_raster.meanCellWidth) arcpy.SetRasterProperties_management(temp_landslides, nodata=[[1, 0]]) out_temp_landslies = arcpy.Raster(temp_landslides) * mask out_temp_landslies.save(out_landslide) # temp_landslides= None arcpy.Delete_management(temp_landslides) return out_landslide
def null_set_range(rastlist, high_thresh=None, low_thresh=None, NoData_Value=None): """ Changes values within a certain range to NoData. similar to ``raster.null_define``, but can take an entire range of values to set to NoData. useful in filtering obviously erroneous high or low values from a raster dataset. :param rastlist: list of rasters for which to set no dta values :param high_thresh: will set all values above this to NoData :param low_thresh: will set all values below this to NoData :return rastlist: list of all rasters modified by this function """ # sanitize filelist input rastlist = enf_rastlist(rastlist) # iterate through each file in the filelist and set nodata values for rastname in rastlist: #load raster as numpy array and save spatial referencing. rast, meta = to_numpy(rastname) if not NoData_Value is None: NoData_Value = meta.NoData_Value if not high_thresh is None: rast[rast >= high_thresh] = NoData_Value if not low_thresh is None: rast[rast <= low_thresh] = NoData_Value from_numpy(rast, meta, rastname) try: arcpy.SetRasterProperties_management(rastname, data_type="#", statistics="#", stats_file="#", nodata="1 " + str(NoData_Value)) except RuntimeError: print("failed to set nodata in {0}".format(rastname)) return
def exportTiff(self, filename, mat): import os.path import gdal, osr from libtiff import TIFF import arcpy mat=np.uint16(mat) xSize=mat.shape[1] ySize=mat.shape[0] step=1 output_file=str(self.pathOutput+filename+'.tif') tiff = TIFF.open(output_file, mode='w') tiff.write_image(mat) tiff.close() arcpy.SetRasterProperties_management(output_file, "GENERIC","#" , "0" , "#")
def null_set_range(rastlist, high_thresh=None, low_thresh=None, NoData_Value=None): """ Changes values within a certain range to NoData similar to raster.null_define, but can take an entire range of values to set to NoData. useful in filtering obviously erroneous high or low values from a raster dataset. inputs: rastlist list of files for which to set NoData values. easily created with "core.list_files" function high_thresh will set all values above this to NoData low_thresh will set all values below this to NoData """ # sanitize filelist input rastlist = enf_rastlist(rastlist) # iterate through each file in the filelist and set nodata values for rastname in rastlist: #load raster as numpy array and save spatial referencing. rast, meta = to_numpy(rastname) if not NoData_Value == None: NoData_Value = meta.NoData_Value if not high_thresh == None: rast[rast >= high_thresh] = NoData_Value if not low_thresh == None: rast[rast <= low_thresh] = NoData_Value from_numpy(rast, meta, rastname) arcpy.SetRasterProperties_management(rastname, data_type="#", statistics="#", stats_file="#", nodata="1 " + str(NoData_Value)) return
def reclassify_raster(raster, threshold, output_raster, threshold_direction='LESS_THAN'): in_true_constant = 1 in_false_constant = 0 rast = arcpy.Raster(raster) if threshold_direction == 'LESS_THAN': out_ras = arcpy.sa.Con(rast < float(threshold), 1) elif threshold_direction == 'EQUALS': out_ras = arcpy.sa.Con(rast == float(threshold), 1) else: out_ras = arcpy.sa.Con(rast > float(threshold), 1) # valid = True # if threshold_direction == 'LESS_THAN': # where_clause = 'VALUE < {0}'.format(threshold) # if rast.maximum < threshold: # # no values returned # valid = False # else: # where_clause = 'VALUE > {0}'.format(threshold) # if rast.minimum > threshold: # # all values would be set to null # valid = False # # # # Execute Con # # out_con = arcpy.sa.Con(in_conditional_raster=raster, in_true_raster_or_constant=in_true_constant, # # in_false_raster_or_constant=in_false_constant, where_clause=where_clause) # # out_con.save(output_raster) # if valid: # out_ras = arcpy.sa.SetNull(in_conditional_raster=raster, in_false_raster_or_constant=1, where_clause=where_clause) # else: # out_ras = arcpy.sa.Con(rast>float(threshold), 1) arcpy.SetRasterProperties_management(out_ras, "GENERIC", nodata=None) out_ras.save(output_raster) del rast return None
def Correction(env, band, radiance, saveDir): specificBand = str(band.split("_")[-1][1]) readGainsOffset(env, specificBand) band_RefMul = GainsOffset['REFLECTANCE_MULT_BAND_' + specificBand] band_RefAdd = GainsOffset['REFLECTANCE_ADD_BAND_' + specificBand] print("{0} has a Gain, Offset and Radiance Values of {1}, {2} and {3} Respectively". format(band, band_RefMul, band_RefAdd, radiance)) print("Setting NoData Value and Executing Radiometric Calibration...") ret_name = band.split("_") bandName = "Scene_" + ret_name[2] + "_" + ret_name[3] + "_" + ret_name[7].strip(".TIF") + "_SurRef.tif" arcpy.SetRasterProperties_management(band, nodata="1 0") bandNom_P1 = arcpy.sa.Times(band, band_RefMul) bandNom_P2 = arcpy.sa.Plus(bandNom_P1, band_RefAdd) bandCor = arcpy.sa.Divide(bandNom_P2, radiance) bandMinVal = float((arcpy.GetRasterProperties_management(in_raster=bandCor, property_type="MINIMUM")).getOutput(0)) bandStdVal = float((arcpy.GetRasterProperties_management(in_raster=bandCor, property_type="STD")).getOutput(0)) bandThreshold = float(bandMinVal + bandStdVal) print("{0} has a Minimum, StdDev and Threshold values of {1}, {2} & {3} Respectively". format(band, bandMinVal, bandStdVal, bandThreshold)) band_refCor = arcpy.sa.Minus(bandCor, bandThreshold) print("Saving Surface Reflectance Output...\n") OutRefName = (os.path.join(saveDir, bandName)) band_refCor.save(OutRefName)
def Correction(env, band, saveName, saveDir): bandSplit = band.split("_") bandValue = str(bandSplit[-1].lstrip("B").strip(".TIF")) bandValueL7 = str(bandSplit[-3].lstrip("B") + "_VCID_" + bandSplit[-1].strip(".TIF")) readGainsOffSet(env, band, bandValue, bandValueL7) G_OValues = [] if band.__contains__("LE07") and band.__contains__("B6"): G_OValues.append(GainsOffset["RADIANCE_MULT_BAND_" + bandValueL7]) G_OValues.append(GainsOffset["RADIANCE_ADD_BAND_" + bandValueL7]) else: G_OValues.append(GainsOffset["RADIANCE_MULT_BAND_" + bandValue]) G_OValues.append(GainsOffset["RADIANCE_ADD_BAND_" + bandValue]) print("{0} has a Gain and Offset Values of {1} & {2} Respectively".format( band, G_OValues[0], G_OValues[1])) print("Setting NoData Value and Executing Radiometric Calibration...") arcpy.SetRasterProperties_management(band, nodata="1 0") bandNom_P1 = Times(band, G_OValues[0]) TOARad = Plus(bandNom_P1, G_OValues[1]) OutRadName = os.path.join(saveDir, saveName) TOARad.save(OutRadName) G_OValues.clear() print("Top of Atmosphere Radiance for " + band + " Saved\n")
def CreateRasters( LocPts, RLoc, num, outFc, Tfolder, Ofolder, Ifolder, Index, Var, PreName, minstr, Spref, Conv, ConvFormula, ConvU, CFolder, CName ): ##### this function will be executed in GWRFunction to create Rasters and Conversion Raster if needed once the Predicted Output shapefile is created. arcpy.env.workspace = Ofolder FIDToXYLocation = {} rows = arcpy.da.SearchCursor(LocPts, ["FID", "X", "Y"]) for row in rows: FIDToXYLocation[row[0]] = [row[1], row[2]] ufdbnamask = arcpy.Raster(RLoc) masklowerleft = arcpy.Point(ufdbnamask.extent.XMin, ufdbnamask.extent.YMin) maskcellsize = ufdbnamask.meanCellWidth npmask = arcpy.RasterToNumPyArray(ufdbnamask) sendrasters = [] convsendrasters = [] sendKML = [] convsendKML = [] npmask.shape newarr = np.full(npmask.shape, 0.0, dtype=np.dtype('Float64')) maskcopy = np.copy(npmask) print "shape" print maskcopy.shape thesefields = arcpy.ListFields(outFc) print "Fields" IDField = "FID" for afield in thesefields: print afield.name if afield.name == "ObjectID": IDField = "ObjectID" with arcpy.da.SearchCursor(outFc, [IDField, "Predicted"]) as rows: # print FIDToXYLocation.keys() for row in rows: # print row[0] # print FIDToXYLocation[row[0]] # print FIDToXYLocation[row[0]][0] X = FIDToXYLocation[row[0]][0] Y = FIDToXYLocation[row[0]][1] newarr[Y, X] = row[1] newras = arcpy.NumPyArrayToRaster(in_array=newarr, lower_left_corner=masklowerleft, x_cell_size=maskcellsize, y_cell_size=maskcellsize, value_to_nodata=0.0) newras.save(Tfolder + "\\outr" + str(num) + ".tif") arcpy.Resample_management(Tfolder + "\\outr" + str(num) + ".tif", Tfolder + "\\outz" + str(num) + ".tif", ".008", "BILINEAR") outCon = Con(IsNull(Tfolder + "\\outz" + str(num) + ".tif"), 0, Tfolder + "\\outz" + str(num) + ".tif") outCon.save(Tfolder + "\\outcon" + str(num) + ".tif") arcpy.DefineProjection_management(Tfolder + "\\outcon" + str(num) + ".tif", coor_system=Spref) opdt = dt.datetime.utcnow() rcraster = arcpy.sa.ReclassByTable( Tfolder + "\\outcon" + str(num) + ".tif", Ifolder + "\\NearTerm.gdb\\RCValTable" + Var, "LowValue", "HighValue", "AssignValue", "NODATA") # rcraster = arcpy.sa.SetNull(rcraster, rcraster, "Value = 0") TifSave = Ofolder + "\\outrrr" + str(Index) + ".tif" rcraster.save(TifSave) ptdt("reclass") ImgSave = Ofolder + "\\outrrr" + str(Index) + ".img" rcraster.save(ImgSave) ptdt("reclass") # Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script # The following inputs are layers or table views: "outrrr0.tif" arcpy.SetRasterProperties_management(in_raster=Ofolder + "\\outrrr" + str(Index) + ".tif", data_type="", statistics="", stats_file="", nodata="1 0", key_properties="") arcpy.AddColormap_management(TifSave, "#", Ifolder + "\\" + Var + ".clr") arcpy.AddColormap_management(ImgSave, "#", Ifolder + "\\" + Var + ".clr") outfilename = Ofolder + "\\" + minstr + "_" + str(Index).zfill( 3) + ".tif" outfilenamewithprefixandsuffix = PreName + "_" + minstr + "_" + str( Index).zfill(3) + ".tif" ptdt(outfilenamewithprefixandsuffix) arcpy.CopyRaster_management(in_raster=TifSave, out_rasterdataset=outfilename, config_keyword="", background_value="", nodata_value=None, onebit_to_eightbit="NONE", colormap_to_RGB="", pixel_type="8_BIT_UNSIGNED", scale_pixel_value="NONE", RGB_to_Colormap="NONE") #####3sendrasters.append([outfilename, outfilenamewithprefixandsuffix]) RasterResult = [outfilename, outfilenamewithprefixandsuffix] TifLyr = PreName + ".lyr" arcpy.MakeRasterLayer_management( outfilename, TifLyr, "", "-179.14732999988 17.675849999878 -49.99532999988 83.123849999878", "") KMLfilename = Ofolder + "\\" + minstr + "_" + str(Index).zfill( 3) + ".kmz" KMLfilenamewithprefixandsuffix = PreName + "_" + minstr + "_" + str( Index).zfill(3) + ".kmz" arcpy.LayerToKML_conversion(TifLyr, KMLfilename, "0", "NO_COMPOSITE", "DEFAULT", "1024", "96") #####sendKML.append([KMLfilename,KMLfilenamewithprefixandsuffix]) KMLResult = [KMLfilename, KMLfilenamewithprefixandsuffix] ##########outfilename = Ofolder + "\\" + minstr + "_" + str(Index).zfill(3) + ".png" ########outfilenamewithprefixandsuffix = PreName + "_" + minstr + "_" + str(Index).zfill(3) + ".png" #########ImgRaster=arcpy.CopyRaster_management(in_raster=TifSave,out_rasterdataset=outfilename,config_keyword="", background_value="", nodata_value=None, onebit_to_eightbit="NONE",colormap_to_RGB="", pixel_type="4_BIT") ###########sendrasters.append([outfilename, outfilenamewithprefixandsuffix]) print "Add Colormap done" # newras.save(out_prediciton_raster) if Conv == "True": rastertoconv = arcpy.Raster(Tfolder + "\\outcon" + str(num) + ".tif") convertedraster = eval( ConvFormula.replace("{raster}", "rastertoconv")) convertedraster.save(Tfolder + "\\outcon" + str(num) + ConvU + ".tif") rcraster = arcpy.sa.ReclassByTable( Tfolder + "\\outcon" + str(num) + ConvU + ".tif", Ifolder + "\\NearTerm.gdb\\RCValTable" + Var + ConvU, "LowValue", "HighValue", "AssignValue", "NODATA") # rcraster = arcpy.sa.SetNull(rcraster, rcraster, "Value = 0") if not os.path.exists(CFolder): os.makedirs(CFolder) CTifSave = CFolder + "\\outrrr" + str(Index) + ConvU + ".tif" rcraster.save(CTifSave) ptdt("reclass") CImgSave = CFolder + "\\outrrr" + str(Index) + ConvU + ".img" rcraster.save(CImgSave) ptdt("reclass") # Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script # The following inputs are layers or table views: "outrrr0.tif" arcpy.SetRasterProperties_management(in_raster=CTifSave, data_type="", statistics="", stats_file="", nodata="1 0", key_properties="") arcpy.AddColormap_management(CTifSave, "#", Ifolder + "\\" + Var + ConvU + ".clr") arcpy.AddColormap_management(CImgSave, "#", Ifolder + "\\" + Var + ConvU + ".clr") outfilename = CFolder + "\\" + minstr + "_" + str(Index).zfill( 3) + ".tif" outfilenamewithprefixandsuffix = CName + "_" + minstr + "_" + str( Index).zfill(3) + ".tif" arcpy.CopyRaster_management(in_raster=CTifSave, out_rasterdataset=outfilename, config_keyword="", background_value="", nodata_value=None, onebit_to_eightbit="NONE", colormap_to_RGB="", pixel_type="8_BIT_UNSIGNED", scale_pixel_value="NONE", RGB_to_Colormap="NONE") ######convsendrasters.append([outfilename, outfilenamewithprefixandsuffix]) ConvRasterResult = [outfilename, outfilenamewithprefixandsuffix] CTifLyr = CName + ".lyr" KMLfilename = CFolder + "\\" + minstr + "_" + str(Index).zfill( 3) + ".kmz" KMLfilenamewithprefixandsuffix = CName + "_" + minstr + "_" + str( Index).zfill(3) + ".kmz" arcpy.MakeRasterLayer_management( outfilename, CTifLyr, "", "-179.14732999988 17.675849999878 -49.99532999988 83.123849999878", "") arcpy.LayerToKML_conversion(CTifLyr, KMLfilename, "0", "NO_COMPOSITE", "DEFAULT", "1024", "96") #####convsendKML.append([KMLfilename,KMLfilenamewithprefixandsuffix]) ConvKMLResult = [KMLfilename, KMLfilenamewithprefixandsuffix] ########outfilename = CFolder + "\\" + minstr + "_" + str(Index).zfill(3) + ".png" ########outfilenamewithprefixandsuffix = CName + "_" + minstr + "_" + str(Index).zfill(3) + ".png" ########arcpy.CopyRaster_management(in_raster= CImgSave,out_rasterdataset=outfilename,config_keyword="", background_value="", nodata_value=None, onebit_to_eightbit="NONE",colormap_to_RGB="", pixel_type="4_BIT") #######convsendrasters.append([outfilename, outfilenamewithprefixandsuffix]) else: ConvRasterResult = None ConvKMLResult = None ptdt("No Conversion Requested") return RasterResult, ConvRasterResult, KMLResult, ConvKMLResult
def _calc_num_days_since(rasters, dslw_fn, dsld_fn, max_days): _counter = 0 # initialise masks - noDataMask is 1 where there is No Data across all rasters, 0 otherwise # since some regions will have No Data for some days, but valid data on other days. Need to # include these regions ALL the time. _no_data_mask = arcpy.sa.Con(arcpy.sa.IsNull(rasters[0]), 1, 0) # Highest Position provides position for all cell values - we need to mask out the dry cells # dryMask is 1 if rainfall is 0 (dry), and -999 if No Data # wetMask is 1 if rainfall is 1 (wet), and -999 if No Data _dry_mask = arcpy.sa.Con(rasters[0], 1, 0, "VALUE <= 0") _wet_mask = arcpy.sa.BooleanNot(_dry_mask) _dry_mask = arcpy.sa.Con(arcpy.sa.IsNull(_dry_mask), -999, _dry_mask) _wet_mask = arcpy.sa.Con(arcpy.sa.IsNull(_wet_mask), -999, _wet_mask) #collect X=numDays consecutive rasters # rasters still contain NoData values _last_x_rasters = [] _last_dry_rasters = [] # make sure we have enough data if _counter + max_days > len(rasters): max_days = len(rasters) - _counter # for i in range(_counter, (_counter + max_days)): # create NoData mask where *all* rasters have NoData _no_data_mask = arcpy.sa.BooleanAnd( _no_data_mask, arcpy.sa.Con(arcpy.sa.IsNull(rasters[i]), 1, 0)) # temporarily set No Data values to -999 so the grid cell isn't ignored # in calculations _temp_raster = arcpy.sa.Con(arcpy.sa.IsNull(rasters[i]), -999, rasters[i]) # _dry_mask is 1 if dry day (<0.5mm rain) OR No Data _dry_mask = arcpy.sa.BooleanAnd( _dry_mask, arcpy.sa.Con(_temp_raster, 1, 0, "VALUE <= 0")) # _wet_mask is 1 if wet day (> threshold) OR No Data _wet_mask = arcpy.sa.BooleanAnd( _wet_mask, arcpy.sa.Con(_temp_raster, 1, 0, "VALUE <> 0")) # raster[i] is 1 if wet, 0 if dry. inverseRaster = 0 if wet, 1 if dry _inverse_raster = arcpy.sa.BooleanNot(rasters[i]) #remove NoData (set to 0) ### temporarily remove this # rasters[i] = arcpy.sa.Con(arcpy.sa.IsNull(rasters[i]), 0, rasters[i]) _inverse_raster = arcpy.sa.Con(arcpy.sa.IsNull(_inverse_raster), 0, _inverse_raster) _last_x_rasters.append(rasters[i]) _last_dry_rasters.append(_inverse_raster) # create raster with number of days since last rain _highest_position_wet = arcpy.sa.HighestPosition(_last_x_rasters) _highest_position_dry = arcpy.sa.HighestPosition(_last_dry_rasters) _days_since_last_wet = _highest_position_wet - 1 _days_since_last_dry = _highest_position_dry - 1 # _days_since_last_dry.save("S:\\WFP2\\PRISM\\data\\Temp\\dsld.tif") # _days_since_last_wet.save("S:\\WFP2\\PRISM\\data\\Temp\\dslw.tif") # _no_data_mask.save("S:\\WFP2\\PRISM\\data\\Temp\\noDataMask.tif") _dry_mask = arcpy.sa.Con(arcpy.sa.IsNull(_dry_mask), -999, _dry_mask) _wet_mask = arcpy.sa.Con(arcpy.sa.IsNull(_wet_mask), -999, _wet_mask) # _wet_mask.save("S:\\WFP2\\PRISM\\data\\Temp\\wetMask.tif") # _dry_mask.save("S:\\WFP2\\PRISM\\data\\Temp\\dryMask.tif") # outHighestPosition.save("S:/WFP/CHIRPS/Daily/2015/p25/lwd/hp.tif") # reset NoData _dslw_no_data = arcpy.sa.SetNull(_no_data_mask, _days_since_last_wet, "VALUE >= 1") _dsld_no_data = arcpy.sa.SetNull(_no_data_mask, _days_since_last_dry, "VALUE >= 1") # outFinal.save(env.workspace + "/lwd/output/outdslw.tif") # outFinal3.save(env.workspace + "/lwd/output/outdsld.tif") _dslw_output = arcpy.sa.Con(_dry_mask, -999, _dslw_no_data, "VALUE >= 1") _dsld_output = arcpy.sa.Con(_wet_mask, -999, _dsld_no_data, "VALUE >= 1") # Save the output arcpy.SetRasterProperties_management(_dsld_output, nodata="1 -999") arcpy.SetRasterProperties_management(_dslw_output, nodata="1 -999") _dslw_output.save(dslw_fn) _dsld_output.save(dsld_fn) return 0
def createRasterDatasetStats(f_path, stat_file_path=None): a = datetime.now() try: # this no data value doesn't apply to all rasters, but easier to just try and move on arcpy.SetRasterProperties_management(f_path, data_type="#", statistics="#", stats_file="#", nodata="1 {}".format( RasterConfig.NODATA_DEFAULT)) except: pass try: arcpy.CalculateStatistics_management(in_raster_dataset=f_path, x_skip_factor="1", y_skip_factor="1", ignore_values="", skip_existing="OVERWRITE", area_of_interest="Feature Set") except Exception as e: arcpy.AddMessage('Could Not Calculate Statistics') arcpy.AddMessage(e) raster_properties = {} rasterObject = arcpy.Raster(f_path) raster_properties[ BAND_COUNT] = rasterObject.bandCount # Integer - The number of bands in the referenced raster dataset. # raster_properties['catalogPath'] = rasterObject.catalogPath # String - The full path and the name of the referenced raster dataset. raster_properties[ COMP_TYPE] = rasterObject.compressionType # String - The compression type. The following are the available types:LZ77,JPEG,JPEG 2000,PACKBITS,LZW,RLE,CCITT GROUP 3,CCITT GROUP 4,CCITT (1D),None. # raster_properties[EXTENT] = rasterObject.extent # Extent - The extent of the referenced raster dataset. raster_properties[ FORMAT] = rasterObject.format # String - The raster format raster_properties[ HAS_RAT] = rasterObject.hasRAT # Boolean - Identifies if there is an associated attribute table: True if an attribute table exists or False if no attribute table exists. raster_properties[ HEIGHT] = rasterObject.height # Integer - The number of rows. raster_properties[ IS_INT] = rasterObject.isInteger # Boolean - The integer state: True if the raster dataset has integer type. raster_properties[ IS_TEMP] = rasterObject.isTemporary # Boolean - The state of the referenced raster dataset: True if the raster dataset is temporary or False if permanent. raster_properties[ MAX] = rasterObject.maximum # Double - The maximum value in the referenced raster dataset. raster_properties[ MEAN] = rasterObject.mean # Double - The mean value in the referenced raster dataset. raster_properties[ MEAN_CELL_HEIGHT] = rasterObject.meanCellHeight # Double - The cell size in the y direction. raster_properties[ MEAN_CELL_WIDTH] = rasterObject.meanCellWidth # Double - The cell size in the x direction. raster_properties[ MIN] = rasterObject.minimum # Double - The minimum value in the referenced raster dataset. #Added to bypass zmin = 'None' error 15 April 2019 BJN if raster_properties[MIN] is None or raster_properties[MIN] < -285: raster_properties[ MIN] = 0 # Double - The minimum value in the referenced raster dataset. raster_properties[ NAME] = rasterObject.name # String - The name of the referenced raster dataset. raster_properties[ NODATA_VALUE] = rasterObject.noDataValue # Double - The NoData value of the referenced raster dataset. raster_properties[ PATH] = rasterObject.path # String - The full path and name of the referenced raster dataset. raster_properties[ PIXEL_TYPE] = rasterObject.pixelType # String - The pixel type of the referenced raster dataset. raster_properties[ SPAT_REF] = rasterObject.spatialReference # SpatialReference - The spatial reference of the referenced raster dataset. raster_properties[ STAND_DEV] = rasterObject.standardDeviation # Double - The standard deviation of the values in the referenced raster dataset. raster_properties[ UNCOMP_SIZE] = rasterObject.uncompressedSize # Double - The size of the referenced raster dataset on disk. raster_properties[ WIDTH] = rasterObject.width # Integer - The number of columns. raster_properties[V_NAME] = None raster_properties[V_UNIT] = None raster_properties[H_NAME] = None raster_properties[H_UNIT] = None raster_properties[H_WKID] = None if rasterObject.spatialReference is not None: raster_properties[V_NAME], raster_properties[ V_UNIT] = Utility.getVertCSInfo(rasterObject.spatialReference) raster_properties[H_NAME] = rasterObject.spatialReference.name raster_properties[ H_UNIT] = rasterObject.spatialReference.linearUnitName raster_properties[H_WKID] = rasterObject.spatialReference.factoryCode raster_properties[XMIN] = rasterObject.extent.XMin raster_properties[YMIN] = rasterObject.extent.YMin raster_properties[XMAX] = rasterObject.extent.XMax raster_properties[YMAX] = rasterObject.extent.YMax valList = [] for key in KEY_LIST: valList.append(raster_properties[key]) keyList = ','.join(KEY_LIST) for i, value in enumerate(valList): valList[i] = str(value) valList = ','.join(valList) # arcpy.AddMessage("\t{}".format(keyList)) # arcpy.AddMessage("\t{}".format(valList)) if stat_file_path is not None: # Output file deleteFileIfExists(stat_file_path) OutFile = open(stat_file_path, 'w') OutFile.write('{}\n{}\n'.format(keyList, valList)) OutFile.close() doTime(a, "\tCreated STATS {}".format(stat_file_path)) doTime(a, "\tCreated STATS {}".format(raster_properties)) return raster_properties
def export_reportimage(imagepath, ordergeometry, auid): ## In memory if os.path.exists(imagepath) == False: arcpy.AddWarning(imagepath + ' DOES NOT EXIST') else: mxd = arcpy.mapping.MapDocument(mxdexport_template) df = arcpy.mapping.ListDataFrames(mxd, '*')[0] sr = arcpy.SpatialReference(4326) df.SpatialReference = sr arcpy.SetRasterProperties_management(imagepath, data_type='PROCESSED') lyrpath = os.path.join(scratch, str(auid) + '.lyr') arcpy.MakeRasterLayer_management(imagepath, lyrpath) image_lyr = arcpy.mapping.Layer(lyrpath) geo_lyr = arcpy.mapping.Layer(ordergeometry) arcpy.mapping.AddLayer(df, geo_lyr, 'TOP') arcpy.mapping.AddLayer(df, image_lyr, 'TOP') image_layer = arcpy.mapping.ListLayers(mxd, "", df)[0] geometry_layer = arcpy.mapping.ListLayers(mxd, 'OrderGeometry', df)[0] geometry_layer.visible = False image_extent = image_layer.getExtent() geo_extent = geometry_layer.getExtent() df.extent = geo_extent if image_collection == 'DOQQ': if int(image_year) in list(range(1990, 2006)): df.scale = 62500 * 1.3 #multiply by 30% to compensate for inaccurate scaling on web mercator projection. 62500 is the scale for 1 quadrangle w_res = 5100 h_res = 6600 elif df.scale > 62500: df.extent = geo_extent df.scale = df.scale * 1.0 try: w_res = 2550 h_res = int((geo_extent.height / geo_extent.width) * w_res) except ZeroDivisionError: w_res = 2550 h_res = 3300 else: df.scale = 25000 w_res = 2550 h_res = 3300 elif image_collection != 'DOQQ': df.extent = image_extent df.scale = ( (df.scale / 100) ) * 85 #very important setting as it defines how much of the image will be displayed to FE try: w_res = 7140 h_res = int((image_extent.height / image_extent.width) * w_res) except ZeroDivisionError: w_res = 5100 h_res = 6600 print image_extent.width, image_extent.height print w_res, h_res arcpy.RefreshActiveView() arcpy.overwriteOutput = True sr2 = arcpy.SpatialReference(4326) desc = arcpy.Describe(lyrpath) bandcount = desc.bandcount jpg_image = image_year + '_' + image_source + '_' + auid + '.jpg' if bandcount == 1: arcpy.mapping.ExportToJPEG(mxd, os.path.join(jpg_image_folder, jpg_image), df, df_export_width=w_res, df_export_height=h_res, world_file=True, color_mode='8-BIT_GRAYSCALE', jpeg_quality=70) else: arcpy.mapping.ExportToJPEG(mxd, os.path.join(jpg_image_folder, jpg_image), df, df_export_width=w_res, df_export_height=h_res, world_file=True, color_mode='24-BIT_TRUE_COLOR', jpeg_quality=70) shutil.copy(os.path.join(jpg_image_folder, jpg_image), os.path.join(scratch, jpg_image)) arcpy.DefineProjection_management( os.path.join(jpg_image_folder, jpg_image), sr2) extent = arcpy.Describe(os.path.join(jpg_image_folder, jpg_image)).extent if AUI_ID == '': clip_stats = getclipflag(image_collection, mxd, df, geo_extent, jpg_image) clip_flag = clip_stats[0] clip_size = clip_stats[1] else: clip_flag = 'Y' clip_size = 0 try: image_extents = str({ "PROCEDURE": Oracle.erisapi_procedures['passclipextent'], "ORDER_NUM": OrderNumText, "AUI_ID": auid, "SWLAT": str(extent.YMin), "SWLONG": str(extent.XMin), "NELAT": (extent.YMax), "NELONG": str(extent.XMax), "INVALID_CLIPIMG_FLAG": clip_flag, "CLIP_IMG_SIZE": str(clip_size) }) message_return = Oracle('test').call_erisapi(image_extents) if message_return[3] != 'Y': raise OracleBadReturn except OracleBadReturn: arcpy.AddError('status: ' + message_return[3] + ' - ' + message_return[2]) mxd.saveACopy(os.path.join(scratch, auid + '_export.mxd')) del mxd
def CreateMasterMosaicDataset(master_fgdb_path, master_md_name, MDMasterFC_path, masterCellSize_meters): Utility.printArguments(["master_fgdb_path", "master_md_name", "MDMasterFC_path", "masterCellSize_meters"], [master_fgdb_path, master_md_name, MDMasterFC_path, masterCellSize_meters], "B02 CreateMasterMosaicDataset") # Ensure the Master gdb exists if os.path.exists(master_fgdb_path): master_md_path = os.path.join(master_fgdb_path, master_md_name) arcpy.AddMessage("Full Master Mosaic Name: {0}".format(master_md_path)) if not arcpy.Exists(master_md_path): # SpatRefMaster = "PROJCS['WGS_1984_Web_Mercator_Auxiliary_Sphere',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator_Auxiliary_Sphere'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],PARAMETER['Auxiliary_Sphere_Type',0.0],UNIT['Meter',1.0],AUTHORITY['EPSG',3857]]" SpatRefMaster = RasterConfig.SpatRef_WebMercator # Create the Master Mosaic Dataset arcpy.CreateMosaicDataset_management(master_fgdb_path, master_md_name, coordinate_system=SpatRefMaster, num_bands="1", pixel_type="32_BIT_FLOAT", product_definition="NONE", product_band_definitions="#") Utility.addToolMessages() # If a boundary is specified (it is optional)... # Write one record to the boundary so it can be subsequently replaced by the import Mosaic Dataset Geometry tool addMasterBoundary(master_fgdb_path, master_md_name, MDMasterFC_path) Raster.addStandardMosaicDatasetFields(md_path=master_md_path) # arcpy.AddField_management(master_md_path, field_name="ProjectID", field_type="TEXT", field_precision="#", field_scale="#", # field_length="100", field_alias="#", field_is_nullable="NULLABLE", field_is_required="NON_REQUIRED", field_domain="#") # Utility.addToolMessages() # arcpy.AddField_management(master_md_path, field_name="ProjectDate", field_type="DATE", field_precision="#", field_scale="#", # field_length="#", field_alias="#", field_is_nullable="NULLABLE", field_is_required="NON_REQUIRED", field_domain="#") # Utility.addToolMessages() # arcpy.AddField_management(master_md_path, field_name="RasterPath", field_type="TEXT", field_precision="#", field_scale="#", # field_length="512", field_alias="#", field_is_nullable="NULLABLE", field_is_required="NON_REQUIRED", field_domain="#") # # Utility.addToolMessages() # arcpy.AddField_management(master_md_path, field_name="ProjectSrs", field_type="TEXT", field_precision="#", field_scale="#", # field_length="100", field_alias="#", field_is_nullable="NULLABLE", field_is_required="NON_REQUIRED", field_domain="#") # Utility.addToolMessages() # arcpy.AddField_management(master_md_path, field_name="ProjectSrsUnits", field_type="TEXT", field_precision="#", field_scale="#", # field_length="20", field_alias="#", field_is_nullable="NULLABLE", field_is_required="NON_REQUIRED", field_domain="#") # Utility.addToolMessages() # arcpy.AddField_management(master_md_path, field_name="ProjectSrsUnitsZ", field_type="TEXT", field_precision="#", field_scale="#", # field_length="20", field_alias="#", field_is_nullable="NULLABLE", field_is_required="NON_REQUIRED", field_domain="#") # Utility.addToolMessages() # arcpy.AddField_management(master_md_path, field_name="ProjectSource", field_type="TEXT", field_precision="#", field_scale="#", # field_length="20", field_alias="#", field_is_nullable="NULLABLE", field_is_required="NON_REQUIRED", field_domain="#") # Utility.addToolMessages() # arcpy.AddField_management(master_md_path, field_name="PCSCode", field_type="TEXT", field_precision="#", field_scale="#", # field_length="20", field_alias="#", field_is_nullable="NULLABLE", field_is_required="NON_REQUIRED", field_domain="#") # Utility.addToolMessages() # arcpy.AddMessage("Creating Indexes on previously created fields in Master GDB...") # Create indexes on all metadata fields to facilitate query # arcpy.AddIndex_management(master_md_path, fields="ProjectID", index_name="ProjectID", unique="NON_UNIQUE", ascending="ASCENDING") # Utility.addToolMessages() # arcpy.AddIndex_management(master_md_path, fields="ProjectDate", index_name="ProjectDate", unique="NON_UNIQUE", ascending="ASCENDING") # Utility.addToolMessages() # arcpy.AddIndex_management(master_md_path, fields="ProjectSrs", index_name="ProjectSrs", unique="NON_UNIQUE", ascending="ASCENDING") # Utility.addToolMessages() # arcpy.AddIndex_management(master_md_path, fields="ProjectSrsUnits", index_name="ProjectSrsUnits", unique="NON_UNIQUE", ascending="ASCENDING") # Utility.addToolMessages() # arcpy.AddIndex_management(master_md_path, fields="ProjectSrsUnitsZ", index_name="ProjectSrsUnitsZ", unique="NON_UNIQUE", ascending="ASCENDING") # Utility.addToolMessages() # arcpy.AddIndex_management(master_md_path, fields="ProjectSource", index_name="ProjectSource", unique="NON_UNIQUE", ascending="ASCENDING") # Utility.addToolMessages() # arcpy.AddIndex_management(master_md_path, fields="PCSCode", index_name="PCSCode", unique="NON_UNIQUE", ascending="ASCENDING") # Utility.addToolMessages() # Set the desired Master MD properties (non-default parameters are listed below): # default mosaic method is "BYATTRIBUTE" w ProjectDate # order_base = 3000 (a year far into the future) # default_compression_type="LERC" # limited the transmission_fields # start_time_field="ProjectDate" (in case we decide to enable time later) # max_num_of_records_returned="2000" (default is 1000) # max_num_of_download_items="40" (default is 20) # max_num_per_mosaic = "40" (default is 20) # data_source_type="ELEVATION" # cell_size = 1 # rows_maximum_imagesize="25000" # columns_maximum_imagesize="25000" # metadata_level = "BASIC" transmissionFields = CMDRConfig.TRANSMISSION_FIELDS arcpy.AddMessage("transmissionFields: {0}".format(transmissionFields)) arcpy.AddRastersToMosaicDataset_management(in_mosaic_dataset=master_md_path, raster_type="Raster Dataset", input_path=RasterConfig.MasterTempRaster, update_cellsize_ranges="UPDATE_CELL_SIZES", update_boundary="UPDATE_BOUNDARY", update_overviews="NO_OVERVIEWS", maximum_pyramid_levels="", maximum_cell_size="0", minimum_dimension="1500", spatial_reference="", filter="#", sub_folder="SUBFOLDERS", duplicate_items_action="ALLOW_DUPLICATES", build_pyramids="NO_PYRAMIDS", calculate_statistics="NO_STATISTICS", build_thumbnails="NO_THUMBNAILS", operation_description="#", force_spatial_reference="NO_FORCE_SPATIAL_REFERENCE") Utility.addToolMessages() arcpy.SetMosaicDatasetProperties_management(master_md_path, rows_maximum_imagesize="25000", columns_maximum_imagesize="25000", allowed_compressions="LERC;JPEG;None;LZ77", default_compression_type="LERC", JPEG_quality="75", LERC_Tolerance="0.001", resampling_type="BILINEAR", clip_to_footprints="NOT_CLIP", footprints_may_contain_nodata="FOOTPRINTS_MAY_CONTAIN_NODATA", clip_to_boundary="NOT_CLIP", color_correction="NOT_APPLY", allowed_mensuration_capabilities="#", default_mensuration_capabilities="NONE", allowed_mosaic_methods="NorthWest;Center;LockRaster;ByAttribute;Nadir;Viewpoint;Seamline;None", default_mosaic_method="ByAttribute", order_field=CMDRConfig.PROJECT_DATE, order_base="3000", sorting_order="ASCENDING", mosaic_operator="FIRST", blend_width="0", view_point_x="600", view_point_y="300", max_num_per_mosaic="40", cell_size_tolerance="0.8", cell_size="{0} {0}".format(masterCellSize_meters), metadata_level="BASIC", transmission_fields=transmissionFields, use_time="DISABLED", start_time_field=CMDRConfig.PROJECT_DATE, end_time_field="#", time_format="#", geographic_transform="#", max_num_of_download_items="40", max_num_of_records_returned="2000", data_source_type="ELEVATION", minimum_pixel_contribution="1", processing_templates="None", default_processing_template="None") Utility.addToolMessages() # arcpy.SetMosaicDatasetProperties_management(in_mosaic_dataset="C:/temp/MDMaster/MDMaster_DSM.gdb/DSM", rows_maximum_imagesize="25000", columns_maximum_imagesize="25000", allowed_compressions="None;JPEG;LZ77;LERC", default_compression_type="None", JPEG_quality="75", LERC_Tolerance="0.001", resampling_type="BILINEAR", clip_to_footprints="NOT_CLIP", footprints_may_contain_nodata="FOOTPRINTS_MAY_CONTAIN_NODATA", clip_to_boundary="NOT_CLIP", color_correction="NOT_APPLY", allowed_mensuration_capabilities="#", default_mensuration_capabilities="NONE", allowed_mosaic_methods="ByAttribute;NorthWest;Center;LockRaster;Nadir;Viewpoint;Seamline;None", default_mosaic_method="ByAttribute", order_field="Project_Date", order_base="3000", sorting_order="ASCENDING", mosaic_operator="FIRST", blend_width="0", view_point_x="600", view_point_y="300", max_num_per_mosaic="40", cell_size_tolerance="0.8", cell_size="1 1", metadata_level="BASIC", transmission_fields="Name;MinPS;MaxPS;LowPS;HighPS;Tag;GroupName;ProductName;CenterX;CenterY;ZOrder;Shape_Length;Shape_Area;Project_ID;Project_Date;Porject_Source;Project_SR_XY;Project_SR_XY_Units;Project_SR_XY_Code;Project_SR_Z_Units", use_time="DISABLED", start_time_field="Project_Date", end_time_field="", time_format="", geographic_transform="", max_num_of_download_items="40", max_num_of_records_returned="2000", data_source_type="ELEVATION", minimum_pixel_contribution="1", processing_templates="None", default_processing_template="None") # set statistics Min = -300 and Max = 2000M # set nodata = default no data value arcpy.SetRasterProperties_management(master_md_path, data_type="ELEVATION", statistics="1 0 2000 # #", stats_file="#", nodata="1 {}".format(RasterConfig.NODATA_DEFAULT)) Utility.addToolMessages() arcpy.RemoveRastersFromMosaicDataset_management(in_mosaic_dataset=master_md_path, where_clause="1=1", update_boundary="UPDATE_BOUNDARY", mark_overviews_items="MARK_OVERVIEW_ITEMS", delete_overview_images="DELETE_OVERVIEW_IMAGES", delete_item_cache="DELETE_ITEM_CACHE", remove_items="REMOVE_MOSAICDATASET_ITEMS", update_cellsize_ranges="UPDATE_CELL_SIZES") Utility.addToolMessages() else: arcpy.AddWarning("Master Mosaic Dataset already exists: {0}. Cannot continue".format(master_md_path)) else: arcpy.AddError("Master Geodatabase doesn't exist {0}".format(master_fgdb_path)) arcpy.AddMessage("Operation complete")
def RevalueRaster(f_path, elev_type, raster_props, target_path, publish_path, minZ, maxZ, bound_path, spatial_ref=None): arcpy.AddMessage("RevalueRaster {} {}: ZRange({},{})".format(elev_type, f_path,minZ,maxZ)) Utility.setArcpyEnv(is_overwrite_output=True) a = datetime.now() nodata = RasterConfig.NODATA_DEFAULT isInt = (elev_type == INT) if isInt: minZ, maxZ = 0, 255 arcpy.AddMessage("RevalueRaster type {} is intensity {}: ZRange({},{})".format(elev_type, f_path,minZ,maxZ)) f_name, target_f_path, publish_f_path, stat_out_folder, stat_file_path, bound_out_folder, vector_bound_path = getFilePaths(f_path, elev_type, target_path, publish_path) # @UnusedVariable # target_f_left, target_f_right = os.path.splitext(target_f_path) # target1_f_path = "{}1{}".format(target_f_left, target_f_right) publish_f_left, publish_f_right = os.path.splitext(publish_f_path) publish1_f_path = "{}1{}".format(publish_f_left, publish_f_right) # Don't maintain fGDB raster format, update to TIFF # if raster_props[FORMAT] == "FGDBR": # target_f_path = "{}.TIF".format(target_f_path) if raster_props[BAND_COUNT] <> 1: arcpy.AddMessage("Skipping Raster {}, not 1 band image.".format(f_path)) else: # Intensity may be another type if not isInt and not (raster_props[PIXEL_TYPE] == PIXEL_TYPE_F32 or raster_props[PIXEL_TYPE] == PIXEL_TYPE_D64): arcpy.AddMessage("Skipping Raster '{}', '{}' not Float32 type image.".format(f_path, raster_props[PIXEL_TYPE])) else: if not (raster_props[FORMAT] == "TIFF" or raster_props[FORMAT] == "GRID" or raster_props[FORMAT] == "IMAGINE Image" or raster_props[FORMAT] == "FGDBR"): arcpy.AddMessage("Skipping Raster '{}', '{}' not supported image format.".format(f_path, raster_props[FORMAT])) else: if arcpy.Exists(target_f_path): arcpy.AddMessage("\tDerived Raster exists: {}".format(target_f_path)) else: deleteFileIfExists(target_f_path, True) arcpy.AddMessage("\tSaving derived raster to {}".format(target_f_path)) # Compression isn't being applied properly so results are uncompressed rasterObject = arcpy.Raster(f_path) if isInt: mean = rasterObject.mean stdDev = rasterObject.standardDeviation maximumPixel = mean + (stdDev * 2) linearTransform = arcpy.sa.TfLinear(maximum=maximumPixel, upperThreshold=maximumPixel) outRescale = arcpy.sa.RescaleByFunction(rasterObject, linearTransform, minZ, maxZ) outRescale.save(target_f_path) del outRescale, rasterObject else: outSetNull = arcpy.sa.Con(((rasterObject >= (float(minZ))) & (rasterObject <= (float(maxZ)))), f_path) # @UndefinedVariable outSetNull.save(target_f_path) del outSetNull, rasterObject if spatial_ref is not None: arcpy.AddMessage("Applying projection to raster '{}' {}".format(target_f_path, spatial_ref)) if str(spatial_ref).lower().endswith(".prj"): arcpy.AddMessage("loading spatial reference from prj file '{}'".format(spatial_ref)) spatial_ref = arcpy.SpatialReference(spatial_ref) arcpy.AddMessage("loaded spatial reference from prj file '{}'".format(spatial_ref)) # 3/22/18 - Handle UTF-8 Encoding - 'u\u2013' From MI Delta try: arcpy.AddMessage("Applying projection '{}'".format( spatial_ref)) arcpy.AddMessage("Applying string projection '{}'".format( spatial_ref.exportToString())) arcpy.AddMessage("Applying encoded projection '{}'".format( spatial_ref.exportToString().encode('utf-8'))) except Exception as e: arcpy.AddMessage('Error: {}'.format(e)) arcpy.DefineProjection_management(in_dataset=target_f_path, coor_system=spatial_ref) # Set the no data default value on the input raster arcpy.SetRasterProperties_management(in_raster=target_f_path, data_type="ELEVATION", nodata="1 {}".format(nodata)) arcpy.CalculateStatistics_management(in_raster_dataset=target_f_path, x_skip_factor="1", y_skip_factor="1", ignore_values="", skip_existing="OVERWRITE", area_of_interest="Feature Set") # arcpy.BuildPyramidsandStatistics_management(in_workspace=target_f_path, # build_pyramids="BUILD_PYRAMIDS", # calculate_statistics="CALCULATE_STATISTICS", # BUILD_ON_SOURCE="BUILD_ON_SOURCE", # pyramid_level="-1", # SKIP_FIRST="NONE", # resample_technique="BILINEAR", # compression_type="LZ77", # compression_quality="75", # skip_existing="SKIP_EXISTING") # make sure we make a new published copy of this if arcpy.Exists(publish_f_path): arcpy.Delete_management(publish_f_path) a = doTime(a, "\tCopied '{}' to '{}' with valid values between {} and {}".format(f_path, target_f_path, minZ, maxZ)) if arcpy.Exists(publish_f_path): arcpy.AddMessage("\tPublish Raster exists: {}".format(publish_f_path)) else: arcpy.AddMessage("\tCopy and clip published raster from {} to {}".format(target_f_path, publish1_f_path)) a = datetime.now() deleteFileIfExists(publish1_f_path, True) deleteFileIfExists(publish_f_path, True) # arcpy.RasterToOtherFormat_conversion(target_f_path, publish_f_path, Raster_Format="TIFF") arcpy.CopyRaster_management(in_raster=target_f_path, out_rasterdataset=publish1_f_path, config_keyword="", background_value="", nodata_value=nodata, onebit_to_eightbit="NONE", colormap_to_RGB="NONE", pixel_type="32_BIT_FLOAT", scale_pixel_value="NONE", RGB_to_Colormap="NONE", format="TIFF", transform="NONE") arcpy.AddMessage("\tCliping temp raster {} to {}".format(publish1_f_path, publish_f_path)) arcpy.Clip_management(in_raster=publish1_f_path, out_raster=publish_f_path, in_template_dataset=bound_path, nodata_value=nodata, clipping_geometry="ClippingGeometry", maintain_clipping_extent="NO_MAINTAIN_EXTENT") deleteFileIfExists(publish1_f_path, True) arcpy.SetRasterProperties_management(in_raster=publish_f_path, data_type="ELEVATION", nodata="1 {}".format(nodata)) arcpy.CalculateStatistics_management(in_raster_dataset=publish_f_path, x_skip_factor="1", y_skip_factor="1", ignore_values="", skip_existing="OVERWRITE", area_of_interest="Feature Set") # arcpy.BuildPyramidsandStatistics_management(in_workspace=publish_f_path, # build_pyramids="BUILD_PYRAMIDS", # calculate_statistics="CALCULATE_STATISTICS", # BUILD_ON_SOURCE="BUILD_ON_SOURCE", # pyramid_level="-1", # SKIP_FIRST="NONE", # resample_technique="BILINEAR", # compression_type="LZ77", # compression_quality="75", # skip_existing="SKIP_EXISTING") a = doTime(a, "\tCopied '{}' to '{}'".format(target_f_path, publish_f_path))
# Created on: 2015-05-12 11:21:44.00000 # (generated by ArcGIS/ModelBuilder) # Description: # --------------------------------------------------------------------------- # Import arcpy module import arcpy import os # Dynamic variables: rasterFolder = arcpy.GetParameterAsText(0) # Set the workspace environment to local file geodatabase arcpy.env.workspace = rasterFolder rasters = arcpy.ListRasters() # Process: Define Projection if rasters != None: tot = len(rasters) arcpy.AddMessage("{0} rasters found in input folder".format(str(tot))) i = 0.0 for raster in rasters: i = i + 1.0 # Process: Set Raster Properties arcpy.SetRasterProperties_management(raster, "GENERIC", "", "", "1 255") msg = "{0:.2%} : No-Data for raster {1} was set to 255.".format( (i / tot), str(raster)) arcpy.AddMessage(msg)
def export_geotiff(imagedict, ordergeometry, image_comment): arcpy.AddMessage("Adding to template: " + str(imagedict)) mxd = arcpy.mapping.MapDocument(mxdexport_template) df = arcpy.mapping.ListDataFrames(mxd, '*')[0] geo_lyr = arcpy.mapping.Layer(ordergeometry) arcpy.mapping.AddLayer(df, geo_lyr, 'TOP') ordered_all_values = imagedict.keys() ordered_all_values.sort() print ordered_all_values for order_value in ordered_all_values: auid = imagedict[order_value][0] image_source = imagedict[order_value][1] imagepath = imagedict[order_value][2] image_background = imagedict[order_value][3].lower() arcpy.SetRasterProperties_management(imagepath, data_type='PROCESSED') set_raster_background(imagepath, image_background) img_sr = arcpy.Describe(imagepath).spatialReference print img_sr.name if img_sr.name == 'Unknown' or img_sr.name == 'GCS_Unknown': arcpy.DefineProjection_management(imagepath, 4326) lyrpath = os.path.join(scratch, str(auid) + '.lyr') arcpy.MakeRasterLayer_management(imagepath, lyrpath) image_lyr = arcpy.mapping.Layer(lyrpath) arcpy.mapping.AddLayer(df, image_lyr, 'TOP') if FactoryCode == '': sr = arcpy.SpatialReference(3857) #web mercator elif FactoryCode == 'UTM': sr = arcpy.GetUTMFromLocation(centroidX, centroidY) else: sr = arcpy.SpatialReference(int(FactoryCode)) df.spatialReference = sr geometry_layer = arcpy.mapping.ListLayers(mxd, 'OrderGeometry', df)[0] geometry_layer.visible = False geo_extent = geometry_layer.getExtent(True) df.extent = geo_extent MapScale = 6000 if PrintScale is not None: df.scale = PrintScale MapScale = PrintScale else: arcpy.AddError('No scale set for order') export_width = 5100 export_height = 6600 arcpy.RefreshActiveView() arcpy.overwriteOutput = True if image_comment != "": report_image_name = image_year + '_' + image_source + '_' + UserMapScale + '_' + image_comment + '.tif' else: report_image_name = image_year + '_' + image_source + '_' + UserMapScale + '.tif' arcpy.AddMessage("Exporting: " + report_image_name) arcpy.env.pyramid = "NONE" arcpy.mapping.ExportToTIFF(mxd, os.path.join(job_fin, report_image_name), df, df_export_width=export_width, df_export_height=export_height, world_file=False, color_mode='24-BIT_TRUE_COLOR', tiff_compression='LZW', geoTIFF_tags=True) arcpy.DefineProjection_management(os.path.join(job_fin, report_image_name), sr) print "projecting" wgs84mxd = arcpy.mapping.MapDocument(wgs84_template) image_report = arcpy.mapping.Layer(os.path.join(job_fin, report_image_name)) df = arcpy.mapping.ListDataFrames(wgs84mxd, '*')[0] arcpy.mapping.AddLayer(df, image_report, 'TOP') imagetodesc = arcpy.mapping.ListLayers(wgs84mxd, '*', df)[0] extent = arcpy.Describe(imagetodesc).extent centerlong = round(extent.XMin + (extent.XMax - extent.XMin) / 2, 7) centerlat = round(extent.YMin + (extent.YMax - extent.YMin) / 2, 7) set_imagedetail(extent, centerlat, centerlong, report_image_name)
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
def set_raster_background(input_raster): desc = arcpy.Describe(input_raster) for i in range(desc.bandCount): arcpy.SetRasterProperties_management(input_raster, nodata=str(i + 1) + ' 255')