I have the following code in Python for ArcMap to first select a property polygon and then with this selection to intersect the layer which contains lines (sewers) (which pass through or are within this polygon). In the Attribute table of the sections is a column (called "BG_HAusPfad") with the file path to its corresponding PDF.

The code then takes this file path and appends the PDF somewhere else. (Because we have all PDFs in one folder but need to do a folder per property with the PDFs of the sections of that polygon).

The code works for one polygon. The issue is that with the second property polygon the internal memory is still the same from before and appends the PDFs from the first property.

How can I overwrite this internal memory?

Here's the Code: (R1_Nummer is the number of the property polygon, aka its name. The folders of the properties are called this way)

import arcpy, os
from arcpy import env
arcpy.env.overwriteOutput = True
R1 = r"R1_NUMMER"
field = r"BG_HAusPfad"
# that's the PDF name
PathPDF = r"I:\…\All-Separate-PDFs"
MainFolder = r"I:\…\All-Properties"

mxd = arcpy.mapping.MapDocument("CURRENT")

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    pageName = mxd.dataDrivenPages.pageRow.getValue("R1_NUMMER")
    Subfolder = os.path.join(MainFolder, str(pageName))
    arcpy.SelectLayerByAttribute_management(r"Property-polygons", "NEW_SELECTION", "{} = '{}'".format(R1, str(pageName)))
    arcpy.MakeFeatureLayer_management(selection, "in_memory\PropertySelected_lyr")
    arcpy.Intersect_analysis([r"Sewer-lines", "in_memory\PropertySelected_lyr"], "in_memory\Sewer_lyr", "ALL", "", "LINE")
    Cursor = arcpy.SearchCursor(r"in_memory\Sewer_lyr")
    for row in cursor:
            Protokolname = row.getValue(field)
            ProtipdfPfad = os.path.join(PathPDF, str(Protokolname) + ".pdf")
                HprotipdfName = os.path.join(str(Subfolder), str(Protokolname) + ".pdf")
                HprotipdfDoc = arcpy.mapping.PDFDocumentCreate(str(HprotipdfName))
                del HprotipdfDoc

overview of property Polygons and sewers and sewers' Attribute table

So in the picture you see the attribute table of the sewers (the layer is actually called "A1 aufgenommen" (since it's german and very specific i just changed it in the Code to sewers for you)

  • 2
    Can you please format your question, parsing the code – Taras May 13 at 11:09
  • You've deliberately obscured your question by hiding the source of the datasets, not very helpfully really if you want help? So I can only guess.If your data is in shapefile format have you added a spatial index? – Hornbydd May 13 at 11:43
  • You state that you think the issues is with SelectByLocation. Have you tried adding timing code, so that you can know? Please remove the interactive continuation formatting (...) so the code is more legible. – Vince May 13 at 12:19
  • thanks for ur replies so far! so yes, the polygons of the properties and the lines of the sewers are in shapefiles. I have never before heard of spatial indexes, so looking that up right now and giving it a shot. I have not tried adding Timing Code, since i dont know how…? but i need to change my statement: selecting by attribute is fast and select by location works as well (a little less) fast. but apparently what is time consuming is everything afterwards, so: the searchCursor and appending the PDFs and so on. – Fiona May 14 at 13:35
  • Your title is "Speeding up SelectbyLocation?" but the comment above suggests the slowness is to do with the PDF document creation so nothing to do with the SelectByLocation tool? Suggest you amend the title to focus peoples attention. – Hornbydd May 20 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.