3

I need to adjust a premade .mxd many times, so I'd like to speed it up with this script. Everything is working except the Select by Location part. The query reduce the Address layer to a single point and I want to use this point to select the layer. The mxd has all the county layers oppened but the input will change in the future. I'm getting the error:ERROR 000368: Invalid input data. ERROR 000732: Selecting Features: Dataset Address does not exist or is not supported
If I run this script in ArcGIS with mxd set as "Current", then everything works. I want to run the script without opening the actual mxd.

import os
import shutil
import glob, os

SID = "12234"
SIDtext = "df-" + str(SID)
address1 = "1233 Antler Dr"
address2 = "Summerton"
address3 = "23332"
address = address1 + "\n" +  address2 + "\nSC " + address3
county = "Clarendon"
owner = "Smith"

test = "C:\\Users\\test\\test.mxd"

mxd = arcpy.mapping.MapDocument(test)
for df in arcpy.mapping.ListDataFrames(mxd):
    #adjusting 1st dataframe
    if df.name == "Overview Map":
        for lyr in arcpy.mapping.ListLayers(mxd):  
            if lyr.name == "Address":  
                lyr.definitionQuery = "Case_ID = '" + str(SID) + "'"
    #adjusting 2nd dataframe
    else:

        df2 = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
        for lyr in arcpy.mapping.ListLayers(mxd, "Address", df2):  
            if lyr.name == "Address":  
                lyr.definitionQuery = "Case_ID = '" + str(SID) + "'"
            df2.panToExtent(lyr.getSelectedExtent())


        arcpy.SelectLayerByLocation_management(county, "INTERSECT", "Address")
        arcpy.MakeFeatureLayer_management(county, "Property Parcel")
        for lyr in arcpy.mapping.ListLayers(mxd):
            if lyr.name == "Property Parcel":
                lyr.visible=True
                symbologyLayer = "Terra selection"
                arcpy.ApplySymbologyFromLayer_management (lyr, symbologyLayer)

        arcpy.SelectLayerByAttribute_management(county, "CLEAR_SELECTION")
        arcpy.RefreshActiveView()

mxd.save()
del mxd

Error message:

Runtime error Traceback (most recent call last): File "", line 33, in File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\management.py", line 7320, in SelectLayerByLocation raise e ExecuteError: ERROR 000368: Invalid input data. ERROR 000732: Selecting Features: Dataset Address does not exist or is not supported

  • Have you checked that your script actually finds an Address layer? In your if lyr.name == "Address" add a print lyr.name to verify it is finding the layer – Midavalo Feb 9 '17 at 2:39
  • The definitionQuery changes if I run the script without the Select by location part. I checked what you mentioned and it does find the layer. – Gary Feb 9 '17 at 2:55
3

If you look at the arguments of arcpy.SelectLayerByLocation_management, which is

SelectLayerByLocation_management (in_layer, {overlap_type}, {select_features}, {search_distance}, {selection_type}, {invert_spatial_relationship}),

you will find that it expects a Selecting Layer, where it complains as in your error message. In your code, "Address" is a string object and this will work as long as either (1) you have an Address layer in Layers data frame and run this script through ArcMap, or (2) Address is a table/feature class in your default workspace, i.e., what's set by arcpy.env.workspace, say you set arcpy.env.workspace = r'C:\Data.gdb' and the referenced feature class is C:\Data.gdb\Address and converted this to a layer, say by using arcpy.MakeFeatureLayer_management("Address", "Address"). I find this approach ad-hoc (specific to a map document and needs to be run through ArcMap) and try to use layer object instead. This is the main problem related to your question. So, the correct syntax would be:

arcpy.SelectLayerByLocation_management(county, "INTERSECT", lyr, 0, "NEW_SELECTION")

assuming your lyr variable is a layer object assigned to the right layer, "Address", through ListLayers().

In addition to that:

  • You need to specify a Selection Type for your selection by location.

  • If you want to update your data frame's extent, you need to use df2.extent = df2.panToExtent(lyr.getSelectedExtent()), respective to the extent that you want to pan. Thanks @jbalk for highlighting this.

  • 1
    It will work @jbalk as per my assumtion, as well as "Address" will as long as the OP has a layer in Layers df with the same name. I edited my answer. – fatih_dur Feb 9 '17 at 8:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.