# Automating calculation of cumulative elevation for multiple line segments (not the profile graph)

I'm using ArcGIS and am trying to figure out how to calculate the cumulative elevation gain (in feet or meters) for over 1,000 line segments representing separate, often unconnected trails. I have a DEM raster and the polylines, so it seems there should be a way without too much headache or expertise, but I'm getting stuck! I've seen the answer to this question if you want to create a profile graph for a line segment, but I don't want to create 1,000 graphs to calculate the amount of cumulative elevation gain for each of these trails.

In the Profile Graph in 3D analyst I can select all my line segments and put them all on one graph so that I can download them in one table for excel, but then the table doesn't identify which trail they belonged to. Stack Profile seems to do the same thing.

Any ideas or hints how to get cumulative elevation gain for each of my 1,000 line segments in an easier way?

• If line goes up 1, down 1, up 1. What gain are you talking about 2 or 0 ? – FelixIP Sep 1 '17 at 4:20
• If line goes up 1, down 1, up 1, then the answer I'm looking for would be 2. Thanks! – Hilary W Sep 3 '17 at 4:59
• In this case it is field calculator or Python script, unless you want to intepolate line, convert verices to points and summarise positives in Excel. Are you prepared to use field calculator expression? – FelixIP Sep 3 '17 at 6:02
• Yes, a field calculator expression would be great! Thanks. – Hilary W Sep 4 '17 at 14:20
• Well this code has calculated elevation gain higher by 4 times for me than the real gain for a cycling route... so not sure, is there something wrong with this code ? I am also modeling with Model Builder and need to calculate the elevation gain for a route. I am wondering whether a simple Surface Length - Shape Length would be an approximately correct calculation for elevation gain? – Aurelija Viluckytė May 7 '18 at 10:58

Here you go:

``````def plusGain(shp):
part=shp.getPart(0); n=len(part); result=0
for i in xrange(n):
p=part.getObject(i);  z=p.Z
if i==0: z0=z
else: result+=max(z-z0,0);z0=z
return result
#----
plusGain( !Shape!)
``````

Note result depends on line direction, table below shows output for 2 lines, one of them being flipped copy of another. 