0

I am building a Python tool to automate a least cost path analysis. For this I need to iterate over layers and values in order to add them into the weighted overlay geoprocessing tool.

Example:

User input = [Layer 1, Influence = 0.8], [Layer 2 influence = 0.2]

WOTable = 
[UILayer, Influence, "Value", "0 10;1 1;NODATA 10", "Layer1"], [UILayer, Influence, "Value", "0 10;1 1;NODATA 10", "Layer2"]

geoprocessing = arcpy.sa.WeightedOverlay(WOTable)

I know the code above doesn't work the way it is, the input isn't the problem. In this example I would like the tool to iterate over the user inputs and add them into the table, I know how to do this when the amount of layers would be predetermined, but I want the user to be able to use as many layers as they want.

How do I achieve this?

I have managed to make a list within a list as a table, which ends up looking like this:

[['C:\\Users\\me\\Downloads\\Layers.gdb\\Layer1', '0.5', 'Value', '0 10;1 1;NODATA 10', ''C:\\Users\\me\\Downloads\\Layers.gdb\\Layer1'], ['C:\\Users\\me\\Downloads\\Layers.gdb\\Layer2', '0.5', 'Value', '0 10;1 1;NODATA 10', 'C:\\Users\\me\\Downloads\\Layers.gdb\\Layer2']]

However when I run it through the geoprocessing tool I get the following error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "<string>", line 46, in ConstraintAreas
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\sa\Functions.py", line 10159, in WeightedOverlay
    in_weighted_overlay_table)
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\sa\Utils.py", line 55, in swapper
    result = wrapper(*args, **kwargs)
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\sa\Functions.py", line 10092, in Wrapper
    m = table_eval_pattern.match(in_weighted_overlay_table)
TypeError: expected string or bytes-like object

The way I run the geoprocessing tool is this, with the weighted list being the above one:

arcpy.sa.WeightedOverlay(WeightedList)
3
  • How do you expect your users to run this code, at the command line prompt or as a script tool in a toolbox? If as a script tool then you need to be exploring how to create a ValueTable parameter that would accept many entries.
    – Hornbydd
    Commented Oct 13, 2022 at 11:13
  • I managed to make a table by using a list in a list. So every layer now has it's own list of layer, influence, value etc. However when I run it through the geoprocessing tool the following error occurs: TypeError: expected string or bytes-like object
    – Amidaz
    Commented Oct 13, 2022 at 11:31
  • Suggest you read the help file for the Weighted Overlay tool, it clearly states that input is a WOTable object not a list of lists. As with all tools there is sample code to study you need only click on the Python tab in the parameters section. Try clicking on the link to the WOTable object for further advice.
    – Hornbydd
    Commented Oct 13, 2022 at 11:53

1 Answer 1

0

Here is an example showing how to setup the weighted overlay table: https://pro.arcgis.com/en/pro-app/latest/tool-reference/spatial-analyst/weighted-overlay.htm

As noted in the documentation, the input raster is expected to be Integer.

Alternatively you could just iterate your list of lists and do the weighted combination using the operators available for a raster. This assumes that all rasters have the same cell size, spatial reference system, and covers the same bounds. https://pro.arcgis.com/en/pro-app/latest/arcpy/spatial-analyst/arithmetic-times-operator.htm

WOTable = 
[[UILayer, .8], [UILayer, .5]]

weighted_rasters = []
for x in WOTable:
    r = Raster(x[0]) * x[1]
    weighted_rasters.append(r)
wr = weighted_rasters.pop()
for x in weighted_rasters:
   wr = wr + x

It's also not a bad idea to divide by the sum of the weights.

Your Answer

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

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