Python工具箱问题



我正在创建一个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经验的人来说,这一切似乎有点太容易了,所以我怀疑我是否忘记了一些重要的东西。如果有人对我如何实现我想要产生的东西有任何想法,我将非常高兴听到他们。

我想你的问题出在这一行:

dist_rast = parameters[2].valueAsText

如果它正在做我认为它是它返回一个string对象,不能乘以一个浮点数(你正试图用math.pi做,可能是distmod)

将其转换为浮点数,然后再试一次。

最新更新