from pydov.util.notebook import HtmlFormatter
from pydov.util.wrappers import AbstractDictLike
from pydov.util.location import EpsgValidator
[docs]
class ReturnFieldList(list):
"""List of return fields used in search methods. """
def __contains__(self, __key: object):
"""Overwrite the default method. Checks on field name.
Parameters
----------
__key
The key to check.
Returns
-------
boolean
Whether the key is one of the names in the ReturnFieldList.
"""
return __key in [i.name for i in self]
[docs]
def get_names(self):
"""Return a list with all the names of the fields in this
ReturnFieldList.
Returns
-------
list of str
List of field names.
"""
return [f.name for f in self]
[docs]
@classmethod
def from_field_names(self, *return_fields):
"""Initiale a ReturnFieldList from a list of return field names.
Parameters
----------
return_fields : list or set or tuple of str
List, set or tuple of return field names.
Returns
-------
ReturnFieldList
Equivalent ReturnFieldList.
Raises
------
AttributeError
If the value of return_fields is not a list, set or tuple.
"""
if len(return_fields) == 0:
return None
if len(return_fields) == 1:
return_fields = return_fields[0]
if isinstance(return_fields, str):
return_fields = (return_fields,)
if return_fields is None:
return None
if not isinstance(return_fields, (list, set, tuple)):
# FIXME: this should be TypeError instead
raise AttributeError(
'return_fields should be a list, set or tuple')
result = ReturnFieldList()
for rf in return_fields:
if isinstance(rf, AbstractReturnField):
result.append(rf)
else:
result.append(ReturnField.from_field_name(rf))
return result
[docs]
class AbstractReturnField:
"""Base class of ReturnField and GeometryReturnField."""
def __init__(self, name):
"""Initialisation.
Parameters
----------
name : str
Name of the return field.
"""
self.name = name
[docs]
@classmethod
def from_field_name(cls, name):
"""Initialise a new instance from a field name.
Parameters
----------
name : str
Field name.
Returns
-------
Instance of this class.
Instance of ReturnField or GeometryReturnField.
"""
return cls(name)
[docs]
class ReturnField(AbstractReturnField):
"""Normal (non-geometry) return field."""
def __init__(self, name):
"""Initialisation.
Parameters
----------
name : str
Name of the return field.
"""
super().__init__(name)
[docs]
class GeometryReturnField(AbstractReturnField, EpsgValidator):
def __init__(self, geometry_field, epsg=None):
"""Initialise a geometry return field.
Parameters
----------
geometry_field : str
Name of the geometry field.
epsg : int
EPSG code of the CRS of the geometries that will be returned.
Raises
------
TypeError
If `epsg` is None, missing or not an integer.
ValueError
If `epsg` invalid.
Notes
-----
See https://epsg.io for a list of valid EPSG codes.
"""
super().__init__(geometry_field)
self._validate_epsg(epsg)
self.epsg = epsg