Select to view content in your preferred language

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

2883
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
ScottChang
Deactivated User
Hi Jake,
I did the Folder Connection as you instructed and I saw the C:\TEMP\BS_Test.gdb folder under the Folder Connections.  Then I loaded the script to the Python Window of my ArcGIS 10.0, and executed it.  I got the same error.  Please help and advise what I should do to resolve this problem.

Thanks,
Scott Chang
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Your script has the path of your geodatabase as:

C:\TEMP\WritingGeometries\BS_Test.gdb

Your previous post states that your path is C:\TEMP\BS_Test.gdb.  If this is so, you'll have to change the path in your script.
0 Kudos
ScottChang
Deactivated User
Hi Jake,

I corrected the thing you said and I executed the corrected script. I ran OK. But Polyline_fc in the TOC does not the polyline in the .mxd Map - I did "Zoom to Layer", and still no polyline appeared because the attribute table is empty. What should I do to get the polyline to appeart on the Map? 

Thanks,
Scott Chang
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Scott,

Attached is a python script and toolbox.  Instructions:

1.  Unzip the zip file
2.  Open ArcMap
3.  In the catalog window go to where you unzipped the data
4.  Expand the toolbox > right-click on the Script > Properties
5.  Click on the Source tab > browse to the python script that was included with the zip file
6.  Click OK
7.  Double-click on the script
8.  Specify an input text file, and an output feature class
9.  You can optionally specify a template feature class
0 Kudos
ScottChang
Deactivated User
Hi Jake,
I have studied your instructions, script1.py and logic behind the steps of doing infile=arcpy.GetParameterAsText(0), fcname= arcpy.GetParameterAsText(1), and template=arcpy.GetParameterAsText(2). I think I understood what you said.  I did Step 1 to unzip your zip file and saved script1.py (name changed to WritingGeometries_JakeSkinner.py) and Writing Geometries.tbx in my C:\TEMP\WritingGeometries folder.  I did Steps 2 and 3. I tried to do Step 4, but I don't know how to do "Expend the toolbox > right-click on the Script > Properties", because I do not know how to "expend"  the script1.py (name changed to WritingGeometries_JakeSkinner.py) in my Catalog window - (I don't know how to attach the Word document that has my Screenshot of Map in this post of Forums. How did you attach your zipped file to your last post?).   Please kindly help and give me more detailed instructions to do Step 4.
Thanks,
Scott Chang
0 Kudos
JakeSkinner
Esri Esteemed Contributor
I created the previous toolbox in 10.1.  Try this one that was created in 10.0.[ATTACH=CONFIG]17798[/ATTACH]
0 Kudos
ScottChang
Deactivated User
Hi Jake, Thanks for your new zip file.
I saved your Writing Geometries 2.tbx in my C:\Temp|Writing Geometries folder. I completed Steps 1-3. I am doing Step 4 - I see the Writing Geometries 2.tbx with the "Text File to Line" script in that folder of Catalog window. I have the "Text File to Line Properties" now.  I specified input text file, output feature class and template feature class. Do I have to change any thing else in the "Parameters" place?  How can I execute the "Text File to Line" scripy? I have the PythonWin version 2.6.5 installed yesterday. Should I debug the "Text File to Line" scripy in the PythonWin version 2.6.5 first, then execute it?  What are advantages in executing the Python script in the PythonWin version 2.6.5 over the Python Winmdow of ArcGIS 10.0?

Note: I don't know how to attach my Word document that has the Screenshots in the thread of this post/Forums.  Please enlighten me how to attach my Word document in this thread.

Thanks again,
Scott Chang
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Just double-click on the script in the catalog window and you will be presented with a dialog to specify your text file, output feature class, and template feature class. 

To upload a screen shot, click on the Image icon.
0 Kudos
ScottChang
Deactivated User
Hi Jake,
I did what you instrucked. I got Error 000576 - see the attached file.  What should I do to fix the problem?
Thanks,
Scott Chang
0 Kudos