tlxy

package
v1.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2025 License: GPL-3.0 Imports: 23 Imported by: 6

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApproxDistance

func ApproxDistance(lonCheck float64, p Point, s Point) float64

ApproxDistance returns the approximate distance in meters between two points.

func ApproxLonMeters

func ApproxLonMeters(p Point) float64

ApproxLatMeters returns the approximate number of meters per degree of latitude at the given point.

func Distance2d

func Distance2d(p1, p2 Point) float64

Distance2d returns the cartesian distance between two points.

func Distance2dLength

func Distance2dLength(line []Point) float64

Distance2dLength returns the cartesian length of line

func DistanceHaversine

func DistanceHaversine(a, b Point) float64

DistanceHaversine returns the Haversine distance between two points.

func EncodePolyline

func EncodePolyline(coords []Point) []byte

func GeojsonToPolylines added in v1.0.0

func GeojsonToPolylines(fc geojson.FeatureCollection, w io.Writer, idKey string, keys []string, polylineScalePow int) error

func Length2d

func Length2d(line []Point) float64

Length2d returns the cartesian length of line

func LengthHaversine

func LengthHaversine(line []Point) float64

LengthHaversine returns the Haversine approximate length of a line.

func LineContains

func LineContains(a []Point, b []Point) bool

func LineEquals

func LineEquals(a []Point, b []Point) bool

func LineFlatCoords

func LineFlatCoords(line []Point) []float64

func LineRelativePositions

func LineRelativePositions(line []Point, points []Point) []float64

LineRelativePositions finds the relative position of the closest point along the line for each point. TODO: use Haversine

func LineRelativePositionsFallback

func LineRelativePositionsFallback(line []Point) []float64

LineRelativePositionsFallback returns the relative position along the line for each point. TODO: use Haversine

func LineSimilarity

func LineSimilarity(a []Point, b []Point) (float64, error)

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 NewApprox

func NewApprox(p Point) Approx

NewApprox returns a new Approx distance helper.

func (*Approx) ApproxDistance

func (a *Approx) ApproxDistance(p Point, s Point) float64

ApproxDistance returns the approximate distance in meters between two points.

func (*Approx) LatMeters

func (a *Approx) LatMeters() float64

LatMeters returns the approximate number of meters per degree of latitude.

func (*Approx) LonMeters

func (a *Approx) LonMeters() float64

LonMeters returns the approximate number of meters per degree of longitude.

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 GeomCache

type GeomCache interface {
	GetStop(string) Point
	GetShape(eid string) []Point
}

GeomCache is an interface for returning stop and shape points.

type Line

type Line []Point

type LineM

type LineM struct {
	Coords []Point
	Data   []float64
}

type Point

type Point struct {
	Lon float64
	Lat float64
}

func CutBetweenPoints

func CutBetweenPoints(line []Point, from Point, to Point) []Point

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

func CutBetweenPositions(line []Point, dists []float64, startDist float64, endDist float64) []Point

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 DecodePolyline(p []byte) ([]Point, error)

func DecodePolylineString added in v1.0.0

func DecodePolylineString(p string) ([]Point, error)

func LineClosestPoint

func LineClosestPoint(line []Point, point Point) (Point, int, float64)

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

func SegmentClosestPoint(a, b, p Point) (Point, float64)

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.

func (*Point) String

func (p *Point) String() string

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

func (*PolylinesCommand) Run added in v1.0.0

func (c *PolylinesCommand) Run(ctx context.Context) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL