1

I am working on a script that involves incidents of fire. I'm taking my values from a text file that is comma delimited. The first line of the text file contains the field names (Latitude,Longitude,Confidence). My goal is to create a new feature class that will take the last field name from my text file (e.g. Confidence) and use this as the field name in my new feature class. I have succeeded in getting arcgis to read the last field name from my text file so that a new feature class is created with the field names: "ID, SHAPE, Confidence", however, my feature class is empty- my for loops seems to stop after reading the first line. I believe that I have the right components, I'm just not sure what order to put them in. The part that confuses me the most is, I start reading the text file in a for loop (which I think is pretty normal) and my instinct is to split this text file and, using indexing, point to the last field name "Confidence" using a variable. Once I do this, I'm not sure how to make my insert cursor and Add Field tool (which are outside of the for loop) recognize this variable that points to "Confidence". Any suggestions?

Below is my code:

import arcpy
work=r"C:\Users\Cara\Documents\WildlandFires.mdb"
arcpy.env.workspace=work


arcpy.env.overwriteOutput=True

iFile= r"C:\Users\Cara\Documents\NorthAmericaWildfires_2007275.txt"
with open(iFile, 'r') as f:
    lstFires = f.readlines()


#new feature class name
newFC= fireIncidents
fc=newFC


#user input mininum confidence threshold
confidenceThreshold=99
minThresh=int(confidenceThreshold)


lstFires = f.readlines()


#Create New Feature Class

arcpy.CreateFeatureclass_management(work, fc, "POINT", "", "", "", "", "",     "", "", "")


#Loop through file line by line and populate new rows in feature class
#Create Insert Cursor to use for inserting new rows into feature class

fields = ["SHAPE@", fieldName]
cur = arcpy.da.InsertCursor (fc, fields)

#add field
arcpy.AddField_management(fc, fieldName , "SHORT")
print "created fields"

cntr = 0 #counter variable used to print out progress of script

for fire in lstFires:

    if 'Latitude' in fire: #read line, use split function
        fn = fire.split(",")
        fieldName = fn[2]

        continue

    lstValues =  fire.split(",")#split out values into separate values

    if int(lstValues[2]) > minThresh: # if confidence value is greater than     user defined confidenc value, continue with code   

        #create point object that will hold x,y coordinate of fire
        pnt = arcpy.CreateObject("Point")


        #pull out individual values using list indexing         
        latitude = float(lstValues[0])
        longitude = float(lstValues[1])
        confid = int(lstValues[2])
        print "I can pull out values"           
        cntr=cntr + 1

        #assign x, y properties of Point object to long and lat values
        pnt.X = longitude
        pnt.Y = latitude



        #Create new row for feature class
        #assigns new Point object to "SHAPE" field and confidence value to     "CONFIDENCEVALUE" field                                  
        row = [pnt, confid]
        print "row's OK"
        #Insert new row to feature class
        cur.insertRow(row)
        print "Record # " + str(cntr) + " written to feature class"



    #release cursor lock on feature class
del cur
2

Are you sure that your confidence values are stored as ints and not floats?

This line of your code:

if int(lstValues[2]) > minThresh:

will always return False if the confidence values stored in your text file are stored as numbers between 0.0 and 1.0, which is typically how confidence values are reported. Try replacing that line with this line to see if it help.

if float(lstValues[2]) > minThresh:

Aslo, completely off topic, you might want to change your syntax for reading the text file to the following:

with open(iFile, 'r') as f:
    lstFires = f.readlines()

This syntax is cleaner and you don't have to worry about closing the file explicitly, it is done automatically.

  • The text file that I have gives confidences values from 0-100. So I am purposefully storing my values as integers. This is not totally normal, however, they are the values that I have. I will give the new syntax for reading text files a go. Thanks! – Cara Aug 3 '15 at 13:02
  • Another thing to check. You set confidenceThreshold to a value of 99. Are there records in the fires file that have confidence values higher than this? If not then you won't get any records that pass the if statement. You also need to remove or comment out the lstFires = f.readlines() line that is outside of the with block. – dblanchett Aug 3 '15 at 14:31
1

Turns out that there was a "\n" hiding at the end of my text file row so I had to strip my "confidence" field name. Once I did that everything worked fine!

Here is an excerpt from the code including the change I made in case anyone else runs into this problem:

for fire in lstFires:

    if 'Latitude' in fire: #read line, use split function
        fn = fire.split(",")
        fName = fn[2].rstrip()#strip to get rid of \n

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.