0

I am currently trying to complete a final project for my class that involves calculating a wildlands index using and elevation raster, a landuse raster, federal lands raster, a state parks raster and a polyline shapefile containing roads as inputs for the county I am using in my analysis. Here is the code. I exported it from Model Builder and made some tweaks.

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# WildlandsCalculator21.py
# Created on: 2019-04-27 20:54:52.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: WildlandsCalculator21 <FedLandsWeight> <LanduseWeight> <SlopeWeight> <StateParkWeight> <DistanceWeight> <WildlandsIndex> <wilkes_asc> <WilkesLnduse> <WilkesRoads_shp_> <WilkesNFNP> <WilkesSPWRC> 
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

#Ensure that the tools are licensed
arcpy.CheckOutExtension("Spatial")

#Set overwrite priveleges for tool.
arcpy.env.overwriteOutput = True

# Script arguments
FedLandsWeight = arcpy.GetParameterAsText(0)
if FedLandsWeight == '#' or not FedLandsWeight:
    FedLandsWeight = "10" # provide a default value if unspecified

LanduseWeight = arcpy.GetParameterAsText(1)
if LanduseWeight == '#' or not LanduseWeight:
    LanduseWeight = "25" # provide a default value if unspecified

SlopeWeight = arcpy.GetParameterAsText(2)
if SlopeWeight == '#' or not SlopeWeight:
    SlopeWeight = "30" # provide a default value if unspecified

StateParkWeight = arcpy.GetParameterAsText(3)
if StateParkWeight == '#' or not StateParkWeight:
    StateParkWeight = "10" # provide a default value if unspecified

DistanceWeight = arcpy.GetParameterAsText(4)
if DistanceWeight == '#' or not DistanceWeight:
    DistanceWeight = "25" # provide a default value if unspecified

arcpy.AddMessage(SlopeWeight)
arcpy.AddMessage(DistanceWeight)
arcpy.AddMessage(LanduseWeight)
arcpy.AddMessage(FedLandsWeight)
arcpy.AddMessage(StateParkWeight)


sumweight = float(SlopeWeight) + float(DistanceWeight) + float(LanduseWeight) + float(FedLandsWeight) + float(StateParkWeight)
if sumweight == 100:
    print "weights equal 100"

else:
    print "weights do not equal 100"  

WildlandsIndex = arcpy.GetParameterAsText(5)
if WildlandsIndex == '#' or not WildlandsIndex:
    WildlandsIndex = "C:\\Users\\Documents\\ArcGIS\\Default.gdb\\WildlandsIndex" # provide a default value if unspecified

wilkes_asc = arcpy.GetParameterAsText(6)
if wilkes_asc == '#' or not wilkes_asc:
    wilkes_asc = "C:\\Users\\Desktop\\GIS6400PythonFinalProj2\\WilkesCountyElevation\\wilkes.asc" # provide a default value if unspecified

WilkesLnduse = arcpy.GetParameterAsText(7)
if WilkesLnduse == '#' or not WilkesLnduse:
    WilkesLnduse = "C:\\Users\\Desktop\\GIS6400PythonFinalProj2\\Wildlandsproject.gdb\\WilkesLnduse" # provide a default value if unspecified

WilkesRoads_shp_ = arcpy.GetParameterAsText(8)
if WilkesRoads_shp_ == '#' or not WilkesRoads_shp_:
    WilkesRoads_shp_ = "C:\\Users\\Desktop\\GIS6400PythonFinalProj2\\WilkesCountyRoads\\WilkesRoads.shp" # provide a default value if unspecified

WilkesNFNP = arcpy.GetParameterAsText(9)
if WilkesNFNP == '#' or not WilkesNFNP:
    WilkesNFNP = "C:\\Users\\Desktop\\GIS6400PythonFinalProj2\\Wildlandsproject.gdb\\WilkesNFNP" # provide a default value if unspecified

WilkesSPWRC = arcpy.GetParameterAsText(10)
if WilkesSPWRC == '#' or not WilkesSPWRC:
    WilkesSPWRC = "C:\\Users\\Desktop\\GIS6400PythonFinalProj2\\Wildlandsproject.gdb\\WilkesSPWRC" # provide a default value if unspecified

# Local variables:
Output_direction_raster = ""
RescaleLanduse = "%scratchGDB%\\RescaleLanduse"
WilkesSlopes = "%scratchGDB%\\WilkesSlopes"
RescaleSlopes = "%scratchGDB%\\RescaleSlopes"
EucDist = "%scratchGDB%\\EucdistWilkes"
RescaleRoads = "%scratchGDB%\\RescaleRoads"

