AnsweredAssumed Answered

Issues looping with Python

Question asked by will_zappa on Mar 9, 2016
Latest reply on Mar 9, 2016 by xander_bakker

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

Outcomes