1

I have polygon.shp and point.shp. I have to check certain attribute where if the point is inside a polygon it have to be True and if it is outside the polygon it has to be False.

I am new to ArcPy and I have no idea which logic to use to find this.

#i tried spatial join, but the thing is that so many points with True as their value got left out

import arcpy

arcpy.env.workspace = 'C:/Desktop/New jersey_Completed'
outworkspace = 'C:/Desktop/Output'

Polygon = "C:/New_Jersey_BFP_Part-2.shp"
point = 'C:/New jersey_Completed/New_Jersey_20220224_Property_Points_Part-2.shp'
arcpy.AddField_management(point, "REST", "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
fields = ['point', 'Polygon']
with arcpy.da.SearchCursor(point, fields) as 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].contain([0]):
            row[0] = 'True'
        else:
            row[0] = 'False'
        cursor.updateRow(row) 
4
  • I have tried Spatial join, but the thing is that In 1000 attributes only 800 gets as an output as True but i want to check all the points and the points which are not inside the polygon has to show an output as false
    – Newbie
    Commented Jun 28, 2022 at 10:27
  • but there are like more than 200,000 polygons tho and my target feature is a polygon which would be kinda taking long for the output
    – Newbie
    Commented Jun 28, 2022 at 11:08
  • 1
    There are a handful of ways to achieve this. Simply use select by location where a point intersects the polygon, set the attribute to True using field calculator (it will only update selected records), repeat except invert selection and set the rest to False. Commented Jun 28, 2022 at 11:15
  • It's rare when you'll get a query with a single cursor to return both a point and a polygon, so this code sample is a flight of fancy (and you appear to be updating a SearchCursor row, and failed to include the Boolean field in your fields list). While it is possible to use an UpdateCursor to select features in another layer and update accordingly, you're missing quite a bit of code to attempt that.
    – Vince
    Commented Jun 28, 2022 at 12:58

1 Answer 1

1

The below is using ArcGIS Pro. If using ArcMap you will need to update the PYTHON3 to PYTHON_9.3 in the CalculateField

import arcpy

## path to your polygon shapefile
polygon = "path/to/polygon.shp"

## path to you point shapefile
point = "path/to/point.shp"

## the field to add to the point shapefile
## this will be filled in with True or False
field = "REST"

## add the field to the point shapefile
arcpy.AddField_management(point, field, "TEXT", "", "", 5, "", "NULLABLE", "NON_REQUIRED", "")

## select all points that intersect a polygon
point_in_poly = arcpy.SelectLayerByLocation_management(point, "INTERSECT", polygon)

## update the attribute to True for the selection
arcpy.CalculateField_management(point_in_poly, field, "'True'", "PYTHON3")

## select all points that do not intersect a polygon
point_not_in_poly = arcpy.SelectLayerByLocation_management(point, "INTERSECT", polygon, invert_spatial_relationship=True)

## update the attribute to False for the selection
arcpy.CalculateField_management(point_not_in_poly, field, "'False'", "PYTHON3")
2
  • Thank you! i will try this and also im using ArcPro
    – Newbie
    Commented Jun 28, 2022 at 11:28
  • 1
    It worked!! thank you, Clubdebambos
    – Newbie
    Commented Jun 29, 2022 at 4:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.