Help using PointGeometry with SelectLayerByLocation

1976
19
Jump to solution
01-11-2019 11:36 AM
ZoltanSzecsei
New Contributor III

Hi,

The following code fails:

pg = arcpy.PointGeometry(vert,'None',True)
arcpy.SelectLayerByLocation_management("hgt_feats", "WITHIN_A_DISTANCE_3D", pg, 0.005, 'NEW_SELECTION')

with:

File "Z:/Projects/Development/python/ToGround/ToGround.py", line 140, in <module>
arcpy.SelectLayerByLocation_management("hgt_feats", "WITHIN_A_DISTANCE_3D", pg, 0.005, 'NEW_SELECTION')
File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\management.py", line 7873, in SelectLayerByLocation
raise e
RuntimeError: Object: Error in executing tool

I'm trying to see if there is an underlying polygon vertex at a specific XYZ location.

I do not need to select the underlying polygon, I just need to know if there is one at that location

Any help would be appreciated.

Regards and thanks,

Zoltan

Tags (1)
0 Kudos
19 Replies
JoshuaBixby
MVP Esteemed Contributor

With ArcGIS Pro, not sure whether from 1.0 or a later release, you can use Select Layer By Location with only ArcPy geometries.  The Select Layer By Location—Data Management toolbox | ArcGIS Pro  alludes to it rather than stating it directly:

Usage

  • If the input is a feature class or dataset path, this tool will automatically create and return a new layer with the result of the tool applied.

>>> import arcpy
>>> 
>>> SR = arcpy.SpatialReference(3857)
>>> polys = [
...     arcpy.FromWKT('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))', SR),
...     arcpy.FromWKT('POLYGON((11 11, 21 11, 21 21, 11 21, 11 11))', SR),
...     arcpy.FromWKT('POLYGON((0 11, 5 11, 5 16, 0 16, 0 11))', SR)
... ]
>>> pt = arcpy.FromWKT('POINT(10.5 10.5)', arcpy.SpatialReference(3857))
>>> 
>>> fl, = arcpy.SelectLayerByLocation_management(
...     polys,
...     'WITHIN_A_DISTANCE',
...     pt,
...     1.0,
...     'NEW_SELECTION'
... )
>>> fl.getSelectionSet()
{1, 2}
>>> 
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The IDs returned from getSelectionSet() are offset by 1 from the index of the list passed to it.

In terms of ArcGIS Desktop/ArcMap, this new functionality was not supposed to be back ported, but it appears to have been back ported because I can run this code with 10.6.1.  That said, I think using an ArcPy geometry as the select_features has worked for several versions.

DanPatterson_Retired
MVP Emeritus

That makes sense (like the interactive featurelayer creation in tools for Pro

... but it does the arcpy's PointGeometry Zoltan is using work?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Should it work for Zoltan with the PointGeometry, I think so, and it appears to be running without errors.

I can't say I am a fan of the changes to Select Layer by Attributes and Selection Layer By Location.  The tools are called "Select Layer," I don't see what is wrong with requiring an actual layer for the in_layer parameter, which is the historical behavior.  The newfangled, "convenient" behavior both confuses the semantics of the tools as well as changes some of the behaviors of the tools when using ArcPy geometries as the select_features.  I find it humorous and annoying how "backwards compatibility" is so often used as the excuse when my organization asks about changing ArcPy behavior, but it doesn't seem to stop Esri from changing long-standing tool behavior when they want.

0 Kudos
DanPatterson_Retired
MVP Emeritus

on 2.3 beta, and I couldn't even get your code to work until I removed the comma on line 9 an

it didn't like fl.getSelectionSet(), and I had to use

for i in fl:
    print(i)
f1635DCC2_72BF_41B1_8863_C2A2
f1635DCC2_72BF_41B1_8863_C2A2
2
So 2.3 will have more changes if you weren't using Pro 2.3, beta-2
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I last tested with Python 3 for ArcPy 10.6.1 on Linux, an ArcGIS Server-based ArcPy bundle.  So, it could be part of what I did only works on either 10.6.1, Linux, or both!

Regarding Pro, I will have to test again next week once I get access again to a machine with it installed.  What I have found with Pro is that certain ArcPy code works/behaves differently when run from within the application vs. outside of the application.  I have logged several bugs recently where code that should work in a script being executed with the bundled Python interpreter doesn't work, but the same code does work within the Pro application either from a script tool or interactive Python window.

0 Kudos
DanPatterson_Retired
MVP Emeritus

To be expected since the 'arcpy's (in it's various incarnations ) are indeed different.

Python 3.6.6 for current ArcGIS Pro.  I don't think that will change much if at all.

3.6.8 ended the 3.6* line, 3.7* is up to 3.7.2 (stable) and 3.8* is under development.

0 Kudos
ZoltanSzecsei
New Contributor III

Yes it doesNotice i==1 in watch window.

0 Kudos
ZoltanSzecsei
New Contributor III

No - this was my first attempt - but see my post with screenshot, above - it does work.

0 Kudos
ZoltanSzecsei
New Contributor III

OK - it seems not to work reliably, si I need to rethink my approach.

I'll start a new thread called "Efficient way to check if a vertex coincides with another feature's vertex"

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

In your new thread, explain what isn't working reliably, the geoprocessing tools themselves or the spatial analysis part.

0 Kudos