我正在创建一个python工具箱来解决基于各种输入的WiFi信号属性,对于最终结果,我希望以光栅的形式输出。我需要这个栅格根据成本栅格(距离栅格)和数字输入(频率类型以GHz和英尺 - 米转换系数)为每个单元格取一个方程值(FSPL)。下面是我尝试过的代码,但我得到一个错误:
Traceback (most recent call last):
File "<string>", line 108, in execute
TypeError: can't multiply sequence by non-int of type 'float'.
Failed to execute (FSPL).
以下是下面的代码(第108行标记了代码本身靠近底部的写入):
import arcpy
import math
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "Toolbox Label Property"
self.alias = "Toolbox Alias Property"
# List of tool classes associated with this toolbox
self.tools = [FSPL, WAP_Buffer]
class FSPL(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Free Space Path Loss"
self.description = "This python script tool will create Free Space Path Loss to determine the dB range output from the WAPs."
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
param0 = arcpy.Parameter(
displayName="Wireless Access Points",
name="wireless_pts",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
param1 = arcpy.Parameter(
displayName="Network Frequency Type",
name="network_freq",
datatype="String",
parameterType="Required",
direction="Input")
param1.filter.type="ValueList"
param1.filter.list = ["2.4 GHz", "5 GHz"]
param2 = arcpy.Parameter(
displayName="Distance Raster",
name="dist_rast",
datatype="GPRasterLayer",
parameterType="Required",
direction="Input")
param3 = arcpy.Parameter(
displayName="Distance Raster Units",
name="units",
datatype="String",
parameterType="Required",
direction="Input")
param3.filter.type="ValueList"
param3.filter.list = ["Feet", "Meters"]
param4 = arcpy.Parameter(
displayName="Output Raster",
name="output_rast",
datatype="GPRasterLayer",
parameterType="Required",
direction="Output")
return [param0, param1, param2, param3, param4]
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
#Get inputs
wireless_pts = parameters[0].valueAsText
network_freq = parameters[1].valueAsText
dist_rast = parameters[2].valueAsText
units = parameters[3].valueAsText
output_rast = parameters[4].valueAsText
shapeFieldName = arcpy.Describe(wireless_pts).shapeFieldName
#Create expression
if network_freq == "2.4 GHz":
hertz=2400000000
else:
hertz=5000000000
if units == "Feet":
distmod=0.3048
else:
distmod=1
#equation
LINE 108 fspl= (4 * math.pi * distmod * dist_rast * hertz)/(2.99792458 * (10**8))
output_rast = fspl
return
我使用python相当新,可能会有一些非常基本的东西,我不会理解。这对于没有太多python经验的人来说似乎有点容易,所以我怀疑我忘记了一些大事。如果有人对如何实现我想要制作的东西有任何想法,我会很高兴听到它们。
答案 0 :(得分:2)
我认为你的问题在于这一行:
dist_rast = parameters[2].valueAsText
如果它正在做我认为它正在返回一个string
对象,该对象不能乘以浮点数(您尝试使用math.pi
,可能distmod
)
相反,将其转换为浮点数并再试一次。