<?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: Select Layer by Location not providing correct result. in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631667#M49181</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you, I'll try adding the Traceback to see if I get any information.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The definition query works correctly. I got that to work before I added the selection statement. The extra backslash was added so I could put the next 'item' on a new line. It just lets python know that the line continues.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 15 Feb 2013 16:13:48 GMT</pubDate>
    <dc:creator>SusanWhitney</dc:creator>
    <dc:date>2013-02-15T16:13:48Z</dc:date>
    <item>
      <title>Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631665#M49179</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I sought assistance with a portion of this code earlier. I have added an additional step, which to me would seem pretty simple, but I am still learning the ups &amp;amp; downs of Python.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My script creates data driven pages. I use a list to populate a layer definition query to run a specific township. Now I am trying to do a selection (SelectLayerByLocation) to only find the townships where there is data. I first just added the SelectLayerByLocation command. Did not work. With further reading, it looked like I needed to run a MakeFeatureLayer on the two sets of data involved in the selection. Still did not work. Do I need to use cursors or is the answer much simpler?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I do not have any error messages. I have added try/except to see if I can find additional information on what is happening with my script. The data exports just fine, but there are maps that do not have any data.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you for any guidance.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;# PoleDDpages.py # Author: slw # Date: XXXXX # Revisions: XXXXX # Description: Create Pole map book series using Data Driven Pages and #&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; insert a cover page&amp;nbsp; # Import ArcPy import os,arcpy&amp;nbsp; try: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Set up variables &amp;nbsp;&amp;nbsp;&amp;nbsp; # Location of pole map .mxd document &amp;nbsp;&amp;nbsp;&amp;nbsp; mxdDoc = r"G:\GEOSPATIAL\Publishing\Pole\Pole_QtrSec.mxd"&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Create the MapDocument object &amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(mxdDoc) &amp;nbsp;&amp;nbsp;&amp;nbsp; df = arcpy.mapping.ListDataFrames(mxd)[0] &amp;nbsp;&amp;nbsp;&amp;nbsp; layer = arcpy.mapping.ListLayers (mxd, "SURVEY_GRID_BNDRY",df)[0] &amp;nbsp;&amp;nbsp;&amp;nbsp; poleLyr = arcpy.mapping.ListLayers (mxd, "Pole",df)[0]&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Overwrite existing file &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.overwriteOutput = True&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Output directory for the pole maps &amp;nbsp;&amp;nbsp;&amp;nbsp; outDir = r"G:\GEOSPATIAL\Publishing\Pole"&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Set the workspace &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.workspace = outDir&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # List of map grids &amp;nbsp;&amp;nbsp;&amp;nbsp; twpList = ['\"TOWNSHIP_C\" = \'D5\' AND \"RANGE_CD\" = \'5\'',\ &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '\"TOWNSHIP_C\" = \'D6\' AND \"RANGE_CD\" = \'4\'']&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; i=1 &amp;nbsp;&amp;nbsp;&amp;nbsp; for twpName in twpList: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer.definitionQuery = twpName&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # refresh() after changing the layer def query to 'redefine' the DDP index limits &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.dataDrivenPages.refresh() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create temporary layers to work with the Selection &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(layer, "surveyLyr") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(poleLyr, "poleLyr_new")&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Clear the Selection before starting &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #arcpy.SelectLayerByAttribute_management("surveyLyr", "CLEAR_SELECTION", "") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #print "The selection has been cleared" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select Layer By Location to limit to just maps with data &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("surveyLyr", "INTERSECT", "poleLyr_new", "", "NEW_SELECTION")&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # refresh() after changing the layer def query to 'redefine' the DDP index limits &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.dataDrivenPages.refresh()&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.saveACopy(os.path.splitext(mxdDoc) [0] + str(i) + os.path.splitext(mxdDoc)[1]) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i += 1 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # The final mapbook name taken from the list &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDFfn = outDir + "\\" + twpName [16:18] + twpName [38:39] + "Pole.pdf" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create the final PDF -- which is just an empty shell right now &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDF = arcpy.mapping.PDFDocumentCreate(finalPDFfn)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # A temporary pdf file for processing &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tmpPDF = outDir + "\\PoleMapPages.pdf"&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Let the user know what is happening! &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Exporting " + twpName [16:18] + twpName [38:39] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Export the data driven pages in the mxd to a temporary PDF &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Exporting map pages to the temporary PDF" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ddp = mxd.dataDrivenPages.exportToPDF(tmpPDF)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Append the temporary pdf to the final pdf &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Cover, map pages, back cover &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Appending Map Pages" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDF.appendPages (r"G:\GEOSPATIAL\Publishing\Pole\PoleCovers\Covers_"\ &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + twpName [16:18] + twpName [38:39] + ".pdf") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDF.appendPages(tmpPDF) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDF.appendPages(r"G:\GEOSPATIAL\Publishing\TwpGrid_Color8x11.pdf")&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Set properties for Adobe Reader and save PDF. &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDF.updateDocProperties(pdf_open_view = "USE_THUMBS", pdf_layout = "SINGLE_PAGE") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDF.saveAndClose()&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Deleting temporary layers &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("surveyLyr") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("poleLyr_new")&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception as e: &amp;nbsp;&amp;nbsp;&amp;nbsp; print e.message &amp;nbsp;&amp;nbsp;&amp;nbsp; print arcpy.GetMessages(2) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Clean up print "Cleaning up" # Delete the temporary PDF using the ArcPy function if arcpy.Exists(tmpPDF):&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(tmpPDF)&amp;nbsp;&amp;nbsp; # Delete objects del mxd, tmpPDF, ddp&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Finished message print "Map compilation completed. Please review the final output."&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Feb 2013 17:45:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631665#M49179</guid>
      <dc:creator>SusanWhitney</dc:creator>
      <dc:date>2013-02-12T17:45:03Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631666#M49180</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The only thing I see is in your List, it seems like there might be an extra back slash between your 2 items. The one after your comma. Should this be there? If I am right, then the definition query is returning nothing and therefore selecting nothing.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;twpList = ['\"TOWNSHIP_C\" = \'D5\' AND \"RANGE_CD\" = \'5\'',&lt;/SPAN&gt;&lt;STRONG&gt;\&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '\"TOWNSHIP_C\" = \'D6\' AND \"RANGE_CD\" = \'4\'']&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, I really like the &lt;/SPAN&gt;&lt;SPAN style="text-decoration:underline;"&gt;Traceback &lt;/SPAN&gt;&lt;SPAN&gt; error handling code to better identify the first line in error. maybe there is something else not firing along the way before it gets to that. Give it a shot:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000000q000000"&gt;http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000000q000000&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Feb 2013 19:03:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631666#M49180</guid>
      <dc:creator>MikeMacRae</dc:creator>
      <dc:date>2013-02-13T19:03:43Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631667#M49181</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you, I'll try adding the Traceback to see if I get any information.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The definition query works correctly. I got that to work before I added the selection statement. The extra backslash was added so I could put the next 'item' on a new line. It just lets python know that the line continues.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Feb 2013 16:13:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631667#M49181</guid>
      <dc:creator>SusanWhitney</dc:creator>
      <dc:date>2013-02-15T16:13:48Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631668#M49182</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I think you should have to refresh the DDP after you set the def query...and the reason for that is you need to make the DDP function 'aware' of the change you made to the index layer, because it generates pages based on it --- kind of similar to refreshing a cache, if you know what I mean?&amp;nbsp; (I added that about the cache to help in a 'conceptual' way.)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So, once DDP 'understands' what it is to export, I am not quite sure the need to 're-refresh', if you'll pardon the term?&amp;nbsp; Even if you had to refresh once just before exporting, that should be sufficient.&amp;nbsp; If you would please, clarify your select or query operation you intend to do in-between the def query set on the index layer (which I know runs fine) and your export to PDF?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;EDIT:&amp;nbsp; Tell me please the reason you need to redefine what is visible in your layers to match the index layer which itself has been redefined?&amp;nbsp; I cannot 'see' what is going on, but it doesn't seem necessary if your redefined index layer by design will only show what is on those pages.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Are you just trying to filter out unanticipated 'no data' pages?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1:50 PM-&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Okay, I think I understand now and see what is wrong - you are trying to further refine the index layer, but you haven't futher manipulated the def query on that layer, so the 2nd 'refresh' command is meaningless.&amp;nbsp; I think you need to get the sections where there is no data (if that is what you're trying to further limit your index layer with) and add to the def query, reset it on the index layer, and then run your refresh again.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Make sense now?&amp;nbsp; ...something like this logic (but of course make it 'grammatically' correct):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;layer.definitionQuery = twnName + 'and not in [twn1, twn2, twn3, etc....]'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...where twn1, twn2, twn3 and so forth represent the townships of no data extracted from your select by location results.&amp;nbsp; Then refresh and your 'layer' should be made aware...&amp;nbsp; I think you're on the right track.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Feb 2013 16:28:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631668#M49182</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-02-15T16:28:43Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631669#M49183</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Good morning,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Definition query defines the township/range to process and the select by location selects only the sections and quarter sections in the township/range that do not have data. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So are you saying to not use the select by location and set it up as a separate definition query or do the select by location first and then the definition query. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I appreciate your assistance. This scripting is my trek into unknown waters.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 18 Feb 2013 13:15:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631669#M49183</guid>
      <dc:creator>SusanWhitney</dc:creator>
      <dc:date>2013-02-18T13:15:50Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631670#M49184</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Susan, I do not know why this didn't dawn on me before, but I think you could do this, provided I have assumed correctly you want to be 'sensitive' to how your pole layer has grown within your grid index layer:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1- Before the loop on your query, run Select By Location (intersect is fine) the grid index layer with the pole layer.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The result should be only grid index features where you have pole layer features.&amp;nbsp; Consider this a 'prefilter' or intermediate result - you were trying to run this in your loop which is not only unnecessary but more processing intensive as well.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2- Copy features to a temp gdb, overwriting as necessary (think you already have this set).&amp;nbsp; Have a lyr file sourced to this temp fc.&amp;nbsp; [or, if you like, I think you could directly source your index layer to the temp fc]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;3- Use UpdateLayer to 'get' the lyr file to update your index layer in your map (from the dynamically 'prefiltered' temp gdb fc of your grid index layer). [This step won't be necessary if you have replaced the datasource as in step 2]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;4- Do your loop to then filter on section/ranges with your query and export pdfs...this way, you only need to refresh the index once for each loop.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That clear?&amp;nbsp; Hope that clears things up, and essentially you were doing things 'backwards'.&amp;nbsp; Also, you could do it via the def query as I suggested earlier, but think that would be more processing intensive as well.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope that helps.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;-Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 18 Feb 2013 13:20:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631670#M49184</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-02-18T13:20:06Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631671#M49185</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Susan - this is your modified code going the modified def query route I initially suggested.&amp;nbsp; Not sure which is the better performer, however this should not require any extra layers or use of a temp gdb - please try it, as it has not been tested, and let me know if you get any errors:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# PoleDDpages.py
