Select to view content in your preferred language

Error in creating Shapefile using Python

3277
5
Jump to solution
12-14-2014 03:53 AM
AhsanAbbas
Deactivated User

I need to create the polygon Shape-file in python, by using given coordinates: [(0, 0), (0, ,000), (1,000, 0), and (1,000, 1,000)].

How can i create it, i tried some methods but they don't work ?

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

The gist of what you need is:

outFC = #path to shapefile, ending in .shp

coords = [[0.0, 0.0],[0.0, 1.0],[1.0, 1.0],[1.0, 0.0]]
arr = arcpy.Array([arcpy.Point(*coord) for coord in coords])
pn = arcpy.Polygon(arr)
arcpy.CopyFeatures_management(pn, outFC)

View solution in original post

5 Replies
JoshuaBixby
MVP Esteemed Contributor

(When I first wrote this reply there was a code snippet that has since been removed, which is unfortunate because it is easier for others to comment on code and it demonstrates previous efforts to solve the problem.  Although my comments might not make sense to other readers, the OP should understand them.)

Reviewing ArcGIS Help for Create Feature Class (Data Management), especially the code samples, would be helpful.  With the current approach you are taking, the Copy Features (Data Management) tool is more likely what you will need.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The gist of what you need is:

outFC = #path to shapefile, ending in .shp

coords = [[0.0, 0.0],[0.0, 1.0],[1.0, 1.0],[1.0, 0.0]]
arr = arcpy.Array([arcpy.Point(*coord) for coord in coords])
pn = arcpy.Polygon(arr)
arcpy.CopyFeatures_management(pn, outFC)
AhsanAbbas
Deactivated User

Yes i removed the code, because there was some bugs init, and i feel its better to remove it. Well thanks for the help. can you please elaborate the line 4, (*coord) ?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If you look at the Help for Point (arcpy), arcpy.point takes up to five optional arguments with the first two ordered arguments being X and Y coordinates.  The coords list is a list of lists with the lists holding XY coordinate pairs.  In Python, the asterisk or splat operator is used for Unpacking Argument Lists.  Instead of passing each argument explicitly, I used the splat operator to unpack the XY coordinate pairs and pass them together for each point.

Following are three different ways to pass the same XY coordinates to arcpy.point:

coord = [1.0, 1.0]

#passing XY as indexed list elements
arcpy.Point(coord[0], coord[1])

#passing XY as named local variables
X = coord[0]
Y = coord[1]
#or
X, Y = coord
arcpy.Point(X, Y)

#passing XY using splat operator
arcpy.Point(*coord)
AhsanAbbas
Deactivated User

Thanks for help

0 Kudos