From 4a5c65dd1a624acbcbb3e762d938f8a20f27727e Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Mon, 26 Feb 2024 11:32:39 +0000 Subject: [PATCH] Init --- go.mod | 5 +++++ go.sum | 10 ++++++++++ main.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ structs.go | 26 ++++++++++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 structs.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..609da60 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.caliban.io/martin/go-geocode + +go 1.22.0 + +require github.com/codingsince1985/geo-golang v1.8.3 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ff3ca8b --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/codingsince1985/geo-golang v1.8.3 h1:73TRG/poj1IUiYOoaEM7gD/+ZBSRg+BPnWoGpAg+NHc= +github.com/codingsince1985/geo-golang v1.8.3/go.mod h1:IQXA9sjsQ1hTJfijQcsQInvnzdn7B0rx+VTNDLpaqiw= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..a9342e4 --- /dev/null +++ b/main.go @@ -0,0 +1,49 @@ +package go_geocode + + +import ( + "log" + + "github.com/codingsince1985/geo-golang" + "github.com/codingsince1985/geo-golang/openstreetmap" +) + +func Geocode(latlong LatLong) *FormattedLocation { + + return tryGeocoder(openstreetmap.Geocoder(), latlong) + +} + +func tryGeocoder(geocoder geo.Geocoder, latlong LatLong) *FormattedLocation { + + + address, _ := geocoder.ReverseGeocode(latlong.Lat, latlong.Long) + + if address != nil { + log.Printf("%+v\n", address) + + return reformatGeocode(*address, latlong) + } else { + return new(FormattedLocation) + } +} + +func reformatGeocode(address geo.Address, latlong LatLong) *FormattedLocation{ + + return &FormattedLocation{ + Lat: latlong.Lat, + Long: latlong.Long, + Country: address.Country, + City: address.City, + State: address.State, + Zipcode: address.Postcode, + StreetName: address.Street, + CountryCode: address.CountryCode, + County: address.County, + Neighbourhood: address.Suburb, + Village: "", + Formatted: address.FormattedAddress, + + } + +} diff --git a/structs.go b/structs.go new file mode 100644 index 0000000..f03e977 --- /dev/null +++ b/structs.go @@ -0,0 +1,26 @@ +package go_geocode + +type LL struct { + Ll string `json:"ll" form:"ll"` +} + +type LatLong struct { + Lat float64 `json:"lat" form:"lat"` + Long float64 `json:"long" form:"long"` +} + +type FormattedLocation struct { + Lat float64 `json:"lat" form:"lat"` + Long float64 `json:"long" form:"long"` + + Country string `json:"country"` + City string `json:"city"` + State string `json:"state"` + Zipcode string `json:"zipcode"` + StreetName string `json:"streetName"` + CountryCode string `json:"countryCode"` + County string `json:"county"` + Neighbourhood string `json:"neighbourhood"` + Village string `json:"village"` + Formatted string `json:"formatted"` +}