# Author: slw
# Date: XXXXX
# Revisions: XXXXX
# Description: Create Pole map book series using Data Driven Pages and
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; insert a cover page

# Import ArcPy
import os,arcpy

# Overwrite existing file
arcpy.env.overwriteOutput = True

try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Set up variables
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Location of pole map .mxd document
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxdDoc = r"G:\GEOSPATIAL\Publishing\Pole\Pole_QtrSec.mxd"

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create the MapDocument object
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(mxdDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; df = arcpy.mapping.ListDataFrames(mxd)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; layer = arcpy.mapping.ListLayers (mxd, "SURVEY_GRID_BNDRY",df)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; poleLyr = arcpy.mapping.ListLayers (mxd, "Pole",df)[0]

&amp;nbsp;&amp;nbsp;&amp;nbsp; ################
&amp;nbsp;&amp;nbsp;&amp;nbsp; # moved part of code to select grids (layer) intersecting 'Pole' features (poleLyr)

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create temporary layers to work with the Selection
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(layer, "surveyLyr")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(poleLyr, "poleLyr_new")

&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("surveyLyr", "CLEAR_SELECTION")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select Layer By Location to limit to just maps with data
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("surveyLyr", "INTERSECT", "poleLyr_new", "", "NEW_SELECTION")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Susan, check that this works, switching the selection...
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("surveyLyr", "SWITCH_SELECTION")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # new code to get at OBJECTIDs to modify def query
&amp;nbsp;&amp;nbsp;&amp;nbsp; IDs = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor("surveyLyr")
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newID = row.OBJECTID
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if newID not in IDs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IDs.append(newID)

&amp;nbsp;&amp;nbsp;&amp;nbsp; del row, rows

&amp;nbsp;&amp;nbsp;&amp;nbsp; subquery = '('
&amp;nbsp;&amp;nbsp;&amp;nbsp; for each in IDs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; subquery = subquery + str(each) + ','

&amp;nbsp;&amp;nbsp;&amp;nbsp; subquery = subquery[0:-1] + ')'
&amp;nbsp;&amp;nbsp;&amp;nbsp; ################

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Output directory for the pole maps
&amp;nbsp;&amp;nbsp;&amp;nbsp; outDir = r"G:\GEOSPATIAL\Publishing\Pole"

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Set the workspace
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.workspace = outDir

&amp;nbsp;&amp;nbsp;&amp;nbsp; # List of map grids
&amp;nbsp;&amp;nbsp;&amp;nbsp; twpList = ['\"TOWNSHIP_C\" = \'D5\' AND \"RANGE_CD\" = \'5\'',\
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '\"TOWNSHIP_C\" = \'D6\' AND \"RANGE_CD\" = \'4\'']


&amp;nbsp;&amp;nbsp;&amp;nbsp; i=1
&amp;nbsp;&amp;nbsp;&amp;nbsp; for twpName in twpList:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # modified the definitionQuery to include the subquery exclusion clause
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer.definitionQuery = twpName + " AND \"OBJECTID\" NOT IN " + subquery

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # refresh() after changing the layer def query to 'redefine' the DDP index limits
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.dataDrivenPages.refresh()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.saveACopy(os.path.splitext(mxdDoc) [0] + str(i) + os.path.splitext(mxdDoc)[1])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i += 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # The final mapbook name taken from the list
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDFfn = outDir + "\\" + twpName [16:18] + twpName [38:39] + "Pole.pdf"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create the final PDF -- which is just an empty shell right now
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDF = arcpy.mapping.PDFDocumentCreate(finalPDFfn)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # A temporary pdf file for processing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tmpPDF = outDir + "\\PoleMapPages.pdf"

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Let the user know what is happening!
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Exporting " + twpName [16:18] + twpName [38:39]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Export the data driven pages in the mxd to a temporary PDF
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Exporting map pages to the temporary PDF"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ddp = mxd.dataDrivenPages.exportToPDF(tmpPDF)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Append the temporary pdf to the final pdf
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Cover, map pages, back cover
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Appending Map Pages"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDF.appendPages (r"G:\GEOSPATIAL\Publishing\Pole\PoleCovers\Covers_"\
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + twpName [16:18] + twpName [38:39] + ".pdf")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDF.appendPages(tmpPDF)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDF.appendPages(r"G:\GEOSPATIAL\Publishing\TwpGrid_Color8x11.pdf")

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Set properties for Adobe Reader and save PDF.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDF.updateDocProperties(pdf_open_view = "USE_THUMBS", pdf_layout = "SINGLE_PAGE")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDF.saveAndClose()

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Deleting temporary layers
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("surveyLyr")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("poleLyr_new")&amp;nbsp;&amp;nbsp;&amp;nbsp; 

except Exception as e:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print e.message
&amp;nbsp;&amp;nbsp;&amp;nbsp; print arcpy.GetMessages(2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
# Clean up
print "Cleaning up"
# Delete the temporary PDF using the ArcPy function
if arcpy.Exists(tmpPDF):&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(tmpPDF)&amp;nbsp; 
# Delete objects
del mxd, tmpPDF, ddp&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 

# Finished message
print "Map compilation completed. Please review the final output."
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:52:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631671#M49185</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2021-12-12T02:52:48Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631672#M49186</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;No errors, Wayne, but I still have the map sheets that do not have any data.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 18 Feb 2013 20:46:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631672#M49186</guid>
      <dc:creator>SusanWhitney</dc:creator>
      <dc:date>2013-02-18T20:46:04Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631673#M49187</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;To be specific, the code I last posted runs without error and the exported map sheets reflect some index pages without pole data?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Immediately after the def query was refreshed, as a check, the following command executed in your loop:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;mxd.saveACopy(os.path.splitext(mxdDoc) [0] + str(i) + os.path.splitext(mxdDoc)[1])&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...and should have written the 2 mxd documents:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;G:\GEOSPATIAL\Publishing\Pole\Pole_QtrSec1.mxd&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;G:\GEOSPATIAL\Publishing\Pole\Pole_QtrSec2.mxd&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In those documents, can you check the index layer def query and the consequent DDP to see if the refresh of those pages has or has not taken place?&amp;nbsp; That will help isolate the error.&amp;nbsp; I suspect if the def query is not in the index layer's properties, then the Select By Location operation did not go as planned.&amp;nbsp; If you will, just copy/paste the different def queries here.&amp;nbsp; If that went well, then I know to look at the pdf export portion of the code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Enjoy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 18 Feb 2013 21:15:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631673#M49187</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-02-18T21:15:45Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631674#M49188</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I did discover an error in the the part I added to the def query - forgot to convert to string and got this error:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;TypeError: cannot concatenate 'str' and 'tuple' objects&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The error occurs on adding 'subquery' which is not a string:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
layer.definitionQuery = twpName + " AND \"OBJECTID\" NOT IN " + subquery
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...so the fix is to add the conversion, simply add 'str' to the line as follows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
layer.definitionQuery = twpName + " AND \"OBJECTID\" NOT IN " + str(subquery)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I did not test the rest of your code, but it seems you should look into better error trapping too.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope that helps - run it again, and if for some reason the PDFs aren't right, follow the instructions on my last post to check the test mxds to let me know at least the definition queries are working.&amp;nbsp; After we know everything is working, you shouldn't need that line anymore to print the mxds - you should be printing your final output from the 'parent' DDP document you've set in the beginning.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Enjoy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:52:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631674#M49188</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2021-12-12T02:52:51Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631675#M49189</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;First you are correct in your statement "To be specific, the code I last posted runs without error and the exported map sheets reflect some index pages without pole data?" &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I ran the script again with the str modification you noted in the definition query and received the same result.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried to open the mxd files G:\GEOSPATIAL\Publishing\Pole\Pole_QtrSec1.mxd and Pole_QtrSec2.mxd. The files would not open. Got a large ArcMap error&amp;nbsp; ... &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Opening the selected Arcmap document failed.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The standard document information of the map document could not be read.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The data frames of the map document could not be read due to the following error:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;0x80040111 (Class factory cannot supply requested class)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The last successfully loaded component was:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;esriDisplay.RgbColor ({7EE9C496-D123-11D0-8383-080009b996cc})&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;the specified file does not contain a valid ArcMap document&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;... so I was not able to check the definition query. I did try to open on more than one machine.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Before I started trying to add the select by location, the definition query was working and providing only the township/range I was asking for. I still get the correct township/range in the resulting file, it's just that I also have pages (sections) that do not have any data. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Looks like I had better research on better error trapping. I guess the beginning class was not enough!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Susan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Feb 2013 13:23:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631675#M49189</guid>
      <dc:creator>SusanWhitney</dc:creator>
      <dc:date>2013-02-19T13:23:02Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631676#M49190</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;hmmm, that's very interesting.&amp;nbsp; Not entirely informative, but interesting nevertheless.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am not saying this is your problem, but it could be you are dealing with an 'older' mxd document, do you know if that is the case?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What you can try is altering the version mxd to saveACopy to - this is the syntax:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;saveACopy (file_name, {version})&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...where 'version' is the optional parameter -- unfortunately I don't think you can test for the original mxd version, but if you think it is an earlier version causing this save corruption to a more recent version, you can test saving it to one of the earlier versions by specifying that optional param (I forgot the current software version you are currently using?):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Directly from the 10.1 webhelp:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;MapDocument (arcpy.mapping)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Desktop » Geoprocessing » ArcPy » Mapping module&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//00s30000000n000000"&gt;http://resources.arcgis.com/en/help/main/10.1/index.html#//00s30000000n000000&lt;/A&gt;&lt;BR /&gt;&lt;SPAN&gt;******************************&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A string that sets the output version number. The default value will use the current version.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;�?�10.1 �??Version 10.1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;�?�10.0 �??Version 10.0&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; �?�8.3 �??Version 8.3&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; �?�9.0 �??Version 9.0/9.1&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; �?�9.2 �??Version 9.2&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; �?�9.3 �??Version 9.3&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;(The default value is None)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;******************************&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So, you can try changing the relevant line in your code to (I have entered the string in bold letters - try an earlier version as necessary):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;mxd.saveACopy(os.path.splitext(mxdDoc) [0] + str(i) + os.path.splitext(mxdDoc)[1], &lt;/SPAN&gt;&lt;STRONG&gt;'10.0'&lt;/STRONG&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And if this is not your problem, I'll try to test the the code segment related to setting the def query later today.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Enjoy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;EDIT:&amp;nbsp; Better yet, if you can open your py script in IDLE, and enter a print command to print the def query just before you set it for the layer:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;print twpName + " AND \"OBJECTID\" NOT IN " + str(subquery)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...then, instead of trying to fix how the test mxds are being saved, you may directly test the def query to see if it is working properly (I could tell you if you pasted it here).&amp;nbsp; Test it 'manually' by copying the result of the print statement from the IDLE window, open your orig mxd and paste it in the def query window, hit OK, then go to your DDP toolbar and refresh your index...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Check your map results...are the index pages correctly 'refined' to the extent of your pole layer?&amp;nbsp; If not, then the def query process needs work.&amp;nbsp; If so, the remaining pdf export component of your code needs work.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Feb 2013 15:00:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631676#M49190</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-02-19T15:00:24Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631677#M49191</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The .mxd was created in version 10.1, so I don't know if that would be the problem.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I added the print statement to the script and this was the result (I stripped out the extra statements):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"TOWNSHIP_C" = 'D5' AND "RANGE_CD" = '5'AND"OBJECTID"NOT IN(186,226,227,248,271,272,274,275,297,322,347,348)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Exporting D55&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"TOWNSHIP_C" = 'D6' AND "RANGE_CD" = '4'AND"OBJECTID"NOT IN(186,226,227,248,271,272,274,275,297,322,347,348)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Exporting D64&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When adding this to the definition query in the original .mxd, it still results in pages without data for both instances. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Susan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Feb 2013 16:17:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631677#M49191</guid>
      <dc:creator>SusanWhitney</dc:creator>
      <dc:date>2013-02-19T16:17:05Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631678#M49192</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Great now we are getting somewhere, Susan - now what I want you to do as part of the troubleshooting process, if you have not already done so, paste one of those queries in the index layer's def query window (of your original mxd) - but click the query button to bring up the query builder dialog, then enter and click 'Verify'....if the query can be evaluated, you should get the msg in a pop-up, "The expression was successfully verified."&amp;nbsp; If so, go refresh the DDP index layer and check out the data in your view - do you still get pages where there is 'no data'.&amp;nbsp; If so, then either something is wrong with the construction of the query or you are looking for something other than what is defined in the problem - the result should be only pages containing pole data.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope that helps.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;-Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Feb 2013 16:31:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631678#M49192</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-02-19T16:31:06Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631679#M49193</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Did all of that exactly. Still get pages with no data.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Feb 2013 16:36:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631679#M49193</guid>
      <dc:creator>SusanWhitney</dc:creator>
      <dc:date>2013-02-19T16:36:37Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631680#M49194</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Could be something silly - the problem here is I'm 'flying blind', cannot see the same thing you are looking at.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;You are limited here as to the size of what you can attach, but can you save a copy of your mxd in 10.0 with only the pole and index&amp;nbsp; (township/range) layers making sure to retain the DDP setup, make it 'relative-pathed' with just the relevant layers in a ver 10.0 gdb (or shapefile format is fine), and attach here?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Quicker to troubleshoot that way - I can pick apart your code way faster and tell you what I have done after having completed it.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Sorry for your trouble!&amp;nbsp; Also, when you're checking your sheets, you are checking the pages in the mxd itself, correct?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;-Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Feb 2013 16:54:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631680#M49194</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-02-19T16:54:20Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631681#M49195</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yes, I checked the sheets in the mxd itself.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;We are working in 10.1, so I need to make everything version 10.0.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I'll try to get that out today or tomorrow.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you for all of your help, Wayne.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Susan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Feb 2013 17:01:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631681#M49195</guid>
      <dc:creator>SusanWhitney</dc:creator>
      <dc:date>2013-02-19T17:01:06Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631682#M49196</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;...one more I guess parting thought for the day on this matter -- the thing about Make Feature Layer is that it 'stamps' whatever may be selected on a given layer into memory.&amp;nbsp; I have assumed nothing is selected and that when we preprocessed layers for the next step, Select by Location, I intended to then 'stamp' one full layer with the other full layer.&amp;nbsp; So that being said, this operation only works as I intended if there are no selections 1st.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Tell me that isn't the problem, lol!&amp;nbsp; Could be the select processing is a little off...but no worries, I am looking at another tool I wrote here that at one point had a similar problem - a user was running a script tool where initially I assumed there were no preselected features --- the tool inadvertently returned no results when that should not have been the case...all due to starting with a subset 'stamped' by Make Feature Layer.&amp;nbsp; (no prior selections results in the whole compliment of features included in the layer).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Anyway, there it is...have to consider it.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Enjoy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Feb 2013 17:18:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631682#M49196</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-02-19T17:18:55Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631683#M49197</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Test this (it is untested, didn't even look at your PDF section, so you have to give it a try):&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# PoleDDpages.py
# Author: slw
# Date: XXXXX
# Revisions: XXXXX
# Description: Create Pole map book series using Data Driven Pages and
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; insert a cover page

# Import ArcPy
import os,arcpy

# Overwrite existing file
arcpy.env.overwriteOutput = True

try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Set up variables
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Location of pole map .mxd document
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxdDoc = r"G:\GEOSPATIAL\Publishing\Pole\Pole_QtrSec.mxd"

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create the MapDocument object
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(mxdDoc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; df = arcpy.mapping.ListDataFrames(mxd)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; layer = arcpy.mapping.ListLayers (mxd, "SURVEY_GRID_BNDRY",df)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; poleLyr = arcpy.mapping.ListLayers (mxd, "Pole",df)[0]

&amp;nbsp;&amp;nbsp;&amp;nbsp; ################
&amp;nbsp;&amp;nbsp;&amp;nbsp; # moved part of code to select grids (layer) intersecting 'Pole' features (poleLyr)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create temporary layers to work with the Selection,
&amp;nbsp;&amp;nbsp;&amp;nbsp; # 1st need to assure no prior selections...the sel by loc syntax:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # SelectLayerByLocation_management (in_layer, {overlap_type}, {select_features}, {search_distance}, {selection_type})
&amp;nbsp;&amp;nbsp;&amp;nbsp; # in_layer is the index layer; select_features is the pole layer
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; SelectBy = [layer, poleLyr]
&amp;nbsp;&amp;nbsp;&amp;nbsp; for lyr in SelectBy:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; iniCount = int(arcpy.GetCount_management(lyr).getOutput(0))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if iniCount != 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(lyr, 'CLEAR_SELECTION')

&amp;nbsp;&amp;nbsp;&amp;nbsp; # should now be properly 'initialized' with no prior selections, i.e., full complement datasets&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(layer, "surveyLyr")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(poleLyr, "poleLyr_new")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select Layer By Location to limit to just maps with data
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("surveyLyr", "INTERSECT", "poleLyr_new", "", "NEW_SELECTION")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Susan, check that this works, switching the selection...
&amp;nbsp;&amp;nbsp;&amp;nbsp; # SelectLayerByAttribute_management (in_layer_or_view, {selection_type}, {where_clause})
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("surveyLyr", "SWITCH_SELECTION")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # new code to get at OBJECTIDs to modify def query
&amp;nbsp;&amp;nbsp;&amp;nbsp; IDs = []
&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.SearchCursor("surveyLyr")
&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IDs.append(row.OBJECTID)

&amp;nbsp;&amp;nbsp;&amp;nbsp; del row, rows

&amp;nbsp;&amp;nbsp;&amp;nbsp; subquery = '('
&amp;nbsp;&amp;nbsp;&amp;nbsp; for each in IDs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; subquery = subquery + str(each) + ','

&amp;nbsp;&amp;nbsp;&amp;nbsp; subquery = subquery[0:-1] + ')'
&amp;nbsp;&amp;nbsp;&amp;nbsp; ################

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Output directory for the pole maps
&amp;nbsp;&amp;nbsp;&amp;nbsp; outDir = r"G:\GEOSPATIAL\Publishing\Pole"

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Set the workspace
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.workspace = outDir

&amp;nbsp;&amp;nbsp;&amp;nbsp; # List of map grids
&amp;nbsp;&amp;nbsp;&amp;nbsp; twpList = ['\"TOWNSHIP_C\" = \'D5\' AND \"RANGE_CD\" = \'5\'',\
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '\"TOWNSHIP_C\" = \'D6\' AND \"RANGE_CD\" = \'4\'']

&amp;nbsp;&amp;nbsp;&amp;nbsp; for twpName in twpList:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # modified the definitionQuery to include the subquery exclusion clause
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer.definitionQuery = twpName + " AND \"OBJECTID\" NOT IN " + str(subquery)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # refresh() after changing the layer def query to 'redefine' the DDP index limits
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.dataDrivenPages.refresh()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # The final mapbook name taken from the list
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDFfn = outDir + "\\" + twpName [16:18] + twpName [38:39] + "Pole.pdf"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create the final PDF -- which is just an empty shell right now
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDF = arcpy.mapping.PDFDocumentCreate(finalPDFfn)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # A temporary pdf file for processing
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tmpPDF = outDir + "\\PoleMapPages.pdf"

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Let the user know what is happening!
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Exporting " + twpName [16:18] + twpName [38:39]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Export the data driven pages in the mxd to a temporary PDF
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Exporting map pages to the temporary PDF"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ddp = mxd.dataDrivenPages.exportToPDF(tmpPDF)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Append the temporary pdf to the final pdf
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Cover, map pages, back cover
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Appending Map Pages"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDF.appendPages (r"G:\GEOSPATIAL\Publishing\Pole\PoleCovers\Covers_"\
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + twpName [16:18] + twpName [38:39] + ".pdf")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDF.appendPages(tmpPDF)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDF.appendPages(r"G:\GEOSPATIAL\Publishing\TwpGrid_Color8x11.pdf")

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Set properties for Adobe Reader and save PDF.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDF.updateDocProperties(pdf_open_view = "USE_THUMBS", pdf_layout = "SINGLE_PAGE")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finalPDF.saveAndClose()

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Deleting temporary layers
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("surveyLyr")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("poleLyr_new")&amp;nbsp;&amp;nbsp;&amp;nbsp; 

except Exception as e:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print e.message
&amp;nbsp;&amp;nbsp;&amp;nbsp; print arcpy.GetMessages(2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
# Clean up
print "Cleaning up"
# Delete the temporary PDF using the ArcPy function
if arcpy.Exists(tmpPDF):&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(tmpPDF)&amp;nbsp; 
# Delete objects
del mxd, tmpPDF, ddp&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 

# Finished message
print "Map compilation completed. Please review the final output."
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This time, if it does not work, I just need better or more feedback from you - more details about what you see...such as perhaps pages with 'no data' - exactly what you mean by that, because just maybe you actually do have pole data on the very edge of the index page so that by the strictest definition you do have intersecting pole data.&amp;nbsp; Make sure too that what is defined in the index layer DDP is what is actually exported...you know the drill, eliminate by parts or steps all possibilities where things may go awry...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Enjoy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:52:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631683#M49197</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2021-12-12T02:52:53Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer by Location not providing correct result.</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631684#M49198</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;It's working! The only issue is, if there is a map (twp/range) where there are no sheets selected, the &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;mxd.dataDrivenPages.refresh() fails and no other maps are output. Is there a way we can handle this?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As an additional note to your last post. The pages that do not have data, really do not have any pole data at all. Nothing on the edges trying to peek through!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Susan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 20 Feb 2013 15:22:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-location-not-providing-correct/m-p/631684#M49198</guid>
      <dc:creator>SusanWhitney</dc:creator>
      <dc:date>2013-02-20T15:22:45Z</dc:date>
    </item>
  </channel>
</rss>

