Documentation
¶
Overview ¶
Package tlxy provides spatial indexing for polygons.
Package tlxy provides simple XY geometry helper functions; these are approximate and designed for our specific use cases.
Index ¶
- func ApproxDistance(lonCheck float64, p Point, s Point) float64
- func ApproxLonMeters(p Point) float64
- func Distance2d(p1, p2 Point) float64
- func Distance2dLength(line []Point) float64
- func DistanceHaversine(a, b Point) float64
- func EncodePolyline(coords []Point) []byte
- func GeojsonToPolylines(fc geojson.FeatureCollection, w io.Writer, idKey string, keys []string, ...) error
- func Length2d(line []Point) float64
- func LengthHaversine(line []Point) float64
- func LineContains(a []Point, b []Point) bool
- func LineEquals(a []Point, b []Point) bool
- func LineFlatCoords(line []Point) []float64
- func LineRelativePositions(line []Point, points []Point) []float64
- func LineRelativePositionsFallback(line []Point) []float64
- func LineSimilarity(a []Point, b []Point) (float64, error)
- func PolylinesToGeojson(r io.Reader) (geojson.FeatureCollection, error)
- type Approx
- type BoundingBox
- type GeomCache
- type Line
- type LineM
- type Point
- func CutBetweenPoints(line []Point, from Point, to Point) []Point
- func CutBetweenPositions(line []Point, dists []float64, startDist float64, endDist float64) []Point
- func DecodePolyline(p []byte) ([]Point, error)
- func DecodePolylineString(p string) ([]Point, error)
- func LineClosestPoint(line []Point, point Point) (Point, int, float64)
- func SegmentClosestPoint(a, b, p Point) (Point, float64)
- type PolygonIndex
- type PolylinesCommand
- func (cmd *PolylinesCommand) AddFlags(fl *pflag.FlagSet)
- func (c *PolylinesCommand) CreateFromGeojson() error
- func (c *PolylinesCommand) CreateFromShapefile() error
- func (c *PolylinesCommand) CreateFromZipGeojson() error
- func (cmd *PolylinesCommand) HelpArgs() string
- func (cmd *PolylinesCommand) HelpDesc() (string, string)
- func (c *PolylinesCommand) Parse(args []string) error
- func (c *PolylinesCommand) Run(ctx context.Context) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApproxDistance ¶
ApproxDistance returns the approximate distance in meters between two points.
func ApproxLonMeters ¶
ApproxLatMeters returns the approximate number of meters per degree of latitude at the given point.
func Distance2d ¶
Distance2d returns the cartesian distance between two points.
func Distance2dLength ¶
Distance2dLength returns the cartesian length of line
func DistanceHaversine ¶
DistanceHaversine returns the Haversine distance between two points.
func EncodePolyline ¶
func GeojsonToPolylines ¶ added in v1.0.0
func LengthHaversine ¶
LengthHaversine returns the Haversine approximate length of a line.
func LineContains ¶
func LineEquals ¶
func LineFlatCoords ¶
func LineRelativePositions ¶
LineRelativePositions finds the relative position of the closest point along the line for each point. TODO: use Haversine
func LineRelativePositionsFallback ¶
LineRelativePositionsFallback returns the relative position along the line for each point. TODO: use Haversine
func PolylinesToGeojson ¶ added in v1.0.0
func PolylinesToGeojson(r io.Reader) (geojson.FeatureCollection, error)
Types ¶
type Approx ¶
type Approx struct {
// contains filtered or unexported fields
}
Approx is a helper for approximate distance calculations.
func (*Approx) ApproxDistance ¶
ApproxDistance returns the approximate distance in meters between two points.
type BoundingBox ¶
type BoundingBox struct { MinLon float64 `json:"min_lon"` MinLat float64 `json:"min_lat"` MaxLon float64 `json:"max_lon"` MaxLat float64 `json:"max_lat"` }
BoundingBox is a simple bounding box.
func ParseBbox ¶
func ParseBbox(v string) (BoundingBox, error)
ParseBbox parses a bounding box from a string.
func (*BoundingBox) Contains ¶
func (v *BoundingBox) Contains(pt Point) bool
Contains returns true if the point is within the bounding box.
type Point ¶
func CutBetweenPoints ¶
CutBetweenPoints extracts a portion of a line between two points by finding the closest segments. It takes a line (represented as a slice of Points), and two points (from and to) as input. The function finds the closest segments to both input points and returns a new line that starts at the projection of 'from' on its closest segment and ends at the projection of 'to' on its closest segment. The returned line includes all original vertices between these segments.
Parameters:
- line: []Point - Input line as a slice of Points
- from: Point - Starting point to cut from
- to: Point - Ending point to cut to
Returns:
- []Point - A new slice of Points representing the cut portion of the original line
- nil if the input line has fewer than 2 points
Note: The function assumes the input points are relatively close to the line. The search for the end point starts from the start point's segment to maintain proper ordering.
func CutBetweenPositions ¶
CutBetweenPositions returns a slice of points representing a segment of the input line between the specified start and end distances along the line.
The function takes a line represented as a slice of Points, a slice of cumulative distances along the line (dists), and start/end distances (startDist, endDist) indicating where to cut.
It returns a new slice containing: - An interpolated point at startDist - All original points between start and end positions - An interpolated point at endDist
Returns nil if the cut positions cannot be determined (e.g. distances out of range).
Parameters:
- line: Slice of Points representing the polyline
- dists: Slice of cumulative distances along the line
- startDist: Distance along the line where cut should start
- endDist: Distance along the line where cut should end
Returns:
- Slice of Points representing the cut segment, or nil if cut is not possible
func DecodePolyline ¶
func DecodePolylineString ¶ added in v1.0.0
func LineClosestPoint ¶
LineClosestPoint finds the nearest point on a line (represented as a slice of Points) to a given point. Based on go-geom DistanceFromPointToLineString It returns three values: - The closest point on the line - The index of the line segment containing the closest point (0-based) - The normalized position along the line (0.0 to 1.0) where the closest point lies
The line must contain at least 2 points. If the line has length 0 (single point or empty), it returns the input point, index 0, and position 0.
The calculation uses Haversine distance for geographic coordinates.
Parameters:
- line: A slice of Points representing the line segments
- point: The reference Point to find the closest position to
Returns:
- Point: The closest point on the line
- int: Index of the segment containing the closest point
- float64: Normalized position along the line (0.0 to 1.0)
func SegmentClosestPoint ¶
SegmentClosestPoint calculates the closest point on a line segment AB to a given point P, and returns both the closest point and the distance to it.
Given three points:
- a: Start point of line segment
- b: End point of line segment
- p: Point to find closest position to
Returns:
- Point: The closest point on segment AB to point P
- float64: The distance between point P and the closest point
The algorithm first checks if P is closest to either endpoint. If not, it projects P onto line AB and clamps the result to the segment. Distance calculation assumes a simplified 2D Euclidean space suitable for small distances.
type PolygonIndex ¶ added in v1.0.0
type PolygonIndex struct {
// contains filtered or unexported fields
}
PolygonIndex enables efficient point-in-polygon lookups using an R-tree.
func NewPolygonIndex ¶ added in v1.0.0
func NewPolygonIndex(fc geojson.FeatureCollection) (*PolygonIndex, error)
NewPolygonIndex builds an index from a GeoJSON FeatureCollection. Returns (index, error).
func (*PolygonIndex) NearestFeature ¶ added in v1.0.0
func (pip *PolygonIndex) NearestFeature(pt Point) (*geojson.Feature, int)
Check does a quick search, then does a nearest feature search if no match is found.
func (*PolygonIndex) WithinFeature ¶ added in v1.0.0
func (pip *PolygonIndex) WithinFeature(pt Point) (*geojson.Feature, int)
WithinFeature returns a polygon containing the point, and the number of matching polygons.
type PolylinesCommand ¶ added in v1.0.0
type PolylinesCommand struct { InputType string Input string OutputPath string IDKey string ExtraKeys []string }
func (*PolylinesCommand) AddFlags ¶ added in v1.0.0
func (cmd *PolylinesCommand) AddFlags(fl *pflag.FlagSet)
func (*PolylinesCommand) CreateFromGeojson ¶ added in v1.0.0
func (c *PolylinesCommand) CreateFromGeojson() error
func (*PolylinesCommand) CreateFromShapefile ¶ added in v1.0.0
func (c *PolylinesCommand) CreateFromShapefile() error
func (*PolylinesCommand) CreateFromZipGeojson ¶ added in v1.0.0
func (c *PolylinesCommand) CreateFromZipGeojson() error
func (*PolylinesCommand) HelpArgs ¶ added in v1.0.0
func (cmd *PolylinesCommand) HelpArgs() string
func (*PolylinesCommand) HelpDesc ¶ added in v1.0.0
func (cmd *PolylinesCommand) HelpDesc() (string, string)
func (*PolylinesCommand) Parse ¶ added in v1.0.0
func (c *PolylinesCommand) Parse(args []string) error