Issues looping with Python

2773
3
03-09-2016 07:58 AM
WilliamZappa1
New Contributor

Hi all,

I'm having a strange problem with Python in Arcmap. I have a map of Europe with layers of polygons for each country, as well as a single layer of many polygons covering all Europe describing the land use type. What I am trying to do is loop through each country, select all the land cover type polygons within that country, and assign their 'country' field equal to the country 2-letter code. My code is below.

---------------

import arcpy

c= ["AT","BE","BG","CH","CY","CZ","DE","DK","EE","EL","ES","FI","FR","HR","HU","IE","IT","LT","LU","LV","MT","NL","NO","PL","PT","RO","SE","SI","SK","UK"]

for ii in c:

     arcpy.SelectLayerByLocation_management("EuropeCorine2006100mpoly",...

            ..."HAVE_THEIR_CENTER_IN",ii,0,"NEW_SELECTION","NOT_INVERT")

     arcpy.CalculateField_management("EuropeCorine2006100mpoly","country","'%s'" %(ii),"PYTHON")

     arcpy.MakeFeatureLayer_management("EuropeCorine2006100mpoly","Corinepoly_%s"%(ii))

-------------------------

My code works for a single country, but then throws an error:

Runtime error  Traceback (most recent call last):   File "<string>", line 1, in <module>   File "c:\program files (x86)\arcgis\desktop10.3.1\desktop10.3\arcpy\arcpy\management.py", line 7359, in SelectLayerByLocation     raise e ExecuteError: ERROR 000622: Failed to execute (Select Layer By Location). Parameters are not valid. ERROR 000628: Cannot set input into parameter in_layer.

Another strange thing is that if I just set the country manually (e.g. ii = "BE"), it works fine. But, if I try changing the country afterwards (e.g. ii = "IT") and running the code again, it throws the same error. BUT, if I close Arcmap and reopen it again, I can run the code and it works again for one country only. It makes no sense to me....can someone help?!

Thanks!

Will

Tags (3)
0 Kudos
3 Replies
WesMiller
Regular Contributor III

You may want to try using make feature layer to isolate the country. You also might have a look at Spatial Join—Help | ArcGIS for Desktop  which would do what you are trying to do

arcpy.MakeFeatureLayer_management(contriesfcorlayer,yourlayer+ii,whereclause to get ii)

Make Feature Layer—Help | ArcGIS for Desktop

You may also wish to move your question to Python​ or python snippets

DanPatterson_Retired
MVP Esteemed Contributor

ii in c returns a string, that is not what that parameter is looking for.   Have you done this manually with your inputs? If it worked examine the structure in the Results window to get the syntax... or go with the spatial join.

XanderBakker
Esri Esteemed Contributor

As Dan Patterson​ mentions, you should have a look at the definition of the parameters in the tool Select Layer By Location—Help | ArcGIS for Desktop :

SelectLayerByLocation_management (in_layer, {overlap_type}, {select_features}, {search_distance}, {selection_type}, {invert_spatial_relationship})

The third parameter (select_features) expects a feature layer (as Wes Miller suggests) while you provide the two character string for the country.