Exemple #1
0
def output(kind, zField):  # Processing
    global cell_size, doPoints, doRaster, doASC

    if doOverWrite:
        cleanDirs(kind)

    env.workspace = path + kind + ".gdb"
    env.snapRaster = path + "Area\\Area.tif"
    env.extent = path + "Area\\Area.tif"
    env.parallelProcessingFactor = "0"
    points = path + "Area\\points.shp"
    locations = path + kind + "\\"
    dirs = os.listdir(locations)

    for file in dirs:
        in_point_features = file.rstrip('.csv')

        try:
            out = Kriging(in_point_features, zField, KrigingModelOrdinary(),
                          cell_size)
        except Exception as e:
            out = Idw(in_point_features, zField, cell_size)
            pass
        midRaster = path + kind + "MID\\" + "MID_" + file.rstrip(
            '.csv') + ".tif"
        out.save(midRaster)

        if doPoints:  # Different situations
            outPoint = path + kind + "MID\\Points\\" + file.rstrip(
                ".csv") + ".shp"
            ExtractValuesToPoints(points, midRaster, outPoint)
            outExcel = path + kind + "XLS\\" + file.rstrip(".csv") + ".xls"
            arcpy.TableToExcel_conversion(outPoint, outExcel)
            pass

        if doRaster and not doASC:
            clipped = path + kind + "TIF\\" + file.rstrip(".csv") + ".tif"
            raster = ExtractByMask(midRaster, path + "Area\\Area.tif")
            raster.save(clipped)
            continue

        if doRaster and doASC:
            clipped = path + kind + "TIF\\" + file.rstrip(".csv") + ".tif"
            raster = ExtractByMask(midRaster, path + "Area\\Area.tif")
            raster.save(clipped)
            outASCII = path + kind + "ASC\\" + file.rstrip('.csv') + '.asc'
            arcpy.RasterToASCII_conversion(clipped, outASCII)
            continue

        if not doRaster and doASC:
            clipped = path + kind + "MID\\Raster\\" + file.rstrip(
                ".csv") + ".tif"
            raster = ExtractByMask(midRaster, path + "Area\\Area.tif")
            raster.save(clipped)
            outASCII = path + kind + "ASC\\" + file.rstrip('.csv') + '.asc'
            arcpy.RasterToASCII_conversion(clipped, outASCII)
            continue
    return
Exemple #2
0
def grass_cwd(core_list):
    """Creating CWD and Back rasters using GRASS r.walk function"""
    cur_path = subprocess.Popen("echo %PATH%",
                                stdout=subprocess.PIPE,
                                shell=True).stdout.read()
    gisdbase = os.path.join(cc_env.proj_dir, "gwksp")

    ccr_grassrc = os.path.join(cc_env.proj_dir, "ccr_grassrc")
    climate_asc = os.path.join(cc_env.out_dir, "cc_climate.asc")
    resist_asc = os.path.join(cc_env.out_dir, "cc_resist.asc")
    core_asc = os.path.join(cc_env.out_dir, "cc_cores.asc")
    climate_lyr = "climate"
    resist_lyr = "resist"
    core_lyr = "cores"

    try:
        lm_util.gprint("\nRUNNING GRASS TO CREATE COST-WEIGHTED DISTANCE "
                       "RASTERS")

        # Convert input GRID rasters to ASCII
        lm_util.gprint("Converting ARCINFO GRID rasters to ASCII")
        # Note: consider moving these to main:
        arcpy.RasterToASCII_conversion(cc_env.prj_climate_rast, climate_asc)
        arcpy.RasterToASCII_conversion(cc_env.prj_resist_rast, resist_asc)
        arcpy.RasterToASCII_conversion(cc_env.prj_core_rast, core_asc)

        # Create resource file and setup workspace
        write_grassrc(ccr_grassrc, gisdbase)

        setup_wrkspace(gisdbase, ccr_grassrc, climate_asc)

        # Make cwd folder for Linkage Mapper
        lm_util.make_cwd_paths(max(core_list))

        # Import files into GRASS
        lm_util.gprint("Importing raster files into GRASS")
        run_grass_cmd("r.in.arc", input=climate_asc, output=climate_lyr)
        run_grass_cmd("r.in.arc", input=resist_asc, output=resist_lyr)
        run_grass_cmd("r.in.arc", input=core_asc, output=core_lyr)

        # Generate CWD and Back rasters
        gen_cwd_back(core_list, climate_lyr, resist_lyr, core_lyr)

    except Exception:
        raise
    finally:
        os.environ['PATH'] = cur_path  # Revert to original windows path
        if not cc_util.remove_grass_wkspc(gisdbase):
            arcpy.AddWarning("Unable to delete temporary GRASS folder. "
                             "Program will contine.")
        cc_util.delete_features(
            [climate_asc, resist_asc, core_asc, ccr_grassrc])
def grass_cwd(core_list):
    """Create CWD and Back rasters using GRASS."""
    out_fldr = cc_env.scratch_dir

    gisdbase = os.path.join(out_fldr, "cc_grass")

    ccr_grassrc = os.path.join(out_fldr, "cc_grassrc")
    climate_asc = os.path.join(out_fldr, "cc_gclimate.asc")
    resist_asc = os.path.join(out_fldr, "cc_gresist.asc")
    core_asc = os.path.join(out_fldr, "cc_gcores.asc")
    climate_lyr = "climate"
    resist_lyr = "resist"
    core_lyr = "cores"

    try:
        lm_util.gprint("\nRUNNING GRASS TO CREATE COST-WEIGHTED DISTANCE "
                       "RASTERS")

        start_path = os.environ["PATH"]

        # Convert input GRID rasters to ASCII
        lm_util.gprint("Converting ARCINFO GRID rasters to ASCII")
        arcpy.RasterToASCII_conversion(cc_env.prj_climate_rast, climate_asc)
        arcpy.RasterToASCII_conversion(cc_env.prj_resist_rast, resist_asc)
        arcpy.RasterToASCII_conversion(cc_env.prj_core_rast, core_asc)

        # Create resource file and setup workspace
        os.environ["PATH"] = cc_env.gpath
        write_grassrc(ccr_grassrc, gisdbase)
        setup_wrkspace(gisdbase, ccr_grassrc, climate_asc)

        # Make cwd folder/s for Linkage Mapper
        lm_util.make_raster_paths(max(core_list), lm_env.CWDBASEDIR,
                                  lm_env.CWDSUBDIR_NM)

        # Import files into GRASS
        lm_util.gprint("Importing raster files into GRASS")
        run_grass_cmd("r.in.gdal", input=climate_asc, output=climate_lyr)
        run_grass_cmd("r.in.gdal", input=resist_asc, output=resist_lyr)
        run_grass_cmd("r.in.gdal", input=core_asc, output=core_lyr)

        # Generate CWD and Back rasters
        gen_cwd_back(core_list, climate_lyr, resist_lyr, core_lyr)

    except Exception:
        raise
    finally:
        os.environ["PATH"] = start_path
        for data in ([
                gisdbase, ccr_grassrc, climate_asc, resist_asc, core_asc
        ]):
            lm_util.delete_data(data)
