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 index.go testing_common.go


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

    // UrbanCenterMinimumPopulation is the minimum population a city requires in
    // order to be considered an urban/metropolitan center.
    UrbanCenterMinimumPopulation = 100000


var (
    ErrNoNearestCity = errors.New("no nearest city")

type CityIndex Uses

type CityIndex struct {
    // contains filtered or unexported fields

func NewCityIndex Uses

func NewCityIndex() *CityIndex

func (*CityIndex) Load Uses

func (ci *CityIndex) Load(source geoattractor.CityRecordSource, r io.Reader) (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) (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 := NewCityIndex()

err = ci.Load(gp, g)

// Do the query.

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

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

// 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) Stats Uses

func (ci *CityIndex) Stats() LoadStats

type LoadStats Uses

type LoadStats 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"`

func (LoadStats) String Uses

func (ls LoadStats) String() string

type VisitHistoryItem Uses

type VisitHistoryItem struct {
    Token string
    City  geoattractor.CityRecord

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