Remeasure routes in field calculator using Python

1100
2
07-07-2011 10:37 AM
PaulReichart
New Contributor
Is it possible to remeasure routes in ArcMap 10 using Python in the field calculator? In 9.3 we used the following vbscript to remeasure routes and it works fine. If I understand correctly, in ArcMap 10, vbscript does not work on geometry any more.

dim pms as imsegmentation
dim pLine as IPolyline
set pLine = [SHAPE]
set pms = [SHAPE]
pms.setandinterpolatemsbetween 0, pLine.length * .0006214
dim pg as igeometry
set pg = pms

__esri_field_calculator_splitter__
pg



Thanks.
Tags (2)
0 Kudos
2 Replies
JacobPetrosky
New Contributor III
I do not think it is possible to remeasure routes via the field calculator using python. 

However, you could use a script similar to the following to recalculate m values for an existing route feature class.  I would change the reference to the feature class and to/from fields and run it in IDLE or PythonWin.  It simulates the functionality of the setandinterpolatemsbetween arcobjects command.  

import arcpy
arcpy.env.overwriteOutput=1
inFC = r'C:\GIS\IncidentData\November\982292_Setandinterpolatemsbetween\test.gdb\routes_w_MPs' #Update path to the feature class
toField = "BEGMP1" #Update name of the To Measure Field
fromField = "ENDMP1" #Update name of the From Measure Field
desc = arcpy.Describe(inFC)
shapeField = desc.shapeFieldName
oidField = desc.OIDFieldName
oidQueryName = arcpy.AddFieldDelimiters(inFC, oidField)
g = arcpy.Geometry()
#Create update cursor on input feature class
rows = arcpy.UpdateCursor(inFC)
#Make feature layer from input feature class
arcpy.MakeFeatureLayer_management(inFC, "featureLayer")

#loop through rows in feature layer and select each OID
for row in rows:
    arcpy.SelectLayerByAttribute_management("featureLayer", "New_Selection", oidQueryName + "=" + str(row.getValue(oidField)))
    #create geometry object from each feature row
    geomList = arcpy.CreateRoutes_lr("featureLayer", toField, g, "TWO_FIELDS", fromField, toField)
    #update shape field for input feature class for each row (replace M values)
    row.setValue(shapeField, geomList[0])
    rows.updateRow(row)

arcpy.Delete_management("featureLayer")
del row, rows  

Many thanks to Chris F. in Redlands for creating this.
0 Kudos
JacobPetrosky
New Contributor III
Slight modification to script below:

import arcpy
arcpy.env.overwriteOutput=1
inFC = r"C:\GIS\IncidentData\November\982292_Setandinterpolatemsbetween\UserData\PwkTransportationFDSCopyNovember2011.gdb\RouteCopySubsetNov2" #Update path to the feature class
toField = "MILEMARKER_END" #Update name of the To Measure Field
fromField = "MILEMARKER_BEGIN" #Update name of the From Measure Field
routeID = "ROUTEID" #Update name of Route ID field
desc = arcpy.Describe(inFC)
shapeField = desc.shapeFieldName
print shapeField
oidField = desc.OIDFieldName
print oidField
oidQueryName = arcpy.AddFieldDelimiters(inFC, oidField)
g = arcpy.Geometry()
#Create update cursor on input feature class
rows = arcpy.UpdateCursor(inFC)
#Make feature layer from input feature class
arcpy.MakeFeatureLayer_management(inFC, "featureLayer")

#loop through rows in feature layer and select each OID
for row in rows:
    arcpy.SelectLayerByAttribute_management("featureLayer", "New_Selection", oidQueryName + "=" + str(row.getValue(oidField)))
    #create geometry object from each feature row
    geomList = arcpy.CreateRoutes_lr("featureLayer", routeID, g, "TWO_FIELDS", fromField, toField)
    #update shape field for input feature class for each row (replace M values)
    row.setValue(shapeField, geomList[0])
    rows.updateRow(row)

arcpy.Delete_management("featureLayer")
del row, rows
0 Kudos