Wiegted sum

482
2
06-26-2014 10:37 AM
mohamadbakhtiari
New Contributor
Hi everyone
I want to build a toolset by using add a script that before I had provided in Pythonwin, this tool overlays some rasters by apply Weighted Sum approach. I found that by command arcpy.getParameterAsText(), we can give flexibility to toolset for introducing parameters. but in case this operation, unlike tools like buffer and clip that introduction of parameters is clear, I don't know how introduce required parameters. can anyone help me about this?
thank you
Tags (2)
0 Kudos
2 Replies
markdenil
Regular Contributor II
You have to create a Weighted Sum Table to input into Weighted Sum, as you know.

These are three item lists, nested in an overall list.
to wit:    WSTable ([[inRaster, field, weight],...])

Even for just a few rasters, that is a lot of command parameters for a user to enter.

It might be better to have the user create a weight parameter text file,
say, comma delimited, with one raster per line.
for example:
inRaster1, field, weight
inRaster2, field, weight
inRaster3, field, weight
...

Then, if the user enters the weight parameter text file (with path) as the command parameter,
the script can first find, read, and parse the text file into the Weighted Sum Table format
that Weighted Sum requires.
That is pretty stratightforward.
        #   make an empty master file
WSTable = []
        #   open the file to read
txt = open(r"c:\weights.txt", 'r')

        #   read each line to the end of the file
while txt.readline() != '':
        # readline gets a line
    line = f.readline()
        # rstrip removes carrige return
    strip = line.rstrip()
        # split makes sure you get the correct chunks
    split = strip.split(',')
        # now make a list of the chunks
    aList = list(split)
        # append the individual list to the master list
    WSTable.append(aList)

        #   close the file
txt.close()


This can, of course, be very much more succinct
furthermore, no warranties are expressed or inplied
0 Kudos
XanderBakker
Esri Esteemed Contributor
Hi Mohamad,

If you create a Toolbox and want to add a script, you can use the "Weighted Sum" Data Type for the parameter to collect the necessary information.

This will return a string with the following structure:
rastername1 fieldname1 weight1;rastername2 fieldname2 weight2

In the Help on Weighted Sum you can see in the example code that (like Mark explains) a WSTable can be created:
wstbl = WSTable([["snow", "VALUE", 0.25], ["land", "VALUE", 0.25],["soil", "VALUE", 0.5]])


If you execute this python code:
import arcpy
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")
wstbl = WSTable([["snow", "VALUE", 0.25], ["land", "VALUE", 0.25], ["soil", "VALUE", 0.5]])
print wstbl


It will return:
'snow' VALUE 0.25; 'land' VALUE 0.25; 'soil' VALUE 0.5

... which is a similar structure as the parameter provides.


Normally to understand what a tool needs to execute, you can perform it manually and from the Results screen Copy the code as Python Snippet.

Although the Help uses a syntax like:
output_raster = arcpy.sa.WeightedSum(wstbl)


The copied Python snippet will use:
arcpy.gp.WeightedSum_sa("snow VALUE 0.25;land VALUE 0.25;soil VALUE 0.5",output_raster)


I would recommend to use the parameter type "Weighted Sum" and provide that as text to the "arcpy.gp.WeightedSum_sa" tool.

import arcpy
arcpy.CheckOutExtension("Spatial")

wstbl = arcpy.GetParameterAsText(0) # list of rasters and weights
out_ras = arcpy.GetParameterAsText(1) # output raster

arcpy.gp.WeightedSum_sa(wstbl,out_ras)



Kind regards, Xander
0 Kudos