1

I am attempting to write a script to include an input from the user that represents a "Minimum Confidence Threshold". I want to use this value to only save & print records to the feature class that have a confidence level above (greater than) this threshold. The data is from a .txt file.

My current code is still printing all 411 records. If I have a confidence level of 99 for example, it should only print out 23 record. I think there must be an error with the way I have arranged my statements or indentations.Any advice?

import arcpy

work = raw_input("Enter the full path of WildlandFires.mdb: ") # C:\Scripts\Lab 7 Data\WildlandFires.mdb
arcpy.env.workspace = work # Set the workspace to the geodatabase
arcpy.env.overwriteOutput = True

iFile = raw_input("Enter the full path of wildfire text file: ")# input text file C:\Scripts\Lab 7 Data\NorthAmericaWildfires_2007275.txt 

output = raw_input("Enter the name of the output feature class: ")
threshold = raw_input("Specify the minimum confidence threshold: ")

f = open(iFile, 'r')# input text file C:\Scripts\Lab 7 Data\NorthAmericaWildfires_2007275.txt in "read" mode

lstFires = f.readlines() # Read the lines of fire data from the input file
f.close() # close the file 

field = ["SHAPE@", "CONFIVALUE"] # represents the field names for each row in the feature class

# Process: Create Feature Class
arcpy.CreateFeatureclass_management(work, output, "POINT")

arcpy.AddField_management(output, "CONFIVALUE", "FLOAT")

cursor_new = arcpy.da.InsertCursor(output, field)# add the new points that you will create to the "NewFires" feature class

cntr = 0 # counter variable

for fire in lstFires:
        if 'Latitude' in fire: # Skip the header
                continue
        pnt2 = arcpy.Point() # Create a new Point object
        lstValues = fire.split(',') # create a list of values "lstValues" based on the type of delimiter
        str_int = lstValues [-3:]       
        str_int = list(map(float, str_int)) #turns string to integers
        latitude = str_int[0] # Latitude
        longitude = str_int[1] # Longitude
        confid = str_int[2] # Confidence Value 

        if (confid > threshold):
                pnt2.X = longitude # Assign the X and Y properties of the Point object
                pnt2.Y = latitude        

        row = [pnt2, confid] # Create a new row for the feature class
        cursor_new.insertRow(row) # Insert the new row to the feature class                
        cntr = cntr + 1 # update the counter
        print "Record # " + str(cntr) + " written to feature class"

del cursor_new # release the cursor lock on the feature class   
2

You should indent the four lines starting from row=... The issue in your code is, you want to create a point if the confidence value from the text file is above the threshold but with this current indentation, you are creating a point for all lines, but only the ones above the threshold appears, the rest has no geometry/

  • adding those remaining lines to the second 'if' loop returns nothing. I wanted it to print out the records that did have a threshold > confid – A_Worthy Feb 4 '17 at 16:38
  • Then make sure both confid and threshold variables are numeric. – fatih_dur Feb 4 '17 at 16:39
  • 1
    That worked I used threshold = eval(raw_input("Specify the minimum confidence threshold: ")) to make sure it was reading an integer and not a string. – A_Worthy Feb 4 '17 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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