+ Reply to Thread
Results 1 to 5 of 5

Thread: Raster Calculator Python

  1. #1
    Johan L
    Join Date
    Oct 2011
    Posts
    24
    Points
    0
    Answers Provided
    0


    0

    Default Raster Calculator Python

    Hi Everyone,

    I am trying to implement a simple raster calculator within my python script as the following
    Code:
    arcpy.gp.RasterCalculator_sa("\"%Temp4%\"/2", Temp2)
    But I am receiving the following error in python

    "Warning (from warnings module):
    File "<string>", line 1
    SyntaxWarning: import * only allowed at module level

    Traceback (most recent call last):
    File "F:\test.py", line 75, in <module>
    arcpy.gp.RasterCalculator_sa("\"%Temp4%\"/2", Temp2)
    File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 474, in <lambda>
    return lambda *args: val(*gp_fixargs(args))
    ExecuteError: ERROR 000539: Error running expression: rcexec() <type 'exceptions.TypeError'>: unsupported operand type(s) for /: 'str' and 'int'
    Failed to execute (RasterCalculator)."

    I am using arcpy.CheckOutExtension("spatial") so im not sure what the first point of the error is implying!

    This is really frustrating... any help would be appreciated!

    Bjebn
    Last edited by bjebn; 11-28-2011 at 04:22 AM.

  2. #2
    Raphael R
    Join Date
    Apr 2010
    Posts
    109
    Points
    62
    Answers Provided
    8


    0

    Default Re: Raster Calculator Python

    according to the help, the raster calculator is for use inside arcmap only, for calculations in python you should use mapalgebra:
    http://help.arcgis.com/en/arcgisdesk...00z7000000.htm

  3. #3
    Johan L
    Join Date
    Oct 2011
    Posts
    24
    Points
    0
    Answers Provided
    0


    0

    Default Re: Raster Calculator Python

    Quote Originally Posted by rafaelr View Post
    according to the help, the raster calculator is for use inside arcmap only, for calculations in python you should use mapalgebra:
    http://help.arcgis.com/en/arcgisdesk...00z7000000.htm
    Ah of course... i did the mistake of exporting the script from the modelbuilder and tried to modify it - what a frustrating mistake!

  4. #4
    Johan L
    Join Date
    Oct 2011
    Posts
    24
    Points
    0
    Answers Provided
    0


    0

    Default Re: Raster Calculator Python

    I am still having problems with map algebra... its probably an obvious mistake but I cant identify it!

    Here is a part of the code im having trouble with...

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

    arcpy.CheckOutExtension("spatial")

    arcpy.env.workspace = r'O:\DATA\test'
    InputFolder = arcpy.env.workspace

    Temp4 = InputFolder + '\\' + temp4

    RasterList = arcpy.ListRasters()
    for RasterImage in RasterList:
    Temp4 = FocalStatistics('RasterImage', NbrRectangle (9, 9, 'CELL'), "RANGE", "DATA")
    print ('Focal Statistics')
    .....
    I receive the following error
    "Traceback (most recent call last):
    File "O:\test.py", line 67, in <module>
    Temp4 = FocalStatistics('RasterImage', NbrRectangle (9, 9, 'CELL'), "RANGE", "DATA")
    File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\sa\Functions.py", line 4796, in FocalStatistics
    ignore_nodata)
    File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\sa\Utils.py", line 47, in swapper
    result = wrapper(*args, **kwargs)
    File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\sa\Functions.py", line 4790, in wrapper
    ignore_nodata)
    File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 474, in <lambda>
    return lambda *args: val(*gp_fixargs(args))
    ExecuteError: Failed to execute. Parameters are not valid.
    ERROR 000865: Input raster: RasterImage does not exist.Failed to execute (FocalStatistics)."

    It does not seem to recognize my for in loop of the RasterImage - any ideas?
    Last edited by bjebn; 11-29-2011 at 06:57 AM.

  5. #5
    Raphael R
    Join Date
    Apr 2010
    Posts
    109
    Points
    62
    Answers Provided
    8


    0

    Default Re: Raster Calculator Python

    try removing the ' before and after RasterImage in the Temp4 = FocalStatistics()... line. It´s a variable, i think they way you´ve got it in there it´s looking for a string

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts