go-geographic-attractor: github.com/dsoprea/go-geographic-attractor/index Index | Examples | Files

package geoattractorindex

import "github.com/dsoprea/go-geographic-attractor/index"



Package Files

common.go constants.go index.go testing_common.go


const (
    // TestingMinimumLevelForUrbanCenterAttraction is the lowest level that
    // we'll compile the city with the highest population within.
    DefaultMinimumLevelForUrbanCenterAttraction = 7

    // TestingUrbanCenterMinimumPopulation is the minimum population a city
    // requires in order to be considered an urban/metropolitan center.
    DefaultUrbanCenterMinimumPopulation = 100000
const (
    // Cache Nearest() lookups.
    MaxNearestLruEntries = 100


var (
    ErrNoNearestCity = errors.New("no nearest city")
    ErrNotFound      = errors.New("not found")
var (
    CityIndexKeyGroup = []string{"attractor", "index", "city_index"}
    FineTokenKeyGroup = []string{"attractor", "index", "fine_token_index"}

func IdPhrase Uses

func IdPhrase(sourceName, id string) string

type AttractorStats Uses

type AttractorStats struct {
    // UnfilteredRecords is the total number of records that were seen in the
    // file before any filtering was applied.
    UnfilteredRecords int `json:"unfiltered_records_parsed"`

    // RecordAdds are the number of new records committed to the index for new
    // cells/levels.
    RecordAdds int `json:"records_added_to_index"`

    // RecordUpdates are the number of records that replaced existing ones
    // (mutually exclusively with RecordAdds).
    RecordUpdates int `json:"records_updated_in_index"`

    // HaversineCalculations is how many times we've calculated distances
    // between points.
    HaversineCalculations int `json:"haversine_calculations"`

    CachedNearestHits   int
    CachedNearestMisses int
    CachedNearestShifts int

func (AttractorStats) String Uses

func (ls AttractorStats) String() string

type CityIndex Uses

type CityIndex struct {
    // contains filtered or unexported fields

func NewCityIndex Uses

func NewCityIndex(kvFilepath string, minimumSearchLevel int, urbanCenterMinimumPopulation int) *CityIndex

NewCityIndex returns a `CityIndex` instance. `minimumSearchLevel` specifies the smallest level (largest region) that we want to search for cities around a certain point.

func NewTestCityIndex Uses

func NewTestCityIndex() (*CityIndex, string)

func (*CityIndex) Close Uses

func (ci *CityIndex) Close() (err error)

func (*CityIndex) GetById Uses

func (ci *CityIndex) GetById(sourceName, id string) (cr geoattractor.CityRecord, err error)

func (*CityIndex) KvCount Uses

func (ci *CityIndex) KvCount() (count int, err error)

func (*CityIndex) KvDump Uses

func (ci *CityIndex) KvDump() (err error)

func (*CityIndex) Load Uses

func (ci *CityIndex) Load(source geoattractor.CityRecordSource, r io.Reader, specificCityIds, specificCountryNames []string) (err error)

Load feeds the given city data into the index. Cities will be stored at multiple levels. If/when we experience collisions, we'll keep whichever has the larger population.

func (*CityIndex) Nearest Uses

func (ci *CityIndex) Nearest(latitude, longitude float64, returnAllVisits bool) (sourceName string, visits []VisitHistoryItem, cr geoattractor.CityRecord, err error)

Nearest returns the nearest urban-center to the given coordinates, or, if none, the nearest city. Note that, because of how cells are layed out, some near urban centers won't be selected while others will be.

Also returns the name of the data-source that produced the final result and the heirarchy of cities that surround the given coordinates up to the largest area that we index for urban centers in.


// Load countries.

countryDataFilepath := path.Join(appPath, "test", "asset", "countryInfo.txt")

f, err := os.Open(countryDataFilepath)

defer f.Close()

countries, err := geoattractorparse.BuildGeonamesCountryMapping(f)

// Load cities.

gp := geoattractorparse.NewGeonamesParser(countries)

cityDataFilepath := path.Join(appPath, "index", "test", "asset", "allCountries.txt.detroit_area_handpicked")
g, err := os.Open(cityDataFilepath)

defer g.Close()

ci, kvFilepath := NewTestCityIndex()

defer os.Remove(kvFilepath)
defer ci.Close()

err = ci.Load(gp, g, nil, nil)

// Do the query.

clawsonCoordinates := []float64{42.53667, -83.15041}

sourceName, visits, cr, err := ci.Nearest(clawsonCoordinates[0], clawsonCoordinates[1], true)

// Print the results.

for _, vhi := range visits {
    fmt.Printf("%s: %s\n", vhi.Token, vhi.City)


fmt.Printf("Source: %s\n", sourceName)
fmt.Printf("ID: %s\n", cr.Id)
fmt.Printf("Country: %s\n", cr.Country)
fmt.Printf("City: %s\n", cr.City)
fmt.Printf("Population: %d\n", cr.Population)
fmt.Printf("Latitude: %.10f\n", cr.Latitude)
fmt.Printf("Longitude: %.10f\n", cr.Longitude)

func (*CityIndex) SetTotalRecords Uses

func (ci *CityIndex) SetTotalRecords(count int)

SetTotalRecords enables us to provide progress information if the number of records is already known.

func (*CityIndex) SetVerbose Uses

func (ci *CityIndex) SetVerbose(flag bool)

func (*CityIndex) Stats Uses

func (ci *CityIndex) Stats() AttractorStats

func (*CityIndex) UrbanCentersEncountered Uses

func (ci *CityIndex) UrbanCentersEncountered() map[string]geoattractor.CityRecord

type IndexEntry Uses

type IndexEntry struct {
    CityRecord geoattractor.CityRecord
    Level      int

    // LeafCellId is the full cell-ID for the current city regardless of which
    // level we indexed it at.
    LeafCellToken string

    SourceName string

type VisitHistoryItem Uses

type VisitHistoryItem struct {
    Token      string
    City       geoattractor.CityRecord
    SourceName string

Package geoattractorindex imports 16 packages (graph) and is imported by 1 packages. Updated 2019-05-16. Refresh now. Tools for package owners.