<?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 Spatial Join script takes hours in Python, 7 minutes in ArcMap in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/spatial-join-script-takes-hours-in-python-7/m-p/616917#M48120</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;My spatial join python script used for two polygon features takes over an hour to complete, yet when I run the same spatial join in ArcMap takes about 7 minutes. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any idea how I could modify the script to speed it up ?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcgisscripting

# Create the geoprocessor object
gp = arcgisscripting.create(9.3)
gp.OverWriteOutput = True

# Set the workspace. List all of the folders within
gp.Workspace = "C:\ZP4"
fcs = gp.ListWorkspaces("*","Folder")

#run spatial join on parcel.shp in each folder, then delete original and replace with "parceljoined"
for fc in fcs:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print fc
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.SpatialJoin_analysis(fc + "\\Parcels.shp", "C:\ESRI\ESRIDATA\USA\usa_zipcodes.shp", fc + "\\Parcelsjoined.shp",)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.CalculateField_management(fc + "\\Parcelsjoined.shp", "SIT_ZIP", "[POSTAL]", "VB", "")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.CalculateField_management(fc + "\\Parcelsjoined.shp", "SIT_CITY", "[CITYNAME]", "VB", "")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.DeleteField_management(fc + "\\Parcelsjoined.shp", "Join_Count;Join_Cou_1;Join_Cou_2;POSTAL")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Delete_management(fc + "\\Parcels.shp")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Rename_management(fc + "\\Parcelsjoined.shp", "Parcels.shp")
&amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'AddZip Error'&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 27 Mar 2012 20:05:28 GMT</pubDate>
    <dc:creator>TurnerNowak</dc:creator>
    <dc:date>2012-03-27T20:05:28Z</dc:date>
    <item>
      <title>Spatial Join script takes hours in Python, 7 minutes in ArcMap</title>
      <link>https://community.esri.com/t5/python-questions/spatial-join-script-takes-hours-in-python-7/m-p/616917#M48120</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;My spatial join python script used for two polygon features takes over an hour to complete, yet when I run the same spatial join in ArcMap takes about 7 minutes. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any idea how I could modify the script to speed it up ?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcgisscripting

# Create the geoprocessor object
gp = arcgisscripting.create(9.3)
gp.OverWriteOutput = True

# Set the workspace. List all of the folders within
gp.Workspace = "C:\ZP4"
fcs = gp.ListWorkspaces("*","Folder")

#run spatial join on parcel.shp in each folder, then delete original and replace with "parceljoined"
for fc in fcs:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print fc
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.SpatialJoin_analysis(fc + "\\Parcels.shp", "C:\ESRI\ESRIDATA\USA\usa_zipcodes.shp", fc + "\\Parcelsjoined.shp",)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.CalculateField_management(fc + "\\Parcelsjoined.shp", "SIT_ZIP", "[POSTAL]", "VB", "")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.CalculateField_management(fc + "\\Parcelsjoined.shp", "SIT_CITY", "[CITYNAME]", "VB", "")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.DeleteField_management(fc + "\\Parcelsjoined.shp", "Join_Count;Join_Cou_1;Join_Cou_2;POSTAL")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Delete_management(fc + "\\Parcels.shp")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Rename_management(fc + "\\Parcelsjoined.shp", "Parcels.shp")
&amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'AddZip Error'&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 27 Mar 2012 20:05:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/spatial-join-script-takes-hours-in-python-7/m-p/616917#M48120</guid>
      <dc:creator>TurnerNowak</dc:creator>
      <dc:date>2012-03-27T20:05:28Z</dc:date>
    </item>
    <item>
      <title>Re: Spatial Join script takes hours in Python, 7 minutes in ArcMap</title>
      <link>https://community.esri.com/t5/python-questions/spatial-join-script-takes-hours-in-python-7/m-p/616918#M48121</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This probably won't speed up your process, but you need to check your file name strings. A single backslash in a Python string acts as an escape character. To be consistant with the rest of your script, change:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;gp.Workspace = "C:\ZP4"
and
gp.SpatialJoin_analysis(fc + "\\Parcels.shp", "C:\ESRI\ESRIDATA\USA\usa_zipcodes.shp", fc + "\\Parcelsjoined.shp",)

to

gp.Workspace = "C:\\ZP4"
and
gp.SpatialJoin_analysis(fc + "\\Parcels.shp", "C:\\ESRI\\ESRIDATA\\USA\\usa_zipcodes.shp", fc + "\\Parcelsjoined.shp",)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You also have that extra comma at the end of the SpatialJoin. Is that a typo, or by design?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:22:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/spatial-join-script-takes-hours-in-python-7/m-p/616918#M48121</guid>
      <dc:creator>BruceNielsen</dc:creator>
      <dc:date>2021-12-12T02:22:09Z</dc:date>
    </item>
    <item>
      <title>Re: Spatial Join script takes hours in Python, 7 minutes in ArcMap</title>
      <link>https://community.esri.com/t5/python-questions/spatial-join-script-takes-hours-in-python-7/m-p/616919#M48122</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Have you indexed everything?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Spatial indexes on shapefiles are not automatic, besides you should have moved decisively to filegeodatabases for analysis like this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;File geodatabases will be much faster.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Where are you scratch folders? They might be different in ArcMap because environments are different.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Do you have a scratch filegeodatabase defined? Not just a temp folder.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Is your default temp folders on a file server instead of local drive.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Some tools are slower in Python, who knows why, but there are ways of making it faster in both systems.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Mar 2012 02:16:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/spatial-join-script-takes-hours-in-python-7/m-p/616919#M48122</guid>
      <dc:creator>KimOllivier</dc:creator>
      <dc:date>2012-03-28T02:16:30Z</dc:date>
    </item>
    <item>
      <title>Re: Spatial Join script takes hours in Python, 7 minutes in ArcMap</title>
      <link>https://community.esri.com/t5/python-questions/spatial-join-script-takes-hours-in-python-7/m-p/616920#M48123</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Well, I took your advice and modified the script. Here is the final result which may be a tad 'faster' :&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcgisscripting

# Create the geoprocessor object
gp = arcgisscripting.create(9.3)
gp.OverWriteOutput = True

# Set the workspace. List all of the folders within
gp.Workspace = "C:\\ZP4"
gp.ScratchWorkspace = "C:\\ESRI\\temp_output"
fcs = gp.ListWorkspaces("*","Folder")

#run spatial join on parcel.shp in each folder, then delete original and replace with "parceljoined"
for fc in fcs:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print fc
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddSpatialIndex_management(fc + "\\Parcels.shp", "0", "0", "0")
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.SpatialJoin_analysis(fc + "\\Parcels.shp", 'C:\\ESRI\\ESRIDATA\\USA\\usa_zipcodes.shp', fc + "\\Parcelsjoined.shp", "")
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.CalculateField_management(fc + "\\Parcelsjoined.shp", "SIT_ZIP", "[POSTAL]", "VB", "")
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.CalculateField_management(fc + "\\Parcelsjoined.shp", "SIT_CITY", "[CITYNAME]", "VB", "")
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.DeleteField_management(fc + "\\Parcelsjoined.shp", "Join_Count;Join_Cou_1;Join_Cou_2;POSTAL")
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Delete_management(fc + "\\Parcels.shp")
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Rename_management(fc + "\\Parcelsjoined.shp", "Parcels.shp")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:22:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/spatial-join-script-takes-hours-in-python-7/m-p/616920#M48123</guid>
      <dc:creator>TurnerNowak</dc:creator>
      <dc:date>2021-12-12T02:22:11Z</dc:date>
    </item>
  </channel>
</rss>

