Select to view content in your preferred language

ArcGIS10.0:Writing geometries in Python Window- Points with ID,X,Y & IDLE Loading

2868
18
09-11-2012 08:37 AM
ScottChang
Deactivated User
Hi all,
From the Help of ArcGIS 10.0, I have learned the creating of Polylines, Polygons and Multipoints from 2 sets of Points in the Python Window of my ArcGIS 10.0. From the IDLE (Python GUI) of Python 2.5, I can edit the ArcPy-Python scripts and load the corrected, desired scripts to the Python Window for execution to obtain the .shp file and the .mxd Map. 
Now I tried to do the "Writing geometries" presented and provided in the Help of ArcGIS 10.0. I have 2 difficulties in storing/using the Point data in the .txt  file and loading/executing the ArcPy-Python script in the Python Window of my ArcGIS 10.0.
The following is copied from the Help of ArcGIS 10.0:
//////////////////////////////////////////////////////
Writing geometries
ArcGIS 10
Using insert and update cursors, scripts can create new features in a feature class or update existing ones. A script can define a feature by creating a point object, populating its properties, and placing it in an array. That array can then be used to set a feature's geometry. A single geometry part is defined by an array of points, so a multipart feature can be created from multiple arrays of points.

Below is an example of a file to be processed by the script that follows. It contains a point ID and an x- and y-coordinate.

1;-61845879.0968;45047635.4861
1;-3976119.96791;46073695.0451
1;1154177.8272;-25134838.3511
1;-62051091.0086;-26160897.9101
2;17365918.8598;44431999.7507
2;39939229.1582;45252847.3979
2;41170500.6291;27194199.1591
2;17981554.5952;27809834.8945
3;15519011.6535;11598093.8619
3;52046731.9547;13034577.2446
3;52867579.6019;-16105514.2317
3;17160706.948;-16515938.0553

The following example shows how to read a text file containing a series of linear coordinates (as above) and use them to create a new feature class.

# Create a new line feature class using a text file of coordinates.
#   Each coordinate entry is semicolon delimited in the format of ID;X;Y

# Import ArcPy and other required modules
#
import arcpy
from arcpy import env
import fileinput
import string
import os

env.overwriteOutput = True

# Get the coordinate ASCII file
#
infile = arcpy.GetParameterAsText(0)

# Get the output feature class
#
fcname = arcpy.GetParameterAsText(1)

# Get the template feature class
#
template = arcpy.GetParameterAsText(2)

try:
   # Create the output feature class
   #
   arcpy.CreateFeatureclass_management(os.path.dirname(fcname),
                                       os.path.basename(fcname),
                                       "Polyline", template)

   # Open an insert cursor for the new feature class
   #
   cur = arcpy.InsertCursor(fcname)

   # Create an array and point object needed to create features
   #
   lineArray = arcpy.Array()
   pnt = arcpy.Point()

   # Initialize a variable for keeping track of a feature's ID.
   #
   ID = -1
   for line in fileinput.input(infile): # Open the input file
      # set the point's ID, X and Y properties
      #
      pnt.ID, pnt.X, pnt.Y = string.split(line,";")
      print pnt.ID, pnt.X, pnt.Y
      if ID == -1:
         ID = pnt.ID

      # Add the point to the feature's array of points
      #   If the ID has changed, create a new feature
      #
      if ID != pnt.ID:
         # Create a new row or feature, in the feature class
         #
         feat = cur.newRow()

         # Set the geometry of the new feature to the array of points
         #
         feat.shape = lineArray

         # Insert the feature
         #
         cur.insertRow(feat)
         lineArray.removeAll()
      lineArray.add(pnt)
      ID = pnt.ID

   # Add the last feature
   #
   feat = cur.newRow()
   feat.shape = lineArray
   cur.insertRow(feat)
     
   lineArray.removeAll()
   fileinput.close()
   del cur
except Exception as e:
   print e.message

An array of points is not necessary when writing point features. A single point object is used to set the geometry of a point feature.

All geometries are validated before they are written to a feature class. Issues such as incorrect ring orientation and self-intersecting polygons, among others, are corrected when the geometry is simplified before its insertion.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Problem #1: Where should I shave the data of ID, X-coordinate, and Y-coordinate of the points? I tried to save the point data in the .txt file of my Notepad:
ID,X,Y,
1;-61845879.0968;45047635.4861
1;-3976119.96791;46073695.0451
1;1154177.8272;-25134838.3511
1;-62051091.0086;-26160897.9101
2;17365918.8598;44431999.7507
2;39939229.1582;45252847.3979
2;41170500.6291;27194199.1591
2;17981554.5952;27809834.8945
3;15519011.6535;11598093.8619
3;52046731.9547;13034577.2446
3;52867579.6019;-16105514.2317
3;17160706.948;-16515938.0553
:Is this file right to use in the project?

