<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Spatial join for sevaral features with a for loop in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/spatial-join-for-several-features-with-a-for-loop/m-p/196418#M15046</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You need to use the syntax highlighter to post code; no one can tell where your for loops end since the indentations are omitted.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Look at what the error is telling you. It may stem from you not properly seting your workspace.&amp;nbsp; I suggest using:&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;env&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;workspace &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; r&lt;SPAN class="string token"&gt;'C:\path\to\your.gdb'&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Otherwis you need to escape each of the back slashes with a back slash:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;env&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;workspace &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'C:\\path\\to\\your.gdb'&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 18 Oct 2018 23:14:50 GMT</pubDate>
    <dc:creator>JoeBorgione</dc:creator>
    <dc:date>2018-10-18T23:14:50Z</dc:date>
    <item>
      <title>Spatial join for several features with a for loop</title>
      <link>https://community.esri.com/t5/python-questions/spatial-join-for-several-features-with-a-for-loop/m-p/196417#M15045</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm trying to do a spatial join between sevaral feature classes as join features and one feature class as the target feature. I have written a python script for this but I get an error when I execute it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;arcpy.CheckExtension('Spatial')&lt;BR /&gt;arcpy.CheckOutExtension('Spatial')&lt;/P&gt;&lt;P&gt;arcpy.env.workspace = 'C:\Lab_5\Lab_5\AAB_gdb\AAB.gdb'&lt;/P&gt;&lt;P&gt;#This is to check if these are the feature classes that I want to use&lt;/P&gt;&lt;P&gt;defor_years = arcpy.ListFeatureClasses("def*")&lt;/P&gt;&lt;P&gt;for i in defor_years:&lt;BR /&gt; print i&lt;/P&gt;&lt;P&gt;#This is to create the output name for each spatial join&lt;/P&gt;&lt;P&gt;output = 'C:\Lab_5\Lab_5\scratch_gdb\scratch.gdb\spatialjoin_'&lt;/P&gt;&lt;P&gt;for i in range(0,len(defor_years)):&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;year = defor_years&lt;I&gt;&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;name = output + str(year) &lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;print name&lt;BR /&gt; join_features = name&lt;/I&gt;&lt;/P&gt;&lt;P&gt;#Setting the variable for the target features&lt;BR /&gt;target_features = 'C:\Lab_5\Lab_5\AAB_gdb\AAB.gdb\commune'&lt;/P&gt;&lt;P&gt;arcpy.SpatialJoin_analysis(target_features, join_features)&lt;/P&gt;&lt;P&gt;When I execute it, I get this.&lt;/P&gt;&lt;P&gt;&lt;IMG __jive_id="424818" class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/424818_pastedImage_1.png" /&gt;&lt;/P&gt;&lt;P&gt;I guess it's not taking each output of the for loop, making that the final spatial join failed. Can anybody help me with this? Thank youuu!!!!.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Oct 2018 20:42:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/spatial-join-for-several-features-with-a-for-loop/m-p/196417#M15045</guid>
      <dc:creator>SilvanaCastillo</dc:creator>
      <dc:date>2018-10-18T20:42:52Z</dc:date>
    </item>
    <item>
      <title>Re: Spatial join for sevaral features with a for loop</title>
      <link>https://community.esri.com/t5/python-questions/spatial-join-for-several-features-with-a-for-loop/m-p/196418#M15046</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You need to use the syntax highlighter to post code; no one can tell where your for loops end since the indentations are omitted.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Look at what the error is telling you. It may stem from you not properly seting your workspace.&amp;nbsp; I suggest using:&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;env&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;workspace &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; r&lt;SPAN class="string token"&gt;'C:\path\to\your.gdb'&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Otherwis you need to escape each of the back slashes with a back slash:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;env&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;workspace &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'C:\\path\\to\\your.gdb'&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Oct 2018 23:14:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/spatial-join-for-several-features-with-a-for-loop/m-p/196418#M15046</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2018-10-18T23:14:50Z</dc:date>
    </item>
    <item>
      <title>Re: Spatial join for several features with a for loop</title>
      <link>https://community.esri.com/t5/python-questions/spatial-join-for-several-features-with-a-for-loop/m-p/196419#M15047</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you for your suggestions. I already&amp;nbsp;change what you said but it didn't work.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Oct 2018 23:31:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/spatial-join-for-several-features-with-a-for-loop/m-p/196419#M15047</guid>
      <dc:creator>SilvanaCastillo</dc:creator>
      <dc:date>2018-10-18T23:31:37Z</dc:date>
    </item>
    <item>
      <title>Re: Spatial join for several features with a for loop</title>
      <link>https://community.esri.com/t5/python-questions/spatial-join-for-several-features-with-a-for-loop/m-p/196420#M15048</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;A href="https://community.esri.com/blogs/dan_patterson/2016/08/14/script-formatting"&gt;/blogs/dan_patterson/2016/08/14/script-formatting&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A class="link-titled" href="http://pro.arcgis.com/en/pro-app/tool-reference/analysis/spatial-join.htm" title="http://pro.arcgis.com/en/pro-app/tool-reference/analysis/spatial-join.htm"&gt;Spatial Join—Help | ArcGIS Desktop&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;spatial join requires two 'layers', so if you aren't running the code in the interactive window or have *.lyr (*.lyrx) files,&lt;/P&gt;&lt;P&gt;it won't work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;you will have to make feature layers before you do the spatial join.&lt;/P&gt;&lt;P&gt;&lt;A class="link-titled" href="http://pro.arcgis.com/en/pro-app/tool-reference/data-management/make-feature-layer.htm" title="http://pro.arcgis.com/en/pro-app/tool-reference/data-management/make-feature-layer.htm"&gt;Make Feature Layer—Data Management toolbox | ArcGIS Desktop&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 19 Oct 2018 00:03:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/spatial-join-for-several-features-with-a-for-loop/m-p/196420#M15048</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2018-10-19T00:03:13Z</dc:date>
    </item>
    <item>
      <title>Re: Spatial join for several features with a for loop</title>
      <link>https://community.esri.com/t5/python-questions/spatial-join-for-several-features-with-a-for-loop/m-p/196421#M15049</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Here's the answer for anyone if they ever have to do something similar:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="comment token"&gt;#Import arcpy module&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; arcpy
