Select to view content in your preferred language

ArcGIS 10.0:Using ArcPy classes in Python to create Polylines & Polygons from Points

4316
8
09-06-2012 05:49 AM
ScottChang
Deactivated User
Hi all,
1) This morning, in the Python Window of my ArcGIS 10.0, I played with the ArcPy Class "Point" and got the following 2 points:
>>> import arcpy
>>> pointA = arcpy.Point(2.0, 4.5)
>>> pointB = arcpy.Point(3.0, 7.0)
>>> print pointA
2 4.5 NaN NaN

>>> print pointB
3 7 NaN NaN

>>> 

(i)  What is the "NaN" in the results of the "print" Command?
(ii)  If I want to use the "World" Geographic system for the X,Y-coordinates, how can I specify it?

2) If I want to use these 2 points to make a Polyline, how can I use the Python programming to connect these 2 points to form a Polyline?
    If I add a more point {poinC = arcpy.Point(0,0)}, how can I use the Python programming to form a Triangle from these 3 points?

Please kindly help, give me the ArcPy commands/Python tools to create Polylines and Polygons from Points, and respond.

Thanks,
Scott Chang
Tags (2)
0 Kudos
8 Replies
ChristopherThompson
Frequent Contributor
NaN is reference to something that is 'Not a Number'... why its appearing in that Print statement i'm not sure though, since a 'point' is conceptually not a number, perhaps that is not surprising.

Take a look at this help about Arrays in arcpy - the code sample at the end shows how to create a polyline from a collection of points.

http://resources.arcgis.com/en/help/main/10.1/index.html#/Array/018z0000006n000000/

Good luck!
0 Kudos
MathewCoyle
Honored Contributor
The NaN values are there are placeholders for z and m values. Essentially it is a null.
0 Kudos
ScottChang
Deactivated User
Christopher, Mathew and Fabian, Thanks for your nice responses.
I stydied the materials of Array, Polyline, Polygon, etc. of ArcPy Classes from the HELP of ArcGIS 10.0. In the Python Window of my ArcGIS 10.0, I executed the Code Samples of Array, Polyline and Polygon, and used "arcpy.CopyFeatures_management(inFC,outFC)" to bring the "polylines" shapefile to the mxd Map.  I got my "Polyline" project OK.  But I got an error in my "Polygon" project, when I tried to run the Code Sample of Polygon - see the following:
>>> coordList = [[[1,2], [2,4], [3,7]],
...             [[6,8], [5,7], [7,2], [9,5]]]
...  
... 
>>> point = arcpy.Point()
>>> array = arcpy.Array()
>>> featureList = []
>>> for feature in coordList:
...     for coordPair in feature:
...         point.X = coordPair[0]
...         point.Y = coordPair[1]
...         array.add(point)
...     polyline = arcpy.Polyline(array)
...     array.removeAll()
...     featureList.append(polyline)
... arcpy.CopyFeatures_management(featureList, "c:/TEMP/PythonWindow10_0/polylines.shp")
... 
>>> arcpy.env.workspace=r'c:\TEMP\PythonWindow10_0'
>>> inFC='polylines.shp'
>>> outFC='polylinesArcPy.shp'
>>> arcpy.CopyFeatures_management(inFC,outFC)
<Result 'c:\\TEMP\\PythonWindow10_0\\polylinesArcPy.shp'>
>>> # To create polygon by using the existing points
>>> for feature in coordList:
...     for coordPair in feature:
...         point.X = coordPair(0)
...         point.Y = coordPair(1)
...         array.add(point)
...     array.add(array.getObject(0))
...     polygon = array.Polygon(array)
...     array.removeAll()
...     featureList.append(polygon)
... arcpy.CopyFeatures_management(featureList, "c:/TEMP/PythonWindow10_0/polygons.shp")
... 
Runtime error <type 'exceptions.TypeError'>: 'list' object is not callable

I have no ideas where I made the mistake in the "Polygon" Python scritpt.  Please kindly help and tell me how I can correct this problem.

Thanks,
Scott Chang

P. S. I don't know how to re-use/copy the good Python-ArcPy code statements in the Python Window.  It is too time consuming to type the good Python-ArcPy code statements.  Please advise on this matter.
0 Kudos
MathewCoyle
Honored Contributor
Polygons require at minimum 4 points to create a triangle. The start and end points must be coincident to close the polygon. The polygon object won't "jump" from the last point to the first point to close itself.

Edit: Ignore the above, I see you did that already. Seems like you are adding both polyline and polygon features to the same feature list and trying to copy them out at the same time. That seems to be the most likely issue. If you need to export both the polyline and polygon features you will need two separate copy feature calls.
0 Kudos
FabianBlau
Deactivated User
#1
Little Error in the second loop:
...         point.X = coordPair(0)
...         point.Y = coordPair(1)

Use:
...         point.X = coordPair[0]
...         point.Y = coordPair[1]


#2
write a scripttool for a toolbox.
You do not need any parameters, if you change them in the script.
It is not that much work.
0 Kudos
ScottChang
Deactivated User
Hi Fabian, Thanks for your nice response.
This morning, I re-typed the Sample Code of creating "polygons" in the Python Window of my ArcGIS 10.0:
>>> import arcpy
>>> coordList = [[[1.2], [2,4], [3,7]],
...             [[6,8], [5,7], [7,2], [9,5]]]
... point = arcpy.Point()
... array = arcpy.Array()
... featureList = []
... for feature in coordList:
...     for coordPair in feature:
...         point.X = coordPair[0]
...         point.Y = coordPair[1]
...         array.add(point)
...     array.add(array.getObject(0))
...     polygon = arcpy.Polygon(array)
...     array.removeAll()
...     featureList.apend(polygon)
... arcpy.CopyFeatures_management(featureList, "c:/TEMP/PythonWindow10_0/polygons.shp"
... 
Parsing error <type 'exceptions.SyntaxError'>: invalid syntax (line 17)
>>> 


I forgot to put �??)�?� in the end of the last code statement and I got a syntax error.  It is time consuming and error prone, if I re-type the Python/ArcPy codes from the beginning of my Python-ArcPy script.  I did Google search and I did not get much out from the Google search about writing a scripttool for a toolbox (without parameter)!!! Two questions to ask you: (1) Is any easy way to correct a syntax error of ArcPy-Python in Python Window directly? (2) How can I write a scripttool for a toolbox (without parameter)?  Please kindly help, advise and respond again.

Many Thanks,
Scott Chang
0 Kudos
DanPatterson_Retired
MVP Emeritus

You spelled append wrong

0 Kudos