# pyGMT find optimal path between two geographic coordinates with elevation gain/loss

**URL:** <https://forum.generic-mapping-tools.org/t/pygmt-find-optimal-path-between-two-geographic-coordinates-with-elevation-gain-loss/5527>\
**Category:** PyGMT Q&A\
**Tags:** gmt\
**Created:** [November 14, 2024, 12:40am UTC](https://forum.generic-mapping-tools.org/t/pygmt-find-optimal-path-between-two-geographic-coordinates-with-elevation-gain-loss/5527 "2024-11-14T00:40:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![su5050](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/su5050/32/4568_2.png) [@su5050](https://forum.generic-mapping-tools.org/u/su5050)\
**Post date:** [November 14, 2024, 12:40am UTC](https://forum.generic-mapping-tools.org/t/pygmt-find-optimal-path-between-two-geographic-coordinates-with-elevation-gain-loss/5527/1 "2024-11-14T00:40:21Z")

</div>

I’m new to pyGMT here, but as an experienced Python developer, I’m struggling with the plethora of awesome features available. I’ve created a map of ~60 points along a mountain range. These points represent known locations on journey by foot. I’m trying to connect them with the optimal path and would like to implement A\* or similar which would penalize paths taken over long distances and high elevation deltas while also going around obstacles which travelers would reasonably go around.

- Does pyGMT offer something like this out of the box?
- can pyGMT generate a transect along the “optimal” line instead of a straight line between A&B (as in the below example)?

I’ve based my code off of this example: [Cross-section along a transect — PyGMT](https://www.pygmt.org/latest/gallery/images/cross_section.html#sphx-glr-gallery-images-cross-section-py)

my data frame of points is very simple and looks like: `df[['name', 'latitude', 'longitude']]`  
I’ve only added this snippet below to map each location on the map

```auto
fig.text(
    text=" "+df['name'],
    x=df['longitude'],
    y=df['latitude'],
    font="2p,Helvetica-Bold",
    justify="lm"
)

```

---

<div class="post-metadata">

**Author:** ![bwwjohnson](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bwwjohnson](https://forum.generic-mapping-tools.org/u/bwwjohnson)\
**Post date:** [November 19, 2024, 3:04pm UTC](https://forum.generic-mapping-tools.org/t/pygmt-find-optimal-path-between-two-geographic-coordinates-with-elevation-gain-loss/5527/2 "2024-11-19T15:04:28Z")

</div>

Hi Su,

As far as I know there are no opimization algorithms of this nature in pygmt. Greenspline is the closest thing I can think of but isn’t quite what you’re looking for and it doesn’t look like it’s wrapped by pygmt yet. [greenspline — GMT 6.6.0 documentation](https://docs.generic-mapping-tools.org/dev/greenspline.html)

Pygmt can solve the forward problem. Grdtrack can generate an elevation profile along a track composed of line segments (not just a straight line), and can find critical points along the profile. But implementing the inverse problem from this would be quite the task.

I wonder if you’d have luck plugging in to the API of an application that specialises in routing, like Komoot or Strava.
