3

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.

asked Mar 28, 2013 at 15:13
1
  • 8
    When calculating in the Python script, make sure you force float calculations, e.g. 12/2 = 6, but 12./2. = 6.0 Commented Mar 28, 2013 at 15:22

1 Answer 1

6

@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.

answered Mar 28, 2013 at 16:25
2
  • Ah yes, I glossed over the first line of code where TotalFlightPoints is cast as int. This should do it. Commented Mar 28, 2013 at 16:57
  • THERE we go. Thank you. (I should have known better, sheesh!) Commented Mar 29, 2013 at 1:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.