if the point is inside the polygon get data from a particular field while selected.

1842
3
12-10-2013 08:46 AM
HectorChapa
Frequent Contributor
I found a snippet of code that returns if the point is inside the polygon. I like the idea but I want the code to do a little more than just that. For example, let's say I use my tool the selected features I select the points and the polygons that is touching that points. I want to get the data from the particular field to the point when only does that is selected. I think the code below will help on doing this ambitious task. Maybe someone else has already the answer I just need someone to lead me either to the answer to the question or lead me to the tools will need to write the script. I am thinking that the bottom script will help in such of way if not then correct me.

import arcpy
 
def point_in_poly(poly, x, y):
    """Returns if the point is inside the polygon.
 
    Parameters:
        poly: arcpy.Polygon() geometry
        x:    x coordinate (float)
        y:    y coordinate (float)
 
    """
    pg = arcpy.PointGeometry(arcpy.Point(x, y), poly.spatialReference)
    return poly.contains(pg)
Tags (2)
0 Kudos
3 Replies
JoshuaChisholm
Frequent Contributor
Hello Hector,

I have a rough solution that uses the select by location tool instead of the above mentioned "contains" method. This should do the trick, but you may have to customize it to suit your exact needs. It assumes you are manually selecting the points.

import arcpy

mxd=arcpy.mapping.MapDocument("C:\\Path\\To\\Your\\Map.mxd") #use "CURRENT" if running in ArcMap Python Window or as a tool

pointLyrName="SamplePoints" #Change to name of point layer you are selecting
polyLyrName="SamplePolygons" #Change to name of point layer you are selecting

#Get Layers by layer name (make sure layer name only occurs once in TOC):
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name==pointLyrName:
        pointLyr=lyr
    if lyr.name==polyLyrName:
        polyLyr=lyr

#Test if there are any selected points:
selectedPointCount=int(arcpy.gp.GetCount_management(arcpy.gp.describe(pointLyr).catalogpath).getoutput(0)) - int(arcpy.gp.GetCount_management(pointLyr).getoutput(0))
if selectedPointCount==0:
    raise Exception("No points selected.")

#Select by location:
arcpy.SelectLayerByLocation_management (polyLyr, "INTERSECT", pointLyr)

#Test if there are any polygons points:
if int(arcpy.GetCount_management(polyLyr).getOutput(0))==0:
    raise Exception("Selected points do not overlap any polygons.")

for row in arcpy.SearchCursor(polyLyr): #will only search through selected records (or all records if none are selected)
    print row.ID #change ID to the field you wish to see

arcpy.RefreshActiveView()

print "Completed!"


There are separate and better tools if you're trying to transfer attributes from polygons to points (see Spatial Join: http://resources.arcgis.com/en/help/main/10.1/index.html#//00080000000q000000). Let us know more about what you're trying to do if this doesn't suit your needs.

Good luck!
~Josh
0 Kudos
HectorChapa
Frequent Contributor
Hello Hector,

I have a rough solution that uses the select by location tool instead of the above mentioned "contains" method. This should do the trick, but you may have to customize it to suit your exact needs. It assumes you are manually selecting the points.

import arcpy

mxd=arcpy.mapping.MapDocument("C:\\Path\\To\\Your\\Map.mxd") #use "CURRENT" if running in ArcMap Python Window or as a tool

pointLyrName="SamplePoints" #Change to name of point layer you are selecting
polyLyrName="SamplePolygons" #Change to name of point layer you are selecting

#Get Layers by layer name (make sure layer name only occurs once in TOC):
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name==pointLyrName:
        pointLyr=lyr
    if lyr.name==polyLyrName:
        polyLyr=lyr

#Test if there are any selected points:
selectedPointCount=int(arcpy.gp.GetCount_management(arcpy.gp.describe(pointLyr).catalogpath).getoutput(0)) - int(arcpy.gp.GetCount_management(pointLyr).getoutput(0))
if selectedPointCount==0:
    raise Exception("No points selected.")

#Select by location:
arcpy.SelectLayerByLocation_management (polyLyr, "INTERSECT", pointLyr)

#Test if there are any polygons points:
if int(arcpy.GetCount_management(polyLyr).getOutput(0))==0:
    raise Exception("Selected points do not overlap any polygons.")

for row in arcpy.SearchCursor(polyLyr): #will only search through selected records (or all records if none are selected)
    print row.ID #change ID to the field you wish to see

arcpy.RefreshActiveView()

print "Completed!"


There are separate and better tools if you're trying to transfer attributes from polygons to points (see Spatial Join: http://resources.arcgis.com/en/help/main/10.1/index.html#//00080000000q000000). Let us know more about what you're trying to do if this doesn't suit your needs.

Good luck!
~Josh


Josh thanks for the code let me try to see if this could work I let you know what happends. Essentially I am trying to do spatial join but without using that tool. For example, if I select 5 points and does 5 points are on top of five different polygons I want to get data from the correct polygon they are intersecting and transfer over to the point shapefile. Thats my goal. Let me see what this code you provide can guide me close to my goal. If not I show so far what I have. Oh ok.
0 Kudos
JoshuaChisholm
Frequent Contributor
Hello Hector,

Did you manage to get the script working? Have you resolved your problem?

I hope everything is working!
0 Kudos