Point.within(layer)

362
5
11-28-2018 09:36 AM
Jean-SimonLevert
New Contributor II

import arcpy

 

sr = arcpy.SpatialReference(4326)

Laye = arcpy.mp.LayerFile(r'X:\Polygone.lyr')

lyr = Laye.listLayers("*")[0]

point = arcpy.PointGeometry(arcpy.Point(Var_x1, Var_y1), sr)
print(point.within(lyr))

I'm doing something wrong.

I'm trying to get a Boolean awser if True or False point is in a .lyr file

 

Does anyone see what's wrong?

Thx alot

5 Replies
JoshuaBixby
MVP Esteemed Contributor

ArcPy Geometry methods don't operate on sets of geometries, like most geoprocessing tools, the methods operate on individual geometries.  This means you cannot pass arcpy.Point.within a data set either directly or through a feature layer.

A LYR file doesn't contain any data, it contains references to data, so a point can never be "in a .lyr file."  That said, I do understand what you are attempting to do.

I recommend using Select Layer By Location because it operates on data sets through feature layers and also accepts ArcPy Geometry objects for selecting geometries.  I want to test something before suggesting specific code, will get back to you a bit later.

UPDATE:  This is the overall approach I would go with

import arcpy

lyr_path = # path to layer file
coords = # tuple containing x,y coordinates
epsg = # epsg code for coordinate system

lyr_file = arcpy.mp.LayerFile(lyr_path)
lyr = lyr_file.listLayers()[0]
point = arcpy.PointGeometry(arcpy.Point(*coords), arcpy.SpatialReference(epsg))

arcpy.SelectLayerByLocation_management(
    lyr,
    "CONTAINS",
    point,
    selection_type="NEW_SELECTION"
)

print(arcpy.GetCount_management(lyr))
Jean-SimonLevert
New Contributor II

Thx alot, its work very well!

I did work on the code to be able to filter some result.

import arcpy

lyr_path = r'X:\Polygone.lyr'
coords = (Var_x1, Var_y1)
epsg = 4326

lyr_file = arcpy.mp.LayerFile(lyr_path)
lyr = lyr_file.listLayers()[0]
point = arcpy.PointGeometry(arcpy.Point(*coords), arcpy.SpatialReference(epsg))

arcpy.SelectLayerByLocation_management(
lyr,
"CONTAINS",
point,
selection_type="NEW_SELECTION"
)
arcpy.SelectLayerByAttribute_management(lyr, selection_type="REMOVE_FROM_SELECTION", where_clause="Croquis1 IS NULL")

at this point " print(arcpy.GetCount_management(lyr)) " will give me a count of 3.

but when i try : 

with arcpy.da.SearchCursor(lyr, 'Ticket_Number') as cursor:
   for row in cursor:
      print(row)

it print the information of the r'X:\Polygone.lyr

What can i do to only print the 3 answers of print(arcpy.GetCount_management(lyr))

Thx alot

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Odd, ArcPy DA cursor should honor the selection set. 

A couple questions since I don't have access to the data.  First, how many records are in the data source referenced in the layer file?  Second, where are you making the initial selection so you can then remove items from the selection?

0 Kudos
Jean-SimonLevert
New Contributor II

There is more then 400 000 polygons. I want to see if there is a polygon at a x,y. Then with SelectLayerByLocation_managenent the result is 9 with Getcount_management. But I have to remove those with "croquis1 IS NULL" so Getcount_management return 3.

Why if Getcount_management(lyr) = 3

with arcpy.da.SearchCursor(lyr, 'Ticket_Number') as cursor:
   for row in cursor:
      print(row)

Will print 400 000+ 'Ticket_Number'? 

Thx alot☺️

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I see what you are talking about, and have run across this same kind of issue with a surprising number of ArcPy functions in Pro.  Specifically, the code runs as expected within ArcGIS Pro itself, e.g,. in the interactive Python window, but it does not run as expected outside of Pro.  Just for kicks, try running the code in Pro itself?

This is the 4th bug like this I have seen in the past 2 weeks.

0 Kudos