Offset label to specific lat/lon?

I have a web app powered on the backend by PyGMT which allows the user to position labels for markers however they want. I have implemented this successfully by tracking the final latitude/longitude of the label as positioned by the user, and using that as the X/Y in the call to figure.text

This works great, as long as I don’t want a line back to the original location. Of course, the figure.text command makes it easy to draw said line using the “offset” parameter, but there’s two problems at the moment:

  1. While I know the original location, and the “new” location, I don’t know how to figure out what offset would place the label in the desired location - to the best of my knowledge (correct me if I am wrong), the offset has to be in “measurement” units such as points, in, or cm, not map units of latitude/longitude.

  2. The new location is provided as the top-left corner of the label, however depending on the label location relative to the original location, that may not be an appropriate anchor point for the line.

So, basically the challenge I have given myself here is to first figure out the most appropriate anchor point to use for each label, and then to figure out an appropriate offset to place the label at the proper point.

I have an idea as to how I can solve #2 on the front end, if needed, and provide a) the proper anchor point, and b) the latitude/longitude of said anchor point to PyGMT for plotting, but that still leaves problem #1 - how to figure out the offset needed to position the text at the desired latitude/longitude.

Any thoughts/suggestions?

It sounds interesting. Do you mind sharing the URL of your web app if it’s publicly available?

  1. While I know the original location, and the “new” location, I don’t know how to figure out what offset would place the label in the desired location - to the best of my knowledge (correct me if I am wrong), the offset has to be in “measurement” units such as points, in, or cm, not map units of latitude/longitude.

Here are my two thoughts:

  1. If you know the parameters that were used to generate the basemap (for example, the -J and -R options in GMT or projection and region in PyGMT), it would be possible to calculate the offsets (in meansure units) of the original and “new” location using mapproject. Then you can append the offset to Figure.text’s offset parameter.
  2. Maybe it’s easier to draw the line between the original and “new” locatio using Figure.plot instead.
1 Like

Pretty sure no one would mind: AVO MapGen Pretty much Alaska centric, and I suspect I still have a few bugs to work out, but it is generally functional. Basically just a web interface for the handful of GMT features that have been requested by the Alaska Volcano Observatory so far.

Cool, didn’t know about that function yet. I’ll have to look into it more, but:

… that’s a very good point. With that, all I have to do is figure out where on each label I want to draw the line to. Then with that information I can easily call plot to draw a line between the two lat/lon points.

Guess I was so focused on the text function that I forgot there were other ways to draw lines… :smile:

Just want to let you know that mapproject is not wrapped in PyGMT yet (get lon,lat from image coordinates (using mapproject) · Issue #1986 · GenericMappingTools/pygmt · GitHub).