Getting street name from json for different types of settlements

有些话、适合烂在心里 提交于 2020-07-23 08:05:27

问题


I am feeding yandex geokoder service with a list of address lines that are within one city but sometimes they are in different settlements within that city:

city Moscow, Yegora Abakumova Street, 9
city Moscow, Moskovskiy Settlement, Village Lapshinka, вл8к1
city Moscow, city Zelenograd, bld 438
city Moscow, Moskovskiy Settlement, Village Lapshinka, вл8к1

(I am sending addresses in russian language, they are translated above). I need to get the street name from API response in json, which looks like this:

    {'response': {'GeoObjectCollection': {'metaDataProperty': {'GeocoderResponseMetaData': {'request': 'Россия, Москва, улица Егора Абакумова, 9',
     'results': '10',
     'found': '1'}},
   'featureMember': [{'GeoObject': {'metaDataProperty': {'GeocoderMetaData': {'precision': 'exact',
        'text': 'Russia, Moscow, Yegora Abakumova Street, 9',
        'kind': 'house',
        'Address': {'country_code': 'RU',
         'formatted': 'Russia, Moscow, Yegora Abakumova Street, 9',
         'postal_code': '129347',
         'Components': [{'kind': 'country', 'name': 'Russia'},
          {'kind': 'province', 'name': 'Tsentralny federalny okrug'},
          {'kind': 'province', 'name': 'Moscow'},
          {'kind': 'locality', 'name': 'Moscow'},
          {'kind': 'street', 'name': 'Yegora Abakumova Street'},
          {'kind': 'house', 'name': '9'}]},
        'AddressDetails': {'Country': {'AddressLine': 'Russia, Moscow, Yegora Abakumova Street, 9',
          'CountryNameCode': 'RU',
          'CountryName': 'Russia',
          'AdministrativeArea': {'AdministrativeAreaName': 'Moscow',
           'Locality': {'LocalityName': 'Moscow',
            'Thoroughfare': {'ThoroughfareName': 'Yegora Abakumova Street',
             'Premise': {'PremiseNumber': '9',
              'PostalCode': {'PostalCodeNumber': '129347'}}}}}}}}},
      'name': 'Yegora Abakumova Street, 9',
      'description': 'Moscow, Russia',
      'boundedBy': {'Envelope': {'lowerCorner': '37.710368 55.876698',
        'upperCorner': '37.718579 55.881313'}},
      'Point': {'pos': '37.714474 55.879005'}}}]}}}

In a simple case of listed above, I am getting street name like that:

response['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['metaDataProperty']['GeocoderMetaData']['AddressDetails']['Country']['AdministrativeArea']['Locality']['Thoroughfare']['ThoroughfareName']

I suspenct that it is not the simplest way to do this, and would welcome your suggestions. In case of the settlement, the description is a bit different, so I am getting another level before the street name — SubAdministrativeArea:

    {'response': {'GeoObjectCollection': {'metaDataProperty': {'GeocoderResponseMetaData': {'request': 'город Москва, поселение Марушкинское, деревня Марушкино, Полевая улица, дом 16',
     'results': '10',
     'found': '1'}},
   'featureMember': [{'GeoObject': {'metaDataProperty': {'GeocoderMetaData': {'precision': 'exact',
        'text': 'Russia, Moscow, Marushkinskoye Settlement, derevnya Marushkino, Polevaya ulitsa, 16',
        'kind': 'house',
        'Address': {'country_code': 'RU',
         'formatted': 'Russia, Moscow, Marushkinskoye Settlement, derevnya Marushkino, Polevaya ulitsa, 16',
         'Components': [{'kind': 'country', 'name': 'Russia'},
          {'kind': 'province', 'name': 'Tsentralny federalny okrug'},
          {'kind': 'province', 'name': 'Moscow'},
          {'kind': 'area', 'name': 'Novomoskovskiy Administrative Okrug'},
          {'kind': 'area', 'name': 'Marushkinskoye Settlement'},
          {'kind': 'locality', 'name': 'derevnya Marushkino'},
          {'kind': 'street', 'name': 'Polevaya ulitsa'},
          {'kind': 'house', 'name': '16'}]},
        'AddressDetails': {'Country': {'AddressLine': 'Russia, Moscow, Marushkinskoye Settlement, derevnya Marushkino, Polevaya ulitsa, 16',
          'CountryNameCode': 'RU',
          'CountryName': 'Russia',
          'AdministrativeArea': {'AdministrativeAreaName': 'Moscow',
           'SubAdministrativeArea': {'SubAdministrativeAreaName': 'Novomoskovskiy Administrative Okrug',
            'Locality': {'LocalityName': 'derevnya Marushkino',
             'Thoroughfare': {'ThoroughfareName': 'Polevaya ulitsa',
              'Premise': {'PremiseNumber': '16'}}}}}}}}},
      'name': 'Polevaya ulitsa, 16',
      'description': 'derevnya Marushkino, Marushkinskoye Settlement, Moscow, Russia',
      'boundedBy': {'Envelope': {'lowerCorner': '37.181503 55.589229',
        'upperCorner': '37.189714 55.593878'}},
      'Point': {'pos': '37.185608 55.591554'}}}]}}}

and here I need to get eighter ThoroughfareName or 'kind': 'street', 'name' that is within Address components, in the followoging way:

response['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['metaDataProperty']['GeocoderMetaData']['Address']['Components'][6]['name']

or:

response['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['metaDataProperty']['GeocoderMetaData']['AddressDetails']['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['Thoroughfare']['ThoroughfareName']

So I want need to get only 'Polevaya ulitsa' by asking for {'kind': 'street', 'name': or 'ThoroughfareName' What is the simplest way of searching for just the street name or ThoroughfareName, regardless of the type of the settlement, that would not include index (number) of the address component?

I asked that question on russian stackoverflow, but provided answer in javascript does not work for me: response['response']['GeoObjectCollection']['featureMember'][0]['GeoObject']['metaDataProperty']['GeocoderMetaData']['Address']['Components'].filter(item => item.kind === 'street')[0]['name']

来源:https://stackoverflow.com/questions/62590167/getting-street-name-from-json-for-different-types-of-settlements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!