Exemple #4
0
def grass_cwd(core_list):
    """Creating CWD and Back rasters using GRASS r.walk function"""
    gisdbase = os.path.join(cc_env.proj_dir, "gwksp")

    ccr_grassrc = os.path.join(cc_env.proj_dir, "ccr_grassrc")
    climate_asc = os.path.join(cc_env.out_dir, "cc_climate.asc")
    resist_asc = os.path.join(cc_env.out_dir, "cc_resist.asc")
    core_asc = os.path.join(cc_env.out_dir, "cc_cores.asc")
    climate_lyr = "climate"
    resist_lyr = "resist"
    core_lyr = "cores"

    try:
        lm_util.gprint("\nRUNNING GRASS TO CREATE COST-WEIGHTED DISTANCE "
                       "RASTERS")

        # Convert input GRID rasters to ASCII
        lm_util.gprint("Converting ARCINFO GRID rasters to ASCII")
        arcpy.RasterToASCII_conversion(cc_env.prj_climate_rast, climate_asc)
        arcpy.RasterToASCII_conversion(cc_env.prj_resist_rast, resist_asc)
        arcpy.RasterToASCII_conversion(cc_env.prj_core_rast, core_asc)

        # Create resource file and setup workspace
        start_path = os.environ["PATH"]
        os.environ["PATH"] = cc_env.gpath
        write_grassrc(ccr_grassrc, gisdbase)
        setup_wrkspace(gisdbase, ccr_grassrc, climate_asc)

        # Make cwd folder for Linkage Mapper
        lm_util.make_cwd_paths(max(core_list))

        # Import files into GRASS
        lm_util.gprint("Importing raster files into GRASS")
        run_grass_cmd("r.in.gdal", input=climate_asc, output=climate_lyr)
        run_grass_cmd("r.in.gdal", input=resist_asc, output=resist_lyr)
        run_grass_cmd("r.in.gdal", input=core_asc, output=core_lyr)

        # Generate CWD and Back rasters
        gen_cwd_back(core_list, climate_lyr, resist_lyr, core_lyr)

    except Exception:
        raise
    finally:
        os.environ["PATH"] = start_path
        if not cc_util.remove_grass_wkspc(gisdbase):
            lm_util.warn("Unable to delete temporary GRASS folder. "
                             "Program will contine.")
        cc_util.delete_features(
            [climate_asc, resist_asc, core_asc, ccr_grassrc])
def EToASC():
    ## Interpolation points data into a raster(by Kriging Method), then clip it as the research area, finally output the raster as .asc
    global cell_size
    env.workspace = "E:/Project/ETo.gdb/"
    env.snapRaster = "E:/Project/Default.gdb/DEM_Hydro1K_Fin"
    env.extent = "E:/Project/Default.gdb/DEM_Hydro1K_Fin"
    env.parallelProcessingFactor = "100%"

    locations = "E:/Project/ETo/"
    z_field = "ETo"

    dirs = os.listdir(locations)
    for file in tqdm(dirs):
        in_point_features = file.rstrip('.csv')
        # out =  Kriging (in_point_features, z_field, KrigingModelOrdinary(),cell_size)
        try:
            out = Kriging(in_point_features, z_field, KrigingModelOrdinary(),
                          cell_size)
        except Exception as e:
            out = Idw(in_point_features, z_field, cell_size)
            pass
        outname = "E:/Project/EToMID/" + "Re_" + file.rstrip('.csv') + ".tif"
        out.save(outname)
        exportname = "E:/Project/EToMID/CLIPPED/Clipped_" + file.rstrip(
            '.csv') + ".tif"
        export = ExtractByMask(outname, "E:/Project/Default.gdb/Location")
        export.save(exportname)
        name = file.rstrip('.csv')
        outASCII = "E:/Project/EToASC/" + name + '.asc'
        arcpy.RasterToASCII_conversion(exportname, outASCII)
