为什么需要浮动?

时间:2016-03-08 04:59:29

标签: python

有一个代码:

from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")


print "Creating and defining X and Y coordinates:"  #set env properties
env.workspace = r"C:\Users\Desktop\data\new"
env.overwriteOutput = 1


theme = '/watersheds_3D.shp' #polygons in vector format

demName = '/demlab4'  # raster of DEM
rasObject = Raster(demName)


my_extent = rasObject.extent #find a grid cell size:
my_cellsize = (rasObject.meanCellHeight + rasObject.meanCellWidth)/2
print my_cellsize

slope_deg = Slope(demName)
slope_deg.save('/demlab4_slope')

for row in arcpy.da.SearchCursor(theme, ["FID","SHAPE@"]):  # inside shp file - 
    print ("Polygon # {}: ".format(row[0]))

    for part in row[1]:  # Inside the polygon

        xCoords = []
        yCoords = [] 
        zCoords = [] 
        for pnt in part:

            print (" {}, {}, {} ".format(pnt.X, pnt.Y, pnt.Z))
            xCoords.append(pnt.X)
            yCoords.append(pnt.Y)
            zCoords.append(pnt.Z) 

        area3D = lab4_arcpy_module.define3Darea(my_cellsize, slope_deg)

并计算此功能:

def define3Darea(my_cellsize, slope_deg): 

    a = my_cellsize
    slope_rad = math.pi * slope_deg / 180 # return in radiant
    c = math.sqrt(a**2 + (math.tan(slope_rad)*a)**2)

    area3D = a*c
    print "Total 3D Area is: ", area3D, "m^2"

    return area3D

它给出:TypeError:需要一个float。 可能有什么问题?

还需要计算每个多边形内的3D区域(4)。怎么做?

2 个答案:

答案 0 :(得分:0)

不确定RasterSlope正在做什么,但是当您尝试对非float类型的变量执行某些浮点运算时会出现TypeError: a float is required。我的猜测是问题在于:slope_deg变量。见这里:

slope_deg = Slope(demName)
slope_deg.save('/demlab4_slope')

您正在使用slope_deg变量声明并执行某些操作。现在在这个声明中:

area3D = lab4_arcpy_module.define3Darea(my_cellsize, slope_deg)

您使用的是同一个变量,它是Slope类的实例而不是浮点变量。要么必须有一些Slope的属性float,要么你必须使用正确的变量。

答案 1 :(得分:0)

我发现尝试打印损失是错误的:

print ("Model paramters:" )      
print ("Weight:%f" %sess.run(W))
print ("bias:%f" %sess.run(b))
#print ("loss:%f" %(loss)) <------

让错误消失,仍然不明白为什么会这样。