spatial

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// EarthRadius Approximate radius of Earth
	EarthRadius = 6370986.884258304
)

Variables

This section is empty.

Functions

func CalcProjection

func CalcProjection(line s2.Polyline, point s2.Point) (projected s2.Point, fraction float64, next int)

CalcProjection Returns projection on line and fraction for point (spherical geometry)

line - s2.Polyline
point - s2.Point

projected - projection of point on line
fraction - number in [0;1], describes how far projected point from first point of polyline
next - index of the next vertex after the projected point

func CalcProjectionEuclidean

func CalcProjectionEuclidean(line s2.Polyline, point s2.Point) (projected s2.Point, fraction float64, next int)

CalcProjectionEuclidean Returns projection on line and fraction for point (Euclidean/planar geometry)

line - s2.Polyline (using Vector.X/Y as Euclidean coordinates)
point - s2.Point (using Vector.X/Y as Euclidean coordinates)

projected - projection of point on line
fraction - number in [0;1], describes how far projected point from first point of polyline
next - index of the next vertex after the projected point

func ExtractCutUpFrom

func ExtractCutUpFrom(polyline s2.Polyline, projected s2.Point, projectedIdx int) (s2.Polyline, s2.Polyline)

ExtractCutUpFrom cuts geometry between neighbor of the projected point index in the polyline and last point

func ExtractCutUpTo

func ExtractCutUpTo(polyline s2.Polyline, projected s2.Point, projectedIdx int) (s2.Polyline, s2.Polyline)

ExtractCutUpTo cuts geometry between very first point and neighbor of the projected point index in the polyline

func GeoJSONToS2PointFeature

func GeoJSONToS2PointFeature(pts *geojson.Geometry) (s2.Point, error)

GeoJSONToS2PointFeature Returns s2.Point representation of *geojson.Geometry (of Point type)

func GeoJSONToS2PolylineFeature

func GeoJSONToS2PolylineFeature(pts *geojson.Geometry) (*s2.Polyline, error)

GeoJSONToS2PolylineFeature Returns *s2.Polyline representation of *geojson.Geometry (of LineString type)

func NewEuclideanS2Point

func NewEuclideanS2Point(x, y float64) s2.Point

NewEuclideanS2Point Returns s2.Point with raw Euclidean coordinates (not normalized) Use this when you need s2.Point for EuclideanStorage without GeoPoint wrapper. Note: s2.PointFromCoords normalizes to unit sphere, which would distort Euclidean coordinates.

func S2PointToGeoJSONFeature

func S2PointToGeoJSONFeature(pt *s2.Point) *geojson.Feature

S2PointToGeoJSONFeature Returns GeoJSON representation of *s2.Point

func S2PolylineToGeoJSONFeature

func S2PolylineToGeoJSONFeature(pts s2.Polyline) *geojson.Feature

S2PolylineToGeoJSONFeature Returns GeoJSON representation of *s2.Polyline

func WKTToS2PointFeature added in v0.10.0

func WKTToS2PointFeature(wkt string) (s2.Point, error)

WKTToS2PointFeature parses WKT POINT and returns s2.Point Expected format: POINT(lon lat)

func WKTToS2PolylineFeature added in v0.10.0

func WKTToS2PolylineFeature(wkt string) (*s2.Polyline, error)

WKTToS2PolylineFeature parses WKT LINESTRING and returns *s2.Polyline Expected format: LINESTRING(lon1 lat1, lon2 lat2, ...)

Types

type Edge

type Edge struct {
	*s2.Polyline
	Weight float64
	ID     int64
	Source int64
	Target int64
}

Edge Representation of segment of road (edge in graph)

ID - unique identifier
Source - identifier of source vertex
Target - identifier of target vertex
Weight - cost of moving on edge (usually it is length or time)
Polyline - geometry of edge, pointer to s2.Polyline (wrapper)

type EuclideanStorage

type EuclideanStorage struct {
	// contains filtered or unexported fields
}

EuclideanStorage Spatial datastore for Euclidean/Cartesian coordinates

func NewEuclideanStorage

func NewEuclideanStorage() *EuclideanStorage

