Extract multiple raster by mask automatically

4091
7
09-20-2019 08:23 AM
gianguyen
New Contributor

Hi Community, 
I am working with a project where I have to extract 3650 rasters. It will take forever to get it done manually. 

I am thinking about using ArcPy or Modelbuilder to do it automatically.

Can you help me with this?

Thank you so much

Gia Nguyen

0 Kudos
7 Replies
George_Thompson
Esri Frequent Contributor

I would look at this GP tool and you can batch it in Model Builder - Extract by Mask—Help | ArcGIS Desktop or write Python to iterate through and do the process.

--- George T.
PavanYadav
Esri Contributor

Alternatively you can use the Clip Raster—Data Management toolbox | ArcGIS Desktop tool. But it all depends on your need. The Clip Raster tool is bit different from the the Extract by Mask tool. The Clip Raster tool cuts out a portion of a raster dataset (extent changes) while Extract by Mask tool does not change the extent of the raster but converts values outside of the clip feature/raster to NoData.

here is one example:

import arcpy
import os

workspace = r"D:\test\Stowe.gdb"
arcpy.env.workspace = "D:\test\Stowe.gdb"
fc = r'D:\test\Stowe.gdb\clip_feature'

desc = arcpy.Describe(fc)

xmin = desc.extent.XMin
xmax = desc.extent.XMax
ymin = desc.extent.YMin
ymax = desc.extent.YMax

rectangle = str(xmin) +' '+ str(ymin) +' '+ str(xmax) +' '+ str(ymax)

rasters = []

walk = arcpy.da.Walk(workspace, datatype="RasterDataset") #

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        print(filename)
        rasters.append(os.path.join(dirpath, filename))

for raster in rasters:  
    arcpy.Clip_management(raster, rectangle, raster + "_clip")

gianguyen
New Contributor

Hi Pavan Yadav,

I appreciate your suggestion. It is very helpful.

I got another issue. I want to convert multiple NetCDF files to rasters

using ArcPy or Python.

Here is the code for a NetCDF file that I learned from ESRI. Can you help

me to edit it to read convert multiple file?

Thanks

  1. Import system modules

import arcpy

  1. Set local variables

inNetCDFFile = "C:/data/netcdf/rainfall.nc"

variable = "pptx"

XDimension = "lon"

YDimension = "lat"

outRasterLayer = "rainfall"

bandDimmension = ""

dimensionValues = ""

valueSelectionMethod = ""

cellRegistration = ""

  1. Execute MakeNetCDFRasterLayer

arcpy.MakeNetCDFRasterLayer_md(inNetCDFFile, variable, XDimension,

YDimension,

outRasterLayer, bandDimmension,

dimensionValues,

valueSelectionMethod, cellRegistration)

0 Kudos
PavanYadav
Esri Contributor

Hi gia nguyen

To filter *.nc files only please try, the following:


ncFiles = []
workspace = r"D:\test"

#datatype limits the results returned. See supported datatypes:
#Walk—Help | ArcGIS for Desktop.


walk = arcpy.da.Walk(workspace)

for dirpath, dirnames, filenames in walk:
    for filename in filenames:

#NC is not a supported datatype so filtering like this: "if filename[-3:]=='.nc'"
        if filename[-3:]=='.nc':
            ncFiles.append(os.path.join(dirpath, filename))

gianguyen
New Contributor

Hi Louise Thank you for your response. While waiting for the best solution,

I tried the code below. It works still now. I use it to convert from NetCDF

files to rasters.

import arcpy

from arcpy import env

from arcpy.sa import *

Input_Temp_folder = "E:\Mekong\DATA\JMP_Deeplearning_data\Mekong_temp\Temp_2017"

Out_Temp_folder_Max

="E:\Mekong\DATA\JMP_Deeplearning_data\Mekong_temp\Temp_2017\Max"

Out_Temp_folder_Min

="E:\Mekong\DATA\JMP_Deeplearning_data\Mekong_temp\Temp_2017\Min"

arcpy.env.workspace = Input_Temp_folder

arcpy.env.overwriteOutput = True

arcpy.env.scratchWorkspace = Out_Temp_folder_Max # Create scratch workspace

arcpy.env.scratchWorkspace = Out_Temp_folder_Min # create Scratch workspace

variableTemp1 = "T2MMAX"

variableTemp2="T2MMIN"

XDimension = "lon"

YDimension = "lat"

time = "time"

NetCDFfiles = arcpy.ListFiles("*.nc4.nc")

print NetCDFfiles

for filename in NetCDFfiles:

print ("Processing: " + filename)

day = filename[27:35]# extract date in the filenames

outRasterLayer_Temp_Max = variableTemp1 + "_" + day # create a

time series for names

outRasterLayer_Temp_Min = variableTemp2 + "_" + day # create a

time series for names

InNetCDTemp = arcpy.env.workspace + "/" + filename # get a file

arcpy.MakeNetCDFRasterLayer_md(InNetCDTemp, variableTemp1,

XDimension, YDimension, outRasterLayer_Temp_Max,time)

arcpy.MakeNetCDFRasterLayer_md(InNetCDTemp, variableTemp2,

XDimension, YDimension, outRasterLayer_Temp_Min,time)

outRasterTemp_Folder = Out_Temp_folder_Min

arcpy.SplitRaster_management

(outRasterLayer_Temp_Max,Out_Temp_folder_Max,outRasterLayer_Temp_Max,"POLYGON_FEATURES","TIFF","NEAREST","","","","DEGREES","","","Mekong_Basin","","","")

arcpy.SplitRaster_management

(outRasterLayer_Temp_Min,Out_Temp_folder_Min,outRasterLayer_Temp_Min,"POLYGON_FEATURES","TIFF","NEAREST","","","","DEGREES","","","Mekong_Basin","","","")

0 Kudos
gianguyen
New Contributor

Thank you for your suggestions. They are very useful. 

Besides, I also found a tool SAMPLE which can extract value from rasters (single/multi bands) into a grid (fishnet). 

Again, thank you so much

0 Kudos
PavanYadav
Esri Contributor

I'm glad you find it useful.

Regards,

Pavan

0 Kudos