2

I need help! I am writing a program that will look through the table of a station layer and then go to the annotation layer and list all the missing values. For Example: The station layer has a field called TFLAG which has the station id in it. The annotation layer has a field called TFLAG with the same station id in it. I want to look through the annotation layer and find all the missing values that the station layer has that the annotation layer is missing. I have written some code. I need to know if I am headed in the right direction!!

import arcpy, os, sys, string
arcpy.env.overwriteOutput = True
fc = "K:\TASS\4_MAPPING_DATA_SUPPORT\Traffic_Mapping\Traffic_Count_Data\2011_Counts\2011_Annual_Stations\Final_Annual_Stations_2012.shp"
fields = ("TFLAG", "T_FLAG")
Value = "TFLAG" <> "T_FLAG"
where = "%s = '%s'" % (field, vlaue)
dhList = []
for w in ws:
    arcpy.env.workspace = w
    gdb = arcpy.ListWorkspaces ("*", "TFLAG")
    for fc in gdb:
        arcpy.env.workspace = fc
Michelle Couden
  • 235
  • 1
  • 4
  • 12

1 Answers1

2

I'd do something more like the below. Note that there certainly are more "Pythonic" approaches; however, this should get you through the problem.

Basically I just create two lists of station id's, then compare them. The first list is the comprehensive one from your master file, with 100% of the id's you're checking for. And the second list is build against your annotations table. In the last action, I loop over the comprehensive list and check to see if that id appears in annos/reduced list. If the id is NOT found, I print it to the screen. ..if you have tons of these missing id's you might need to write them into a .txt file, but if you just have a few dozen, dumping to the screen should suffice.

(Fair warning! I don't have Arc 10 so I can't test this, but I think the approach should be passable. I hope it helps.)

[Edited to accommodate Arc 10.0 and up.]

import arcpy, os, sys, string
# These imports are probably a bit excessive but I left them as-is.

# Access the comprehensive Feature Class and build a list of id's.
stationsFC = "K:\TASS\4_MAPPING_DATA_SUPPORT\Traffic_Mapping\Traffic_Count_Data\2011_Counts\2011_Annual_Stations\Final_Annual_Stations_2012.shp"

allStationIds = []
for row in arcpy.SearchCursor(stationsFC, "", "", "TFLAG")
    allStationIds.append(str(row.getValue("TFLAG")))


# Next, access your annos table and build a list of those id's.
# Obviously, you'll need to fix this! But here's another way
# to get into your data if it's in a geodatabase..
annosFC = "c:/Path_To_Your_Data/gisGeoDB.gdb/stationAnnos"

allAnnoStations = []
for row in arcpy.SearchCursor(annosFC, "", "", "TFLAG")
    allAnnoStations.append(str(row.getValue("TFLAG")))


# Finally, loop over the master list, and test each value to learn
# whether it appears among the annotations.
for stationId in allStationIds:
    if not stationId in allAnnoStations:
        print("Missing station: " + str(stationId))
elrobis
  • 6,456
  • 1
  • 31
  • 53
  • up vote for comment lines! – Geocurious Apr 18 '13 at 17:52
  • I am getting an exception raised. But I can't get it to highlight the row it has trouble with. I've tried toggle breakpoint and then step over. What else should I try? – Michelle Couden Apr 19 '13 at 19:09
  • When I run it in ArcMAP Python window, it says object has no attribute 'da'. – Michelle Couden Apr 19 '13 at 19:11
  • OK, when I took the da out. I get an IOError:Unknown. One idea I just had, this should probably run off the layer names. They are always named AADT, AADTAnnoLabel. Let me explain. My boss has asked me to write scripts to make one guys job easier because of how many times stuff has not been right. So, he built his maps wrong in the beginning, and has all his annotation in different gdbs. Don't ask!!! So, I'll try changing it to layers in the code. Please help if you have time. Thanks!! – Michelle Couden Apr 19 '13 at 19:25
  • No, the .da is important because it's a SearchCursor in a different library. At the top, try adding import arcpy.da among the other imports. What version of ArcGIS do you have? – elrobis Apr 19 '13 at 20:52
  • The da is short for DataAccess module, and I got the arcpy.da.SearchCursor bit from this answer, but here is the ESRI page documenting that class. I haven't been able to quickly confirm if the whole da module is exclusive to Arc 10.1, or if it's in 10. – elrobis Apr 19 '13 at 21:12
  • Hi @MichelleCouden, I'm starting to guess you may have ArcGIS 10 (rather than 10.1)? If so, you may need to implement the SearchCursor differently (without going through the da module, and even applying your fieldname parameter in a different part of the constructor). I added two commented-out lines of code that you can swap out for the lines I provided initially. Hopefully that will make the difference. – elrobis Apr 19 '13 at 21:24
  • 1
    @elrobis, the arcpy.da module was added at 10.1. – blah238 Apr 19 '13 at 21:25
  • @blah238, thanks for confirming that---do you think the optional lines I added for 10.0 would work in its place? – elrobis Apr 19 '13 at 21:32
  • 1
    No, I think you would need to use getValue to get the value of the field, as regular SearchCursor row objects are different from da.SearchCursor row objects (which are actually tuples): Compare http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000001q000000 (10.0) vs. http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000001q000000 (10.1). It is unfortunate that they do not highlight the usage differences. – blah238 Apr 19 '13 at 21:37
  • Also note that you can certainly use regular SearchCursors in 10.1. – blah238 Apr 19 '13 at 21:40
  • @MichelleCouden, I made a handful of changes to the code in this answer, largely thanks to blah238's insight. If it still does not work for you, I'll delete my answer to avoid misleading anyone else. – elrobis Apr 19 '13 at 21:51