NewEuclideanStorage Returns pointer to created EuclideanStorage

func (*EuclideanStorage) AddEdge

func (storage *EuclideanStorage) AddEdge(edgeID uint64, edge *Edge) error

AddEdge Add edge (polyline) to storage

func (*EuclideanStorage) FindInRadius

func (storage *EuclideanStorage) FindInRadius(pt s2.Point, radiusMeters float64) (map[uint64]float64, error)

FindInRadius implements Storage interface For EuclideanStorage: uses pt.Vector.X/Y as Cartesian coordinates

func (*EuclideanStorage) FindNearest added in v0.11.0

func (storage *EuclideanStorage) FindNearest(pt s2.Point, n int) ([]NearestObject, error)

FindNearest implements Storage interface using iterative radius expansion For EuclideanStorage: uses pt.Vector.X/Y as Cartesian coordinates

func (*EuclideanStorage) FindNearestInRadius

func (storage *EuclideanStorage) FindNearestInRadius(pt s2.Point, radiusMeters float64, n int) ([]NearestObject, error)

FindNearestInRadius implements Storage interface For EuclideanStorage: uses pt.Vector.X/Y as Cartesian coordinates

func (*EuclideanStorage) GetEdge

func (storage *EuclideanStorage) GetEdge(edgeID uint64) *Edge

GetEdge Returns edge by ID from storage

type GeoPoint

type GeoPoint struct {
	s2.Point
	// contains filtered or unexported fields
}

GeoPoint Wrapper around of s2.Point

Needs additional field "srid" to determine what algorithm has to be used to calculate distance between objects
SRID = 0, Euclidean distance
SRID = 4326 (WGS84), Distance on sphere

func NewEuclideanPoint

func NewEuclideanPoint(x, y float64) *GeoPoint

NewEuclideanPoint Returns pointer to created GeoPoint with SRID = 0 Uses raw r3.Vector to preserve exact Cartesian coordinates. Note: s2.PointFromCoords normalizes to unit sphere, which would distort Euclidean coordinates.

func NewWGS84Point

func NewWGS84Point(lon, lat float64) *GeoPoint

NewWGS84Point Returns pointer to created GeoPoint with SRID = 4326

func (*GeoPoint) DistanceTo

func (gp *GeoPoint) DistanceTo(gp2 *GeoPoint) float64

DistanceTo Compute distance between two points.

Algorithm of distance calculation depends on SRID.
SRID = 0, Euclidean distance
SRID = 4326 (WGS84), Distance on sphere

func (*GeoPoint) GeoJSON

func (gp *GeoPoint) GeoJSON() *geojson.Feature

GeoJSON Returns GeoJSON representation of GeoPoint

func (*GeoPoint) SRID

func (gp *GeoPoint) SRID() int

SRID Returns SRID of point

func (*GeoPoint) SetSRID

func (gp *GeoPoint) SetSRID(srid int)

SetSRID Sets SRID for point

func (*GeoPoint) String

func (gp *GeoPoint) String() string

String Pretty print

type NearestObject

type NearestObject struct {
	EdgeID     uint64
	DistanceTo float64
}

NearestObject Nearest object to given point

EdgeID - unique identifier
DistanceTo - distance to object

type S2Storage

type S2Storage struct {
	*btree.BTree
	// contains filtered or unexported fields
}

S2Storage Spatial datastore

storageLevel - level for S2
edges - map of edges
BTree - b-tree (wraps)

func NewS2Storage

func NewS2Storage(storageLevel int, degree int) *S2Storage

NewS2Storage Returns pointer to created S2Storage

storageLevel - level for S2
degree - degree of b-tree

func (*S2Storage) AddEdge

func (storage *S2Storage) AddEdge(edgeID uint64, edge *Edge) error

AddEdge Add edge (polyline) to storage

edgeID - unique identifier
edge - edge

func (*S2Storage) FindInRadius

func (storage *S2Storage) FindInRadius(pt s2.Point, radiusMeters float64) (map[uint64]float64, error)

FindInRadius implements Storage interface

func (*S2Storage) FindNearest added in v0.11.0

