0

I am trying to insert three polygon into a shapefile. The goal is to use a function that will create the polygon. That way, I can pass the coordinates of the polygon into the function. I have read multiple pages of documentation on this.

I am new and not seeing where the error is. The error I keep getting is that:

float is not iterable

I am writing this in a Jupyter Notebook using ArcGIS Online.

import arcpy
import shapefile
import csv
import sys
# Set the workspace
arcpy.env.workspace = "/arcgis/home/"
# Sets the Spatial Reference
sr = arcpy.SpatialReference(4326) #WGS84
# Create a polygon feature class
fc = "polygon_file1.shp"
arcpy.CreateFeatureclass_management(arcpy.env.workspace, fc, "POLYGON", spatial_reference=sr)

# Create polygon object and write it to the feature class 
def insert_polygon(x1, y1, x2, y2, x3, y3, x4, y4):
    polygon = arcpy.Polygon([arcpy.Point(x1, y1), arcpy.Point(x2, y2), arcpy.Point(x3, y3), arcpy.Point(x4,y4), arcpy.Point(x1,y1)])
    with arcpy.da.InsetCursor(fc, ["SHAPE@"]) as cursor:
        cursor.insertRow([polygon])
    return

insert_polygon(-92.421457,39.447925,-92.419858,39.448451,-92.417868,39.446918,-92.421349,39.446094)
insert_polygon(-92.413072,39.446835,-92.413072,39.447879,-92.412267,39.446073,-92.416441,39.445087)
insert_polygon(-92.417900,39.449872,-92.412493,39.451015,-92.411978,39.449888,-92.417299,39.448778)
2
  • 3
    Please do not use HTML formatting tags in code. Instead, use a code block (4-space indent or use of triple-tics above and below), which will make the code legible. Spacing is extremely significant in Python, so pasting without it is as bad as not posting the code at all. Commented Nov 5, 2024 at 19:54
  • Thank you will keep that in mind. Commented Nov 6, 2024 at 0:32

1 Answer 1

1

Your code had numerous errors, I have corrected these as shown below:

import arcpy

# Set the workspace
arcpy.env.workspace = r"c:\scratch" # Use a correct full path
arcpy.env.overwriteOutput = True

# Sets the Spatial Reference
sr = arcpy.SpatialReference(4326) #WGS84

# Create a polygon feature class
fc = "polygon_file1.shp"
arcpy.CreateFeatureclass_management(arcpy.env.workspace, fc, "POLYGON", spatial_reference=sr)

# Create polygon object and write it to the feature class
def insert_polygon(x1, y1, x2, y2, x3, y3, x4, y4):
    # Create an Array
    arr = arcpy.Array([arcpy.Point(x1, y1), arcpy.Point(x2, y2), arcpy.Point(x3, y3), arcpy.Point(x4,y4), arcpy.Point(x1,y1)])
    polygon = arcpy.Polygon(arr, sr) # Build polygon from array and set spatial reference.
    with arcpy.da.InsertCursor(fc, ["SHAPE@"]) as cursor:
        cursor.insertRow([polygon]) # Use correct spelling
    return

insert_polygon(-92.421457,39.447925,-92.419858,39.448451,-92.417868,39.446918,-92.421349,39.446094)
insert_polygon(-92.413072,39.446835,-92.413072,39.447879,-92.412267,39.446073,-92.416441,39.445087)
insert_polygon(-92.417900,39.449872,-92.412493,39.451015,-92.411978,39.449888,-92.417299,39.448778)
print("done")

Results

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.