I am trying to calculate a field for some points within a python script using arcpy functions. (This is calculating the percentage visibility from a cumulative viewshed, if that matters.)
# Process: Get Count
TotalFlightPoints = int(arcpy.GetCount_management(FlightPath_Pts).getOutput(0))
# Process: Add Field
Obsv_Vis = arcpy.AddField_management(Obsv_Vis, "PercentVis", "FLOAT")
# Process: Calculate Field (2)
Obsv_Vis = arcpy.CalculateField_management(Obsv_Vis, "PercentVis", "!RASTERVALU! / " + str(TotalFlightPoints), "PYTHON", "")
Even though I cast that field as a FLOAT when adding it, the result is consistently an integer (0 if less than 1, 1 if the point is 100% visible). I can get around this by "!RASTERVALU! * 100 / " + str(TotalFlightPoints)
, which will at least give me the percentage (without any trailing decimals). That level of accuracy may be sufficient for this application, at least for now.
But, if I use the Field Calculator within ArcMap on a "float" field (with, say, !RASTERVALU! / 121
as the equation), it gives me the result as a float. So I'm wondering why the field calculator seems to work differently in different situations, and/or what I can tweak to get what I really want.
1 Answer 1
@Jason's comment is correct: you need to be dividing by a float, but you define TotalFlightPoints
as an integer. Try it with
TotalFlightPoints = float(arcpy.GetCount_management(FlightPath_Pts).getOutput(0))
and you should see different results.
-
Ah yes, I glossed over the first line of code where
TotalFlightPoints
is cast asint
. This should do it.Jason Bellino– Jason Bellino2013年03月28日 16:57:03 +00:00Commented Mar 28, 2013 at 16:57 -
THERE we go. Thank you. (I should have known better, sheesh!)Erica– Erica2013年03月29日 01:02:32 +00:00Commented Mar 29, 2013 at 1:02
Explore related questions
See similar questions with these tags.
12/2 = 6
, but12./2. = 6.0