Skip to content

data.location #

make sure to do

brew install libpq

fn new #

fn new(mut db_client postgresql_client.PostgresClient, reset bool) !Location

new creates a new Location instance

fn new_location_db #

fn new_location_db(mut db_client postgresql_client.PostgresClient, reset bool) !LocationDB

new_location_db creates a new LocationDB instance

struct AlternateName #

struct AlternateName {
pub:
	id            int    @[primary; sql: serial]
	city_id       int    @[fkey: 'City.id'; required]
	name          string @[index; max_len: 200; required]
	language_code string @[max_len: 2]
	is_preferred  bool
	is_short      bool
}

struct City #

struct City {
pub:
	id              int    @[index; unique]
	name            string @[index; max_len: 200; required]
	ascii_name      string @[index; max_len: 200; required] // Normalized name without special characters
	country_iso2    string @[fkey: 'Country.iso2'; required]
	postal_code     string @[index; max_len: 20] // postal code
	state_name      string @[max_len: 100]       // State/Province name
	state_code      string @[max_len: 20]        // State/Province code
	county_name     string @[max_len: 100]
	county_code     string @[max_len: 20]
	community_name  string @[max_len: 100]
	community_code  string @[max_len: 20]
	latitude        f64    @[index: 'idx_coords']
	longitude       f64    @[index: 'idx_coords']
	population      i64
	timezone        string @[max_len: 40]
	feature_class   string @[max_len: 1]  // For filtering (P for populated places)
	feature_code    string @[max_len: 10] // Detailed type (PPL, PPLA, etc.)
	search_priority int
	accuracy        i16 = 1 // 1=estimated, 4=geonameid, 6=centroid of addresses or shape
}

struct CoordinateSearchOptions #

struct CoordinateSearchOptions {
pub:
	coordinates Coordinates
	radius      f64 // in kilometers
	limit       int = 10
}

CoordinateSearchOptions represents parameters for coordinate-based searches

struct Coordinates #

struct Coordinates {
pub:
	latitude  f64
	longitude f64
}

Coordinates represents a geographic point

struct Country #

struct Country {
pub:
	iso2        string @[index; max_len: 2; primary; sql: 'iso2'; unique]
	name        string @[index; required; unique]
	iso3        string @[index; max_len: 3; required; sql: 'iso3'; unique]
	continent   string @[max_len: 2]
	population  i64
	timezone    string @[max_len: 40]
	import_date i64 // Epoch timestamp of last import
}

struct Location #

struct Location {
mut:
	db        LocationDB
	db_client postgresql_client.PostgresClient
}

Location represents the main API for location operations

fn (Location) download_and_import #

fn (mut l Location) download_and_import(redownload bool) !

init_database downloads and imports the initial dataset

struct LocationDB #

struct LocationDB {
pub mut:
	db        pg.DB
	db_client postgresql_client.PostgresClient
	tmp_dir   pathlib.Path
	db_dir    pathlib.Path
}

LocationDB handles all database operations for locations

fn (LocationDB) close #

fn (mut l LocationDB) close() !

close closes the database connection

fn (LocationDB) download_and_import_data #

fn (mut l LocationDB) download_and_import_data(redownload bool) !

download_and_import_data downloads and imports GeoNames data

fn (LocationDB) reset_import_dates #

fn (mut l LocationDB) reset_import_dates() !

reset_import_dates sets all country import_dates to 0

struct SearchOptions #

struct SearchOptions {
pub:
	query        string
	country_code string
	limit        int = 10
	fuzzy        bool
}

SearchOptions represents parameters for location searches

struct SearchResult #

struct SearchResult {
pub:
	city       City
	country    Country
	similarity f64 // Search similarity score
}

SearchResult represents a location search result with combined city and country info