Documentation
¶
Overview ¶
Package thailocate resolves a WGS84 latitude/longitude coordinate to the Thai administrative area that contains it: province (จังหวัด), district (อำเภอ, or เขต inside Bangkok) and subdistrict (ตำบล, or แขวง inside Bangkok).
Boundary data is embedded in the compiled binary, so lookups never touch the network or disk at runtime.
Basic usage:
loc, err := thailocate.New()
if err != nil { ... }
detail, err := loc.GetLocationDetail(13.7563, 100.5018)
A ready-to-use default instance is also available without constructing one yourself:
detail, err := thailocate.GetLocationDetail(13.7563, 100.5018)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Geometry ¶ added in v0.2.0
Geometry is a GeoJSON-style Polygon or MultiPolygon describing an arbitrary area to query with FindSubdistrictsIntersecting — e.g. a rider shift zone, delivery area, or any other caller-defined boundary. It is intentionally not tied to the embedded administrative boundary data.
Coordinates follows standard GeoJSON coordinate nesting for Type: rings of [lng, lat] pairs, with ring[0] the outer boundary and any further rings holes cut out of it.
- "Polygon": [][][2]float64 (one ring set)
- "MultiPolygon": [][][][2]float64 (one ring set per disjoint part)
Coordinates also accepts the generic nesting ([]any of []any of ... numbers) produced by decoding arbitrary JSON/BSON into `any`, so callers can pass a value straight out of a database decode without re-typing it into the concrete Go shape above — including unmarshaling a standard GeoJSON geometry object straight off the wire, since json.Unmarshal populates Coordinates with exactly that nesting.
type LocationDetail ¶
type LocationDetail struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Found bool `json:"found"`
CountryEN string `json:"country_en,omitempty"`
CountryTH string `json:"country_th,omitempty"`
ProvinceEN string `json:"province_en,omitempty"`
ProvinceTH string `json:"province_th,omitempty"`
ProvinceCode string `json:"province_code,omitempty"`
// District is the อำเภอ (Amphoe) — or เขต (Khet) for the 50 districts
// of Bangkok. DistrictType tells you which term applies.
DistrictEN string `json:"district_en,omitempty"`
DistrictTH string `json:"district_th,omitempty"`
DistrictCode string `json:"district_code,omitempty"`
DistrictType string `json:"district_type,omitempty"` // "amphoe" | "khet"
// Subdistrict is the ตำบล (Tambon) — or แขวง (Khwaeng) for Bangkok's
// subdistricts. SubdistrictType tells you which term applies.
SubdistrictEN string `json:"subdistrict_en,omitempty"`
SubdistrictTH string `json:"subdistrict_th,omitempty"`
SubdistrictCode string `json:"subdistrict_code,omitempty"`
SubdistrictType string `json:"subdistrict_type,omitempty"`
MatchLevel MatchLevel `json:"match_level"`
}
LocationDetail is everything thailocate knows about a coordinate.
func GetLocationDetail ¶
func GetLocationDetail(lat, lng float64) (*LocationDetail, error)
GetLocationDetail is a package-level convenience wrapper around a lazily initialized, shared Locator. Use this if you just want to call thailocate.GetLocationDetail(lat, lng) without managing a Locator yourself.
type Locator ¶
type Locator struct {
// contains filtered or unexported fields
}
Locator holds the parsed boundary polygons in memory and answers lookups. It is safe for concurrent use by multiple goroutines.
func New ¶
New parses the embedded boundary data and returns a ready-to-use Locator. This does a few megabytes of JSON parsing, so construct one Locator and reuse it (it's safe for concurrent use) rather than calling New per request.
func (*Locator) FindSubdistrictsIntersecting ¶ added in v0.2.0
func (l *Locator) FindSubdistrictsIntersecting(geometry Geometry) ([]SubdistrictMatch, error)
FindSubdistrictsIntersecting resolves an arbitrary polygon — e.g. a rider shift zone or delivery area — to every subdistrict (tambon/khwaeng) whose boundary it touches or overlaps. It is the many-to-many counterpart of GetLocationDetail: a single point resolves to at most one subdistrict, but a polygon commonly spans several.
The result is a slice in no particular order, and an empty slice (not an error) is a normal result if the polygon falls entirely outside Thailand or in a gap between simplified boundaries. An error is only returned for a malformed geometry (missing/unsupported Type, empty or non-numeric coordinates).
func (*Locator) GetLocationDetail ¶
func (l *Locator) GetLocationDetail(lat, lng float64) (*LocationDetail, error)
GetLocationDetail resolves lat/lng (WGS84 decimal degrees) to the Thai province / district / subdistrict that contains it.
If the point falls outside Thailand, or in a gap in the simplified boundary data, Found is false and MatchLevel is "none" — this is not treated as an error, since "not in Thailand" is a perfectly valid answer. An error is only returned for malformed input.
type MatchLevel ¶
type MatchLevel string
MatchLevel indicates the finest administrative level that was actually resolved for a query. Lookups almost always resolve at the subdistrict level; province/district-only results are a rare fallback for points that fall in tiny gaps between simplified polygons (e.g. right at a coastline).
const ( MatchSubdistrict MatchLevel = "subdistrict" MatchDistrict MatchLevel = "district" MatchProvince MatchLevel = "province" MatchNone MatchLevel = "none" )
type SubdistrictMatch ¶ added in v0.2.0
type SubdistrictMatch struct {
ProvinceEN string `json:"province_en"`
ProvinceTH string `json:"province_th"`
ProvinceCode string `json:"province_code"`
// District is the อำเภอ (Amphoe) — or เขต (Khet) for the 50 districts
// of Bangkok. DistrictType tells you which term applies.
DistrictEN string `json:"district_en"`
DistrictTH string `json:"district_th"`
DistrictCode string `json:"district_code"`
DistrictType string `json:"district_type"` // "amphoe" | "khet"
// Subdistrict is the ตำบล (Tambon) — or แขวง (Khwaeng) for Bangkok's
// subdistricts. SubdistrictType tells you which term applies.
SubdistrictEN string `json:"subdistrict_en"`
SubdistrictTH string `json:"subdistrict_th"`
SubdistrictCode string `json:"subdistrict_code"`
SubdistrictType string `json:"subdistrict_type"`
}
SubdistrictMatch is one administrative area (province/district/subdistrict) returned by FindSubdistrictsIntersecting.
func FindSubdistrictsIntersecting ¶ added in v0.2.0
func FindSubdistrictsIntersecting(geometry Geometry) ([]SubdistrictMatch, error)
FindSubdistrictsIntersecting is a package-level convenience wrapper around a lazily initialized, shared Locator. See Locator.FindSubdistrictsIntersecting.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
thailocate-server
command
Command thailocate-server exposes the thailocate library over HTTP.
|
Command thailocate-server exposes the thailocate library over HTTP. |
|
example
|
|
|
basic
command
|