Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrLocationNotFound = errors.New("country not found")
ErrLocationNotFound is returned when no country is found for given coordinates.
Functions ¶
func Countries10 ¶
func Countries10() []byte
func Countries110 ¶
func Countries110() []byte
func CountriesEEZ10 ¶ added in v1.4.0
func CountriesEEZ10() []byte
CountriesEEZ10 is an alternative to Countries10 built from the MarineRegions EEZ land union rather than Natural Earth. Its polygons extend to the edge of each country's exclusive economic zone, roughly 200 nautical miles offshore, rather than stopping at the coastline, so it resolves coordinates at sea that the other datasets report as ErrLocationNotFound. Do not use that error as an "is at sea" test with it; use Countries10 or Countries110 for that.
It also attributes some coastal territories differently. Dependencies with their own ISO code keep it (Greenland resolves to GRL, not DNK), but territories the crosswalk cannot describe resolve as their sovereign state, and Hong Kong, Macao, Åland and the British Indian Ocean Territory are absent because MarineRegions has no EEZ record for them.
The data is CC-BY 4.0; see data/EEZ_land_union_v4_202410.txt for the required attribution. It is rebuilt by ./scripts/eez/simplify.sh.
func Provinces10 ¶
func Provinces10() []byte
Types ¶
type Location ¶
type Location struct {
// Commonly used country name
Country string `json:"country,omitempty"`
// Formal name of country
CountryLong string `json:"country_long,omitempty"`
// ISO 3166-1 alpha-1 and alpha-2 codes
CountryCode2 string `json:"country_code_2,omitempty"`
CountryCode3 string `json:"country_code_3,omitempty"`
Continent string `json:"continent,omitempty"`
Region string `json:"region,omitempty"`
SubRegion string `json:"subregion,omitempty"`
Province string `json:"province,omitempty"`
// ISO 3166-2 code
ProvinceCode string `json:"province_code,omitempty"`
City string `json:"city,omitempty"`
}
Location is the return type for ReverseGeocode.
type Rgeo ¶
type Rgeo struct {
// contains filtered or unexported fields
}
Rgeo is the type used to hold pre-created polygons for reverse geocoding.
func New ¶
New returns an Rgeo struct which can then be used with ReverseGeocode. It takes any number of datasets as an argument. The included datasets are: Countries110, Countries10, CountriesEEZ10, Provinces10 and Cities10. Provinces10 includes all of the country information so if that's all you want don't use Countries as well. Cities10 only includes cities so you'll probably want to use Provinces10 with it.
Countries110, Countries10, Provinces10 and Cities10 use Natural Earth country polygons, which stop at the coastline. CountriesEEZ10 instead uses the MarineRegions EEZ land union; see its documentation for the differences.
func (*Rgeo) Build ¶ added in v1.3.0
func (r *Rgeo) Build()
Build builds the underlying shape index. This ensures that future calls to ReverseGeocode will be fast. If Build is not called, then the first lookup will build the index implicitly and experience a 1s+ delay.
func (*Rgeo) ReverseGeocode ¶
ReverseGeocode returns the country in which the given coordinate is located.
The input is a geom.Coord, which is just a []float64 with the longitude in the zeroth position and the latitude in the first position (i.e. []float64{lon, lat}).
Example ¶
r, err := New(Countries110)
if err != nil {
// Handle error
}
loc, err := r.ReverseGeocode([]float64{0, 52})
if err != nil {
// Handle error
}
fmt.Printf("%s\n", loc.Country)
fmt.Printf("%s\n", loc.CountryLong)
fmt.Printf("%s\n", loc.CountryCode2)
fmt.Printf("%s\n", loc.CountryCode3)
fmt.Printf("%s\n", loc.Continent)
fmt.Printf("%s\n", loc.Region)
fmt.Printf("%s\n", loc.SubRegion)
Output: United Kingdom United Kingdom of Great Britain and Northern Ireland GB GBR Europe Europe Northern Europe
Example (City) ¶
r, err := New(Provinces10, Cities10)
if err != nil {
// Handle error
}
loc, err := r.ReverseGeocode([]float64{141.35, 43.07})
if err != nil {
// Handle error
}
fmt.Println(loc)
Output: <Location> Sapporo, Hokkaidō, Japan (JPN), Asia
Directories
¶
| Path | Synopsis |
|---|---|
|
Command datagen converts GeoJSON files into go files containing functions that return the compressed GeoJSON, it can also merge properties from one GeoJSON file into another using the -merge flag (which it does by matching the country names).
|
Command datagen converts GeoJSON files into go files containing functions that return the compressed GeoJSON, it can also merge properties from one GeoJSON file into another using the -merge flag (which it does by matching the country names). |