Exemple #6
0
    def write_raster(self, array, output_name, directory=''):
        """Write raster (numpy array) to ASCII file.

        :param array: numpy array
        :param output_name: output filename
        :param directory: directory where to write output file
        """
        file_output = self._raster_output_path(output_name, directory)

        # prevent call globals before values assigned
        if (GridGlobals.xllcorner == None):
            raise GlobalsNotSet()
        if (GridGlobals.yllcorner == None):
            raise GlobalsNotSet()
        if (GridGlobals.dx == None):
            raise GlobalsNotSet()
        if (GridGlobals.dy == None):
            raise GlobalsNotSet()

        lower_left = arcpy.Point(
            GridGlobals.xllcorner,
            GridGlobals.yllcorner,
        )
        raster = arcpy.NumPyArrayToRaster(
            array,
            lower_left,
            GridGlobals.dx,
            GridGlobals.dy,
            value_to_nodata=GridGlobals.NoDataValue)
        arcpy.RasterToASCII_conversion(raster, file_output)

        self._print_array_stats(array, file_output)
    def execute(self, parameters, messages):
        arcpy.AddMessage("Batch convert rasters to ASCII and MXE formats")
        for param in parameters:
            arcpy.AddMessage("Parameter: %s = %s" %
                             (param.name, param.valueAsText))

        input_directory = parameters[0].valueAsText
        output_directory = parameters[1].valueAsText
        out_mxe = parameters[2].valueAsText
        path_mxe = parameters[3].valueAsText

        if not os.path.exists(output_directory):
            os.makedirs(output_directory)

        arcpy.env.workspace = input_directory
        rasterlist = arcpy.ListRasters("*")
        arcpy.AddMessage("There are " + str(len(rasterlist)) +
                         " rasters to process.")

        for raster in rasterlist:
            arcpy.AddMessage("Converting " + str(raster) + ".")
            if not arcpy.Exists(os.path.join(output_directory,
                                             raster + ".asc")):
                arcpy.RasterToASCII_conversion(
                    raster, os.path.join(output_directory, raster + ".asc"))

        if out_mxe:
            command = 'java -cp "' + os.path.join(path_mxe, "maxent.jar") + '" density.Convert ' + \
                      output_directory + " asc " + output_directory + " mxe"
            arcpy.AddMessage("Calling Maxent to convert ASCII to MXE: " +
                             str(command))
            os.system(str(command))

        return
Exemple #8
0
def Raster2ASCII(workDir, inRaster):
    # Name: Raster2ASCII.py
    # Description: Converts a raster dateset to ASCII features.
    # Requirement: None
    # Input: working Dir, r"c:\work\path\to\dir\"
    #        Raster filename
    # Output: inRaster+'.tif'
    # Dr. Liang Kuang, 2016/02/26

    #Import system modules
    import os
    from os import path
    import arcpy
    from arcpy import env

    # Set environment settings
    env.workspace = workDir
    #env.workspace = r"R:\Projects\ESTOFS_for_Micronesia\Data\Bathymetry\ngdc_bathy\BAG\NorthernMarianas"

    # Set local variables
    #inRaster = "W00157_MB_8m_MLLW_Combined1.tif"
    strList = inRaster.split('.')
    outPoint = strList[0] + '.tif'

    # Execute Raster2ASCII
    arcpy.RasterToASCII_conversion(inRaster, outPoint)

    print('Raster successfully converted to ASCII feature!')
Exemple #9
0
    def FlowAccumulationDinf(self, ang, Qsurf, numpy_array_location):

        # Inputs
        inlyr = ang
        desc = arcpy.Describe(inlyr)
        ang=str(desc.catalogPath)
        arcpy.AddMessage("\nInput Dinf Flow Direction file: "+ang)

        # Execute flow direction and slope on the first day of model operation or at the end of each month
        output_asc = numpy_array_location + "\output_asc.asc"
        output_tiff = numpy_array_location + "\output_tif.tif"
        Qsurf_ascii = arcpy.RasterToASCII_conversion(Qsurf, output_asc)
        Qsurf_tiff = arcpy.ASCIIToRaster_conversion(Qsurf_ascii, output_tiff, "FLOAT")

        weightgrid= Qsurf_tiff
        if arcpy.Exists(weightgrid):
            desc = arcpy.Describe(weightgrid)
            wtgr=str(desc.catalogPath)
            arcpy.AddMessage("\nInput Weight Grid: "+wtgr)

        edgecontamination='false'
        arcpy.AddMessage("\nEdge Contamination: "+edgecontamination)

        # Input Number of Processes
        inputProc=str(8)
        arcpy.AddMessage("\nInput Number of Processes: "+inputProc)

        # Output
        sca = str(numpy_array_location + "\osca.tif")
        arcpy.AddMessage("\nOutput Dinf Specific Catchment Area Grid: "+sca)

        # Construct command
        cmd = 'mpiexec -n ' + inputProc + ' AreaDinf -ang ' + '"' + ang + '"' + ' -sca ' + '"' + sca + '"'

        if arcpy.Exists(weightgrid):
            cmd = cmd + ' -wg ' + '"' + wtgr + '"'
        if edgecontamination == 'false':
            cmd = cmd + ' -nc '
    
        arcpy.AddMessage("\nCommand Line: "+cmd)

        # Submit command to operating system
        os.system(cmd)

        # Capture the contents of shell command and print it to the arcgis dialog box
        process=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
        arcpy.AddMessage('\nProcess started:\n')
        for line in process.stdout.readlines():
            arcpy.AddMessage(line)

        # Calculate statistics on the output so that it displays properly
        arcpy.AddMessage('Executing: Calculate Statistics\n')
        arcpy.CalculateStatistics_management(sca)
        
        return sca
def Resize():
    locations = "E:/Project/EToASC_/"
    dirs = os.listdir(locations)
    global cell_size

    for file in tqdm(dirs):
        in_raster = locations + file
        out_raster = "E:/Project/EToASC_MID/" + file.rstrip(".asc") + ".tif"
        arcpy.Resample_management(in_raster, out_raster, cell_size)
        outASCII = "E:/Project/EToASC/" + file
        arcpy.RasterToASCII_conversion(out_raster, outASCII)
def weightedCostRaster(outputCodeName, layerOrder=None):
    """
	Source dir: preprocessed
	Output dir: costRasters

	outputCodeName: 1311011.asc where value of each digit |-->  weight
									  index of each digit |-->  layer index in given list
																(so use the same layer order every time in every function!)
	
	layerOrder: ['agl_resis', 'bcmp_resis', 'bestp_resis', ...] --- layer names in order that corresponds to outputCode
	"""

    if layerOrder is None:
        print "No layer order specified, that's kinda important."
        return

    sources = layerOrder
    weights = map(int, list(outputCodeName.replace(".asc", "")))

    # outputCode = "".join(map(str, weights))
    print "Making weighted cost raster {}".format(outputCodeName)

    paths.setEnv(env)

    output = paths.join(paths.costRasters, outputCodeName)
    if arcpy.Exists(output) and not redoExistingOutput:
        print "{} already exists, leaving it as is.".format(output)
        return output

    active = [(paths.join(paths.preprocessed, source), weight)
              for source, weight in zip(sources, weights) if weight > 0]
    if len(active) <= 1:
        # TODO: add the straight-line-distance raster, so least cost paths don't freak out?
        pass

    costRas = sa.CreateConstantRaster(0.0001, "FLOAT", env.cellSize,
                                      env.extent)
    for source, weight in active:
        costRas += sa.Raster(source) * weight

    arcpy.RasterToASCII_conversion(costRas, output)
    arcpy.CalculateStatistics_management(output,
                                         x_skip_factor=30,
                                         y_skip_factor=30)

    return output
