|
| 1 | +// Package yandex is a geo-golang based Yandex Maps Location API |
| 2 | +package yandex |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/codingsince1985/geo-golang" |
| 10 | +) |
| 11 | + |
| 12 | +type ( |
| 13 | + baseURL string |
| 14 | + geocodeResponse struct { |
| 15 | + Response struct { |
| 16 | + GeoObjectCollection struct { |
| 17 | + MetaDataProperty struct { |
| 18 | + GeocoderResponseMetaData struct { |
| 19 | + Request string `json:"request"` |
| 20 | + Found string `json:"found"` |
| 21 | + Results string `json:"results"` |
| 22 | + } `json:"GeocoderResponseMetaData"` |
| 23 | + } `json:"metaDataProperty"` |
| 24 | + FeatureMember []*yandexFeatureMember `json:"featureMember"` |
| 25 | + } `json:"GeoObjectCollection"` |
| 26 | + } `json:"response"` |
| 27 | + } |
| 28 | + |
| 29 | + yandexFeatureMember struct { |
| 30 | + GeoObject struct { |
| 31 | + MetaDataProperty struct { |
| 32 | + GeocoderMetaData struct { |
| 33 | + Kind string `json:"kind"` |
| 34 | + Text string `json:"text"` |
| 35 | + Precision string `json:"precision"` |
| 36 | + Address struct { |
| 37 | + CountryCode string `json:"country_code"` |
| 38 | + PostalCode string `json:"postal_code"` |
| 39 | + Formatted string `json:"formatted"` |
| 40 | + Components []struct { |
| 41 | + Kind string `json:"kind"` |
| 42 | + Name string `json:"name"` |
| 43 | + } `json:"Components"` |
| 44 | + } `json:"Address"` |
| 45 | + } `json:"GeocoderMetaData"` |
| 46 | + } `json:"metaDataProperty"` |
| 47 | + Description string `json:"description"` |
| 48 | + Name string `json:"name"` |
| 49 | + BoundedBy struct { |
| 50 | + Envelope struct { |
| 51 | + LowerCorner string `json:"lowerCorner"` |
| 52 | + UpperCorner string `json:"upperCorner"` |
| 53 | + } `json:"Envelope"` |
| 54 | + } `json:"boundedBy"` |
| 55 | + Point struct { |
| 56 | + Pos string `json:"pos"` |
| 57 | + } `json:"Point"` |
| 58 | + } `json:"GeoObject"` |
| 59 | + } |
| 60 | +) |
| 61 | + |
| 62 | +const ( |
| 63 | + componentTypeHouseNumber = "house" |
| 64 | + componentTypeStreetName = "street" |
| 65 | + componentTypeLocality = "locality" |
| 66 | + componentTypeStateDistrict = "area" |
| 67 | + componentTypeState = "province" |
| 68 | + componentTypeCountry = "country" |
| 69 | +) |
| 70 | + |
| 71 | +// Geocoder constructs Yandex geocoder |
| 72 | +func Geocoder(apiKey string, baseURLs ...string) geo.Geocoder { |
| 73 | + return geo.HTTPGeocoder{ |
| 74 | + EndpointBuilder: baseURL(getURL(apiKey, baseURLs...)), |
| 75 | + ResponseParserFactory: func() geo.ResponseParser { return &geocodeResponse{} }, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func getURL(apiKey string, baseURLs ...string) string { |
| 80 | + if len(baseURLs) > 0 { |
| 81 | + return baseURLs[0] |
| 82 | + } |
| 83 | + return fmt.Sprintf("https://geocode-maps.yandex.ru/1.x/?results=1&lang=en_US&format=json&apikey=%s&", apiKey) |
| 84 | +} |
| 85 | + |
| 86 | +func (b baseURL) GeocodeURL(address string) string { |
| 87 | + return string(b) + "geocode=" + address |
| 88 | +} |
| 89 | + |
| 90 | +func (b baseURL) ReverseGeocodeURL(l geo.Location) string { |
| 91 | + return string(b) + fmt.Sprintf("sco=latlong&geocode=%f,%f", l.Lat, l.Lng) |
| 92 | +} |
| 93 | + |
| 94 | +func (r *geocodeResponse) Location() (*geo.Location, error) { |
| 95 | + if r.Response.GeoObjectCollection.MetaDataProperty.GeocoderResponseMetaData.Found == "0" { |
| 96 | + return nil, nil |
| 97 | + } |
| 98 | + if len(r.Response.GeoObjectCollection.FeatureMember) == 0 { |
| 99 | + return nil, nil |
| 100 | + } |
| 101 | + featureMember := r.Response.GeoObjectCollection.FeatureMember[0] |
| 102 | + result := &geo.Location{} |
| 103 | + latLng := strings.Split(featureMember.GeoObject.Point.Pos, " ") |
| 104 | + if len(latLng) > 1 { |
| 105 | + // Yandex return geo coord in format "long lat" |
| 106 | + result.Lat, _ = strconv.ParseFloat(latLng[1], 64) |
| 107 | + result.Lng, _ = strconv.ParseFloat(latLng[0], 64) |
| 108 | + } |
| 109 | + |
| 110 | + return result, nil |
| 111 | +} |
| 112 | + |
| 113 | +func (r *geocodeResponse) Address() (*geo.Address, error) { |
| 114 | + if r.Response.GeoObjectCollection.MetaDataProperty.GeocoderResponseMetaData.Found == "0" { |
| 115 | + return nil, nil |
| 116 | + } |
| 117 | + if len(r.Response.GeoObjectCollection.FeatureMember) == 0 { |
| 118 | + return nil, nil |
| 119 | + } |
| 120 | + |
| 121 | + return parseYandexResult(r.Response.GeoObjectCollection.FeatureMember[0]), nil |
| 122 | +} |
| 123 | + |
| 124 | +func parseYandexResult(r *yandexFeatureMember) *geo.Address { |
| 125 | + addr := &geo.Address{} |
| 126 | + res := r.GeoObject.MetaDataProperty.GeocoderMetaData |
| 127 | + |
| 128 | + for _, comp := range res.Address.Components { |
| 129 | + switch comp.Kind { |
| 130 | + case componentTypeHouseNumber: |
| 131 | + addr.HouseNumber = comp.Name |
| 132 | + continue |
| 133 | + case componentTypeStreetName: |
| 134 | + addr.Street = comp.Name |
| 135 | + continue |
| 136 | + case componentTypeLocality: |
| 137 | + addr.City = comp.Name |
| 138 | + continue |
| 139 | + case componentTypeStateDistrict: |
| 140 | + addr.StateDistrict = comp.Name |
| 141 | + continue |
| 142 | + case componentTypeState: |
| 143 | + addr.State = comp.Name |
| 144 | + continue |
| 145 | + case componentTypeCountry: |
| 146 | + addr.Country = comp.Name |
| 147 | + continue |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + addr.Postcode = res.Address.PostalCode |
| 152 | + addr.CountryCode = res.Address.CountryCode |
| 153 | + addr.FormattedAddress = res.Address.Formatted |
| 154 | + |
| 155 | + return addr |
| 156 | +} |
0 commit comments