3

How can I calculate the straight distance from the start to the end point of a river (red line in image)? I don't know how to do this automatically.

enter image description here

  • 2
    What software are you using? – ArMoraer Apr 29 '16 at 17:58
  • I am using in 90 % ArcMap sometimes qgis. – komy Apr 29 '16 at 18:09
2

You can use Feature Vertices to Points tool with the option to put the points at the Start and End of the line. Then use the Near tool or the Point Distance tool, this will then give you the linear distance between the end and start point. This can be added to a model to automate it.

http://help.arcgis.com/EN/ARCGISDESKTOP/10.0/HELP/index.html#//00170000003p000000

https://geonet.esri.com/thread/43024

Looking for a Tool to Calculate Distance Between Points (Between Layers)

2

It depends on what coordinate system you're using. If you are using a projected coordinate system (with units in meters or feet), you can use the field calculator (use the Python parser) with this expression:

math.hypot(!SHAPE!.firstPoint.X-!SHAPE!.lastPoint.X,
           !SHAPE!.firstPoint.Y-!SHAPE!.lastPoint.Y)

Or if you want distance on the earth's surface (if you're using a geographic coordinate system with decimal degrees), check out the answer to this question for the haversine formula (https://stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points). For this, you would put the haversine function in the code block of the field calculator and call on it in the expression below the code block.

Either way, the thing to use is the !SHAPE!.firstPoint and !SHAPE!.lastPoint (http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-classes/geometry.htm)

EDIT: I just double checked the code and realized it needed some edits before it would work in a field calculator. If you're doing this in ArcMap, add a field (like endDist) of type double, select Python parser, and paste the following into the "Pre-Logic Script Code" window:

def haversine(lon1, lat1, lon2, lat2):
    """
    Calculate the great circle distance between two points 
    on the earth (specified in decimal degrees)
    """
    # convert decimal degrees to radians 
    lon1, lat1, lon2, lat2 = map(math.radians, [lon1, lat1, lon2, lat2])

    # haversine formula 
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2
    c = 2 * math.asin(math.sqrt(a)) 
    r = 6371 # Radius of earth in kilometers. Use 3956 for miles
    return c * r

With the following expression:

haversine(!SHAPE!.firstPoint.X, !SHAPE!.firstPoint.Y, !SHAPE!.lastPoint.X, !SHAPE!.lastPoint.Y)

That will work. Hope that helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.