Exemple #12
0
def makerasters(intervalfile,inputraster,epsg):
    spatialref_default = arcpy.SpatialReference(4326) #define the default GEBCO projection
    spatialref_proj = arcpy.SpatialReference(epsg) #define the projection for the project
    baseraster = arcpy.ASCIIToRaster_conversion(r"input/"+inputraster) #open input ASCII file and convert to raster format
    arcpy.DefineProjection_management(baseraster,spatialref_default) #set the projection of the raster to default
    arcpy.ProjectRaster_management(baseraster,"output/baseraster_projected.tif",spatialref_proj,"BILINEAR") #reproject raster into correct project projection to enable accurate distance measurements
    inraster = "output/baseraster_projected.tif" #load new raster
    arcpy.DefineProjection_management(inraster,spatialref_proj) #set new raster projection
    for i in intervalfile['MeanDepth'].tolist(): #iterate through Mean Depth values (1 per interval)
        x = int(intervalfile[intervalfile['MeanDepth']==i]['Interval']) #pull associated interval value for each Mean Depth value
        print("Generating island raster for interval " +str(x) + ", sea level = " + str(i)) #print status messages
        outraster = Raster(inraster) >= i #most important part of this function -- defining land/sea boundary based on interval sea level
        outraster_reclass = Con(outraster, 1, 100, "value > 0") #convert non-land pixels to value = 100, for least cost distance analysis
        filename = "interval"+str(x) #define output name string
        arcpy.RasterToASCII_conversion(outraster_reclass,"output/raster/"+filename+".asc") #save reclassed raster to ASCII file
        outraster_polygon = Con(outraster, 1, "", "value > 0") #convert non-land pixels into NODATA for polygon generation
        arcpy.RasterToPolygon_conversion(outraster_polygon,"output/shapefile/"+filename+".shp") #save reclassed raster as shapefile
def PREASC():
    ## Since we have same data in a dataset, Kriging COULD NOT interpolate the data, IDW method is the alternation.
    global cell_size

    env.workspace = "E:/Project/PRE.gdb/"
    env.snapRaster = "E:/Project/Default.gdb/DEM_Hydro1K_Fin"
    env.extent = "E:/Project/Default.gdb/DEM_Hydro1K_Fin"
    env.parallelProcessingFactor = "100%"

    locations = "E:/Project/PRE/"
    z_field = "PRE"

    dirs = os.listdir(locations)
    for file in tqdm(dirs):
        # flag=False
        # x=0
        in_point_features = file.rstrip('.csv')
        # with open((locations+file),'r')as f:
        #     reader=csv.DictReader(f)
        #     column=[row['PRE'] for row in reader]
        #     if (len(set(column))==1):
        #         flag = False
        #     else:
        #         flag = True

        # if(flag):
        #     out =  Kriging (in_point_features, z_field, KrigingModelOrdinary(),cell_size)
        # else:
        #     out = Idw (in_point_features, z_field, cell_size)
        try:
            out = Kriging(in_point_features, z_field, KrigingModelOrdinary(),
                          cell_size)
        except Exception as e:
            out = Idw(in_point_features, z_field, cell_size)
            pass
        outname = "E:/Project/PREMID/" + "Re_" + file.rstrip('.csv') + ".tif"
        out.save(outname)
        exportname = "E:/Project/PREMID/CLIPPED/Clipped_" + file.rstrip(
            '.csv') + ".tif"
        export = ExtractByMask(outname, "E:/Project/Default.gdb/Location")
        export.save(exportname)
        name = file.rstrip('.csv')
        outASCII = "E:/Project/PREASC/" + name + '.asc'
        arcpy.RasterToASCII_conversion(exportname, outASCII)
Exemple #14
0
def EToASC():
    ## Interpolation points data into a raster(by Kriging Method), then clip it as the research area, finally output the raster as .asc
    global cell_size
    env.workspace = "E:/Project/ETo.gdb/"
    env.snapRaster = "E:/Project/Default.gdb/DEM_Hydro1K_Fin"
    env.extent = "E:/Project/Default.gdb/DEM_Hydro1K_Fin"
    env.parallelProcessingFactor = "100%"

    locations = "E:/Project/ETo/"
    z_field = "ETo"

    dirs = os.listdir(locations)
    for file in dirs:
        # flag=False
        # x=0
        in_point_features = file.rstrip('.csv')
        # with open((locations+file),'r')as f:
        #     reader=csv.DictReader(f)
        #     column=[row['ETo'] for row in reader]
        #     if (len(set(column))==1):
        #         flag = False
        #     else:
        #         flag = True

        # if(flag):
        #     out =  Kriging (in_point_features, z_field, KrigingModelOrdinary(),cell_size)
        # else:
        #     out = Idw (in_point_features, z_field, cell_size)
        try:
            out = Kriging(in_point_features, z_field, KrigingModelOrdinary(),
                          cell_size)
        except Exception as e:
            out = Idw(in_point_features, z_field, cell_size)
            pass
        outname = "E:/Project/EToMID/" + "Re_" + file.rstrip('.csv') + ".tif"
        out.save(outname)
        exportname = "E:/Project/EToMID/CLIPPED/Clipped_" + file.rstrip(
            '.csv') + ".tif"
        export = ExtractByMask(outname, "E:/Project/Default.gdb/Location")
        export.save(exportname)
        name = file.rstrip('.csv')
        outASCII = "E:/Project/EToASC/" + name + '.asc'
        arcpy.RasterToASCII_conversion(exportname, outASCII)
Exemple #15
0
def SwathPull(lookuppath,storepath,dataset,dataname,NumSect):
    #Read in pickle file with points
    print("Loading points Pickle File")
    points = pickle.load(open(lookuppath + "points","rb"))
    print("Successfully loaded points")
    for j in np.arange(2,NumSect+1):
        key2 = "seg_{}".format(j)
        key1 = "seg_{}".format(j-1)
        print("Extracting swath between "+key1+" and "+key2)
        line1 = points[key1]
        line2 = points[key2]
        maxx = np.max([line1[0],line2[0]])
        minx = np.min([line1[0],line2[0]])
        maxy = np.max([line1[1],line2[1]])
        miny = np.min([line1[2],line2[2]])
        if maxx == minx or maxy == miny:
            print("Invalid swath "+key1)
        else:
            rectangle = str(minx)+" "+str(miny)+" "+str(maxx)+" "+str(maxy)
            outraster = storepath+dataname+"_"+str(j)+".tif"
            arcpy.Clip_management(dataset,rectangle,outraster,"#","-9999","NONE","MAINTAIN_EXTENT")
            out_ascii_file = lookuppath+dataname+"_"+key1+".txt"
            arcpy.RasterToASCII_conversion(outraster,out_ascii_file)
Exemple #16
0
    def areaDailyAvg(self, LoCAFile, ws):
        env.workspace = ws
        env.overwriteOutput = True

        #convert input raster to ascii
        arcpy.RasterToASCII_conversion(LoCAFile, ws + "\\TempText.txt")
        arcpy.AddMessage("success converting raster to ascii")

        #read the output ascii file
        readFile = open(ws + "\\TempText.txt", "r")
        lines = readFile.readlines()
        CalcArr = []
        count = 0
        #loop through each line within the ascii, and ignore the header information
        for line in lines:
            if line[0].isdigit() or line[0] == '-':
                #create a list of the filtered input line that removes null values
                ListVals = list(
                    filter(lambda ListElem: ListElem != "-9999",
                           line.split(' ')))
                #get a count as to how many values are in each row
                count += len(ListVals[:-1])

                #map each value in the filtered row to a float value from their native text value
                y = list(map(lambda ListElem: float(ListElem), ListVals[:-1]))

                #reduce each element in the filtered list
                x = reduce(lambda Elem, Elem2: Elem + Elem2, y)

                #append the sum value for each row to the CalcArr array
                CalcArr.append(float(x))
        readFile.close()
        arcpy.AddMessage("daily Avg complete")

        #store the calculated average for the user specified area of interest
        self.avg = sum(CalcArr) / count
tempEnvironment0 = arcpy.env.snapRaster
arcpy.env.snapRaster = "C:\\Users\\hmpou\\Documents\\TXDEM051915\\final_zipped_grids\\CM\\elev.asc"
tempEnvironment1 = arcpy.env.extent
arcpy.env.extent = "-103.6698019919 29.032009197477 -102.942579769096 29.529231420097"
tempEnvironment2 = arcpy.env.cellSize
arcpy.env.cellSize = "2.77777778000001E-04"
tempEnvironment3 = arcpy.env.mask
arcpy.env.mask = "C:\\Users\\hmpou\\Documents\\TXDEM051915\\final_zipped_grids\\CM\\elev.asc"
arcpy.gp.ExtractByMask_sa(ldist_ridwgs, elev_asc, maskedcm)
arcpy.env.snapRaster = tempEnvironment0
arcpy.env.extent = tempEnvironment1
arcpy.env.cellSize = tempEnvironment2
arcpy.env.mask = tempEnvironment3

# Process: Raster to ASCII
arcpy.RasterToASCII_conversion(maskedcm, ldist_ridge2_asc)

# Process: extract mask GM
tempEnvironment0 = arcpy.env.snapRaster
arcpy.env.snapRaster = "C:\\Users\\hmpou\\Documents\\TXDEM051915\\final_zipped_grids\\GM\\elev.asc"
tempEnvironment1 = arcpy.env.extent
arcpy.env.extent = "-105.1699300894 31.718225977625 -104.48520786663 32.186559311333"
tempEnvironment2 = arcpy.env.cellSize
arcpy.env.cellSize = "2.77777778000002E-04"
tempEnvironment3 = arcpy.env.mask
arcpy.env.mask = "C:\\Users\\hmpou\\Documents\\TXDEM051915\\final_zipped_grids\\GM\\elev.asc"
arcpy.gp.ExtractByMask_sa(ldist_ridwgs, elev_asc__3_, maskedgm)
arcpy.env.snapRaster = tempEnvironment0
arcpy.env.extent = tempEnvironment1
arcpy.env.cellSize = tempEnvironment2
arcpy.env.mask = tempEnvironment3
Exemple #18
0
arcpy.env.extent = dem
arcpy.env.cellSize = cellsize

rg10ras = arcpy.PolygonToRaster_conversion(st, "rg10", "rg10ras",
                                           "CELL_CENTER", "", cellsize)
rg20ras = arcpy.PolygonToRaster_conversion(st, "rg20", "rg20ras",
                                           "CELL_CENTER", "", cellsize)
rg70ras = arcpy.PolygonToRaster_conversion(st, "rg70", "rg70ras",
                                           "CELL_CENTER", "", cellsize)
soilras = arcpy.PolygonToRaster_conversion(st, "soiltype", "soilras",
                                           "CELL_CENTER", "", cellsize)

arcpy.env.workspace = folder

##ASC format�nda kaydetti�imizde decimal ayrac� virg�l oluyor bu y�zden TXT kaydedece�iz
arcpy.RasterToASCII_conversion(rg10ras, "rg10.txt")
arcpy.RasterToASCII_conversion(rg20ras, "rg20.txt")
arcpy.RasterToASCII_conversion(rg70ras, "rg70.txt")
arcpy.RasterToASCII_conversion(soilras, "soiltype.txt")

