Referencing Shapefiles in Python and Printing Shapefile Entries

5470
19
Jump to solution
04-08-2015 01:35 PM
RachaelJohnson
Occasional Contributor

I am a complete Python novice and I have been searching and searching and so far, I am unable to find a straightforward answer to my question. 

I want to write a statement that checks if a shapefile has a field "Area", and if not, make one. This is what I have:

outline="LRoutline"

if "Area" not in outline:

    arcpy.AddField_management(outline, "Area", "FLOAT", 6, 3)

The If statement isn't working.  When I run it again, the code gets hung up because there is already a field named "Area".  Obviously, the code is not looking if there is a field named "Area" but rather it is looking for that string in the variable.  How do I reference the fields inside a shapefile within an If statement? outline.Area doesn't work and neither does !Area!.  I have tried the SearchCursor thing using the following code and it outputs <Cursor object at 0x137ed0f0[0x13587f80]>, and I don't know what that means.

arcpy.SearchCursor(outline)

Furthermore, I want to sum the areas in that Area field after they are calculated, so I need to be able to reference the field, right?  I looked at the arcpy.Statistics_analysis() function and I need to create a whole separate table to pull one value from the shapefile?  That's not required to view the summary statistics in the ArcGIS GUI (right click on field in attribute table-> Summary...).  Is there a less cluttered way to sum the entries in a shapefile field in Python?

I have downloaded the shapefile package but that doesn't seem to fit what I need either. 

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

Make your list of fields using ListFields. If you're first learning cursors, you should skip to using the arcpy.da.SearchCursor. Both links contain examples at the bottom.

View solution in original post

19 Replies
DarrenWiens2
MVP Honored Contributor

Make your list of fields using ListFields. If you're first learning cursors, you should skip to using the arcpy.da.SearchCursor. Both links contain examples at the bottom.

RachaelJohnson
Occasional Contributor

Thank you so much for pointing me to ListFields and arcpy.da.SearchCursor!  How is da.SearchCursor different from regular SearchCursor? 

Anyway, I will see what I can do with these two commands.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Data module cursors are much faster and provide access to geometry tokens. Probably other unknown benefits.

BlakeTerhune
MVP Regular Contributor

The regular (non da) cursors are also older and will eventually get depreciated by Esri. The new ones are more future proof.

BlakeTerhune
MVP Regular Contributor

It looks like you spent a lot of time formatting your Python code to highlight syntax like the IDE. There's a handy way to let the GeoNet forums do that for you!

Posting Code blocks in the new GeoNet

0 Kudos
RachaelJohnson
Occasional Contributor

I actually just copy/pasted from PyCharm and it looked like that without any effort! But thank you. 

0 Kudos
JamesCrandall
MVP Frequent Contributor
fc = r'PathToLayer\LayerNamel'
field_names = [f.name for f in arcpy.ListFields(fc)]
if 'Area' not in field_names:
    arcpy.AddField_management(fc, "Area", "FLOAT", 6, 3)

Resources:

List Fields ArcGIS Help (10.2, 10.2.1, and 10.2.2)

AddFields ArcGIS Help (10.2, 10.2.1, and 10.2.2)

BlakeTerhune
MVP Regular Contributor

To expand on what James Crandall posted and accomplish your second task of getting the total area for all features in the shapefile, you could use a SearchCursor like Darren Wiens mentioned, or the Summary Statistics that you mentioned yourself. If you go with the Summary Statistics, you could use in-memory workspace for the output table, but you'd still have to use a SearchCursor to get the values out of it, so you might as well just do your own adding.

import arcpy

fc = r'PathToLayer\LayerNamel'
field_names = [f.name for f in arcpy.ListFields(fc)]
if 'Area' not in field_names:
    arcpy.AddField_management(fc, "Area", "FLOAT", 6, 3)

arcpy.CalculateField_management(
    fc,
    "Area",
    "!SHAPE.area!",
    "PYTHON_9.3",
)

fcAreas = [row[0] for row in arcpy.da.SearchCursor(fc, ["Area"])]
print sum(fcAreas)

EDIT:

After thinking about this, it might actually be better if you used Summary Statistics instead of calculating the field because if the geometry of the features change, the Area field will not get updated unless you run this code again. The values in the Area field could be misleading.

RachaelJohnson
Occasional Contributor

Thank you!  Would you mind helping me understand that code? What is the purpose of the row[0]?  Is it telling the computer to start with the first entry in the "Area" field? Or is it creating a list? Or something else? 

0 Kudos