# Process: Euclidean Distance
arcpy.gp.EucDistance_sa(WilkesRoads_shp_, EucDist, "", "1.7065600005435E-03", Output_direction_raster, "PLANAR")

# Process: Slope
arcpy.gp.Slope_sa(wilkes_asc, WilkesSlopes, "DEGREE", "1", "PLANAR", "METER")

# gets the maximum pixel value from a raster dataset (2)
max = arcpy.GetRasterProperties_management(WilkesSlopes, "MAXIMUM", "")
# rescales the raster from 0.0 to 1.0
arcpy.gp.Divide_sa(WilkesSlopes, max, RescaleSlopes)

# gets the maximum pixel value from a raster dataset
max = arcpy.GetRasterProperties_management(EucDist, "MAXIMUM", "")
# rescales the raster from 0.0 to 1.0
arcpy.gp.Divide_sa(EucDist, max, RescaleRoads)

# gets the maximum pixel value from a raster dataset (3)
max = arcpy.GetRasterProperties_management(WilkesLnduse, "MAXIMUM", "")
# rescales the raster from 0.0 to 1.0
arcpy.gp.Divide_sa(WilkesLnduse, max, RescaleLanduse)

# Process: Weighted Sum
arcpy.gp.WeightedSum_sa(RescaleRoads +  " Value " + DistanceWeight + "; " + RescaleSlopes + " Value " + SlopeWeight + "; " + RescaleLanduse + " Value " + LanduseWeight + "; " + WilkesSPWRC + " Value " + StateParkWeight + "; " + WilkesNFNP + " Value " + FedLandsWeight, WildlandsIndex)

When I script without importing it into a toolbox, it runs fine, however when I import it, I get this message. enter image description here

I am at my wits end with this script. I do not know how to fix this problem. I have tried playing witht the data types in the parameters window of for my tool and I still been unable to get it to work in arcmap. It has something to do with the roads feature class I am using for my wildlands index.

I am getting this error when I remove the quotations from my weight values.

Traceback (most recent call last):
  File "C:\Python27\ArcGIS10.6\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Users\tyqui\OneDrive\Desktop\GIS6400PythonFinalProj2\PythonScripts\WildlandsCalculator22.py", line 108, in <module>
    arcpy.gp.WeightedSum_sa(RescaleRoads +  " Value " + DistanceWeight + "; " + RescaleSlopes + " Value " + SlopeWeight + "; " + RescaleLanduse + " Value " + LanduseWeight + "; " + WilkesSPWRC + " Value " + StateParkWeight + "; " + WilkesNFNP + " Value " + FedLandsWeight, WildlandsIndex)
TypeError: cannot concatenate 'str' and 'int' objects

Here are the pictures of the parameters windowenter image description here

enter image description here

4
  • Try taking the "" off the values used in the Slopeweight defaults. It may be sending them through to the equation as string. Commented Apr 29, 2019 at 1:27
  • 1
    Please remember to always include the full error message as text (makes it legible and searchable). You don't need to include the come below the error unless it impacts the flow of control.
    – Vince
    Commented Apr 29, 2019 at 1:28
  • Now I am getting this error once I removed the quotations from the default values for my weights. Commented Apr 29, 2019 at 1:48
  • I have edited my original post to include the full error message and my tool parameters window. Commented Apr 29, 2019 at 2:53

1 Answer 1

3

You have defined the weights as parameters 0-4 and the layers as parameters 5-10 in your script:

FedLandsWeight = arcpy.GetParameterAsText(0)  # <=== 1st parameter
etc...
DistanceWeight = arcpy.GetParameterAsText(4)  # <=== 5th parameter
etc...
WildlandsIndex = arcpy.GetParameterAsText(5)  # <=== 6th parameter  
etc...    
WilkesSPWRC = arcpy.GetParameterAsText(10)    # <=== 11th parameter

But you have specified a different order in your script tool parameters:

  • Elevation (DEM) <=== 1st parameter
  • etc...
  • Landuse Raster <=== 5th parameter
  • Landuse Weight <=== 6th parameter
  • Federal Lands Raster <=== 7th parameter
  • Federal Lands Weight <=== 8th parameter
  • etc...

Either re-arrange the parameters in your script:

wilkes_asc = arcpy.GetParameterAsText(0)
etc...
WilkesLnduse = arcpy.GetParameterAsText(4)
LanduseWeight = arcpy.GetParameterAsText(5)
FedLandsRaster = arcpy.GetParameterAsText(6)
FedLandsWeight = arcpy.GetParameterAsText(7)
etc...

Or re-arrange the parameters in your script tool parameters properties.

1
  • This worked. All I had to do was put the parameters in the proper order in the model builder and then exported it to a python script where the parameters had to be in the same order. Thank you. Commented Apr 29, 2019 at 3:36

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.