Writing Geometries - how do I adapt code to create an M-aware polyline with attribute

2040
1
03-02-2012 08:07 AM
LisaSawyer1
New Contributor
This is an ArcGIS 10 query.  I am attempting to create an M-aware polyline features that is spatially referenced and takes input from coordinates in a text file. 
The text input is in the following format: ID,X,Y,M

I would like to write the ID value to be the Route Reference in the m-aware polyline.
Grateful for any help.


# Import system modules
import arcpy
from arcpy.sa import *
import sys
import fileinput
import string
import os

arcpy.env.overwriteOutput = True

# Variable Declarations:
infile = arcpy.GetParameterAsText(0)
routes = arcpy.GetParameterAsText(1)
template = arcpy.GetParameterAsText(2)
prjFile = r"C:\MODEL\CoordinateSystems\WGS 1984 UTM Zone 31N.prj"
spatialRef = arcpy.SpatialReference(prjFile)

# PROCESS: Create route features
# Routes are M-Aware Polyline features

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

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

   # 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, pnt.M = string.split(line,";")
      print pnt.ID, pnt.X, pnt.Y, pnt.M
      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
Tags (2)
0 Kudos
1 Reply
AndrewVitale3
New Contributor III

It's hard to say specifically without a better understanding of the data, but here's a start.

Are you looking to add the "ID" field from your text file as an attribute in the "routes" feature class attribute table? If so, presumably there is a field in the "template" file that you would like to use? If so, you need to specify those fields in the insert cursor:

# Let's pretend ROUTE_ID is a field already in the template:
cur = arcpy.InsertCursor(routes, ['SHAPE@', 'ROUTE_ID'])
# Then in your loop:
for line in fileinput.input(infile):
    # ... geoprocessing steps ...
    cur.insertRow([feat, ID])‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

If you're looking to add a new field to the attribute table, you would just use arcpy.AddField_management before initializing the cursor.

If you're trying to do something totally different than what I assumed, I'd suggest clarifying your question and code so that people can help you out.

Hope this helps...

0 Kudos