##.....................................................................................
## ROCKS Shapes Calculating
arcpy.AddMessage("CALCULATING ROCKS SHAPES")

arcpy.env.workspace = ws

##Rocks tablosu alanlar�n hepsi siliniyor nid alan� ekleniyor sadece nid alan�n�n kalmas� sa�lan�yor

del_fields = [b.name for b in arcpy.ListFields(rocks) if not b.required]
arcpy.AddMessage(del_fields)
Exemple #19
0
##Input_Raster_Folder = arcpy.GetParameterAsText(0)
Input_Raster_Folder = r"C:\Users\Dorn\SharePoint\WHCR Migration Stopover Habit - Dat\bio_30s_esri\bio"

##OutputRaster_asc = arcpy.GetParameterAsText(1)

##nlcd270_asc = arcpy.GetParameterAsText(2)
##if nlcd270_asc == '#' or not nlcd270_asc:
##    nlcd270_asc = "C:\\Users\\Dorn\\SharePoint\\WHCR Migration Stopover Habit - Dat\\Maxent\\layers\\nlcd270.asc" # provide a default value if unspecified
cellRaster = arcpy.env.cellSize
arcpy.env.cellSize ="C:\\Users\\Dorn\\SharePoint\\WHCR Migration Stopover Habit - Dat\\Maxent\\layers\\270bio_1.asc"

# Preamble
arcpy.env.workspace =Input_Raster_Folder

# Process Layers
rList=arcpy.ListRasters()
for r in rList:
	print "Processing Raster layer {0}.".format(r)
	# Process: Resample
	tempEnvironment0 = arcpy.env.snapRaster
	arcpy.env.snapRaster = "C:\\Users\\Dorn\\SharePoint\\WHCR Migration Stopover Habit - Dat\\Maxent\\layers\\270bio_1.asc"
	tempEnvironment1 = arcpy.env.extent
	arcpy.env.extent = "C:\\Users\\Dorn\\SharePoint\\WHCR Migration Stopover Habit - Dat\\Maxent\\layers\\270bio_1.asc"
	arcpy.Resample_management(r, "temp{0}".format(r), cellRaster, "MAJORITY")
	arcpy.env.snapRaster = tempEnvironment0
	arcpy.env.extent = tempEnvironment1

	# Process: Raster to ASCII
	arcpy.RasterToASCII_conversion("temp{0}".format(r), "C:\\Users\\Dorn\\Desktop\\Maxent\\layers\\270{0}.asc".format(r))
	print "New layer 270{0}.asc created.".format(r)
print "ASCii files created in c:\Users\Dorn\Desktop\Maxent\layers\ directory."
                                 "NUMBER_OF_TILES", "GRID", "#", dim, "#", "5",
                                 "DEGREES", "#", "#")
    arcpy.SplitRaster_management(OutRaster3, dirout, "longitude",
                                 "NUMBER_OF_TILES", "GRID", "#", dim, "#", "5",
                                 "DEGREES", "#", "#")

    varlist = "altitude", "latitude", "longitude"

    print "\n CONVERTING TO ASCII BY TILES"
    for i in range(0, int(ntiles), 1):

        diroutasc = dirout + "\\tile-" + str(i + 1)
        if not os.path.exists(diroutasc):
            os.system("mkdir " + diroutasc)

        for var in varlist:
            arcpy.RasterToASCII_conversion(dirout + "\\" + var + str(i),
                                           diroutasc + "\\" + var + ".asc")
            arcpy.Delete_management(dirout + "\\" + var + str(i))

print "\n CONVERTING TO ASCII REGIONAL"

if not arcpy.Exists(dirout + "\\lon-prj-" + region + ".asc"):
    gp.RasterToASCII_conversion(OutAlt,
                                dirout + "\\alt-prj-" + region + ".asc")
    gp.RasterToASCII_conversion(OutRaster2,
                                dirout + "\\lat-prj-" + region + ".asc")
    gp.RasterToASCII_conversion(OutRaster3,
                                dirout + "\\lon-prj-" + region + ".asc")

print "\t ..done!!"
Exemple #21
0
    time.sleep(1.5)

    arcpy.ASCII3DToFeatureClass_3d(input=input_file,
                                   in_file_type="XYZ",
                                   out_feature_class=database + "/" +
                                   pointinfo + "_feature",
                                   out_geometry_type="MULTIPOINT",
                                   average_point_spacing=avg_space)

    time.sleep(1.5)

    outIDW = arcpy.sa.Idw(database + "/" + pointinfo + "_feature", "Shape.Z",
                          0.15, 2, arcpy.sa.RadiusVariable(12))

    time.sleep(1.5)

    inRaster = database + "/" + pointinfo + "_feature"

    outputDir = r"C:\Users\lenovo\Desktop\rasters-batsmen"
    outputFile = pointinfo + ".asc"
    outASCII = outputDir + "\\" + outputFile
    arcpy.RasterToASCII_conversion(outIDW, outASCII)

    time.sleep(1.5)
    print bowler_name

test = os.listdir(outputDir)

for item in test:
    if item.endswith(".xml"):
        os.remove(os.path.join(outputDir, item))
Exemple #22
0
    print("Outline created")

    # --- Check out the ArcGIS Spatial Analyst extension license
    arcpy.CheckOutExtension("Spatial")

    # --- Extract's the desired DEM from the outline mask
    arcpy.gp.ExtractByMask_sa(DEM, os.path.join(rasterfolder, "Outline.shp"), os.path.join(rasterfolder, "Raster_Outline.tif"))

    # logtekst = logtekst + "\n" + "Outline created"
    print ("Raster created")

    # --- Extract's the desired DEM from the outline mask
    arcpy.gp.ExtractByMask_sa(os.path.join(rasterfolder, "Raster_Outline.tif"), HAV, os.path.join(rasterfolder, "Raster_Outline_Minus_Hav.tif"))

    # --- Raster to ASCII
    arcpy.RasterToASCII_conversion(in_raster=os.path.join(rasterfolder, "Raster_Outline_Minus_Hav.tif"), out_ascii_file=os.path.join(rasterfolder, "inputpra.asc"))

    print ("Raster to ASCII; Completed")

    # --- Load required toolboxes
    arcpy.ImportToolbox(PRATBX)

    print ("Calculating PRA")

    # --- Potential Release Area
    arcpy.gp.toolbox = PRATBX;
    arcpy.gp.PRA(os.path.join(rasterfolder, "inputpra.asc"), os.path.join(rasterfolder, "outputpra.asc"), "2,3", "Regular", "0", "180", WorkingDirectory, "")

    print ("PRA; Completed")

    # --- Define Projection For PRA
