diff --git a/msc_pygeoapi/loader/marine_weather_realtime.py b/msc_pygeoapi/loader/marine_weather_realtime.py index 23877c83..754bc7ca 100644 --- a/msc_pygeoapi/loader/marine_weather_realtime.py +++ b/msc_pygeoapi/loader/marine_weather_realtime.py @@ -29,20 +29,17 @@ # ================================================================= from datetime import datetime, timedelta, timezone -import json import logging import os from pathlib import Path import re import click -from elasticsearch import exceptions from lxml import etree from parse import parse from msc_pygeoapi import cli_options from msc_pygeoapi.connector.elasticsearch_ import ElasticsearchConnector -from msc_pygeoapi.env import MSC_PYGEOAPI_BASEPATH from msc_pygeoapi.loader.base import BaseLoader from msc_pygeoapi.util import ( configure_es_connection, @@ -785,48 +782,63 @@ def _create_datetime_dict(self, datetime_elems): def _set_area_info(self): """ - Gets the area name from the marine weather XML document and - looks up the equivalent meteocode forecast polygon feature ID to - query the forecast_polygons_water ES index for the corresponding - document. If document is found, assigns the self.area class attribute - that contains region name, subregion name, area name and the - associated geometry. + Gets the area name from the English marine weather XML document and + queries the forecast_polygons_water ES index for the document whose + NAME field matches the area name, case-insensitively. The resulting + document's geometry is associated with the marine weather feature, + and area info (region name, subregion name, area name) is set for + both languages from their respective XML area elements. :return: `bool` representing successful setting of self.area attribute """ - for lang in self.xml_roots: - with open( - os.path.join( - MSC_PYGEOAPI_BASEPATH, - 'resources/meteocode_lookup.json', - ) - ) as json_file: - meteocode_lookup = json.load(json_file) - forecast_id = meteocode_lookup[self.region_name_code] - - try: - result = self.conn.Elasticsearch.get( - index=FORECAST_POLYGONS_WATER_ES_INDEX, - id=forecast_id, - _source=['geometry'] - ) + area_elem_en = self.xml_roots['en'].find('area') + area_name_en = ( + area_elem_en.text.strip() + if area_elem_en is not None and area_elem_en.text + else None + ) - geometry = result['_source'].get('geometry') + if not area_name_en: + LOGGER.warning('No area name found in en XML.') + return False - area = self._node_to_dict( - self.xml_roots[lang].find('area'), lang=lang - ) + query = { + 'query': { + 'term': { + 'properties.NAME.normalize': area_name_en + } + } + } - self._set_nested_value( - self.marine_weather_feature['properties'], - ['area'], - area - ) + result = self.conn.Elasticsearch.search( + index=FORECAST_POLYGONS_WATER_ES_INDEX, + body=query, + _source=['geometry'], + size=1 + ) + + hits = result['hits']['hits'] + if not hits: + LOGGER.warning( + f'Could not find forecast polygon document matching ' + f'NAME: {area_name_en}' + ) + return False - self.marine_weather_feature['geometry'] = geometry + self.marine_weather_feature['geometry'] = hits[0]['_source'].get( + 'geometry' + ) + + for lang in self.xml_roots: + area = self._node_to_dict( + self.xml_roots[lang].find('area'), lang=lang + ) + + self._set_nested_value( + self.marine_weather_feature['properties'], + ['area'], + area + ) - except exceptions.NotFoundError: - LOGGER.warning(f'Could not get forecast polygon document with id: {forecast_id}') # noqa - return False return True def _set_warning(self): diff --git a/msc_pygeoapi/resources/meteocode_lookup.json b/msc_pygeoapi/resources/meteocode_lookup.json deleted file mode 100644 index 263d23d0..00000000 --- a/msc_pygeoapi/resources/meteocode_lookup.json +++ /dev/null @@ -1,206 +0,0 @@ -{ -"m0000001": "fea1-724", -"m0000002": "fea1-278", -"m0000003": "fea1-472", -"m0000004": "fea1-730", -"m0000005": "fea1-275", -"m0000006": "fea1-99", -"m0000007": "fea1-745", -"m0000008": "fea1-719", -"m0000009": "fea1-26", -"m0000010": "fea1-35", -"m0000011": "fea1-175", -"m0000012": "fea1-754", -"m0000013": "fea1-131", -"m0000014": "fea1-514", -"m0000015": "fea1-178", -"m0000016": "fea1-722", -"m0000017": "fea1-150", -"m0000018": "fea1-153", -"m0000019": "fea1-718", -"m0000020": "fea1-511", -"m0000021": "fea1-170", -"m0000022": "fea1-723", -"m0000023": "fea1-284", -"m0000024": "fea1-237", -"m0000025": "fea1-83", -"m0000026": "fea1-508", -"m0000027": "fea1-546", -"m0000028": "fea1-31", -"m0000029": "fea1-717", -"m0000030": "fea1-174", -"m0000031": "fea1-623", -"m0000032": "fea1-617", -"m0000033": "fea1-388", -"m0000034": "fea1-173", -"m0000035": "fea1-181", -"m0000036": "fea1-425", -"m0000037": "fea1-759", -"m0000038": "fea1-483", -"m0000039": "fea1-517", -"m0000040": "fea1-480", -"m0000041": "fea1-477", -"m0000042": "fea1-478", -"m0000043": "fea1-37", -"m0000044": "fea1-737", -"m0000045": "fea1-738", -"m0000046": "fea1-184", -"m0000047": "fea1-616", -"m0000048": "fea1-523", -"m0000049": "fea1-453", -"m0000050": "fea1-526", -"m0000051": "fea1-479", -"m0000052": "fea1-73", -"m0000053": "fea1-207", -"m0000054": "fea1-615", -"m0000055": "fea1-632", -"m0000056": "fea1-445", -"m0000057": "fea1-502", -"m0000058": "fea1-735", -"m0000059": "fea1-505", -"m0000060": "fea1-187", -"m0000061": "fea1-736", -"m0000062": "fea1-287", -"m0000063": "fea1-39", -"m0000064": "fea1-30", -"m0000065": "fea1-38", -"m0000066": "fea1-627", -"m0000067": "fea1-520", -"m0000068": "fea1-734", -"m0000069": "fea1-95", -"m0000070": "fea1-564", -"m0000071": "fea1-561", -"m0000072": "fea1-86", -"m0000073": "fea1-463", -"m0000074": "fea1-614", -"m0000075": "fea1-451", -"m0000076": "fea1-331", -"m0000077": "fea1-452", -"m0000078": "fea1-613", -"m0000079": "fea1-57", -"m0000080": "fea1-106", -"m0000081": "fea1-211", -"m0000082": "fea1-208", -"m0000083": "fea1-631", -"m0000084": "fea1-408", -"m0000085": "fea1-442", -"m0000086": "fea1-391", -"m0000087": "fea1-64", -"m0000088": "fea1-444", -"m0000089": "fea1-443", -"m0000090": "fea1-190", -"m0000091": "fea1-449", -"m0000092": "fea1-220", -"m0000093": "fea1-214", -"m0000094": "fea1-733", -"m0000095": "fea1-293", -"m0000096": "fea1-290", -"m0000097": "fea1-470", -"m0000098": "fea1-54", -"m0000099": "fea1-553", -"m0000100": "fea1-586", -"m0000101": "fea1-461", -"m0000102": "fea1-34", -"m0000103": "fea1-469", -"m0000104": "fea1-348", -"m0000105": "fea1-741", -"m0000106": "fea1-50", -"m0000107": "fea1-585", -"m0000108": "fea1-649", -"m0000109": "fea1-758", -"m0000110": "fea1-447", -"m0000111": "fea1-610", -"m0000112": "fea1-36", -"m0000113": "fea1-612", -"m0000114": "fea1-587", -"m0000115": "fea1-757", -"m0000116": "fea1-496", -"m0000117": "fea1-493", -"m0000118": "fea1-499", -"m0000120": "fea1-647", -"m0000121": "fea1-310", -"m0000122": "fea1-217", -"m0000123": "fea1-568", -"m0000124": "fea1-53", -"m0000125": "fea1-746", -"m0000126": "fea1-600", -"m0000127": "fea1-630", -"m0000128": "fea1-606", -"m0000129": "fea1-727", -"m0000130": "fea1-365", -"m0000131": "fea1-648", -"m0000132": "fea1-582", -"m0000133": "fea1-579", -"m0000134": "fea1-603", -"m0000135": "fea1-468", -"m0000136": "fea1-611", -"m0000137": "fea1-659", -"m0000138": "fea1-486", -"m0000139": "fea1-489", -"m0000140": "fea1-48", -"m0000141": "fea1-129", -"m0000142": "fea1-241", -"m0000143": "fea1-653", -"m0000144": "fea1-115", -"m0000145": "fea1-456", -"m0000146": "fea1-576", -"m0000147": "fea1-576", -"m0000148": "fea1-656", -"m0000149": "fea1-590", -"m0000150": "fea1-130", -"m0000151": "fea1-595", -"m0000152": "fea1-49", -"m0000153": "fea1-596", -"m0000154": "fea1-258", -"m0000155": "fea1-594", -"m0000156": "fea1-385", -"m0000157": "fea1-593", -"m0000158": "fea1-535", -"m0000159": "fea1-530", -"m0000160": "fea1-538", -"m0000161": "fea1-103", -"m0000162": "fea1-567", -"m0000163": "fea1-281", -"m0000164": "fea1-61", -"m0000165": "fea1-314", -"m0000166": "fea1-729", -"m0000167": "fea1-728", -"m0000168": "fea1-450", -"m0000169": "fea1-680", -"m0000170": "fea1-685", -"m0000171": "fea1-643", -"m0000172": "fea1-681", -"m0000173": "fea1-698", -"m0000174": "fea1-689", -"m0000175": "fea1-692", -"m0000176": "fea1-696", -"m0000177": "fea1-683", -"m0000178": "fea1-641", -"m0000179": "fea1-652", -"m0000180": "fea1-642", -"m0000181": "fea1-651", -"m0000182": "fea1-646", -"m0000183": "fea1-650", -"m0000184": "fea1-638", -"m0000185": "fea1-660", -"m0000186": "fea1-645", -"m0000187": "fea1-687", -"m0000188": "fea1-684", -"m0000189": "fea1-697", -"m0000190": "fea1-690", -"m0000191": "fea1-661", -"m0000192": "fea1-682", -"m0000193": "fea1-644", -"m0000194": "fea1-691", -"m0000195": "fea1-663", -"m0000196": "fea1-662", -"m0000197": "fea1-699", -"m0000198": "fea1-126", -"m0000199": "fea1-127", -"m0000200": "fea1-700", -"m0000201": "fea1-695", -"m0000202": "fea1-694", -"m0000203": "fea1-686", -"m0000204": "fea1-679", -"m0000205": "fea1-670" -}