Documentation
¶
Overview ¶
Package geosegment provides utilities for working with geographical points, segments, and distance calculations.
Example usage:
seg := geosegment.Segment{
Start: geosegment.Point{Lat: -6.2, Lng: 106.816666}, // Jakarta
End: geosegment.Point{Lat: -6.914744, Lng: 107.609810}, // Bandung
}
fmt.Printf("Length: %.2f km\n", seg.Length())
This package supports: - Haversine distance calculation - Segment length & midpoint - Extensible unit conversion (km, miles, nautical miles in v2+)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Distance ¶
Distance returns the distance between two points in meters.
Parameters:
- a: the first point (longitude, latitude) in degrees
- b: the second point (longitude, latitude) in degrees
Returns:
- float64: the distance between the two points in meters
func Length ¶
func Length(ls orb.LineString) float64
Length returns the length of a LineString in meters.
Parameters:
- ls: the orb.LineString representing the line (polyline or route)
Returns:
- float64: the length of the line in meters
func SliceLine ¶
func SliceLine(a, b orb.Point, ls orb.LineString) orb.LineString
SliceLine returns a subsection of a LineString between two points.
Parameters:
- ls: the original orb.LineString (polyline) to be sliced
- a: the starting point of the slice
- b: the ending point of the slice
Returns:
- orb.LineString: a new LineString representing the segment of ls from the closest point to start to the closest point to end
Notes:
- The function finds the closest points on the line to the provided start and end points using ClosestPointOnLine.
- The order of points is maintained; if start comes after end in the original LineString, the slice is reversed automatically.
- Useful for extracting a route segment from a larger path based on GPS coordinates.
Types ¶
type LineString ¶
type LineString = orb.LineString
type NearestPoint ¶
type NearestPoint struct {
Type string `json:"type"`
Geometry orb.Point `json:"geometry"`
Properties map[string]any `json:"properties"`
}
func NearestPointOnLine ¶
func NearestPointOnLine(pt orb.Point, ls orb.LineString) NearestPoint
NearestPointOnLine finds the nearest point on a LineString to a given point and returns detailed information about the result.
Parameters:
- ls: the orb.LineString representing the line (polyline or route)
- pt: the orb.Point to find the nearest point to
Returns:
- NearestPoint: a struct containing the nearest point, distance, segment index, and cumulative distance along the line.
Notes:
- The function iterates through each segment of the LineString and projects the point onto each segment using ClosestPointOnSegment.
- Distance calculations are done using Haversine, so the results are accurate for geographic coordinates (lat/lon).
- The returned NearestPoint.Properties map includes:
- "dist": distance in meters from the point to the nearest point on the line
- "index": the index of the segment's starting point where the nearest point lies
- "location": cumulative distance along the line to the nearest point
- Useful for snapping GPS points to a route or path and calculating distances along it.
type SnapPoint ¶
type SnapPoint struct {
Geometry orb.Point `json:"geometry"`
Distance float64 `json:"distance"`
Direction float64 `json:"direction"`
}
func SnapToRoad ¶
SnapToRoad snaps a point to the nearest location on a road represented by a LineString, taking into account the context of previous, current, and next points along the path.
Parameters:
- prevPt: the previous point along the path (can be empty for the start of the route)
- currPt: the current point along the path (can be empty for departure points)
- nextPt: the next point along the path (can be empty at the end of the route)
- pt: the point to be snapped to the road
- ls: the orb.LineString representing the road or route
Returns:
- SnapPoint: a struct containing the snapped geometry point, the distance from the original point to the road, and the direction (cumulative distance along the road)
Notes:
- The function slices the road between appropriate points based on the route context (start, departure, arrival, or end-to-end).
- Uses SliceLine to extract the relevant segment and NearestPointOnLine to find the closest point.
- The returned SnapPoint provides:
- Geometry: the snapped orb.Point on the road
- Distance: distance in meters from the original point to the road
- Direction: cumulative distance along the road to the snapped point
- Useful for GPS point snapping in navigation, routing, or map-matching applications.