# How to do point-to-point near analysis conditional on common attributes of the two layers in Arcgis 10.2 (advanced)

I am measuring the closest straight distance from cities of interest (in a layer) to their capital cities (in another layer). Basically, distance between a city and a capital city is calculated if only if their country codes are identical. Arcgis license is advanced.

Solution:

The following solution is based on @dklassen. Modification has been made so that I can loop a list of numerical number.

``````import arcpy
ctryID = [row.ID_0 for row in arcpy.SearchCursor(countryID_feature_class)]
arcpy.MakeFeatureLayer_management('PATH_TO_CITIES_FC','cityfc')
arcpy.MakeFeatureLayer_management('PATH_TO_CAPITALS_FC','capitalfc')

for grd in ctryID:
arcpy.SelectLayerByAttribute_management('cityfc','NEW_SELECTION',""""ID_0" = {}""".format(grd))
arcpy.SelectLayerByAttribute_management('capitalfc','NEW_SELECTION',""""ID_0" = {}""".format(grd))
arcpy.Near_analysis('cityfc','capitalfc',"#","NO_LOCATION","NO_ANGLE")
arcpy.CopyFeatures_management("cityfc", "c:/temp/ctry{}".format(grd))

arcpy.CreateFeatureclass_management("c:/temp/", "ctryall.shp", "POINT", "c:/temp/ctry1.shp", "DISABLED", "DISABLED")

for grd in ctryID:
arcpy.Append_management("c:/temp/ctry{}.shp".format(grd), "c:/temp/ctryall.shp")
arcpy.Delete_management("c:/temp/ctry{}.shp".format(grd))
``````

What you could do is within a loop - create feature layers out of your points - then do the point near analysis. As a brief example:

``````import arcpy
arcpy.MakeFeatureLayer_management('PATH_TO_CITIES_FC','cityfc')
arcpy.MakeFeatureLayer_management('PATH_TO_CAPITALS_FC','capitalfc')

for country in countries:
arcpy.SelectLayerByAttribute_management('cityfc','NEW_SELECTION',"COUNTRYNAME = '" + str(country) + "'")
arcpy.SelectLayerByAttribute_management('capitalfc','NEW_SELECTION',"COUNTRYNAME = '" + str(country) + "'")
# DO POINT NEAR ANALYSIS HERE ON THE 2 Feature Layers
``````

Also if you don't want to type out the list of countries - you could do something like this:

``````countries = [row.COUNTRYNAME for row in arcpy.SearchCursor(country_feature_class)]
``````
• I am taking your suggestion but using country ID (numerical variable) instead country name. How to modify your codes correspondingly, especially the last two codes (because they are not string variables any more). Thanks. May 2, 2014 at 5:01
• I think I have successfully get it to work with country ID, but your last codes come with the following error: ERROR 000732: Layer Name or Table View: Dataset captialfc does not exist or is not supported. May 2, 2014 at 5:40
• Sorry, it appears to be mis-spelled between when I make the layer and when I select it. I have changed the code now. May 2, 2014 at 15:24
• You have to be careful about typo's like that in Python - and any coding. Sorry about the error! May 2, 2014 at 15:34
• I added the following codes, it only got me city distances in one country. What else I have missed? <!-- language: lang-python --> arcpy.Near_analysis('cityfc','capitalfc',"#","NO_LOCATION","NO_ANGLE") May 2, 2014 at 18:18