arcpy.Point()- break down array/list/tuple to use as parameter?

2728
8
Jump to solution
06-08-2017 03:22 PM
JoshuaBrengel
New Contributor III

I have a 2D list: coordlist = [[0.0,0.0], [0.0, 1000.0], [1000.0, 0.0], [1000.0, 1000.0]]

I'd like to figure out a way to add each element of coordlist (e.g. pairs of coordinates- [0.0,0.0]) into the arcpy.Point() class, but arcpy.Point() requires numbers as its inputs (e.g. arcpy.Point(0, 0).  I don't think it can take anything else (e.g. a list in this case).  So, something like arcpy.Point(0, 0) would work fine, but arcpy.Point(coordlist[0]) will give an error since it's a list.

Any ideas on how to break down a list/tuple/array so that you can use it in arcpy.Point() to make point objects?  I tried flattening coordlist and then using a loop, hoping to skip over every other x and y value in the flattened list, but could not figure that code out.

I see this as potentially being pretty useful if you are trying to create a polygon from point objects and you have a large number of potential point objects in a list/array, not just 4 (since you can easily type up 4 point objects). 

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

Or another view...

import arcpy

coordlist = [[0.0,0.0], [0.0, 1000.0], [1000.0, 0.0], [1000.0, 1000.0]]

pnts = []

for pnt in coordlist:
    x, y = pnt
    pnts.append(arcpy.Point(x, y))
    

pnts
Out[5]: 
[<Point (0.0, 0.0, #, #)>,
 <Point (0.0, 1000.0, #, #)>,
 <Point (1000.0, 0.0, #, #)>,
 <Point (1000.0, 1000.0, #, #)>]

View solution in original post

8 Replies
RebeccaStrauch__GISP
MVP Emeritus

There could be a number of ways to do this, but I would start with the sample in the help PointGeometry—Help | ArcGIS Desktop  to "create a point feature class from scratch"

import arcpy

# A list of coordinate pairs
#
pointList = [[1,2],[3,5],[7,3]]

# Create an empty Point object
#
point = arcpy.Point()

# A list to hold the PointGeometry objects
#
pointGeometryList = []

# For each coordinate pair, populate the Point object and create
#  a new PointGeometry
for pt in pointList:
    point.X = pt[0]
    point.Y = pt[1]

    pointGeometry = arcpy.PointGeometry(point)
    pointGeometryList.append(pointGeometry)

# Create a copy of the PointGeometry objects, by using pointGeometryList
#  as input to the CopyFeatures tool.
#
arcpy.CopyFeatures_management(pointGeometryList, "c:/geometry/a.gdb/points")
JoshuaBrengel
New Contributor III

Thank you for your help and the included link Rebecca!!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The most Pythonic way, at least in my eyes, is to use the *expression syntax to unpack your coordinates when passing them to the ArcPy Point constructor:

>>> coordlist = [[0.0,0.0], [0.0, 1000.0], [1000.0, 0.0], [1000.0, 1000.0]]
>>> pointlist = [arcpy.Point(*coord) for coord in coordlist]
>>> pointlist
[<Point (0.0, 0.0, #, #)>, <Point (0.0, 1000.0, #, #)>, <Point (1000.0, 0.0, #, #)>, <Point (1000.0, 1000.0, #, #)>]
>>> ‍‍‍‍‍‍‍‍‍‍

This usage is covered under 5. Expressions - Calls — Python 2.7.13 documentation:

If the syntax *expression appears in the function call, expression must evaluate to an iterable. Elements from this iterable are treated as if they were additional positional arguments; if there are positional arguments x1, ..., xN, and expression evaluates to a sequence y1, ..., yM, this is equivalent to a call with M+N positional arguments x1, ..., xN, y1, ..., yM.

JoshuaBrengel
New Contributor III

Thank you for the explanation Josh (sorry I'm just getting around to it- I found a solution outside geonet).  As a person who is relatively new in arcpy, I found the explanation you included extra helpful. Thanks!!

0 Kudos
DanPatterson_Retired
MVP Emeritus

Or another view...

import arcpy

coordlist = [[0.0,0.0], [0.0, 1000.0], [1000.0, 0.0], [1000.0, 1000.0]]

pnts = []

for pnt in coordlist:
    x, y = pnt
    pnts.append(arcpy.Point(x, y))
    

pnts
Out[5]: 
[<Point (0.0, 0.0, #, #)>,
 <Point (0.0, 1000.0, #, #)>,
 <Point (1000.0, 0.0, #, #)>,
 <Point (1000.0, 1000.0, #, #)>]
JoshuaBrengel
New Contributor III

Sorry I'm just getting around to this.  I was able to find a solution that is very similar to this answer.  Thank you Dan!

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

jbrengel  Sinc eyour issues is resolved, make sure to mark any helpful suggests and one as a correct solution.  If you did something different, you could summarize what you did and mark that as correct, or just make the thread as "assumed answered"  It will help others searching for the answer in the future.  Thanks

0 Kudos
JoshuaBrengel
New Contributor III

Ok, thank you Rebecca.  The answer that I have is very similar to Dan's response, so I marked his response as correct.