Exemple #23
0
    def calculate_slope_fraction_flow_direction_dinf(self, DTM, numpy_array_location):   
        start = time.time()
        
        # Execute flow direction and slope on the first day of model operation or at the end of each month
        output_asc = numpy_array_location + "\output_asc.asc"
        output_tiff = numpy_array_location + "\output_tif.tif"

        # Input
        ele_ascii = arcpy.RasterToASCII_conversion(DTM, output_asc)
        ele_tiff = arcpy.ASCIIToRaster_conversion(ele_ascii, output_tiff, "FLOAT")

        # New version for calculating flow directions     
        inlyr = ele_tiff
        desc = arcpy.Describe(inlyr)
        fel=str(desc.catalogPath)
        arcpy.AddMessage("\nInput Pit Filled Elevation file: "+fel)

        # Input Number of Processes
        inputProc=str(8)
        arcpy.AddMessage("\nInput Number of Processes: "+inputProc)

        # Outputs
        ang = str(numpy_array_location + "\oang.tif")
        arcpy.AddMessage("\nOutput Dinf Flow Direction File: "+ang)
        slp = str(numpy_array_location + "\oslp.tif")
        arcpy.AddMessage("\nOutput Dinf Slope File: "+slp)

        # Construct command 
        cmd = 'mpiexec -n ' + inputProc + ' DinfFlowDir -fel ' + '"' + fel + '"' + ' -ang ' + '"' + ang + '"' + ' -slp ' + '"' + slp + '"'
        arcpy.AddMessage("\nCommand Line: "+cmd)

        # Submit command to operating system
        os.system(cmd)

        # Capture the contents of shell command and print it to the arcgis dialog box
        process=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
        arcpy.AddMessage('\nProcess started:\n')
        for line in process.stdout.readlines():
            arcpy.AddMessage(line)

        # Calculate statistics on the output so that it displays properly
        arcpy.AddMessage('Executing: Calculate Statistics\n')
        arcpy.CalculateStatistics_management(ang)
        arcpy.CalculateStatistics_management(slp)
      
        # Old ArcGIS method but still used for the sediment transport aspect
        DTM = Fill(DTM)
        flow_direction_raster = FlowDirection(DTM)
        arcpy.AddMessage("Flow Direcion Calculated")
        arcpy.AddMessage("-------------------------")
        slope = Slope(DTM, "DEGREE")
        arcpy.AddMessage("Slope Calculated")
        arcpy.AddMessage("-------------------------")

        # Convert fill, slope, flow direction to numpy array
        DTM = arcpy.RasterToNumPyArray(DTM,'#','#','#', -9999)
        slope = arcpy.RasterToNumPyArray(slope, '#','#','#', -9999)
        flow_direction_np = arcpy.RasterToNumPyArray(flow_direction_raster, '#','#','#', -9999)

        np.radians(slope)
        np.tan(slope)
        slope[slope == 0] = 0.0001
        arcpy.AddMessage("Calculating took " + str(round(time.time() - start,2)) + "s.")

        return slope, DTM, flow_direction_np, flow_direction_raster, ang
            if arcpy.Exists(xy_Layer):
                arcpy.Delete_management(xy_Layer)

            arcpy.MakeXYEventLayer_management(
                pointFile, "x", "y", xy_Layer,
                "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119522E-09;0.001;0.001;IsHighPrecision",
                "")

            if arcpy.Exists(ImageFile):
                continue

            else:
                print ImageFile
                # Process: interpolation from point data to create grided data
                # Process: Kriging (for others :: better way)
                # arcpy.gp.Kriging_sa(xy_Layer, "values", ImageFile, "Spherical #", "0.01", "VARIABLE 12", "")

                # Process: IDW (for prcp)
                arcpy.gp.Idw_sa(xy_Layer, "values", ImageFile, "0.01", "2",
                                "VARIABLE 12", "")

            if arcpy.Exists(AsciiFile):
                continue
            else:
                # # Process: Raster to ASCII
                arcpy.RasterToASCII_conversion(ImageFile, AsciiFile)

        except IOError as e:
            # print "I/O error({0}): {1}".format(e.errno, e.strerror)
            arcpy.GetMessages()
####convert rasters in 4 folders to asciis

import arcpy
from arcpy import env
import os
from arcpy.sa import *

env.workspace="F:\\Remote Sensing Data\\Bottom_hycom_bottom_temp_U"
dirs=os.listdir(env.workspace) #see what directories are in the workspace
out_directory="F:\\Remote Sensing Data\\Bottom_hycom_bottom_temp_U\\asciis"
print(dirs)

for d in arcpy.ListWorkspaces("Depth*"): #see what directories are in the workspace
	print(d)
	env.workspace=d
	for file in arcpy.ListRasters():
		print("file equals",file)
		outasc=arcpy.Describe(file).basename+".asc"
		print("outasc equals",outasc)
		outASCII=os.path.join(out_directory,outasc)
		print("outASCII equals",outASCII)
		arcpy.RasterToASCII_conversion(file,outASCII)
		
