Multiple Field calculations

2457
4
Jump to solution
01-12-2016 08:26 AM
KONPETROV
Occasional Contributor III

Hi I have severous viewsheds (view1, view2, view3 etc) and I want to calculate the percent of visible areas per viewshed. I have the following code but I always get zero "0" at the second for loop and i have been trying that for hours, any help?

Thanks in advance

import arcpy
import os
from arcpy import env
from arcpy.sa import *


#workspace = arcpy.GetParameterAsText(0)
arcpy.env.workspace = "C:Visibility/Calc_View/View"
ras = arcpy.ListRasters("view*", "GRID")
#cellsize = arcpy.GetParameterAsText(1)
fields = ["VALUE", "COUNT"]
for r in ras:
    with arcpy.da.UpdateCursor(r, fields) as cursor:
        total = 0
        for row in cursor:
            total = total + row[1]
            print r, total
        C = arcpy.da.UpdateCursor(r, fields, """"VALUE" = 1""")
        for row in C:
            percent = row[1] / total
            print r, percent
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

  percent = row[1] /  float(total)  maybe or row[1] is zero...print them before your percent calc

View solution in original post

4 Replies
DanPatterson_Retired
MVP Emeritus

  percent = row[1] /  float(total)  maybe or row[1] is zero...print them before your percent calc

KONPETROV
Occasional Contributor III

aaa close, thank you a lot

0 Kudos
XanderBakker
Esri Esteemed Contributor

A view comments.

  • row 8: C:Visibility/Calc_View/View is missing a forward slash (should be C:/Visibility/Calc_View/View)
  • I assume the rasters are of type integer, otherwise the raster will not have a value attribute table
  • When updating an attribute table of a raster, you need to use the old arcpy.UpdateCursor. arcpy.da.UpdateCursor does not support value attribute tables.
KONPETROV
Occasional Contributor III

thank you, I didn't want to update it I was just want to take the value, I will just pass it to a txt.

0 Kudos