1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
require 'geocoder/results/base'
module Geocoder::Result
class Yandex < Base
# Yandex result has difficult tree structure,
# and presence of some nodes depends on exact search case.
# Also Yandex lacks documentation about it.
# See https://tech.yandex.com/maps/doc/geocoder/desc/concepts/response_structure-docpage/
# Ultimatly, we need to find Locality and/or Thoroughfare data.
# It may resides on the top (ADDRESS_DETAILS) level.
# example: 'Baltic Sea'
# "AddressDetails": {
# "Locality": {
# "Premise": {
# "PremiseName": "Baltic Sea"
# }
# }
# }
ADDRESS_DETAILS = %w[
GeoObject metaDataProperty GeocoderMetaData
AddressDetails
].freeze
# On COUNTRY_LEVEL.
# example: 'Potomak'
# "AddressDetails": {
# "Country": {
# "AddressLine": "reka Potomak",
# "CountryNameCode": "US",
# "CountryName": "United States of America",
# "Locality": {
# "Premise": {
# "PremiseName": "reka Potomak"
# }
# }
# }
# }
COUNTRY_LEVEL = %w[
GeoObject metaDataProperty GeocoderMetaData
AddressDetails Country
].freeze
# On ADMIN_LEVEL (usually state or city)
# example: 'Moscow, Tverskaya'
# "AddressDetails": {
# "Country": {
# "AddressLine": "Moscow, Tverskaya Street",
# "CountryNameCode": "RU",
# "CountryName": "Russia",
# "AdministrativeArea": {
# "AdministrativeAreaName": "Moscow",
# "Locality": {
# "LocalityName": "Moscow",
# "Thoroughfare": {
# "ThoroughfareName": "Tverskaya Street"
# }
# }
# }
# }
# }
ADMIN_LEVEL = %w[
GeoObject metaDataProperty GeocoderMetaData
AddressDetails Country
AdministrativeArea
].freeze
# On SUBADMIN_LEVEL (may refer to urban district)
# example: 'Moscow Region, Krasnogorsk'
# "AddressDetails": {
# "Country": {
# "AddressLine": "Moscow Region, Krasnogorsk",
# "CountryNameCode": "RU",
# "CountryName": "Russia",
# "AdministrativeArea": {
# "AdministrativeAreaName": "Moscow Region",
# "SubAdministrativeArea": {
# "SubAdministrativeAreaName": "gorodskoy okrug Krasnogorsk",
# "Locality": {
# "LocalityName": "Krasnogorsk"
# }
# }
# }
# }
# }
SUBADMIN_LEVEL = %w[
GeoObject metaDataProperty GeocoderMetaData
AddressDetails Country
AdministrativeArea
SubAdministrativeArea
].freeze
# On DEPENDENT_LOCALITY_1 (may refer to district of city)
# example: 'Paris, Etienne Marcel'
# "AddressDetails": {
# "Country": {
# "AddressLine": "Île-de-France, Paris, 1er Arrondissement, Rue Étienne Marcel",
# "CountryNameCode": "FR",
# "CountryName": "France",
# "AdministrativeArea": {
# "AdministrativeAreaName": "Île-de-France",
# "Locality": {
# "LocalityName": "Paris",
# "DependentLocality": {
# "DependentLocalityName": "1er Arrondissement",
# "Thoroughfare": {
# "ThoroughfareName": "Rue Étienne Marcel"
# }
# }
# }
# }
# }
# }
DEPENDENT_LOCALITY_1 = %w[
GeoObject metaDataProperty GeocoderMetaData
AddressDetails Country
AdministrativeArea Locality
DependentLocality
].freeze
# On DEPENDENT_LOCALITY_2 (for special cases like turkish "mahalle")
# https://en.wikipedia.org/wiki/Mahalle
# example: 'Istanbul Mabeyinci Yokuşu 17'
# "AddressDetails": {
# "Country": {
# "AddressLine": "İstanbul, Fatih, Saraç İshak Mah., Mabeyinci Yokuşu, 17",
# "CountryNameCode": "TR",
# "CountryName": "Turkey",
# "AdministrativeArea": {
# "AdministrativeAreaName": "İstanbul",
# "SubAdministrativeArea": {
# "SubAdministrativeAreaName": "Fatih",
# "Locality": {
# "DependentLocality": {
# "DependentLocalityName": "Saraç İshak Mah.",
# "Thoroughfare": {
# "ThoroughfareName": "Mabeyinci Yokuşu",
# "Premise": {
# "PremiseNumber": "17"
# }
# }
# }
# }
# }
# }
# }
# }
DEPENDENT_LOCALITY_2 = %w[
GeoObject metaDataProperty GeocoderMetaData
AddressDetails Country
AdministrativeArea
SubAdministrativeArea Locality
DependentLocality
].freeze
def coordinates
@data['GeoObject']['Point']['pos'].split(' ').reverse.map(&:to_f)
end
def address(_format = :full)
@data['GeoObject']['metaDataProperty']['GeocoderMetaData']['text']
end
def city
result =
if state.empty?
find_in_hash(@data, *COUNTRY_LEVEL, 'Locality', 'LocalityName')
elsif sub_state.empty?
find_in_hash(@data, *ADMIN_LEVEL, 'Locality', 'LocalityName')
else
find_in_hash(@data, *SUBADMIN_LEVEL, 'Locality', 'LocalityName')
end
result || ""
end
def country
find_in_hash(@data, *COUNTRY_LEVEL, 'CountryName') || ""
end
def country_code
find_in_hash(@data, *COUNTRY_LEVEL, 'CountryNameCode') || ""
end
def state
find_in_hash(@data, *ADMIN_LEVEL, 'AdministrativeAreaName') || ""
end
def sub_state
return "" if state.empty?
find_in_hash(@data, *SUBADMIN_LEVEL, 'SubAdministrativeAreaName') || ""
end
def state_code
""
end
def street
thoroughfare_data.is_a?(Hash) ? thoroughfare_data['ThoroughfareName'] : ""
end
def street_number
premise.is_a?(Hash) ? premise.fetch('PremiseNumber', "") : ""
end
def premise_name
premise.is_a?(Hash) ? premise.fetch('PremiseName', "") : ""
end
def postal_code
return "" unless premise.is_a?(Hash)
find_in_hash(premise, 'PostalCode', 'PostalCodeNumber') || ""
end
def kind
@data['GeoObject']['metaDataProperty']['GeocoderMetaData']['kind']
end
def precision
@data['GeoObject']['metaDataProperty']['GeocoderMetaData']['precision']
end
def viewport
envelope = @data['GeoObject']['boundedBy']['Envelope'] || fail
east, north = envelope['upperCorner'].split(' ').map(&:to_f)
west, south = envelope['lowerCorner'].split(' ').map(&:to_f)
[south, west, north, east]
end
private # ----------------------------------------------------------------
def top_level_locality
find_in_hash(@data, *ADDRESS_DETAILS, 'Locality')
end
def country_level_locality
find_in_hash(@data, *COUNTRY_LEVEL, 'Locality')
end
def admin_locality
find_in_hash(@data, *ADMIN_LEVEL, 'Locality')
end
def subadmin_locality
find_in_hash(@data, *SUBADMIN_LEVEL, 'Locality')
end
def dependent_locality
find_in_hash(@data, *DEPENDENT_LOCALITY_1) ||
find_in_hash(@data, *DEPENDENT_LOCALITY_2)
end
def locality_data
dependent_locality || subadmin_locality || admin_locality ||
country_level_locality || top_level_locality
end
def thoroughfare_data
locality_data['Thoroughfare'] if locality_data.is_a?(Hash)
end
def premise
if thoroughfare_data.is_a?(Hash)
thoroughfare_data['Premise']
elsif locality_data.is_a?(Hash)
locality_data['Premise']
end
end
def find_in_hash(source, *keys)
key = keys.shift
result = source[key]
if keys.empty?
return result
elsif !result.is_a?(Hash)
return nil
end
find_in_hash(result, *keys)
end
end
end
|