Select to view content in your preferred language

Help with updating an old arcgisscripting file to arcpy.

1661
11
11-08-2017 08:17 AM
WhitneyNewcomb
Regular Contributor

Hi everyone.

I was put to the task of comparing some data and found the perfect script already created that did pretty much everything I need. HOWEVER it's was written originally for ArcMap9.3 and used the arcgisscripting python library. 

I've just about used the extent of my python knowledge to convert it to arcpy. 

I was able to use this script to compare point shapefiles, but now I need it to work on polygons as well. And I keep getting an error on line 69. I believe that it is having a problem with the differences to the describe function between the two libraries. 

I'm totally stuck on this and really need this script to work. I feel a little bad just dumping my script here and hoping someone can fix/figure out what's wrong with the script.

((I also realize that within the script I have not updated everything to be the quickest (i.e. arcpy.SearchCursor changed to arcpy.da.SearchCursor) This is because I tried and failed to do so..))

0 Kudos
11 Replies
RandyBurton
MVP Alum

Posting code in a code block (use the syntax highlighter by clicking "More" when you create/edit your message) has a number of advantages.  Convenience is probably the most important; people don't have to download and extract to see what you are attempting.  Using the code block properly will add formatting and line numbering which will aid the discussion.  A brief description of the project will help focus the discussion.  Sometimes uploading sample of your data (and/or a picture of it) will assist in testing code.

Regarding the AreaFieldName, you could check to see if the attribute exists, as in:

if shapeType == "Polygon":
    if hasattr(dscB, "AreaFieldName"):
        areaField = dscB.AreaFieldName
        attributesToCompare.append(areaField)‍‍‍‍

With a SearchCursor you can use tokens in place of field names, so you wouldn't need to know the name of the area field. (Check out the field names in the syntax help section.)  As an example, you can get the object ID using OID@ and the area using SHAPE@AREA.

fc = r"C:\Path\To\shapefile.shp"
with arcpy.da.SearchCursor(fc,["OID@","SHAPE@AREA"]) as cursor:
    for row in cursor:
        print '{}, {}'.format(row[0],row[1])

'''
0, 24556814787.3
1, 28419119732.9
2, 12772853815.8
3, 46299780205.9
'''
‍‍‍‍‍‍‍‍‍‍‍
curtvprice
MVP Esteemed Contributor

so you wouldn't need to know the name of the area field. 

The token SHAPE@AREA doesn't refer to a field -- the area is accessed from the data in the feature table's shape field -- that means Randy's useful approach would work with shapefiles, which don't have an automatic Shape_Area field calculated.