<?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 Select By Location on a large SDE featureclass in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/select-by-location-on-a-large-sde-featureclass/m-p/28649#M2196</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have looked around and have not found any solution to this problem.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I am writing a GP tool that will allow the user to define a point by clicking on a point within a map service.&amp;nbsp; The user has the ability to pick which feature set they would like to run the tool against.&amp;nbsp; This tool works in under 10 seconds for every data layer that is in the map service other than the Common Land Unit layer.&amp;nbsp; This layer is in SDE, has a spatial index, and has 36 million features (rows).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So in arcmap, I can simply use the selection tool &amp;gt; click on a polygon &amp;gt; right click &amp;gt; Selection\Create Layer from selected features and then export this polygon to a new featureclass.&amp;nbsp; I can do it in about 20 seconds depending on how fast i can click &lt;span class="lia-unicode-emoji" title=":face_with_tongue:"&gt;😛&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Using python however this process took 33 minutes to run using the code attached below.&amp;nbsp; It looks like when I use MakeFeatureLayer_management it creates a copy in memory of the 36 million features.&amp;nbsp; Then when I do the spatial selection it finds the one record its looking for.&amp;nbsp; Using print statements, this part of the code does not appear to take very long.&amp;nbsp; It appears that the part taking a long time is CopyFeatures_management.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To me it looks like it is using a search cursor to go through the 33million records to find the one that is selected and then exporting it to the scratch shapefile.&amp;nbsp; This makes no sense to me.&amp;nbsp; If the script can make a feature layer and do the selection in seconds then why does it take 30 minutes to export a single record?&amp;nbsp; Are there any environment settings that I need to use?&amp;nbsp; Is there a way to just use the features of this featureLayer that are in the current extent of the map service?&amp;nbsp; Is there a better approach than the code below?&amp;nbsp; Is there something that our SDE Manager should do to the data to make it faster to query on location?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am thinking if I can do it manually in arcmap in a few seconds then there is something that is completely wrong with my script (or perhaps arcpy).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any help or feedback would be greatly appreciated!!!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy, sys from arcpy import env&amp;nbsp; layerSelection = sys.argv[1] featureSet = sys.argv[2] scratch_ws = arcpy.env.scratchWorkspace arcpy.AddMessage(scratch_ws)&amp;nbsp; try: &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.overwriteOutput = True &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(layerSelection,'lyr') &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('Running Spatial Intersect...') &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management('lyr', 'INTERSECT', featureSet, 0, r'NEW_SELECTION') &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('Copying Selected Features to Output...') &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management('lyr', scratch_ws + r'\selection.shp') &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_Management('lyr') except: &amp;nbsp;&amp;nbsp;&amp;nbsp; print arcpy.GetMessages()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 16 May 2012 20:57:20 GMT</pubDate>
    <dc:creator>WesleyMarcell</dc:creator>
    <dc:date>2012-05-16T20:57:20Z</dc:date>
    <item>
      <title>Select By Location on a large SDE featureclass</title>
      <link>https://community.esri.com/t5/python-questions/select-by-location-on-a-large-sde-featureclass/m-p/28649#M2196</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have looked around and have not found any solution to this problem.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I am writing a GP tool that will allow the user to define a point by clicking on a point within a map service.&amp;nbsp; The user has the ability to pick which feature set they would like to run the tool against.&amp;nbsp; This tool works in under 10 seconds for every data layer that is in the map service other than the Common Land Unit layer.&amp;nbsp; This layer is in SDE, has a spatial index, and has 36 million features (rows).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So in arcmap, I can simply use the selection tool &amp;gt; click on a polygon &amp;gt; right click &amp;gt; Selection\Create Layer from selected features and then export this polygon to a new featureclass.&amp;nbsp; I can do it in about 20 seconds depending on how fast i can click &lt;span class="lia-unicode-emoji" title=":face_with_tongue:"&gt;😛&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Using python however this process took 33 minutes to run using the code attached below.&amp;nbsp; It looks like when I use MakeFeatureLayer_management it creates a copy in memory of the 36 million features.&amp;nbsp; Then when I do the spatial selection it finds the one record its looking for.&amp;nbsp; Using print statements, this part of the code does not appear to take very long.&amp;nbsp; It appears that the part taking a long time is CopyFeatures_management.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To me it looks like it is using a search cursor to go through the 33million records to find the one that is selected and then exporting it to the scratch shapefile.&amp;nbsp; This makes no sense to me.&amp;nbsp; If the script can make a feature layer and do the selection in seconds then why does it take 30 minutes to export a single record?&amp;nbsp; Are there any environment settings that I need to use?&amp;nbsp; Is there a way to just use the features of this featureLayer that are in the current extent of the map service?&amp;nbsp; Is there a better approach than the code below?&amp;nbsp; Is there something that our SDE Manager should do to the data to make it faster to query on location?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am thinking if I can do it manually in arcmap in a few seconds then there is something that is completely wrong with my script (or perhaps arcpy).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any help or feedback would be greatly appreciated!!!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy, sys from arcpy import env&amp;nbsp; layerSelection = sys.argv[1] featureSet = sys.argv[2] scratch_ws = arcpy.env.scratchWorkspace arcpy.AddMessage(scratch_ws)&amp;nbsp; try: &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.overwriteOutput = True &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(layerSelection,'lyr') &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('Running Spatial Intersect...') &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management('lyr', 'INTERSECT', featureSet, 0, r'NEW_SELECTION') &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('Copying Selected Features to Output...') &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management('lyr', scratch_ws + r'\selection.shp') &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_Management('lyr') except: &amp;nbsp;&amp;nbsp;&amp;nbsp; print arcpy.GetMessages()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 May 2012 20:57:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-location-on-a-large-sde-featureclass/m-p/28649#M2196</guid>
      <dc:creator>WesleyMarcell</dc:creator>
      <dc:date>2012-05-16T20:57:20Z</dc:date>
    </item>
    <item>
      <title>Re: Select By Location on a large SDE featureclass</title>
      <link>https://community.esri.com/t5/python-questions/select-by-location-on-a-large-sde-featureclass/m-p/28650#M2197</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I was able to figure out the solution on my own.&amp;nbsp; It was quite a wrestling match between myself, arcpy.env, and the scratch workspace but I got a script that will run in 3 seconds pointing at my 36 million feature dataset.&amp;nbsp; I buffered the point out 100ft and then used a copy features.&amp;nbsp; Copy features actually respects your environment settings unlike make feature layer.&amp;nbsp; Basically only features within 100 feet of the point are selected and copied to the temp directory.&amp;nbsp; This works for my script since I am only looking for one feature to be returned.&amp;nbsp; If you were looking to do more you could always use an if layerSelection == "CLU" to only do this process on the giant layer...&amp;nbsp; here is some code goodness (even has metadata!!!)&amp;nbsp; I hope it helps someone out.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy, sys from arcpy import env&amp;nbsp; ##Input Variables and setting scratch workspace variable layerSelection = sys.argv[1] featureSet = sys.argv[2] scratch_ws = arcpy.env.scratchWorkspace arcpy.env.overwriteOutput = True&amp;nbsp; ##Creating Variables for model derived&amp;nbsp; Buffer = scratch_ws + r'\scratch.gdb\buffer' TempLayer = scratch_ws + r'\scratch.gdb\layer' selection = scratch_ws + r'\scratch.gdb\selection'&amp;nbsp; try: &amp;nbsp;&amp;nbsp;&amp;nbsp; ##Create a buffer of input feature set.&amp;nbsp; This will be used to set the extent of the model. &amp;nbsp;&amp;nbsp;&amp;nbsp; ##If this is not used the script will try to process the entire 36 million feature CLU dataset. &amp;nbsp;&amp;nbsp;&amp;nbsp; ##This works because we are only interested in returning one feature to the geoprocessing service. &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Buffer_analysis(featureSet, Buffer, "100 feet") &amp;nbsp;&amp;nbsp;&amp;nbsp; desc = arcpy.Describe(Buffer) &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.extent = Buffer&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ##Copy the selected features to a temporary layer.&amp;nbsp; This will respect your environment setting, &amp;nbsp;&amp;nbsp;&amp;nbsp; ##so only records within 100ft of your point will be written to disk. &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management(layerSelection, TempLayer)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ##Create a temporary feature layer for use in select by location &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(TempLayer, 'lyr') &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('Running Spatial Intersect...')&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ##Select features that intersect the user input point.&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management('lyr', 'INTERSECT', featureSet, 0, r'NEW_SELECTION')&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('Copying Selected Features to Output...')&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ##Copy the selected features to the arcgisjobs scratch directory &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management('lyr', selection)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ##Delete Temporary Data &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(Buffer) &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(TempLayer)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ##Will count the number of features in output featureclass &amp;nbsp;&amp;nbsp;&amp;nbsp; result = arcpy.GetCount_management(selection)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ##If there are no records in the output featureclass a custom error will be returned to the user&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if int(result.getOutput(0)) == 0: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError ("No Features in " + layerSelection + " at selected location") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ##Delete empty featureclass so that the server will retun an empty result &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(selection) &amp;nbsp;&amp;nbsp;&amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass&amp;nbsp; except: &amp;nbsp;&amp;nbsp;&amp;nbsp; print arcpy.GetMessages()&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 May 2012 21:08:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-location-on-a-large-sde-featureclass/m-p/28650#M2197</guid>
      <dc:creator>WesleyMarcell</dc:creator>
      <dc:date>2012-05-17T21:08:02Z</dc:date>
    </item>
  </channel>
</rss>

