def _expand_properties(self, entity, keys=(), uri=''): """ Private function: Parse STA entity into feature :param entity: SensorThings entity :param keys: keys used in properties block :param uri: uri of STA entity :returns: dict of SensorThings feature properties """ if self.entity == 'Things': extra_props = entity['Locations'][0].get('properties', {}) entity['properties'].update(extra_props) elif 'Thing' in entity.keys(): t = entity.get('Thing') extra_props = t['Locations'][0].get('properties', {}) t['properties'].update(extra_props) for k, v in entity.items(): # Create intra links path_ = 'collections/{}/items/{}' ks = f'{k}s' if self.uri_field is not None and k in ['properties']: uri = v.get(self.uri_field, '') elif k in self._linkables.keys(): if self._linkables[k]['u'] != '': for i, _v in enumerate(v): v[i] = _v['properties'][self._linkables[k]['u']] continue for i, _v in enumerate(v): id = _v[self.id_field] id = f"'{id}'" if isinstance(id, str) else str(id) v[i] = url_join(self._rel_link, path_.format(self._linkables[k]['n'], id)) elif ks in self._linkables.keys(): if self._linkables[ks]['u'] != '': entity[k] = v['properties'][self._linkables[ks]['u']] continue id = v[self.id_field] id = f"'{id}'" if isinstance(id, str) else str(id) entity[k] = url_join( self._rel_link, path_.format(self._linkables[ks]['n'], id)) # Make properties block if entity.get('properties', None): entity.update(entity.pop('properties')) if keys: ret = {} for k in keys: ret[k] = entity.pop(k) entity = ret # Retain URI if present if self.uri_field is not None and uri != '': entity[self.uri_field] = uri return entity
def __init__(self, provider_def): """ STA Class constructor :param provider_def: provider definitions from yml pygeoapi-config. data,id_field, name set in parent class :returns: pygeoapi.provider.base.SensorThingsProvider """ LOGGER.debug("Logger STA Init") super().__init__(provider_def) try: self.entity = provider_def['entity'] self._url = url_join(self.data, self.entity) except KeyError: raise RuntimeError('name/type/data are required') # Default id if self.id_field is None or not self.id_field: self.id_field = '@iot.id' # Create intra-links self.intralink = provider_def.get('intralink', False) self._linkables = {} if provider_def.get('rel_link') and self.intralink: # For pytest self._rel_link = provider_def['rel_link'] else: self._from_env() self.get_fields()
def get_tiles_service(self, baseurl=None, servicepath=None, dirpath=None, tile_type=None): """ Gets mvt service description :param baseurl: base URL of endpoint :param servicepath: base path of URL :param dirpath: directory basepath (equivalent of URL) :param tile_type: tile format type :returns: `dict` of item tile service """ url = urlparse(self.data) baseurl = baseurl or '{}://{}'.format(url.scheme, url.netloc) # @TODO: support multiple types tile_type = tile_type or self.format_type servicepath = \ servicepath or \ '{}/tiles/{{{}}}/{{{}}}/{{{}}}/{{{}}}{}'.format( url.path.split('/{z}/{x}/{y}')[0], 'tileMatrixSetId', 'tileMatrix', 'tileRow', 'tileCol', tile_type) if servicepath.startswith(baseurl): self._service_url = servicepath else: self._service_url = url_join(baseurl, servicepath) self._service_metadata_url = urljoin( self.service_url.split('{tileMatrix}/{tileRow}/{tileCol}')[0], 'metadata') links = { 'links': [{ 'type': self.mimetype, 'rel': 'item', 'title': 'This collection as Mapbox vector tiles', 'href': self.service_url, 'templated': True }, { 'type': 'application/json', 'rel': 'describedby', 'title': 'Metadata for this collection in the TileJSON format', 'href': '{}?f=json'.format(self.service_metadata_url), 'templated': True }] } return links
def __init__(self, provider_def): """ Initialize object :param provider_def: provider definition :returns: pygeoapi.provider.MVT.MVTProvider """ super().__init__(provider_def) if is_url(self.data): url = urlparse(self.data) baseurl = '{}://{}'.format(url.scheme, url.netloc) param_type = '?f=mvt' servicepath = \ '{}/tiles/{{{}}}/{{{}}}/{{{}}}/{{{}}}{}'.format( url.path.split('/{z}/{x}/{y}')[0], 'tileMatrixSetId', 'tileMatrix', 'tileRow', 'tileCol', param_type) self._service_url = url_join(baseurl, servicepath) self._service_metadata_url = urljoin( self.service_url.split('{tileMatrix}/{tileRow}/{tileCol}')[0], 'metadata') else: data_path = Path(self.data) if not data_path.exists(): msg = 'Service does not exist: {}'.format(self.data) LOGGER.error(msg) raise ProviderConnectionError(msg) self._service_url = data_path metadata_path = data_path.joinpath('metadata.json') if not metadata_path.exists(): msg = 'Service metadata does not exist: {}'.format( metadata_path.name) LOGGER.error(msg) raise ProviderConnectionError(msg) self._service_metadata_url = metadata_path