func (storage *S2Storage) FindNearest(pt s2.Point, n int) ([]NearestObject, error)

FindNearest implements Storage interface using iterative cell expansion. Expands search from center cell outward until n edges are found. Uses incremental frontier expansion to avoid recalculating BFS on each ring.

func (*S2Storage) FindNearestInRadius

func (storage *S2Storage) FindNearestInRadius(pt s2.Point, radiusMeters float64, n int) ([]NearestObject, error)

FindNearestInRadius implements Storage interface

func (*S2Storage) GetEdge

func (storage *S2Storage) GetEdge(edgeID uint64) *Edge

GetEdge Returns edge by ID from storage

func (*S2Storage) NearestNeighborsInRadius

func (storage *S2Storage) NearestNeighborsInRadius(pt s2.Point, radius float64, n int) ([]NearestObject, error)

NearestNeighborsInRadius Returns edges in radius with max objects restriction (KNN)

pt - s2.Point
radius - radius of search
n - first N closest edges

func (*S2Storage) SearchInRadius

func (storage *S2Storage) SearchInRadius(pt s2.Point, radius float64) (map[uint64]float64, error)

SearchInRadius Returns edges in radius

pt - s2.Point
radius - radius of search

func (*S2Storage) SearchInRadiusLonLat

func (storage *S2Storage) SearchInRadiusLonLat(lon, lat float64, radius float64) (map[uint64]float64, error)

SearchInRadiusLonLat Returns edges in radius

lon - longitude
lat - latitude
radius - radius of search

type Storage

type Storage interface {
	// AddEdge adds an edge to the storage
	AddEdge(edgeID uint64, edge *Edge) error

	// GetEdge returns an edge by ID
	GetEdge(edgeID uint64) *Edge

	// FindInRadius searches for edges within a radius from point
	// For spherical: uses s2.Point on unit sphere
	// For Euclidean: uses s2.Point.Vector.X/Y as Cartesian coordinates
	// Returns map of edge ID to distance
	FindInRadius(pt s2.Point, radiusMeters float64) (map[uint64]float64, error)

	// FindNearestInRadius returns the N nearest edges within a radius
	// For spherical: uses s2.Point on unit sphere
	// For Euclidean: uses s2.Point.Vector.X/Y as Cartesian coordinates
	FindNearestInRadius(pt s2.Point, radiusMeters float64, n int) ([]NearestObject, error)

	// FindNearest returns the N nearest edges using iterative cell expansion
	// No radius limit - expands search until N edges are found or maxRings reached
	// For spherical: uses s2.Point on unit sphere
	// For Euclidean: uses s2.Point.Vector.X/Y as Cartesian coordinates
	FindNearest(pt s2.Point, n int) ([]NearestObject, error)
}

Storage is the interface for spatial storage implementations

func NewStorage

func NewStorage(storageType StorageType, opts ...StorageOption) Storage

NewStorage creates a new Storage based on provided options

type StorageOption

type StorageOption func(*StorageOptions)

StorageOption is a functional option for configuring Storage

func WithBTreeDegree

func WithBTreeDegree(degree int) StorageOption

WithBTreeDegree sets the B-tree degree

func WithStorageLevel

func WithStorageLevel(level int) StorageOption

WithStorageLevel sets the S2 cell level for spherical storage

type StorageOptions

type StorageOptions struct {
	StorageType  StorageType
	StorageLevel int // S2 cell level for spherical storage
	BTreeDegree  int // B-tree degree
}

StorageOptions holds configuration for creating a Storage

type StorageType

type StorageType int

StorageType represents the type of spatial storage

const (
	// StorageTypeSpherical uses S2 geometry for WGS84 coordinates (lon/lat)
	StorageTypeSpherical StorageType = iota
	// StorageTypeEuclidean uses Euclidean geometry for Cartesian coordinates (future)
	StorageTypeEuclidean
)

type Vertex

type Vertex struct {
	*s2.Point
	ID int64
}

Vertex Representation of node on a road (vertex in graph)

ID - unique identifier (user defined, should be contained in parent graph)
Point - geometry of vertex, pointer to s2.Point (wrapper)

Jump to

Keyboard shortcuts

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