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

package geoattractorindex

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

Index

Examples

Package Files

common.go index.go testing_common.go

Constants

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
)

Variables

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.

Code:

// Load countries.

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

f, err := os.Open(countryDataFilepath)
log.PanicIf(err)

defer f.Close()

countries, err := geoattractorparse.BuildGeonamesCountryMapping(f)
log.PanicIf(err)

// Load cities.

gp := geoattractorparse.NewGeonamesParser(countries)

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

defer g.Close()

ci := NewCityIndex()

err = ci.Load(gp, g)
log.PanicIf(err)

// Do the query.

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

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

// Print the results.

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

fmt.Printf("\n")

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)

Output:

8824c5cc: CityRecord<ID=[4989005] COUNTRY=[United States] CITY=[Clawson] POP=(12015) LAT=(42.5333700000) LON=(-83.1463200000)>
8824c5d: CityRecord<ID=[4989005] COUNTRY=[United States] CITY=[Clawson] POP=(12015) LAT=(42.5333700000) LON=(-83.1463200000)>
8824c5c: CityRecord<ID=[4989005] COUNTRY=[United States] CITY=[Clawson] POP=(12015) LAT=(42.5333700000) LON=(-83.1463200000)>
8824c5: CityRecord<ID=[4989005] COUNTRY=[United States] CITY=[Clawson] POP=(12015) LAT=(42.5333700000) LON=(-83.1463200000)>
8824c4: CityRecord<ID=[5012639] COUNTRY=[United States] CITY=[Troy] POP=(83280) LAT=(42.6055900000) LON=(-83.1499300000)>
8824d: CityRecord<ID=[5014051] COUNTRY=[United States] CITY=[Warren] POP=(134056) LAT=(42.4904400000) LON=(-83.0130400000)>
8824c: CityRecord<ID=[5014051] COUNTRY=[United States] CITY=[Warren] POP=(134056) LAT=(42.4904400000) LON=(-83.0130400000)>

Source: GeoNames
ID: 5014051
Country: United States
City: Warren
Population: 134056
Latitude: 42.4904400000
Longitude: -83.0130400000

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 2 packages. Updated 2019-01-05. Refresh now. Tools for package owners.