Hi Keith D ,
If I´m correct in 10.1 you have access to the cursors in the data access module and also the polyline::positionAlongLine method. In that case you could do something like:
import arcpy
def main():
fc = r'D:\Xander\GeoNet\GetSegmentAlongLine\gdb\data.gdb\myPolylines'
fld_start = 'StartValue'
fld_end = 'EndValue'
fc_out = r'D:\Xander\GeoNet\GetSegmentAlongLine\gdb\data.gdb\result'
lst_feats = []
flds = ('SHAPE@', fld_start, fld_end)
with arcpy.da.SearchCursor(fc, flds) as curs:
for row in curs:
polyline = row[0]
start_m = row[1]
end_m = row[2]
segment = getSegmentAlongLine(polyline, start_m, end_m)
lst_feats.append(segment)
arcpy.CopyFeatures_management(lst_feats, fc_out)
def getSegmentAlongLine(polyline, start_m, end_m):
''' get a segment along the line (ArcGIS 10.1)
cut is not available nor is SegmentAlongLine
polyline needs to be projected!
polyline: the input polyline
start_m: start measurement (m)
end_m: end measurement (m)
'''
sr = polyline.spatialReference
length = polyline.length
if start_m < 0:
start_m = 0
if end_m > length:
end_m = length
if start_m > end_m:
start_m, end_m = end_m, start_m
if start_m == end_m:
return None
elif start_m == 0 and end_m == length:
return polyline
else:
lst_points = []
len_acum = 0
pnt_prev = None
m = 0
for part in polyline:
for pnt in part:
if pnt_prev == None:
pnt_prev = pnt
dist = getDist2Points(pnt_prev, pnt, sr)
m_prev = m
m += dist
if in_range(start_m, m_prev, m):
pnt_s = getPointBasedOnRelativePosition(pnt_prev, pnt, sr,
start_m, m_prev, m)
if pnt_s:
lst_points.append(pnt_s.firstPoint)
if in_range_or_equal(m, start_m, end_m):
lst_points.append(pnt)
if in_range(end_m, m_prev, m):
pnt_e = getPointBasedOnRelativePosition(pnt_prev, pnt, sr,
end_m, m_prev, m)
if pnt_e:
lst_points.append(pnt_e.firstPoint)
pnt_prev = pnt
segment = arcpy.Polyline(arcpy.Array(lst_points), sr)
return segment
def getDist2Points(pnt1, pnt2, sr):
try:
pntg1 = arcpy.PointGeometry(pnt1, sr)
pntg2 = arcpy.PointGeometry(pnt2, sr)
dist = pntg1.distanceTo(pntg2)
return dist
except Exception as e:
print 'ERR getDist2Points', e
return 0
def in_range(value, minimum, maximum):
return value > minimum and value < maximum
def in_range_or_equal(value, minimum, maximum):
return value >= minimum and value <= maximum
def getPointBasedOnRelativePosition(pnt_from, pnt_to, sr, m_pnt, m_from, m_to):
try:
line = arcpy.Polyline(arcpy.Array([pnt_from, pnt_to]), sr)
position = (m_pnt - m_from) / (m_to - m_from)
return line.positionAlongLine(position, True)
except Exception as e:
print 'ERR getPointBasedOnRelativePosition', e
return None
if __name__ == '__main__':
main()
Test results:
- in black the input polylines and indicating with the black labels the OBJECTID of the input features
- in green the resulting lines (indicating the direction too)
Based on a featureclass with this attribute table: