AnsweredAssumed Answered

I can't force Python raster calculator to sum the rasters

Question asked by stanpol on May 8, 2016
Latest reply on May 14, 2016 by curtvprice

Hi, Im newbie two Python scripting at all, anyway.

I'm trying to sum two rasters in Python toolbox, but it doesn't work the way I want it to. Here's the code:

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


class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "Toolbox"
        self.alias = ""
        # List of tool classes associated with this toolbox
        self.tools = [Tool]


class Tool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Tool"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        param0 = arcpy.Parameter(displayName="Input Features", name="in_features", datatype="DERasterDataset",  parameterType="Required", direction="Input", multiValue = True)
        params = [param0]
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        # Set environment settings
        env.workspace = "C:/temp"

        #Set local variables
        inRaster = parameters[0].valueAsText
        rasters = inRaster.split(';')

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

        #Execute sum
        output = Raster(rasters[0]) + Raster(rasters[1])

        #Save the output
        output.save("C:/temp/result/output02")

        return

I have two different 0-1 rasters in the same region created from different buffers. I expected it sums for each pixels it values, when on the same pixels i need it to give two when both pixels are black, one when when one of them is black and 0 when both are 0. But it just gives me nearly the same raster as the first. What do I do wrong? I expect it to work this way:

n9rAV.png

Here're the results I get, at first look the result's streams are not continous, but due to the image it should be:

Dokument1.jpg

Outcomes