Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
7  
When calculating in the Python script, make sure you force float calculations, e.g. 12/2 = 6, but 12./2. = 6.0 –  Jason Mar 28 '13 at 15:22

1 Answer 1

up vote 5 down vote accepted

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

share|improve this answer
    
Ah yes, I glossed over the first line of code where TotalFlightPoints is cast as int. This should do it. –  Jason Mar 28 '13 at 16:57
    
THERE we go. Thank you. (I should have known better, sheesh!) –  Erica Mar 29 '13 at 1:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.