Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var UsStateCodes = map[string]string{
"AL": "Alabama", "AK": "Alaska", "AZ": "Arizona", "AR": "Arkansas",
"CA": "California", "CO": "Colorado", "CT": "Connecticut", "DE": "Delaware",
"FL": "Florida", "GA": "Georgia", "HI": "Hawaii", "ID": "Idaho",
"IL": "Illinois", "IN": "Indiana", "IA": "Iowa", "KS": "Kansas",
"KY": "Kentucky", "LA": "Louisiana", "ME": "Maine", "MD": "Maryland",
"MA": "Massachusetts", "MI": "Michigan", "MN": "Minnesota", "MS": "Mississippi",
"MO": "Missouri", "MT": "Montana", "NE": "Nebraska", "NV": "Nevada",
"NH": "New Hampshire", "NJ": "New Jersey", "NM": "New Mexico", "NY": "New York",
"NC": "North Carolina", "ND": "North Dakota", "OH": "Ohio", "OK": "Oklahoma",
"OR": "Oregon", "PA": "Pennsylvania", "RI": "Rhode Island", "SC": "South Carolina",
"SD": "South Dakota", "TN": "Tennessee", "TX": "Texas", "UT": "Utah",
"VT": "Vermont", "VA": "Virginia", "WA": "Washington", "WV": "West Virginia",
"WI": "Wisconsin", "WY": "Wyoming",
"AS": "American Samoa", "DC": "District of Columbia",
"FM": "Federated States of Micronesia", "GU": "Guam",
"MH": "Marshall Islands", "MP": "Northern Mariana Islands",
"PW": "Palau", "PR": "Puerto Rico", "VI": "Virgin Islands",
"AA": "Armed Forces Americas", "AE": "Armed Forces Europe", "AP": "Armed Forces Pacific",
}
UsStateCodes maps US state abbreviations to full names.
Functions ¶
func CountryCount ¶
func CountryCount() int
CountryCount returns the number of unique country codes in the lookup table. Useful for testing and debugging.
func RegenerateCache ¶
func RegenerateCache() error
RegenerateCache forces a reload from raw data files and regenerates the cache. This is useful for updating the embedded cache after downloading fresh data. The raw data files must exist in ./geobed-data/ before calling this function.
After running, compress the cache files with bzip2:
bzip2 -f geobed-cache/*.dmp
func RegionCount ¶
func RegionCount() int
RegionCount returns the number of unique region codes in the lookup table. Useful for testing and debugging.
func ValidateCache ¶
func ValidateCache() error
ValidateCache loads the cache and performs integrity and functional checks. Returns an error if validation fails.
Types ¶
type AdminDivision ¶
type AdminDivision struct {
Code string // Admin1 code (e.g., "TX", "08")
Name string // Full name (e.g., "Texas", "Ontario")
}
AdminDivision represents a first-level administrative division (state, province, etc.)
type CountryInfo ¶
type CountryInfo struct {
Country string
Capital string
Area int32
Population int32
GeonameId int32
ISONumeric int16
ISO string
ISO3 string
Fips string
Continent string
Tld string
CurrencyCode string
CurrencyName string
Phone string
PostalCodeFormat string
PostalCodeRegex string
Languages string
Neighbours string
EquivalentFipsCode string
}
CountryInfo contains metadata about a country from Geonames.
type DataSource ¶
type DataSource struct {
URL string // Download URL
Path string // Local file path
ID DataSourceID // Identifier for processing logic
}
DataSource defines a data source for geocoding data.
type DataSourceID ¶
type DataSourceID string
DataSourceID identifies a data source type.
const ( DataSourceGeonamesCities DataSourceID = "geonamesCities1000" DataSourceGeonamesCountry DataSourceID = "geonamesCountryInfo" DataSourceGeonamesAdmin1 DataSourceID = "geonamesAdmin1Codes" DataSourceMaxMindCities DataSourceID = "maxmindWorldCities" )
type GeoBed ¶
type GeoBed struct {
Cities Cities // All loaded cities, sorted by name
Countries []CountryInfo // Country metadata from Geonames
// contains filtered or unexported fields
}
GeoBed provides offline geocoding using embedded city data. Safe for concurrent use after initialization.
func GetDefaultGeobed ¶
GetDefaultGeobed returns a shared GeoBed instance, initializing it on first call. Unlike sync.Once, transient errors (e.g., network down during download) allow retry.
func NewGeobed ¶
NewGeobed creates a new GeoBed instance with geocoding data loaded into memory.
Options can be provided to customize data and cache directories:
g, err := NewGeobed(WithDataDir("/custom/data"), WithCacheDir("/custom/cache"))
Example:
g, err := NewGeobed()
if err != nil {
log.Fatal(err)
}
city := g.Geocode("Austin, TX")
fmt.Printf("%s: %f, %f\n", city.City, city.Latitude, city.Longitude)
func (*GeoBed) Geocode ¶
func (g *GeoBed) Geocode(n string, opts ...GeocodeOptions) GeobedCity
Geocode performs forward geocoding, converting a location string to coordinates.
func (*GeoBed) ReverseGeocode ¶
func (g *GeoBed) ReverseGeocode(lat, lng float64) GeobedCity
ReverseGeocode converts lat/lng coordinates to a city location.
type GeobedCity ¶
type GeobedCity struct {
City string // City name
CityAlt string // Alternate names (comma-separated)
Latitude float32 // Latitude in degrees
Longitude float32 // Longitude in degrees
Population int32 // Population count
// contains filtered or unexported fields
}
GeobedCity represents a city with geocoding data. Memory-optimized: uses indexes for Country/Region, float32 for coordinates.
func (GeobedCity) Country ¶
func (c GeobedCity) Country() string
Country returns the ISO 3166-1 alpha-2 country code (e.g., "US", "FR").
func (GeobedCity) Region ¶
func (c GeobedCity) Region() string
Region returns the administrative region code (e.g., "TX", "CA").
type GeobedConfig ¶
type GeobedConfig struct {
DataDir string // Directory for raw data files (default: "./geobed-data")
CacheDir string // Directory for cache files (default: "./geobed-cache")
}
GeobedConfig contains configuration options for GeoBed initialization.
type GeocodeOptions ¶
type GeocodeOptions struct {
ExactCity bool // Require exact city name match
FuzzyDistance int // Max edit distance for typo tolerance (0 = disabled, 1-2 recommended)
}
GeocodeOptions configures geocoding behavior.
type Option ¶
type Option func(*GeobedConfig)
Option is a functional option for configuring GeoBed.
func WithCacheDir ¶
WithCacheDir sets the directory for cache files.
func WithDataDir ¶
WithDataDir sets the directory for raw data files.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
update-cache
command
Command update-cache regenerates the geobed cache files from raw data and validates the result.
|
Command update-cache regenerates the geobed cache files from raw data and validates the result. |