Can not create polygon from text file with ID , X, Y, values in Python

8074
12
05-18-2016 12:19 AM
SadanandacharB1
New Contributor III


Hi Everyone,

  I am new to Python, I am creating one empty feature class using python code and trying to create polygon using text file which is having ID, X, Y values as below,

my text file points.txt and the code is as follows

1 542935 1619969

2 543015 1619964

3 543079 1619924

4 543095 1619896

5 543107 1619816

6 543099 1619768

7 543067 1619669

8 543047 1619629

9 543007 1619593

10 542979 1619577

11 542923 1619569

12 542883 1619577

13 542810 1619625

14 542738 1619649

15 542698 1619701

16 542690 1619733

17 542699 1619773

18 542719 1619821

19 542775 1619893

20 542883 1619953

21 542935 1619969

>>> import arcpy, fileinput, string, os
>>> from arcpy import env
>>> env.workspace = "D:/Data"
>>> inputfile = "D:/Data/points.txt"
>>> fc = "newpoly.shp"
>>> arcpy.CreateFeatureclass_management("D:/Data", fc, "polygon")
<Result 'D:\\Data\\newpoly.shp'>
>>> cursor = arcpy.da.InsertCursor(fc, ["SHAPE@"])
>>> array = arcpy.Array()
>>> point = arcpy.Point()
>>> for line in fileinput.input(inputfile):
...     point.ID, point.X, point.y = string.split(line, " ")
...     array.add(arcpy.Point(X, Y))
...     cursor.insertRow([arcpy.Polygon(array)])
...     fileinput.close()
...     del cursor

But it is unable to create polygon feature, after running the code "newpoly.shp" is empty with values like

FID Shape Id

0   Polygon 0

where am i wrong?, why it is not writing geometry, Please help me

Thanks You

Sadananad

0 Kudos
12 Replies
DanPatterson_Retired
MVP Emeritus

Can you format your code so indentation can be checked.

Code Formatting... the basics++

  In any event, are you saying that you couldn't see a polygon or that you were expecting to see the point number because you won't see them.  Are the points in clockwise order? Is it a simple polygon?  And you might want to try CopyFeatures rather than CreateFeatureClass

...

arcpy.Polygon(
arcpy.Array([arcpy.Point(*coords) for coords in feature])))

,,,

Copy Features—Help | ArcGIS for Desktop vx

Create Feature Class—Help | ArcGIS for Desktop

0 Kudos
SadanandacharB1
New Contributor III

Thank You So Much

0 Kudos
NeilAyres
MVP Alum

It looks like you are closing your input text file after reading the first line.

Put the .close outside of the loop.

I believe that the geometry constructor sorts out the directionality of the coordinate loop first before writing the geometry.

SadanandacharB1
New Contributor III

Thank You So Much

0 Kudos
NeilAyres
MVP Alum

And the del cursor

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

First, you should really be working with spatial references.  As it stands now, you are creating polygons and feature classes/shape files that have no spatial references.

Beyond what Neil Ayres​ covered, i.e., closing the file after one point is read, I am surprised the code runs completely.  Specifically, the following line should generate an error:

array.add(arcpy.Point(X, Y))

You have not defined X or Y anywhere in the code you have posted, so it should generate a NameError.  Since you have already defined a Point and set its ID, X, and Y attributes; all you need to do is pass the point into the array:

array.add(point)

Your current code will not populate an "ID" field seperate from the FID for two reasons:

  • Your feature class only has an auto-generated ID field (FID) and a shape field.
  • Even if your feature class had an additional "ID" field, you have defined your insert cursor with only the shape field.  The FID field will be created automatically for you, but no other fields will be populated.
SadanandacharB1
New Contributor III

Thank You So Much

0 Kudos
BlakeTerhune
MVP Regular Contributor

I think I did it like this

spatial_ref = # Define Spatial referene here
point = arcpy.Point(X, Y)
point_geometry = arcpy.PointGeometry(point, spatial_ref)

Then you can use point_geometry with your insert cursor (which should be opened with the ID field as well as SHAPE@) to create the feature with the ID.

curtvprice
MVP Esteemed Contributor

There are also some good examples in the help, I'd stay closer to their recipe

Writing geometries—Help | ArcGIS for Desktop

Also see some of my tribulations here

Creating polygons with holes using ArcPy