0

I haven't used python yet; I want to read multiple GeoTiff files and resample them based on another single file, and be able to write all the files in a new directory. I can use R raster package to that, but it takes a long time. Here is what I do in R and I want to be able to do the exact process in Python using ArcPy module.

    setwd("path to directory")
    files <- list.files(path=".", pattern="tif", all.files=FALSE, full.names=TRUE) 
    s <- stack(files)
    resamp <- raster("new_raster.tif")
    s2 <- resample(s, resamp,method='bilinear')
    s3 <- unstack(s2)
    setwd("new_directory)
    for(i in seq_along(s3)){writeRaster(s3[[i]], file=outputnames[i],format="GTiff", options=c("INTERLEAVE=BAND","COMPRESS=NONE"))}
4

This process involves creating a list of your rasters and instantiating a for loop to resample each raster in your list.

A python script using arcpy methods would look SOMETHING like this:

import arcpy
arcpy.env.workspace = <rasterDirectory>
outDir = <output directory>
RstList = arcpy.ListRasters("*", "TIFF")
xResult= arcpy.GetRasterProperties(<in_rst>, "CELLSIZEX")
yResult= arcpy.GetRasterProperties(<in_rst>, "CELLSIZEY")
x = xResult.getOutput(0)
y = yResult.getOutput(0)
for Rst in RstList:

       arcpy.Resample_management(Rst, outDir+arcpy.Describe(Rst).name, x y, "BILINEAR")
  • Thanks! do you know how I can read in that single file (resamp in the example')? I want to use that instead of the <cell size> to make sure the resampling process changes the dimension as well. – Geo-sp Nov 5 '15 at 17:57
  • Sorry, missed that. You can use arcpy. GetRasterProperties_management(in_raster, <property type>) . Set a variable (ex. cellSzX) equal to this method and another variable to get the result object (cellSzX.getOutput(0)) and plug it into the resample method. Here is the resource link. resources.arcgis.com/EN/HELP/MAIN/10.2/index.html#//… You may need to pass two variables into <cell size>, CELLSIZEX and CELLSIZEY. – Jeff May Nov 5 '15 at 18:04
  • I updated my initial answer to include what I think may work. Let me know how it pans out. You can even pass the path of the input raster into the <cell size> argument rather than use GetRasterProperties. This would be the easiest way to go. – Jeff May Nov 5 '15 at 18:17
  • I think I should you clip and resample, cause I want to change the dimensions and the cell size. – Geo-sp Nov 5 '15 at 18:18
  • pass a single number into the cell size parameter to specify the square cell size, whatever you want. arcpy.Clip_management(Rst) can be added to the loop (before resample) if you want to clip all rasters in your list, this of course will create another output so just be sure to keep your file names in check. Arcpy is very well documented in the online arcgis resources if you need more instruction on how to use any of the arcpy methods. – Jeff May Nov 5 '15 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.