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.
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