Problem #2:
I tried to use the IDLE (Python GUI) to load the script of "Writing geometries" to the Python Window of my ArcGIS 10.0 - see the following:
... >>> # Create a new line feature class using a text file of coordinates.
... #   Each coordinate entry is semicolon delimited in the format of ID;X;Y
... # Import ArcPy and other required modules
... #
... import arcpy
... from arcpy import env
... import fileinput
... import string
... import os
... env.overwriteOutput = True
... # Get the coordinate ASCII file
... #
... infile = arcpy.GetParameterAsText(0)
... # Get the output feature class
... #
... fcname = arcpy.GetParameterAsText(1)
... # Get the template feature class
... #
... template = arcpy.GetParameterAsText(2)
... try:
...    # Create the output feature class
...    #
...    arcpy.CreateFeatureclass_management(os.path.dirname(fcname),
...                                        os.path.basename(fcname), 
...                                        "Polyline", template)
...    # Open an insert cursor for the new feature class
...    #

I am lost now.  Please kindly help and enlighten me in handling the above-mentioned 2 problems.
Thanks in advance,
Scott Chang
Tags (2)
0 Kudos
18 Replies
JakeSkinner
Esri Esteemed Contributor
Problem #1:

Yes, you can store your coordinates within a text file.

Problem #2:

Change 'infile = arcpy.GetParameterAsText(0)' and 'fcname = arcpy.GetParameterAsText(1)' to the paths of your text file and feature class.  You will then be able to execute this code in IDLE.  Ex:

infile = r"C:\temp\python\Coordinates.txt"

fcname = r"C:\temp\python\Test.gdb\Polyline_fc"
0 Kudos
ScottChang
Deactivated User
Hi Jake, Thanks for your nice, valuable response.
(1) This morning, I tried to type the Python Script of Writing geometries (listed in the Help of ESRI) one code statement by one code statement manually in the Python Window of my ArcGIS 10.0. I typed in the 2 code statements you specified - see the following:
>>> import arcpy
>>> from arcy import env
Runtime error <type 'exceptions.ImportError'>: No module named arcy
>>> from arcpy import env
>>> import fileinput
>>> import string
>>> import os
>>> env.overwriteOutput = True
>>> infile = r"C:\TEMP\WritingGeometries\WritingGeometries_3features.txt"
>>> fcname = r"C:\TEMP\WritingGeometries\Test.gdb\Polyline_fc"
>>> template = arcpy.GetParameterAsText(2) # Scott Chang thinks this code statement is not right for my project!? Jake, what is the right statement for this "template"?


(2) If I used the IDLE (of my Python 2.5) to copy the entire Python script of "Writing geometries" listed in the Help of ArcGIS 10.0 to the Python Window of my ArcGIS 10.0. I got an error "Failed to execute, Parameters are not valid. ERROR 000735 - see the following:[CODE>>> # Create a new line feature class using a text file of coordinates.
... #   Each coordinate entry is semicolon delimited in the format of ID;X;Y
... # Import ArcPy and other required modules
... #
... import arcpy
... from arcpy import env
... import fileinput
... import string
... import os
... env.overwriteOutput = True
... # Get the coordinate ASCII file
... #
... infile = arcpy.GetParameterAsText(0)
... # Get the output feature class
... #
... fcname = arcpy.GetParameterAsText(1)
... # Get the template feature class
... #
... template = arcpy.GetParameterAsText(2)
... try:
...    # Create the output feature class
...    #
...    arcpy.CreateFeatureclass_management(os.path.dirname(fcname),
...                                        os.path.basename(fcname),
...                                        "Polyline", template)
...    # Open an insert cursor for the new feature class
...    #
...    cur = arcpy.InsertCursor(fcname)
...    # Create an array and point object needed to create features
...    #
...    lineArray = arcpy.Array()
...    pnt = arcpy.Point()
...    # Initialize a variable for keeping track of a feature's ID.
...    #
...    ID = -1
...    for line in fileinput.input(infile): # Open the input file
...       # set the point's ID, X and Y properties
...       #
...       pnt.ID, pnt.X, pnt.Y = string.split(line,";")
...       print pnt.ID, pnt.X, pnt.Y
...       if ID == -1:
...          ID = pnt.ID
...       # Add the point to the feature's array of points
...       #   If the ID has changed, create a new feature
...       #
...       if ID != pnt.ID:
...          # Create a new row or feature, in the feature class
...          #
...          feat = cur.newRow()
...          # Set the geometry of the new feature to the array of points
...          #
...          feat.shape = lineArray
...          # Insert the feature
...          #
...          cur.insertRow(feat)
...          lineArray.removeAll()
...       lineArray.add(pnt)
...       ID = pnt.ID
...    # Add the last feature
...    #
...    feat = cur.newRow()
...    feat.shape = lineArray
...    cur.insertRow(feat)
...      
...    lineArray.removeAll()
...    fileinput.close()
...    del cur
... except Exception as e:
...    print e.message
...
Failed to execute. Parameters are not valid.
ERROR 000735: Feature Class Location: Value is required
]
I am lost now.  Please kindly help and tell me how I can resolve the 2 above-mentioned problems.
Many Thanks,
Scott Chang
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Try setting the 'template' variable to an existing feature class.  Ex:

template = r"C:\temp\python\test.gdb\WaterMains"
0 Kudos
ScottChang
Deactivated User
Hi Jake,

Two more questions to ask you: Is the test.gdb existing geodatabase also? or I have to create a new geodatabase for the "test.gdb" and my new feature class?

Please respond again.

Thanks,
Scott Chang
0 Kudos
JakeSkinner
Esri Esteemed Contributor
You don't have to create a new geodatabase.  You can use an existing feature class in an existing geodatabase.  The output feature class created from this script will have the same schema as the feature class you specify for the 'template' variable.
0 Kudos
ScottChang
Deactivated User
Hi Jake,
I managed to revise the Python Script of Writing geometries in the IDLE and then I executed that Python Script in the Python Window of my ArcGIS 10.0:
>>> # Create a new line feature class using a text file of coordinates.
... #   Each coordinate entry is semicolon delimited in the format of ID;X;Y
... # Import ArcPy and other required modules
... #
... import arcpy
... from arcpy import env
... import fileinput
... import string
... import os
... env.overwriteOutput = True
... # Get the coordinate ASCII file
... #
... #infile = arcpy.GetParameterAsText(0)
... infile = r"C:\TEMP\WrintingGeometries\WritingGeometries_3features.txt"
... # Get the output feature class
... # fcname = arcpy.GetParameterAsText(1)
... fcname = r"C:\TEMP\WritingGeometries\BS_Test.gdb\Polyline_fc"
... # Get the template feature class
... #
... # template = arcpy.GetParameterAsText(2)
... template = r"C:\TEMP\WritingGeometries\BS_Test.gdb\bsInFeatureClass"
... try:
...    # Create the output feature class
...    #
...    arcpy.CreateFeatureclass_management(os.path.dirname(fcname),
...                                        os.path.basename(fcname), 
...                                        "Polyline", template)
...    # Open an insert cursor for the new feature class
...    #
...    cur = arcpy.InsertCursor(fcname)
...    # Create an array and point object needed to create features
...    #
...    lineArray = arcpy.Array()
...    pnt = arcpy.Point()
...    # Initialize a variable for keeping track of a feature's ID.
...    #
...    ID = -1 
...    for line in fileinput.input(infile): # Open the input file
...       # set the point's ID, X and Y properties
...       #
...       pnt.ID, pnt.X, pnt.Y = string.split(line,";")
...       print pnt.ID, pnt.X, pnt.Y
...       if ID == -1:
...          ID = pnt.ID
...       # Add the point to the feature's array of points
...       #   If the ID has changed, create a new feature
...       #
...       if ID != pnt.ID:
...          # Create a new row or feature, in the feature class
...          #
...          feat = cur.newRow()
...          # Set the geometry of the new feature to the array of points
...          #
...          feat.shape = lineArray
...          # Insert the feature
...          #
...          cur.insertRow(feat)
...          lineArray.removeAll()
...       lineArray.add(pnt)
...       ID = pnt.ID
...    # Add the last feature
...    #
...    feat = cur.newRow()
...    feat.shape = lineArray
...    cur.insertRow(feat)
...       
...    lineArray.removeAll()
...    fileinput.close()
...    del cur
... except Exception as e:
...    print e.message
... 
Failed to execute. Parameters are not valid.
ERROR 000732: Feature Class Location: Dataset C:\TEMP\WritingGeometries\BS_Test.gdb does not exist or is not supported
ERROR 000732: Template Feature Class: Dataset C:\TEMP\WritingGeometries\BS_Test.gdb\bsInFeatureClass does not exist or is not supported
Failed to execute (CreateFeatureclass).

What did I do wrong in this script? I just created BS_Test.gdb in my ArcCatalog 10.0.  I don't know why I got the error.  Please kindly help and advise.

Thanks,
Scott Chang
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Check to make sure these feature classes exist by running the following:

if arcpy.Exists(r"C:\TEMP\WritingGeometries\BS_Test.gdb\Polyline_fc"):
    print 'true'
else:
    print 'does not exist'
if arcpy.Exists(r"C:\TEMP\WritingGeometries\BS_Test.gdb\bsInFeatureClass"):
    print 'true'
else:
    print 'does not exist'
0 Kudos
ScottChang
Deactivated User
Hi Jake,

In my ArcCatalog 10.0, Home-TEMP
                               WritingGeometris
                                BS_Test..gdb
                                  bsInFeatureClass
                                BS_Tbx1.tbx
                                   bsScript1
But, I checked the C:\TEMP\ folder (via My Computer) and I don't see them in the TEMP folder that were created yesterday!!??  I am lost now. What should I do to fix this problem?

Please  help and respond again.

Thanks,
Scott Chang
0 Kudos
JakeSkinner
Esri Esteemed Contributor
In ArcCatalog, right-click on 'Folder Connection's > Connect to Folder.  Create a connection to your C:\temp directory.  Do you see the geodatabase there?
0 Kudos