Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I've been playing around with 'Maintaining Attribute Integrity' functions found here:

http://resources.arcgis.com/en/help/...000000n000000/

I see how I can set domains and subtypes to control the values in the attribute table to maintain quality and how to validate features and relationships using the tools ArcGIS provides.

I don't know if I am missing something, but I have a situation where values in one field in an attribute table, depend on the value in another field in the same attribute table.

Let's have 3 fields (CITY, PROVINCE, COUNTRY)

Let's say I set a domain on the CITY field to populate a list of all the cities in the world. I want to be able to validate that the PROVINCE and COUNTRY fields are correct. So, for example, if I enter 'Toronto' in the CITY field, I want to validate that the PROVINCE field is 'Ontario' and the COUNTRY field is 'Canada'

I'm not sure how to set up the domains or subtypes to do this. I want to be able to run the validate functionality found here:

http://resources.arcgis.com/en/help/...000000v000000/

To throw an invalid error if these conditions aren't met. Can anyone suggest how I can accomplish this?

I can do this through python, but the validation in ArcGIS does a lot of things I was going to script, so I'm hoping it will do this for me too.

Thanks, Mike

share|improve this question
2  
Good question! ArcInfo Workstation had a command called CODEFIND which did exactly this but it may have been lost in translation to ArcGIS Desktop. DataReviewer is the most likely place to find it. –  PolyGeo May 10 '13 at 20:32
    
Thanks PolyGeo. I'm working with a Standard license, so I don't have ArcInfo. Unfortunately, the direction I got from my manager is that I am allowed to write a script, but he won't let me fork out $2500 for the Data Reviewer license :( –  Mike May 10 '13 at 20:52
add comment

1 Answer

up vote 2 down vote accepted

I have just submitted an ArcGIS Idea for Validation of Hierarchical Fields but since your comment says that your requirement is now for Python I thought I would put together some code showing how to do this using dictionaries.

  • The CityProvinceLookup table has two fields CITY and PROVINCE with a row for each valid combination.
  • The ProvinceCountryLookup table has two fields PROVINCE and COUNTRY with a row for each valid combination.
  • The test table has fields CITY, PROVINCE and COUNTRY.

The code is not as pythonic as it could be but I wanted to keep it fairly readable.

import arcpy

# Make dictionaries of lookup tables
dictCityProvince = {}
rows = arcpy.SearchCursor(r"C:\temp\test.gdb\CityProvinceLookup")
for row in rows:
    dictCityProvince[row.CITY] = row.PROVINCE
del row,rows
dictProvinceCountry = {}
rows = arcpy.SearchCursor(r"C:\temp\test.gdb\ProvinceCountryLookup")
for row in rows:
    dictProvinceCountry[row.PROVINCE] = row.COUNTRY
del row,rows

# Now test the data
rows = arcpy.SearchCursor(r"C:\temp\test.gdb\test")
for row in rows:
    if row.CITY == None or row.CITY == "" or row.CITY == " ":
        print "Row " + str(row.OBJECTID) + " has no value for CITY"
        hasCity = False
    else:
        hasCity = True

    if row.PROVINCE == None or row.PROVINCE == "" or row.PROVINCE == " ":
        print "Row " + str(row.OBJECTID) + " has no value for PROVINCE"
        hasProvince = False
    else:
        hasProvince = True

    if row.COUNTRY == None or row.COUNTRY == "" or row.COUNTRY == " ":
        print "Row " + str(row.OBJECTID) + " has no value for COUNTRY"
        hasCountry = False
    else:
        hasCountry = True

    if hasCity and hasProvince:
        if row.CITY in dictCityProvince:
            if row.PROVINCE == dictCityProvince[row.CITY]:
                pass
            else:
                print row.CITY + " is NOT in " + row.PROVINCE + " (Row " + str(row.OBJECTID) + ")"
        else:
            print row.CITY + " is NOT in CityProvinceLookup (Row " + str(row.OBJECTID) + ")"
    if hasProvince and hasCountry:
        if row.PROVINCE in dictProvinceCountry:
            if row.COUNTRY == dictProvinceCountry[row.PROVINCE]:
                pass
            else:
                print row.PROVINCE + " is NOT in " + row.COUNTRY + " (Row " + str(row.OBJECTID) + ")"
        else:
            print row.PROVINCE + " is NOT in ProvinceCountryLookup (Row " + str(row.OBJECTID) + ")"
del row,rows
share|improve this answer
    
Hey PolyGeo, I finally had a chance to look at this code. I made a couple adjustments to suit my needs, but it works well. I like the idea of using dictionaries this way. It's new to me. Thanks for taking the time creating and posting this code! –  Mike May 30 '13 at 17:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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