0

I am writing an Arcpy script to use in ArcMap (ArcGIS 10.7.1) as a tool to interpolate existing soil points, and make a nutrient recommendation afterwards. However, I'm getting the TypeError: can't multiply sequence by non-int of type 'float' error, and I'm not sure what's going on. The funny thing is that this script will run as a standalone script, but when I upload it into Arcmap to be used as a tool, it gives me this error. How can I fix the problem?

The code I have is shown below. I have commented out the Arcmap parameter type that I set each parameter as when I upload the script as a tool.

#Importing required modules
import arcpy
from arcpy import env
from arcpy.sa import *
from arcpy.da import *

#Environment settings
env.workspace = #Workspace parameter
env.overwriteOutput = True

#Set main field parameters
fieldBoundary = #Feature class
soilPoints = #Feature class (points to be interpolated)

crop = #String, indicating crop type
yieldGoal = #Long integer, such as 200
nField = #Field (of the soilPoints) indicating nitrogen levels at each point
organicMatter = #Field (of the soilPoints) indicating organic matter at each point
previousCrop = #String indicating previous crop grown

#Check out extensions
arcpy.CheckOutExtension("Spatial")
arcpy.CheckkOutExtensison("3d")

#Interpolate nitrogen soil points
field = nField
n_krig = Kriging(soilPoints, field, kModelOrdinary, cellSize, kRadius, outRaster)
nitrogenField = ExtractByMask(n_krig, fieldBoundary)

#Interpolate organic matter soil points
field = organicMatter
om_krig = Kriging(soilPoints, field, kModelOrdinary, cellSize, kRadius, outRaster)
organicMatterField = ExtractByMask(om_krig, fieldBoundary)

#Set previous crop adjustments, which will be based off of user selection in parameters
if previousCrop == "Corn":
           pca = int(0)
        elif previousCrop == "Wheat":
            pca = int(0)
        elif previousCrop == "Milo":
            pca = int(0)
        elif previousCrop == "Sunflowers":
            pca = int(0)
        elif previousCrop == "Soybeans":
            pca = int(40)
        elif previousCrop == "Alfalfa: >5 plants/square foot":
            pca = int(120)
        elif previousCrop == "Alfalfa: 2-5 plants/square foot":
            pca = int(80)
        elif previousCrop == "Alfalf: 2-3 plants/square foot":
            pca = int(40)
        elif previousCrop == "Alfalfa: <2 plants/square foot":
            pca = int(0)
        else:
            pca = int(0)

#Nitrogen needs equation. Produces a raster output
#ERROR OCCURS AFTER THIS LINE

nitrogen_recommendation = ((1.6 * yieldGoal) - (organicMatterField * 20) - (nitrogenField) - (pca))

Traceback (most recent call last):
  File "C:\script.py", line 125, in <module>
    nitrogen_recommendation = ((1.6 * yieldGoal) - (organicMatterField * 20) - (nitrogenField) - (pca))
TypeError: can't multiply sequence by non-int of type 'float'
  • @user2856 no it does not. After adjusting that code, I get this new error: Traceback (most recent call last): File "C:\script.py", line 125, in <module> nitrogen_recommendation = ((1.6 * int(yieldGoal)) - (Raster(organicMatterField) * 20) - (Raster(nitrogenField)) - (pca)) TypeError: expected a raster or layer name – ihb Dec 9 '19 at 1:01
  • try yieldgoal = int('{0}'.format(200)). Do you use GetParameterAsText when use it as a tool in Arcmap? – BBG_GIS Dec 9 '19 at 17:12
0

As the standalone script is dynamically typed and anticipates the type of the variables, ArcMap is a little more sensitive to variable types. So, the following works:

nitrogen_recommendation = (1.6 * float(yieldGoal)) - (organicMatterField * 20.0) - (nitrogenField) - float(pca)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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