Select polygon under feature set point

1102
11
Jump to solution
03-18-2012 08:37 PM
StephenBarrow
New Contributor
Hi

I am trying basically copy the selectlayerbylocation functionality but using a point from a feature set as my input but after trying a few different things I can't get my script to select a polygon under the point in the same way select layer by location does.

Anyone have any idea how to do this under arcpy?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Hi Stephen,

Add the following (in bold) to your script.  This will be the same as setting the Output Coordinate System to 'Same as Display'.

import arcpy from arcpy import mapping from arcpy import env  mxd = mapping.MapDocument("CURRENT") df = mapping.ListDataFrames(mxd)[0] spatialReference = df.spatialReference  env.outputCoordinateSystem = spatialReference  # Script arguments Point_Selection = arcpy.GetParameterAsText(0)  Polygon_Selection = arcpy.GetParameterAsText(1)  # Process: Select Layer By Location arcpy.SelectLayerByLocation_management(Polygon_Selection, "INTERSECT", Point_Selection, "", "NEW_SELECTION")  arcpy.RefreshActiveView()

View solution in original post

0 Kudos
11 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Stephen,

Here is an example:

import arcpy

# Script arguments
Point_Selection = arcpy.GetParameterAsText(0)

Polygon_Selection = arcpy.GetParameterAsText(1)

# Process: Select Layer By Location
arcpy.SelectLayerByLocation_management(Polygon_Selection, "INTERSECT", Point_Selection, "", "NEW_SELECTION")

arcpy.RefreshActiveView()


Then you can import the script into a toolbox and set the parameters:

[ATTACH=CONFIG]12802[/ATTACH]


For the Feature Set, be sure to set the 'schema' to an existing shapefile/feature class.
0 Kudos
StephenBarrow
New Contributor
Hi Jake

Thanks for your response and suggestion, unfortunately this matches what I have tried and although the code completes successfully nothing is selected in the polygon featureclass or shapefile underneath either onscreen or in the attribute table.  Is there something else I need to do?

Stephen
0 Kudos
StephenBarrow
New Contributor
Interestingly if I apply a GetCount_management to the selection layer it returns 0 records even though there are 40308 features within, the feature set returns the correct number of features.

Further testing- if I add the polygon fc and run the script the correct number of features is returned by the count but on a second run straight after the count is back to 0, in both cases still no selected polygons.

The only hint I get (and this may not be relevant) is that the tool messages include this:
Executing: SelectLayerByLocation AHGFContractedCatchment INTERSECT in_memory\{68DDA71F-1C9E-4477-A9EE-4746D9B0F4E3} # NEW_SELECTION
Which may mean that this is becoming an in memory process and not transferred to the activeview?

As you can see my code is practically the same as your suggestion:

import arcpy
features = arcpy.GetParameterAsText(0)
layertoSelect = arcpy.GetParameterAsText(1)
result = arcpy.GetCount_management(layertoSelect)
arcpy.AddMessage("x = " + str(result.getOutput(0)))
arcpy.SelectLayerByLocation_management(layertoSelect,"INTERSECT",features,"","NEW_SELECTION")
for m in range(0,arcpy.GetMessageCount()-1):
    arcpy.AddMessage(arcpy.GetMessage(m))
arcpy.RefreshActiveView()


Stephen
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Can you send a screen shot of your Parameters tab within the Script's properties?
0 Kudos
StephenBarrow
New Contributor
This is my setup:

[ATTACH=CONFIG]12881[/ATTACH]

The full path is to a point feature class
D:\GeoFabric\HR_Catchments\HR_Catchments.gdb\HR_Catchments\AHGFNode
0 Kudos
StephenBarrow
New Contributor
I think I have worked out what the problem is, the selectlayerbylocation doesn't work when the layer coordinate system is different to the dataframe's in that is one is projected and the other is geographic then the layer that is different refuses to be selected. 

When I started with a fresh mxd it all worked as expected.

Is this a bug or "as designed"?
0 Kudos
StephenBarrow
New Contributor
hmm, and then I do a retest of the theory and it now refuses to work again...
0 Kudos
JakeSkinner
Esri Esteemed Contributor
I was able to reproduce this behavior when the data frame is set to a different coordinate system than the feature class the selection is applied to.  I was able to workaround this by going into the Environments > Output Coordinates and changing Output Coordinate System to 'Same as Display'.
0 Kudos
StephenBarrow
New Contributor
Hi Jake

Looks like the environment setting 'Same as Display' works (even or one layer loaded into a fresh mxd that wasn't working).

After a look through the help files it looks like this isn't something I can set in python code, do you know of a way to set this in code? I can only see the ability to set the projection in code.

Thanks

Stephen
0 Kudos