HOW TO

Relocate an existing point into a polygon with a common attribute using ArcPy in ArcGIS Pro

Last Published: March 28, 2024

Summary

In ArcGIS Pro, placing points within polygons containing similar attributes is important for a variety of spatial analysis such as spatial aggregation and data integration. A common attribute typically refers to a field or property shared by features within a dataset.

Point features are placed out of the polygon features

This article provides the workflow to place a point in a polygon with a common attribute using ArcPy in ArcGIS Pro.

Procedure

Note:
This workflow requires a full script to run in the ArcGIS Pro Python window. All the indents are to be kept as portrayed in the code block.
  1. In ArcGIS Pro, open the map containing the point and polygon feature layers.
  2. Open the Python window. Refer to ArcGIS Pro: Python window for more information.
  3. Run the following script:
Note:
This workflow creates a new point feature in the geodatabase that is not added into the map.
  1. Import the necessary modules and create a user defined function to create a new reference point feature for relocation.
import arcpy
import os

def place_points_inside_polygons(input_polygon_fc, output_point_fc, common_attribute_field):
    arcpy.management.CreateFeatureclass(
        out_path=os.path.dirname(output_point_fc),
        out_name=os.path.basename(output_point_fc),
        geometry_type="POINT",
        spatial_reference=input_polygon_fc
    )
  1. Add the common attribute field to the reference point feature class.
    arcpy.management.AddField(output_point_fc, common_attribute_field, "TEXT")
  1. Iterate through each polygon in the input feature class, calculate the centroid point inside each polygon, and insert a point feature into the polygon feature with the same common attribute value as the corresponding polygon.
    with arcpy.da.InsertCursor(output_point_fc, ["SHAPE@", common_attribute_field]) as cursor:
        with arcpy.da.SearchCursor(input_polygon_fc, ["SHAPE@", common_attribute_field]) as search_cursor:
            for row in search_cursor:
                polygon_geom = row[0]
                common_attribute_value = row[1]
                point_inside_polygon = polygon_geom.centroid
                cursor.insertRow([point_inside_polygon, common_attribute_value])
  1. Set the input parameters. Replace <pathToPolygonFc> and <pathToPointFc> with the paths of the polygon and point feature class respectively. Replace <fieldName> with the common attribute field name.
input_polygon_fc = r"<pathToPolygonFc>"
output_point_fc = r"<pathToOriginalPointFc>"
common_attribute_field = "<fieldName>"
  1. Call the function to place points inside polygons with the common attribute.
place_points_inside_polygons(input_polygon_fc, output_point_fc, common_attribute_field)

The code block below demonstrates the full script.

import arcpy
import os

def place_points_inside_polygons(input_polygon_fc, output_point_fc, common_attribute_field):
    arcpy.management.CreateFeatureclass(
        out_path=os.path.dirname(output_point_fc),
        out_name=os.path.basename(output_point_fc),
        geometry_type="POINT",
        spatial_reference=input_polygon_fc
    )

    arcpy.management.AddField(output_point_fc, common_attribute_field, "TEXT")

    with arcpy.da.InsertCursor(output_point_fc, ["SHAPE@", common_attribute_field]) as cursor:
        with arcpy.da.SearchCursor(input_polygon_fc, ["SHAPE@", common_attribute_field]) as search_cursor:
            for row in search_cursor:
                polygon_geom = row[0]
                common_attribute_value = row[1]
                point_inside_polygon = polygon_geom.centroid
                cursor.insertRow([point_inside_polygon, common_attribute_value])

input_polygon_fc = r"C:\Users\Documents\ArcGIS\Projects\MyProject40\MyProject40.gdb\Polygon"
output_point_fc = r"C:\Users\Documents\ArcGIS\Projects\MyProject40\MyProject40.gdb\Pointsss"
common_attribute_field = "Distance"

place_points_inside_polygons(input_polygon_fc, output_point_fc, common_attribute_field)

The image below shows the point placed in the polygon with a common attribute using ArcPy.

The points are placed in the polygon feature

Article ID: 000032318

Software:
  • ArcGIS Pro 3 1
  • ArcGIS Pro 3 0
  • ArcGIS Pro 3 2

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options