&lt;SPAN class="comment token"&gt;#Checking if Spatial Analysis Extension is available&lt;/SPAN&gt;
arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;CheckExtension&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'Spatial'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;#Taking Spatial Analysis to work with it&lt;/SPAN&gt;
arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;CheckOutExtension&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'Spatial'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

&lt;SPAN class="comment token"&gt;#Setting the environment workspace&lt;/SPAN&gt;
arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;env&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;workspace &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'C:\Lab_5\Lab_5\AAB_gdb\AAB.gdb'&lt;/SPAN&gt;

&lt;SPAN class="comment token"&gt;#List of the files we are going to work with &lt;/SPAN&gt;
defor_years &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;ListFeatureClasses&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"def*"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; i &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; defor_years&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp; &lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt; i
&lt;SPAN class="comment token"&gt;# Setting the output for the new files&lt;/SPAN&gt;
output &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'C:\Lab_5\Lab_5\scratch_gdb\scratch.gdb\spatjoin'&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;# Setting target features&lt;/SPAN&gt;
target_features &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'C:\Lab_5\Lab_5\AAB_gdb\AAB.gdb\commune'&lt;/SPAN&gt;

&lt;SPAN class="comment token"&gt;# Creating a for loop to iterate over files&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; i &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; range&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;len&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;defor_years&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; year &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; defor_years&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;i&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;# Since there are going to be several files as output,it's necessary to create a different name for each&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; name &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; output &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; str&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;year&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;# Prints te name of the new files or outputs&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"name ="&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; name&amp;nbsp;&amp;nbsp; 
&lt;SPAN class="comment token"&gt;# Executing the spatial join using as join features the files that were been iterated&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;# and as output_feature class the name established above for each new file&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;#The option KEEP COMMON maintains only the records that were common between the target&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;# and join features&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;SpatialJoin_analysis&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;target_features&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; year &lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;name&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;''&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'KEEP_COMMON'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:48:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/spatial-join-for-several-features-with-a-for-loop/m-p/196421#M15049</guid>
      <dc:creator>SilvanaCastillo</dc:creator>
      <dc:date>2021-12-11T09:48:45Z</dc:date>
    </item>
  </channel>
</rss>