Exemple #26
0
                # The following inputs are layers or table views: "dtm", "square_test"
                arcpy.Clip_management(
                    in_raster="mosaic",
                    rectangle=ExtStr,
                    out_raster=pathdab + "dtm_clip",
                    in_template_dataset=pathdab + "miniarea_square",
                    nodata_value="-3.402823e+038",
                    clipping_geometry="NONE",
                    maintain_clipping_extent="NO_MAINTAIN_EXTENT")

                # Get input Raster properties
                inRas = arcpy.Raster('dtm_clip')

                # or I could convert it to ascii
                arcpy.RasterToASCII_conversion(
                    inRas, outASCII + crater_id[ix] + "_visible.asc")

                ix = ix + 1

                arcpy.Delete_management("dtm_clip")
                arcpy.Delete_management("miniarea_square")
                arcpy.Delete_management("miniarea_TMP")
                arcpy.Delete_management("CENTER_PROJ")
                arcpy.Delete_management("CENTER_TMP")
                arcpy.Delete_management("mosaic")

                for DTM_tmp in DTMs_kaguya_list:
                    arcpy.Delete_management(DTM_tmp.split(".")[0])
        except:
            ix = ix + 1
Exemple #27
0
## display and are removed with extract by mask in toolbox. But the line below does not work for some reason.

costRaster = arcpy.sa.ExtractByMask(costRaster, linkLayer)

#Set up inputs for Circuitscape
arcpy.AddMessage("Performing Circuitscape Modeling...")

#Convert inputs to ASCII
arcpy.AddMessage("\tConverting Cost Surface to ASCII...")
# desc = arcpy.Describe(costRaster)

costName = outName + "_cost.asc"

### BUG!!!! This step is removing some nodata cells from the output!!!! Tried setting a snapraster to the input but no luck!!!
### This step works correctly when used manually from ArcToolbox.
arcpy.RasterToASCII_conversion(costRaster, costName)

desc = arcpy.Describe(source)
dType = desc.dataType
sourceName = outName + "_source.asc"

# Check if source patches are raster and convert if necessary
if dType == "ShapeFile" or dType == "FeatureLayer":
    arcpy.AddMessage("\tConverting Source Patches to ASCII...")
    arcpy.FeatureToRaster_conversion(
        source, sourceID, "in_memory\\xxsrcrast", CellSize
    )  # Tried to use .getOutput(0) to set sourceRaster but get inconsistent results and errors.
    sourceRaster = "in_memory\\xxsrcrast"

    if arcpy.Describe(sourceRaster).SpatialReference.name == 'Unknown':
        sr = arcpy.Describe(source).SpatialReference
                var = os.path.basename(raster).split("_")[0]
                month = os.path.basename(raster).split("_")[1]
                zone = os.path.basename(raster).split("_")[-1]

                dirAsc = dirTmp + "\\" + rcp + "_" + model + "_" + period + "\\ascii\\" + var + "_" + zoneDc[
                    zone]
                if not os.path.exists(dirAsc):
                    os.system('mkdir ' + dirAsc)

                outAsc = dirAsc + "\\" + var + "_" + month + ".asc"
                prjAsc = dirAsc + "\\" + var + "_" + month + ".prj"
                # print outAsc
                if not os.path.exists(outAsc):
                    try:
                        ##########os.system("gdal_translate -of AAIGrid -ot Int16 -quiet " + arcpy.env.workspace + "\\" + raster + " " + outAsc)
                        arcpy.RasterToASCII_conversion(raster, outAsc)

                    except:
                        arcpy.CalculateStatistics_management(raster)
                        arcpy.RasterToASCII_conversion(raster, outAsc)
                    print "\t", os.path.basename(raster), " converted to ascii"
                    # arcpy.Delete_management(raster)
                else:
                    print "\t", os.path.basename(raster), " converted"

                if os.path.exists(prjAsc):
                    os.system("del /s /q " + prjAsc)

            #################### Compress by Zones
            print "\n .> Compress by Zones: ", rcp, model, str(
                res), ens, period, "\n"
def Raster2Ascii(raster, ascii, isprint):
    arcpy.RasterToASCII_conversion(raster, ascii)
    if isprint:
        print('Raster2Ascii is finished....')
tempEnvironment0 = arcpy.env.snapRaster
arcpy.env.snapRaster = "C:\\Users\\hmpou\\Documents\\TXDEM051915\\final_zipped_grids\\CM\\elev.asc"
tempEnvironment1 = arcpy.env.extent
arcpy.env.extent = "-103.6698019919 29.032009197477 -102.942579769096 29.529231420097"
tempEnvironment2 = arcpy.env.cellSize
arcpy.env.cellSize = "2.77777778000001E-04"
tempEnvironment3 = arcpy.env.mask
arcpy.env.mask = "C:\\Users\\hmpou\\Documents\\TXDEM051915\\final_zipped_grids\\CM\\elev.asc"
arcpy.gp.ExtractByMask_sa(ldvalleywgs, cm_elev, clipped_CM)
arcpy.env.snapRaster = tempEnvironment0
arcpy.env.extent = tempEnvironment1
arcpy.env.cellSize = tempEnvironment2
arcpy.env.mask = tempEnvironment3

# Process: Raster to ASCII CM
arcpy.RasterToASCII_conversion(clipped_CM, ldist_valley2_asc)

# Process: extract mask GM
tempEnvironment0 = arcpy.env.snapRaster
arcpy.env.snapRaster = "C:\\Users\\hmpou\\Documents\\TXDEM051915\\final_zipped_grids\\GM\\elev.asc"
tempEnvironment1 = arcpy.env.extent
arcpy.env.extent = "-105.1699300894 31.718225977625 -104.48520786663 32.186559311333"
tempEnvironment2 = arcpy.env.cellSize
arcpy.env.cellSize = "2.77777778000002E-04"
tempEnvironment3 = arcpy.env.mask
arcpy.env.mask = "C:\\Users\\hmpou\\Documents\\TXDEM051915\\final_zipped_grids\\GM\\elev.asc"
arcpy.gp.ExtractByMask_sa(ldvalleywgs, gm_elev, clipped_GM)
arcpy.env.snapRaster = tempEnvironment0
arcpy.env.extent = tempEnvironment1
arcpy.env.cellSize = tempEnvironment2
arcpy.env.mask = tempEnvironment3