2

How can I write arcpy code to determine if the points of a point feature class are inside polygons of a polygon feature class and write the result (true/false) to the point FC's attribute field?

I need to use the cursor function, but I don't know where to use it.

# Import arcpy module
import arcpy

# Script arguments
Point = arcpy.GetParameterAsText(0)
if Point == '#' or not Point:
    Point = "Point" # provide a default value if unspecified

Polygon= arcpy.GetParameterAsText(1)
if Polygon== '#' or not Polygon:
    Polygon= "Polygon" # provide a default value if unspecified

# Local variables:
Point__2_ = Point
Point__3_ = Point
Point__5_ = Point__3_
Point__4_ = Point__3_

# Process: Add Field
arcpy.AddField_management(Point, "REST", "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field
arcpy.CalculateField_management(Point__3_, "REST", "\"False\"", "PYTHON", "")

# Process: Select Layer By Location
arcpy.SelectLayerByLocation_management(Point, "COMPLETELY_WITHIN", Polygon, "", "NEW_SELECTION", "NOT_INVERT")

# Process: Calculate Field (2)
arcpy.CalculateField_management(Point__2_, "REST", "\"True\"", "PYTHON", "")```


4
  • 1
    The spatial join tool would do this in one go, explore that.
    – Hornbydd
    May 19 '20 at 15:58
  • Please how? I am new to arcpy @Hornbydd
    – Geoibro
    May 19 '20 at 16:03
  • 1
    No arcpy, read the help file and run the tool, You can then add your boolean field and run field calculate. This could all be done in modelbuilder so you can re-run your steps.
    – Hornbydd
    May 19 '20 at 16:17
  • The code is working, i already created a script with it. but i am to use the Cursor function in my code, which i dont know how to use the cursor function.
    – Geoibro
    May 19 '20 at 16:21
1

These are great resources for your issue.
https://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-classes/geometry.htm
https://pro.arcgis.com/en/pro-app/arcpy/data-access/updatecursor-class.htm

import arcpy

# Location of point and polygon shapefiles
arcpy.env.workspace = r'C:\gispy\stackexchange'
Point = 'Point.shp'
Polygon = 'Polygon.shp'

polyGeom = arcpy.CopyFeatures_management(Polygon, arcpy.Geometry())
arcpy.AddField_management(Point, "REST", "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# For each point, determine if the geometry is within the polygon feature class
fields = ['REST', 'SHAPE@']
with arcpy.da.UpdateCursor(Point, fields) as cursor:
    for row in cursor:
        # row[0] corresponds to the newly create 'REST' attribute..0 index
        # row[1] accesses the geometry of your point feature class.. 1 index
        if row[1].within(polyGeom[0]):
            row[0] = 'True'
        else:
            row[0] = 'False'
        cursor.updateRow(row) 
5
  • Thanks so u can just use the point and polygon for my parameters , no output field?
    – Geoibro
    May 20 '20 at 6:03
  • I edited my original answer. The two shapefiles are your input parameters. The UpdateCursor iterates through every row in the Point shapefile and utilizes the within arcpy method to check if the point feature is within the polygon feature. The output field you created using AddField_management is accessed through the UpdateCursor and is represented by row[0]. May 20 '20 at 13:13
  • Thanks, i have tried it, but The script is still giving me error, or will there be output parameter too, because the code i used model builder for does not have output parameter
    – Geoibro
    May 20 '20 at 14:26
  • Did you update the workspace and shapefile names? Are both shapefiles in the same directory? What is the error? May 20 '20 at 16:07
  • Its working fine now. Thanks a lot.
    – Geoibro
    May 20 '20 at 21:16

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.