3

I've been trying several times to sample (Sample tool from Spatial Analysis ArcGis 10.5.1) a list of rasters to a location sample (also in raster format .tif).

Here's the code I've been using, and apparently the problem raised up when Python execute the line of the tool - it creates well all the files in the specified folder:

import arcpy, os, sys, string
from arcpy import env
from arcpy.sa import *

arcpy.env.workspace = r'C:\Users\Victoria\Desktop\***\***'
inputFolder = arcpy.env.workspace
outputFolder = os.path.join(inputFolder, "sample")

raster = arcpy.ListRasters('*br*')
colony = arcpy.ListRasters('*col_1*')

for r in raster:
    outName = os.path.splitext(r)[0] + "_col1"
    saveDir = os.path.join(outputFolder, outName)
    Sample(r, colony, saveDir, "NEAREST", "Value", "CURRENT_SLICE")
    print '{0} saved in {1}.'.format(outName, saveDir)

And here's the error that shows up after run the code:

    Traceback (most recent call last):
  File "C:\Python27\ArcGIS10.5\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Users\Victoria\Desktop\***\***\prueba1.py", line 15, in <module>
    proc = Sample(r, colony, saveDir, "NEAREST", "Value", "CURRENT_SLICE")
  File "C:\Program Files\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 1617, in Sample
    process_as_multidimensional)
  File "C:\Program Files\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
    result = wrapper(*args, **kwargs)
  File "C:\Program Files\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 1609, in Wrapper
    process_as_multidimensional)
  File "C:\Program Files\ArcGIS\Desktop10.5\ArcPy\arcpy\geoprocessing\_base.py", line 510, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
RuntimeError: Object: Error in executing tool

I'm really new in this.

6
  • You said it creates all files well - could you clarify what the issue is, besides just posting the Python-generated errors?
    – jyingling
    Feb 12, 2019 at 17:58
  • Sure! I want to get a table for each sampling item (I'm sampling 10 different rasters to one single raster with the locations points). The aim of this is to extract each NDVI value (which are each of the 10 rasters I mentioned before) from pixels that overlap with my locations. I didn't define the extension of the file in the output cause I read on ArcGis Sample Help Window that it creates automatically an INFO table for each one. Hope that's make sense! Feb 12, 2019 at 18:07
  • Shouldnt in_rasters parameter be a list of rasters? You are providing one raster at a time. And in_location_data should only be one raster layer or one feature layer but you are providing a list of rasters. Look at the documentation for sample.
    – BERA
    Feb 12, 2019 at 18:19
  • 1
    When you do for r in rasters, r will be a single raster. If Sample wants a list of rasters and you are inputing one raster (r) it will not work.
    – BERA
    Feb 12, 2019 at 18:27
  • 1
    Indeed. Thanks for your comment, it totally helped me solving the issue. It works perfectly now. Feb 15, 2019 at 16:37

1 Answer 1

1

As commented by @BERA:

When you do for r in rasters, r will be a single raster. If Sample wants a list of rasters and you are inputing one raster (r) it will not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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