geocoding

package
v0.60.5 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheConfig

type CacheConfig struct {
	// L1MaxSize is the maximum number of entries in the in-memory LRU cache.
	// Default: 10000
	L1MaxSize int

	// L1TTL is the time-to-live for L1 cache entries.
	// Default: 24h
	L1TTL time.Duration

	// L2Enabled enables the database-backed persistent cache.
	// Requires a database connection.
	L2Enabled bool
}

CacheConfig controls caching behavior for geocoding results.

type Config

type Config struct {
	// Enabled controls whether geocoding is active.
	Enabled bool

	// Provider configuration for the geocoding API.
	Provider ProviderConfig

	// RateLimit configuration to control API request rates.
	RateLimit RateLimitConfig

	// Cache configuration for L1 (in-memory) and L2 (database) caching.
	Cache CacheConfig
}

Config holds the configuration for the geocoding service.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults.

type Coordinates

type Coordinates struct {
	Latitude  float64
	Longitude float64
	// Accuracy indicates the precision level of the geocoding result.
	// Possible values: "rooftop", "street", "city", "state", "country", "approximate"
	Accuracy string
}

Coordinates represents geographic coordinates returned by a geocoding provider.

type Geocoder

type Geocoder interface {
	// Geocode converts an address to geographic coordinates.
	// Returns nil coordinates if the address cannot be geocoded.
	Geocode(ctx context.Context, address search.Address) (*Coordinates, error)

	// Name returns the provider name for logging and metrics.
	Name() string
}

Geocoder is the interface that geocoding providers must implement.

type GoogleGeocoder

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

GoogleGeocoder implements the Geocoder interface using the Google Maps API.

func NewGoogleGeocoder

func NewGoogleGeocoder(conf ProviderConfig) (*GoogleGeocoder, error)

NewGoogleGeocoder creates a new Google Maps geocoder.

func (*GoogleGeocoder) Geocode

func (g *GoogleGeocoder) Geocode(ctx context.Context, addr search.Address) (*Coordinates, error)

Geocode converts an address to coordinates using the Google Maps API.

func (*GoogleGeocoder) Name

func (g *GoogleGeocoder) Name() string

Name returns the provider name.

type NoOpGeocoder

type NoOpGeocoder struct{}

NoOpGeocoder is a geocoder that does nothing, used when geocoding is disabled.

func (*NoOpGeocoder) Geocode

func (n *NoOpGeocoder) Geocode(ctx context.Context, address search.Address) (*Coordinates, error)

func (*NoOpGeocoder) Name

func (n *NoOpGeocoder) Name() string

type NominatimGeocoder

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

NominatimGeocoder implements the Geocoder interface using OpenStreetMap's Nominatim API. Note: The public Nominatim server has strict usage policies (max 1 request/second). For production use, consider hosting your own Nominatim instance.

func NewNominatimGeocoder

func NewNominatimGeocoder(conf ProviderConfig) (*NominatimGeocoder, error)

NewNominatimGeocoder creates a new Nominatim geocoder. APIKey is not required for Nominatim (it's free and open).

func (*NominatimGeocoder) Geocode

func (g *NominatimGeocoder) Geocode(ctx context.Context, addr search.Address) (*Coordinates, error)

Geocode converts an address to coordinates using the Nominatim API.

func (*NominatimGeocoder) Name

func (g *NominatimGeocoder) Name() string

Name returns the provider name.

type OpenCageGeocoder

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

OpenCageGeocoder implements the Geocoder interface using the OpenCage API.

func NewOpenCageGeocoder

func NewOpenCageGeocoder(conf ProviderConfig) (*OpenCageGeocoder, error)

NewOpenCageGeocoder creates a new OpenCage geocoder.

func (*OpenCageGeocoder) Geocode

func (g *OpenCageGeocoder) Geocode(ctx context.Context, addr search.Address) (*Coordinates, error)

Geocode converts an address to coordinates using the OpenCage API.

func (*OpenCageGeocoder) Name

func (g *OpenCageGeocoder) Name() string

Name returns the provider name.

type ProviderConfig

type ProviderConfig struct {
	// Name of the provider: "opencage", "google", or "nominatim"
	Name string

	// APIKey for authentication with the provider.
	// Can be set via GEOCODING_API_KEY environment variable.
	APIKey string

	// BaseURL allows overriding the default API endpoint.
	// Useful for self-hosted Nominatim instances.
	BaseURL string

	// Timeout for API requests. Default: 10s
	Timeout time.Duration
}

ProviderConfig holds settings for a geocoding provider.

type RateLimitConfig

type RateLimitConfig struct {
	// RequestsPerSecond defines the sustained request rate.
	// Free tier typically allows 1/sec, paid tiers allow up to 40/sec.
	RequestsPerSecond float64

	// Burst allows temporary exceeding of the rate limit.
	Burst int
}

RateLimitConfig controls the rate of geocoding API requests.

type Repository

type Repository interface {
	// Get retrieves coordinates from the cache by key.
	// Returns nil if not found.
	Get(ctx context.Context, cacheKey string) (*Coordinates, error)

	// Set stores coordinates in the cache.
	Set(ctx context.Context, cacheKey string, coords *Coordinates) error
}

Repository defines the interface for the L2 (database) cache.

func NewRepository

func NewRepository(database db.DB) Repository

NewRepository creates a new database repository for geocoding cache.

type Service

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

Service provides geocoding with caching and rate limiting. It manages L1 (in-memory) and L2 (database) caches and applies rate limiting to external API calls.

func NewService

func NewService(logger log.Logger, conf Config, database db.DB) (*Service, error)

NewService creates a new geocoding service with the provided configuration. Returns nil if geocoding is disabled.

func (*Service) GeocodeAddress

func (s *Service) GeocodeAddress(ctx context.Context, addr search.Address) (*Coordinates, error)

GeocodeAddress geocodes a single address with caching and rate limiting. Returns nil if geocoding fails or is disabled.

func (*Service) GeocodeAddresses

func (s *Service) GeocodeAddresses(ctx context.Context, addresses []search.Address) []search.Address

GeocodeAddresses geocodes multiple addresses and returns them with coordinates populated. This method is the primary integration point for entity mapping.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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