Compare field attributes

2491
16
Jump to solution
03-23-2016 07:40 AM
CCWeedcontrol
Regular Contributor

I have a points layer  and polygon layer , I need to compare field "SiteAddres" from the points layer to "SiteAddress" to the polygon layer and if it matches i need it to populate field "Verifi" with "Match" or if it doesn't match to populate "Verifi" with "No Match".  Although i am not sure what would be the best way to achieve what i am trying to do because i am trying to compare address which will have upper case and lower case and spaces. I would prefer not to do a spatial join every time i need to verify and i looked into doing it with a python script but i am not sure how to do it?

I get no error but nothing the field "Verifi" doens't get populated with "Match" or "No Match"

fc1 = "Points"
fc2 = "Polygons"

cursor1 = arcpy.da.SearchCursor(fc1, ["SiteAddres", "Verifi"])
cursor2 = arcpy.da.SearchCursor(fc2, ["SiteAddres"])

for row1 in cursor1:
    for row2 in cursor2:  
        if row2[0] == row1[0]:
            for row1 in cursor1:
                row1[1] = "Match"
            else:
                row1[1] = "No Match"

I tried spatially joining the two layers and using field calculator but i am getting an invalid syntex error on line 2.

def ifBlock( SiteAddres , [SiteAddr_1 ):
 if SiteAddres = =SiteAddr_1:
  return "Match"
 else:
  return" No Match"

ifBlock( SiteAddres , SiteAddr_1 )

Thanks.

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

python parser

#code block
def ifBlock( SiteAddres , SiteAddr_1 ):  
    if SiteAddres == SiteAddr_1:  
        return "Match"  
    else:  
        return "No Match"  
#expression block
ifBlock( !SiteAddres! , !SiteAddr_1! )

View solution in original post

16 Replies
AdrianWelsh
MVP Notable Contributor

Hi,

For your bottom block of code, using an If statement, you need to put the equal signs together

if SiteAddres == SiteAddr_1:

Plus, in the def area, in the bottom block of code, it looks like you have an open bracket without a closed bracket.

And you might need to indent more in the bottom block as well (4 spaces for an indent).

0 Kudos
CCWeedcontrol
Regular Contributor

I changed it to the following but i get Error 000539:,

which states...

"This error code covers a number of Python errors:

  • Example error 1: exceptions.TypeError: cannot concatenate 'str' and 'int' objects.

The above is a Python-specific error. The calculation is attempting to add or concatenate a string and a number. "

def ifBlock( SiteAddres , SiteAddr_1 ): 
        if SiteAddres == SiteAddr_1 :
           return "Match"
        else:
           return "NoMatch"

ifBlock( SiteAddres , SiteAddr_1)
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

python parser

#code block
def ifBlock( SiteAddres , SiteAddr_1 ):  
    if SiteAddres == SiteAddr_1:  
        return "Match"  
    else:  
        return "No Match"  
#expression block
ifBlock( !SiteAddres! , !SiteAddr_1! )
CCWeedcontrol
Regular Contributor

Hi Dan.

The after making the following like you suggest it ran" ifBlock( !SiteAddres! , !SiteAddr_1! )".

but all the records returned as "No Match". I just realized that the attributes in SiteAddr_1 are all uppercase and that the SiteAddres are proper case. I had to change the following.

if SiteAddres.lower() == SiteAddr_1.lower():

Thanks!

0 Kudos
FreddieGibson
Regular Contributor II

To update the data the first cursor needs to be an update cursor and after setting the value for the field you'd want to call the updateRow method to commit it.

See the example at the bottom of the following page.

UpdateCursor—Help | ArcGIS for Desktop

CCWeedcontrol
Regular Contributor

I have tried the following but nothing happens i get no error either.

fc1 = "Points"
fc2 = "Polygons"

cursor1 = arcpy.da.SearchCursor(fc1, ["SiteAddres", "Verifi"])
cursor2 = arcpy.da.SearchCursor(fc2, ["SiteAddres"])

# Create update cursor for feature class 
with arcpy.da.UpdateCursor(fc1, "Verifi") as cursor:
    for row1 in cursor1:
        for row2 in cursor2:  
            if row2[0] == row1[0]:
                for row1 in cursor1:
                    row1[1] = "Match"
                else:
                    row1[1] = "No Match"

                # Update the cursor with the updated list
                cursor.updateRow(row)
0 Kudos
WesMiller
Regular Contributor III

You could set up a model that does:

  1. Spatial join
  2. joins the spatial join with the tables
  3. Select the addresses that are <>
  4. You could then field calculate verified or field calculate the addresses
  5. remove the table join

or do your spatial join in model builder and export to python and use the resulting field as an input to the following script. The script prints the oids of the non matching of both feature classes you could easily modify to get what you need

#Script to compare 2 feature classes
#Author Wes Miller


import arcpy
import numpy as np
import datetime


#Print start time and date 
dt = datetime.datetime.now()
print dt


#The Feature Class to Check
originFC = arcpy.GetParameterAsText(0)
#The feature class with updates needed for feature class above
changeFC = arcpy.GetParameterAsText(1)


origDesc = arcpy.Describe(originFC)
oidName = origDesc.oidFieldName


field_names = arcpy.GetParameterAsText(2)#Example: "Shape","TMS","OWNERNAME","TOTBDGVAL","DISTRICT","ADD1","ADD2","ADD3","MAP","BLOCK","PARCEL","CALCULATED_ACREAGE","ADDR_SITE"


originFCarr = arcpy.da.FeatureClassToNumPyArray(originFC,(field_names),null_value=-9999)
originFCarrID = arcpy.da.FeatureClassToNumPyArray(originFC,(oidName))
changeFCarr = arcpy.da.FeatureClassToNumPyArray(changeFC,(field_names),null_value=-9999)
changeFCarrID = arcpy.da.FeatureClassToNumPyArray(changeFC,(oidName))


print "Arrays Complete"
count = 0
deletesarr = np.where(np.invert(np.in1d(originFCarr,changeFCarr)))
addsarr = np.where(np.invert(np.in1d(changeFCarr,originFCarr)))
adds = []
for each in  addsarr[0]:
    adds.append(changeFCarrID[each][0])
deletes = []
for each in deletesarr[0]:
    deletes.append(originFCarrID[each][0])


#Current Outputs are printed here for testing purposes
print adds
print 
print deletes


ndt = datetime.datetime.now()
print ndt
CCWeedcontrol
Regular Contributor

I did try making a model with model builder but i did not have any success.

Export Graphic.pngImage2.pngImage3.pngImage4.png

0 Kudos
WesMiller
Regular Contributor III

Add your field or create a copy and add a field then make your model similar to the one below.

0 Kudos