0
$\begingroup$

I have a list of coordinates and want to draw lines on a map between these coordinates. These lines may either be paths (GeoPath) or just straight lines (Line). The problem I am facing is that I want to draw two lines between each pair of coordinates, to represent going back and forth, and these two lines should be a different color. An example is this image, https://docplayer.nl/docs-images/69/61593419/images/9-0.jpg, but, as explained above, in my case the two parts of the road should be able to have a different color as well.

I was hoping to be able to draw one line between every pair of coordinates, with this line being divided vertically into two colors, to represent the two lines. I know you can draw a line with two colors, but I only know how to do this with a horizontal division, e.g.:

Graphics[{Thick, Line[{{0, 0}, {2, 1}}, VertexColors -> {Red, Green}]}]

My solution now is to place two lines close to each other, like this:

Graphics[{{Thick, Red, Line[{{0, 0}, {2, 1}}]}, {Thick, Green, Line[{{0, 0.005}, {2, 1.005}}]}}]

but this gives problems if you have to connect multiple lines on a map, as, dependent on the slope of the line, these two might overlap:

Graphics[{{Thick, Red, Line[{{0, 0}, {2, 1}}]}, {Thick, Green, Line[{{0, 0.005}, {2, 1.005}}]}, Thick, Red, Line[{{2, 1}, {2.2, 2}}]}, {Thick, Green, Line[{{2, 1.005}, {2.2, 2.005}}]}}]

An example set of coordinates to tryout with (intersections on the A16 road in the Netherlands):

{GeoPosition[{51.874635307793, 4.570947724013}],GeoPosition[{51.857146702404, 4.59740340268}], GeoPosition[{51.700838317451, 4.649052525846}], GeoPosition[{51.645409145412, 4.692113271953}], GeoPosition[{51.581462919313, 4.724696855408}}

$\endgroup$
2
  • $\begingroup$ So you would like that line and a line parallel to it in a different colour, is that correct? $\endgroup$
    – Carl Lange
    Mar 11, 2021 at 9:57
  • $\begingroup$ @CarlLange Yes, two parallel lines of different colors $\endgroup$ Mar 11, 2021 at 10:00

1 Answer 1

1
$\begingroup$

We can make a parallel line by doing the following:

makeParallel[pts_, dist_] :=
 GeoDestination[#[[1]], {dist, 
     GeoDirection[#[[1]], #[[2]]] - 
      Quantity[90, "AngularDegrees"]}] & /@ 
  Partition[pts, 2, 1, {1, 1}, Last@pts]

used like:

line = {GeoPosition[{51.874635307793, 4.570947724013}], 
  GeoPosition[{51.857146702404, 4.59740340268}], 
  GeoPosition[{51.700838317451, 4.649052525846}], 
  GeoPosition[{51.645409145412, 4.692113271953}], 
  GeoPosition[{51.581462919313, 4.724696855408}]}

GeoGraphics[{
  Blue,
  Line@line,
  Red,
  Line@makeParallel[line, 300]
}, GeoBackground -> None]

enter image description here

Basically, we get each pair of points in the list and calculate their normal (GeoDirection minus 90 degrees), then translate each point by the normal and some arbitrary distance dist with GeoDestination[pt, {dist, normal}].

$\endgroup$
2
  • $\begingroup$ Thank you, this helps a lot! For a lot of lines your function does a perfect job. For some of the sets of coordinates the result is not fully correct and the lines intersect (e.g. for {GeoPosition[{52.350495664864, 4.96232904696}], GeoPosition[{52.328341223555, 4.913557669966}], GeoPosition[{52.338032031812, 4.842070619223}], GeoPosition[{52.397547735184, 4.845495736403}], GeoPosition[{52.42344948688, 4.877876809137}]}. Do you know why this happens, I do not fully understand why it does not work in this case? $\endgroup$ Mar 11, 2021 at 10:58
  • $\begingroup$ Sorry, yes, this happens because of the direction of the line segments (going left rather than right). I'm not sure right this second how best to solve that - I'm sure there's a simple way, but my head isn't screwed on today! I hope the answer so far is helpful :) $\endgroup$
    – Carl Lange
    Mar 11, 2021 at 18:43

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.