10

I am trying to write a script that creates a new polygon feature class containing a single ( square ) polygon with the following coordinates: ( 0, 0 ), ( 0, 1,000 ), ( 1,000, 0 ), and ( 1,000, 1,000 ).


import arcpy
import fileinput
import string
import os

from arcpy import env    
env.workspace = "D:/UW/Winter_2016/501/w5_more_arcpy8_9_10/ex8" 
env.overwriteOutput = True

infile = "D:/UW/Winter_2016/501/w5_more_arcpy8_9_10/ex8/coordinatesEx8.txt"

for line in fileinput.FileInput(infile):
    ID, X, Y = string.split(line, " ")
    array.add(arcpy.Point(X, Y))
cursor.insertRow([arcpy.Polygon(array)])
fileinput.close()

outpath = "D:/UW/Winter_2016/501/w5_more_arcpy8_9_10/ex8"
newfc = "newFeatureClassEx8.shp"
arcpy.CreateFeatureclass_management(outpath, newfc, "Polygon")

cursor = arcpy.da.InsertCursor(newfc, ["SHAPE@"])
array = arcpy.Array()

del cursor

coordinatesEx8.txt looks like this below

1 0 1000 
2 1000 0 
3 1000 1000 
4 0 0

Hanging up on this line:

for line in fileinput.FileInput(infile):

Error message:

ValueError: too many values to unpack

any suggestions? I'm at a loss, I thought it was the set up of my .txt file but keeps hanging on that error no matter what. Not sure how to proceed, this is the question before my actual homework question which will be much harder...also originally the for loop was:

for line in fileinput.input(infile):

which gave a different error...

3 Answers 3

12

To:

Write a script that creates a new polygon feature class containing a single ( square ) polygon with the following coordinates: ( 0, 0 ), ( 0, 1,000 ), ( 1,000, 0 ), and ( 1,000, 1,000 ).

I would take the following approach, which is a slight adaptation from the Insert Cursor documentation example.

import arcpy

# Create a polygon geometry
array = arcpy.Array([arcpy.Point(0, 0),
                     arcpy.Point(0, 1000),
                     arcpy.Point(1000, 1000),
                     arcpy.Point(1000, 0)
                     ])
polygon = arcpy.Polygon(array)

# Open an InsertCursor and insert the new geometry
cursor = arcpy.da.InsertCursor(r'C:\path\to\your\geodatabase.gdb\polygon', ['SHAPE@'])
cursor.insertRow([polygon])

# Delete cursor object
del cursor
0
2

In your question it is hard to tell where the exercise you are trying to complete finishes and your code attempt starts, but it is my suspicion that the code below will give you the same error:

import fileinput
import string
import os

infile = "D:/UW/Winter_2016/501/w5_more_arcpy8_9_10/ex8/coordinatesEx8.txt"

for line in fileinput.FileInput(infile):
    ID, X, Y = string.split(line, " ")
fileinput.close()

If so, then you can conclude that you are encountering a Python (IT), rather than ArcPy (GIS) issue, that should be researched/asked at Stack Overflow rather than here.

3
  • I always get the two sites mixed up and what should be posted where, I'll make sure to consider that in the future before posting. Editing the code above, a piece was left in that needed to be deleted.
    – Staley
    Feb 15, 2016 at 5:22
  • 1
    @Staley As a rule of thumb, if it throws an error mentioning ArcPy then it probably belongs here, if not then chances are that it may be a Python error instead. Recognizing where ArcPy ends and Python begins is hard initially but, I think it is an important skill to acquire, because it lets you zero in on where errors are coming from, and thus where they should be researched, that much quicker.
    – PolyGeo
    Feb 15, 2016 at 5:40
  • 1
    I hadn't thought about it like that, pretty smart, I'll definitely use and apply that going forward.
    – Staley
    Feb 15, 2016 at 5:56
0

This script worked for me:

(inspired by previous answers and from examples here: https://pro.arcgis.com/en/pro-app/arcpy/get-started/writing-geometries.htm)

  1. Get the extent of the raster
  2. Save extent values as points (coordinates in [x,y])
  3. Create new feature class to store new geometry
  4. Insert cursor as geometry to new feature class

Script:

# Create polygon from raster extent

import arcpy, os
from arcpy.sa import *   # import Spatial analysis extension

arcpy.CheckOutExtension("spatial")

# Define path to your data
inWD = "C:/Users/myData/rasterExtentToPoly"

# set working environment
arcpy.env.workspace = os.path.join(inWD)
arcpy.env.overwriteOutput = True

# Read input raster
raster = "eu_dem_v11_E40N20.TIF"

# Get raster extent
myRaster = Raster(raster)   # need to read raster as Raster object
myExtent = myRaster.extent

# Store extent coordinates 
xmax = myExtent.XMax
xmin = myExtent.XMin
ymax = myExtent.YMax
ymin = myExtent.YMin

# Store extent values as list of coordinates 
coordinates = [(xmin, ymin),
               (xmin, ymax),
               (xmax, ymax),
               (xmax, ymin)]

# Get coordinate system
sr = arcpy.Describe(raster).spatialReference


# Create new feature class and than insert the polygon geometry 
result = arcpy.management.CreateFeatureclass(inWD,
                                             "rasterExtent",
                                              "POLYGON",
                                              "",
                                              "",
                                              "",
                                              sr) # define projection

# Create feature class
outPolyExtent= result[0]

# Use Insert cursor to add new geometry to feature class Write feature to new feature class
with arcpy.da.InsertCursor(outPolyExtent, ['SHAPE@']) as cursor:
    cursor.insertRow([coordinates])

# Return the Spatial Analysis extension 
arcpy.CheckInExtension("Spatial")

Your Answer

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

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