Find the middle in the path

316 Views Asked by At

I have to find a midpoint in a coordinates set. Let's suppose I have the following GeoJSON with coordinates

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            18.060101866722107,
            59.33226461448578
          ],
          [
            18.0600106716156,
            59.33233028244786
          ],
          [
            18.05976927280426,
            59.332565591603085
          ],
          [
            18.059420585632324,
            59.33292676062448
          ],
          [
            18.05930256843567,
            59.3329869550881
          ],
          [
            18.06055784225464,
            59.333318022731746
          ],
          [
            18.060520291328427,
            59.3333453835497
          ]
        ]
      }
    }
  ]
}

The coordinates are located on the streets as you can see at the image below (I used geojson.io to draw it): enter image description here

I was able to calculate midpoint only between two coordinates, but in described case there are 7 coordinates and each coordinate has to be taken into account during calculation because the midpoint must be located at the route that is build by coordinates.

In order to calculate midpoint between two coordinates I used the following formula:

Bx = cos φ2 ⋅ cos Δλ
By = cos φ2 ⋅ sin Δλ
φm = atan2( sin φ1 + sin φ2, √(cos φ1 + Bx)² + By² )
λm = λ1 + atan2(By, cos(φ1)+Bx)

where φ is latitude, λ is longitude

How can I calculate midpoint for case with several coordinates?

Appreciate for your help!

1

There are 1 best solutions below

2
On BEST ANSWER

There's no such thing as a midpoint between points. There is a midpoint of a segment between to endpoints. And a midpoint of a curved path.

It sounds to me you are travel from point to point and want to find the point where you've traveled half the difference.

Keep tally of distance travelled

Point A: 18.060101866722107, 59.33226461448578. Distance traveled = $0$.

Point B: 18.0600106716156, 59.33233028244786. Distance traveled Between A and B = $1.1237806144690070986243816541936e-4$

Point C: 18.05976927280426, 59.332565591603085. Distance traveled Between B and C = $3.3711093819256007255265270932638e-4$ Total so far $4.4948899963946078241509087474574e-4$.

Point D: 18.059420585632324, 59.33292676062448

Distance traveled between C and D = $5.0202171854228338129392415486338e-4$ Totals so far = $9.5151071818174416370901502960912e-4$

And I'm going to quit because this is a pain.

Half the total distance is: $4.7575535909087208185450751480456e-4$

That occurs somewher between point $C$ and $D$.

At point $C$ we have traveled: $4.4948899963946078241509087474574e-4$ and we need to travel $2.6266359451411299439416640058822e-5$ more.

The total distance between $C$ and $D$ is $5.0202171854228338129392415486338e-4$ so we need to travel $\frac {2.6266359451411299439416640058822e-5}{5.0202171854228338129392415486338e-4} =0.05232116157779134771573009976586$ of that distance.

So $x = C_x + (D_x - C_x)\cdot 0.05232116157779134771573009976586$ =

$18.05976927280426 + (18.059420585632324-18.05976927280426)\cdot 0.05232116157779134771573009976586= 18.05975102908640$

And $x = C_y + (D_y - Cy)\cdot 0.05232116157779134771573009976586=$

$59.332565591603085+(59.33292676062448-59.332565591603085)\cdot 0.05232116157779134771573009976586=59.332584488385810301$

Of course this is only one interpretation. A more common interpretation would be the point, $p$ where the sum of the distances from $p$ to points A,B,C, D are least.