I have a linestring DbGeography object and I want to find some point along the linestring given a fractional value of the total distance. For example, if I give 0.5, I should get the midpoint of the line as a DbGeography object with a point WKT (Well Known Text).
I've tried digging through the DbGeography documentation, but no apparent "easy" method seems to exist.
I would think this method would be similar to the Google Maps JS API spherical geometry interpolate method. Any suggestions?
I've done this before using the following approach:
iterate through the LineString's points until you find the pair of adjacent points between which the target point must lie (the target distance is either given or can be easily computed from the distance fraction you mention); then
use
DbGeography.Buffer
on each of the two points, providing"remaining distance to the target" + "small distance padding"
* with the first and"distance between the two points" - "remaining distance to the target" + "small distance padding"
with the second; thenuse
DbGeography.Intersection
on the two points, obtaining a small polygon, not a point; thenconvert that polygon to a
DbGeometry
object and take itsCentroid
; thenconvert that result point back to a
DbGeography
object if desired.Note that this won't be technically the most precise, because the geometric centroid is not usually identical with the geographic center. However, it's almost certainly good enough in this case because the polygon from which the centroid is taken can be so darn small (e.g. by using 1 meter as the buffer distance padding).
*: The distance padding is provided to try to avoid "near misses" due to rounding errors with floating-point. If we had infinite precision, obviously you'd just take the buffers with no padding and then the intersection would give you the exact point you're looking for.