Documentation
¶
Overview ¶
Package kenyageo holds Kenya's 47 counties, their constituencies and 1,450 wards, and 890 post offices, with 2019 census figures, IEBC registered voters and boundaries, all embedded in the package.
Start with Default, which loads the embedded data once and is safe for concurrent use:
geo := kenyageo.Default()
nakuru, _ := geo.CountyByName("Nakuru")
ward, _ := geo.WardAt(-1.2884, 36.8233)
Example ¶
package main
import (
"fmt"
kenyageo "github.com/rmiyoyo/kenya-geo"
)
func main() {
geo := kenyageo.Default()
nakuru, _ := geo.CountyByName("nakuru")
fmt.Println(nakuru.Code, nakuru.Name, nakuru.Population)
}
Output: 32 Nakuru County 2162202
Index ¶
- Variables
- type Address
- type County
- type Data
- func (d *Data) Constituencies(countyCode int) []string
- func (d *Data) ConstituencyVoters(name string) (VoterTotal, bool)
- func (d *Data) Counties() []County
- func (d *Data) CountyAt(lat, lng float64) (County, bool)
- func (d *Data) CountyBoundary(code int) (Feature[County], bool)
- func (d *Data) CountyByCode(code int) (County, bool)
- func (d *Data) CountyByName(name string) (County, bool)
- func (d *Data) CountyVoters(code int) (VoterTotal, bool)
- func (d *Data) LoadCountyShapes(r io.Reader) error
- func (d *Data) LoadWardShapes(r io.Reader) error
- func (d *Data) National() (National, bool)
- func (d *Data) NationalLiving() (Living, bool)
- func (d *Data) NationalVoters() VoterTotal
- func (d *Data) NeighbouringCounties(code int) []County
- func (d *Data) ParseAddress(s string) (Address, error)
- func (d *Data) PostOffices(postalCode string) []PostOffice
- func (d *Data) PostOfficesInCounty(countyCode int) []PostOffice
- func (d *Data) PostOfficesNear(lat, lng float64, n int) []NearbyPostOffice
- func (d *Data) Search(query string, limit int) []Match
- func (d *Data) WardAt(lat, lng float64) (Ward, bool)
- func (d *Data) WardBoundary(code string) (Feature[Ward], bool)
- func (d *Data) WardByCode(code string) (Ward, bool)
- func (d *Data) WardsInConstituency(name string) []Ward
- func (d *Data) WardsInCounty(countyCode int) []Ward
- type Feature
- type Geometry
- type Kind
- type LatLng
- type Living
- type Match
- type National
- type NearbyPostOffice
- type PostOffice
- type Religion
- type VoterTotal
- type Ward
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrNoBox = errors.New("no P.O. Box or Private Bag found") ErrUnknownPostOffice = errors.New("unknown post office") ErrTownMismatch = errors.New("town does not match the postal code") )
Errors returned by Data.ParseAddress. Test for them with errors.Is.
Functions ¶
This section is empty.
Types ¶
type Address ¶ added in v0.2.0
type Address struct {
Box string `json:"box,omitempty"`
PrivateBag bool `json:"private_bag"`
PostalCode string `json:"postal_code"`
Town string `json:"town,omitempty"`
PostOffice *PostOffice `json:"post_office"`
}
Address is a parsed Kenyan postal address such as "P.O. Box 123-00100 Nairobi".
type County ¶
type County struct {
Code int `json:"code"`
Name string `json:"name"`
ISOCode string `json:"iso_code"`
FormerProvince string `json:"former_province"`
Headquarters string `json:"headquarters"`
// HeadquartersAliases lists other names in use for the headquarters
// town, such as Vihiga for Mbale.
HeadquartersAliases []string `json:"headquarters_aliases,omitempty"`
Population int `json:"population"`
PopulationMale int `json:"population_male"`
PopulationFemale int `json:"population_female"`
PopulationIntersex int `json:"population_intersex"`
PopulationUrban int `json:"population_urban"`
PopulationRural int `json:"population_rural"`
AreaKm2 float64 `json:"area_km2"`
Centroid LatLng `json:"centroid"`
BBox [4]float64 `json:"bbox"`
Neighbours []int `json:"neighbours"`
Living Living `json:"living"`
Religion Religion `json:"religion"`
}
County is one of Kenya's 47 counties, with its 2019 census figures, location and neighbours.
func (County) UrbanPct ¶ added in v0.3.0
UrbanPct returns the percentage of the county's people who live in urban areas.
Example ¶
package main
import (
"fmt"
kenyageo "github.com/rmiyoyo/kenya-geo"
)
func main() {
geo := kenyageo.Default()
kilifi, _ := geo.CountyByCode(3)
fmt.Printf("%.1f%% urban, %.1f%% use the internet\n", kilifi.UrbanPct(), kilifi.Living.InternetPct)
}
Output: 27.1% urban, 15.1% use the internet
type Data ¶
type Data struct {
// contains filtered or unexported fields
}
Data holds the counties, wards and post offices and answers questions about them. Use Default for the embedded data, or Load for your own. A Data is safe for concurrent use once loaded.
func Default ¶
func Default() *Data
Default returns the data embedded in the package, including boundaries and national figures. It is loaded on first use and shared afterwards.
func Load ¶
Load reads counties, wards and post offices in the JSON format of the files in the data directory. Boundaries are not loaded; add them with Data.LoadWardShapes and Data.LoadCountyShapes.
func (*Data) Constituencies ¶
Constituencies returns the names of a county's constituencies, sorted.
func (*Data) ConstituencyVoters ¶ added in v0.2.0
func (d *Data) ConstituencyVoters(name string) (VoterTotal, bool)
ConstituencyVoters returns the registered voters in a constituency's wards, matching the name like Data.CountyByName.
func (*Data) CountyAt ¶ added in v0.2.0
CountyAt returns the county containing a point. Boundaries are simplified, so treat points near a county line as approximate.
Example ¶
c, ok := Default().CountyAt(-0.0917, 34.768) fmt.Println(c.Code, c.Name, ok)
Output: 42 Kisumu County true
Example ¶
package main
import (
"fmt"
kenyageo "github.com/rmiyoyo/kenya-geo"
)
func main() {
geo := kenyageo.Default()
county, _ := geo.CountyAt(-0.0917, 34.768)
fmt.Println(county.Name)
}
Output: Kisumu County
func (*Data) CountyBoundary ¶ added in v0.2.0
CountyBoundary returns a county's outline as a GeoJSON Feature.
Example ¶
f, _ := Default().CountyBoundary(47) b, _ := json.Marshal(f) fmt.Println(f.Properties.Name, f.Geometry.Type, len(b) > 1000)
Output: Nairobi County MultiPolygon true
func (*Data) CountyByCode ¶
CountyByCode returns the county with a code from 1 to 47.
func (*Data) CountyByName ¶
CountyByName returns the county with a name. Case, apostrophes, dashes and a trailing "County" don't matter, so "nakuru" and "Murang'a County" both work.
Example ¶
c, ok := Default().CountyByName("taita taveta")
fmt.Println(c.Code, c.Name, c.Headquarters, ok)
Output: 6 Taita-Taveta County Mwatate true
func (*Data) CountyVoters ¶ added in v0.2.0
func (d *Data) CountyVoters(code int) (VoterTotal, bool)
CountyVoters returns the registered voters in a county's wards.
Example ¶
package main
import (
"fmt"
kenyageo "github.com/rmiyoyo/kenya-geo"
)
func main() {
geo := kenyageo.Default()
v, _ := geo.CountyVoters(47)
fmt.Println(v.RegisteredVoters2022, v.Wards, v.Complete)
}
Output: 2415310 85 true
Example ¶
v, _ := Default().CountyVoters(47) fmt.Println(v.RegisteredVoters2022, v.Wards, v.Complete)
Output: 2415310 85 true
func (*Data) LoadCountyShapes ¶ added in v0.2.0
LoadCountyShapes reads county boundaries in the format of data/countyshapes.json, for data from Load.
func (*Data) LoadWardShapes ¶ added in v0.2.0
LoadWardShapes reads ward boundaries in the format of data/wardshapes.json, for data from Load.
func (*Data) National ¶ added in v0.3.0
National returns the census figures for Kenya as a whole. It reports false for data from Load, which has no national figures.
Example ¶
package main
import (
"fmt"
kenyageo "github.com/rmiyoyo/kenya-geo"
)
func main() {
geo := kenyageo.Default()
kenya, _ := geo.National()
fmt.Printf("%.1f%% Christian, %.1f%% of homes on mains electricity\n",
kenya.Religion.Pct(kenya.Religion.Christian()), kenya.Living.ElectricityPct)
}
Output: 85.5% Christian, 50.4% of homes on mains electricity
func (*Data) NationalLiving ¶ added in v0.3.0
NationalLiving returns Kenya's living conditions, like Data.National.
func (*Data) NationalVoters ¶ added in v0.2.0
func (d *Data) NationalVoters() VoterTotal
NationalVoters returns the registered voters in every ward.
func (*Data) NeighbouringCounties ¶ added in v0.2.0
NeighbouringCounties returns the counties that share a border with the county with code.
Example ¶
for _, c := range Default().NeighbouringCounties(47) {
fmt.Println(c.Name)
}
Output: Machakos County Kiambu County Kajiado County
func (*Data) ParseAddress ¶ added in v0.2.0
ParseAddress parses a postal address and finds its post office. It accepts "P.O. Box", "Box" and "Private Bag", with the postal code joined to the box by a dash or written separately. When the town and postal code belong to different offices it returns the parsed address with ErrTownMismatch.
Example ¶
package main
import (
"errors"
"fmt"
kenyageo "github.com/rmiyoyo/kenya-geo"
)
func main() {
geo := kenyageo.Default()
a, err := geo.ParseAddress("p.o. box 123, nairobi 00100")
fmt.Println(a, a.PostOffice.Name, err)
_, err = geo.ParseAddress("P.O. Box 90-40100 Eldoret")
fmt.Println(errors.Is(err, kenyageo.ErrTownMismatch))
}
Output: P.O. Box 123-00100 Nairobi Nairobi Gpo <nil> true
Example ¶
a, err := Default().ParseAddress("P.O. Box 30100, 00100 GPO Nairobi")
fmt.Println(a.Box, a.PostalCode, a.PostOffice.Name, err)
fmt.Println(a)
Output: 30100 00100 Nairobi Gpo <nil> P.O. Box 30100-00100 Nairobi
func (*Data) PostOffices ¶
func (d *Data) PostOffices(postalCode string) []PostOffice
PostOffices returns the post offices with a postal code. Several offices can share one code.
func (*Data) PostOfficesInCounty ¶
func (d *Data) PostOfficesInCounty(countyCode int) []PostOffice
PostOfficesInCounty returns the post offices in a county, by postal code.
func (*Data) PostOfficesNear ¶ added in v0.2.0
func (d *Data) PostOfficesNear(lat, lng float64, n int) []NearbyPostOffice
PostOfficesNear returns up to n post offices closest to a point, nearest first, by great-circle distance. Offices with no known location are skipped.
Example ¶
package main
import (
"fmt"
kenyageo "github.com/rmiyoyo/kenya-geo"
)
func main() {
geo := kenyageo.Default()
for _, p := range geo.PostOfficesNear(-0.2833, 36.0667, 2) {
fmt.Printf("%s %s %.1f km\n", p.Code, p.Name, p.DistanceKm)
}
}
Output: 20100 Nakuru 2.7 km 20112 Lanet 8.2 km
Example ¶
for _, p := range Default().PostOfficesNear(-0.2833, 36.0667, 2) {
fmt.Printf("%s %s %.1f km\n", p.Code, p.Name, p.DistanceKm)
}
Output: 20100 Nakuru 2.7 km 20112 Lanet 8.2 km
func (*Data) Search ¶
Search finds counties, constituencies, wards and post offices whose names match query, allowing for typos, best first. A limit of 0 or less returns every match.
Example ¶
package main
import (
"fmt"
kenyageo "github.com/rmiyoyo/kenya-geo"
)
func main() {
geo := kenyageo.Default()
for _, m := range geo.Search("nakru", 3) {
fmt.Println(m.Kind, m.Name, m.Code)
}
}
Output: county Nakuru County 32 post office Nakuru 20100
Example ¶
for _, m := range Default().Search("makadara", 3) {
fmt.Printf("%s %s\n", m.Kind, m.Name)
}
Output: constituency Makadara post office Makadara ward MJI WA KALE/MAKADARA
func (*Data) WardAt ¶ added in v0.2.0
WardAt returns the ward containing a point. It reports false outside Kenya and in the few areas with no known ward boundary.
Example ¶
package main
import (
"fmt"
kenyageo "github.com/rmiyoyo/kenya-geo"
)
func main() {
geo := kenyageo.Default()
ward, ok := geo.WardAt(-1.2884, 36.8233)
fmt.Println(ok, ward.Name, ward.Constituency, ward.CountyName)
}
Output: true NAIROBI CENTRAL Starehe Nairobi County
Example ¶
w, ok := Default().WardAt(-1.2884, 36.8233) fmt.Println(w.Code, w.Name, w.Constituency, w.CountyName, ok)
Output: 1439 NAIROBI CENTRAL Starehe Nairobi County true
func (*Data) WardBoundary ¶ added in v0.2.0
WardBoundary returns a ward's outline as a GeoJSON Feature. It reports false for the few wards with no known boundary.
func (*Data) WardByCode ¶
WardByCode returns the ward with a four-digit IEBC code such as "0040".
func (*Data) WardsInConstituency ¶
WardsInConstituency returns the wards in the constituency with a name, matched like Data.CountyByName.
func (*Data) WardsInCounty ¶
WardsInCounty returns the wards in a county, by ward code.
type Feature ¶ added in v0.2.0
type Feature[P any] struct { Type string `json:"type"` Geometry Geometry `json:"geometry"` Properties P `json:"properties"` }
Feature is a GeoJSON Feature with a MultiPolygon geometry and P as its properties.
type Geometry ¶ added in v0.2.0
type Geometry struct {
Type string `json:"type"`
Coordinates [][][][2]float64 `json:"coordinates"`
}
Geometry is a GeoJSON MultiPolygon: polygons made of rings of [lng, lat] points.
type Kind ¶
type Kind int
Kind is the kind of place a search Match found.
The kinds of place Data.Search can find.
type Living ¶ added in v0.3.0
type Living struct {
Households int `json:"households"`
HouseholdSize float64 `json:"average_household_size"`
ElectricityPct float64 `json:"electricity_pct"`
PipedWaterPct float64 `json:"piped_water_pct"`
InternetPct float64 `json:"internet_pct"`
MobilePhonePct float64 `json:"mobile_phone_pct"`
SchoolAttendancePct float64 `json:"school_attendance_pct"`
}
Living holds living conditions from the 2019 census for a county or for Kenya. The fields ending in Pct are percentages from 0 to 100.
type Match ¶
Match is one search result. Code is the county code, ward code or postal code, and is empty for constituencies. Score runs from 0 to 1, where 1 is an exact match.
type National ¶ added in v0.3.0
type National struct {
PopulationUrban int `json:"population_urban"`
PopulationRural int `json:"population_rural"`
Living Living `json:"living"`
Religion Religion `json:"religion"`
}
National holds the 2019 census figures for Kenya as a whole.
type NearbyPostOffice ¶ added in v0.2.0
type NearbyPostOffice struct {
PostOffice
DistanceKm float64 `json:"distance_km"`
}
NearbyPostOffice is a post office and its distance from a point.
type PostOffice ¶
type PostOffice struct {
Code string `json:"postal_code"`
Name string `json:"name"`
CountyCode int `json:"county_code"`
CountySource string `json:"county_source"`
Location *LatLng `json:"location"`
LocationSource string `json:"location_source"`
}
PostOffice is a post office and the postal code it serves.
type Religion ¶ added in v0.3.0
type Religion struct {
Total int `json:"total"`
Catholic int `json:"catholic"`
Protestant int `json:"protestant"`
Evangelical int `json:"evangelical"`
AfricanInstituted int `json:"african_instituted"`
Orthodox int `json:"orthodox"`
OtherChristian int `json:"other_christian"`
Islam int `json:"islam"`
Hindu int `json:"hindu"`
Traditionist int `json:"traditionist"`
OtherReligion int `json:"other_religion"`
NoReligion int `json:"no_religion"`
DontKnow int `json:"dont_know"`
NotStated int `json:"not_stated"`
}
Religion counts people in households by religious affiliation in the 2019 census. The groups add up to Total.
type VoterTotal ¶ added in v0.2.0
type VoterTotal struct {
RegisteredVoters2022 int `json:"registered_voters_2022"`
Wards int `json:"wards"`
Complete bool `json:"complete"`
}
VoterTotal adds up the 2022 registered voters in a group of wards. Complete is false when one or more wards have no published figure, so the total is a lower bound.
type Ward ¶
type Ward struct {
Code string `json:"ward_code"`
Name string `json:"name"`
Constituency string `json:"constituency_name"`
CountyCode int `json:"county_code"`
CountyName string `json:"county_name"`
RegisteredVoters2022 *int `json:"registered_voters_2022"`
Centroid *LatLng `json:"centroid"`
}
Ward is an IEBC electoral ward, with its constituency, county and 2022 registered voters.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package api serves the kenyageo data as a JSON HTTP API.
|
Package api serves the kenyageo data as a JSON HTTP API. |
|
cmd
|
|
|
kenyageo
command
Kenyageo looks up Kenya's counties, wards, post offices and addresses from the command line.
|
Kenyageo looks up Kenya's counties, wards, post offices and addresses from the command line. |
|
kenyageo-server
command
Kenyageo-server serves the kenyageo JSON API over HTTP.
|
Kenyageo-server serves the kenyageo JSON API over HTTP. |
|
internal
|
|
|
gendata
command
|