|
1 | | -# Copyright (C) 2021 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131 |
2 | | -# Karlsruhe, Germany. |
3 | | -# |
4 | | -# This program is free software: you can redistribute it and/or modify |
5 | | -# it under the terms of the GNU Lesser General Public License as published by |
6 | | -# the Free Software Foundation, either version 3 of the License, or |
7 | | -# (at your option) any later version. |
8 | | -# |
9 | | -# This program is distributed in the hope that it will be useful, |
10 | | -# but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | -# GNU Lesser General Public License for more details. |
13 | | -# |
14 | | -# You should have received a copy of the GNU Lesser General Public License |
15 | | -# along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | | - |
17 | | -import jsonpickle |
18 | | -import datetime |
19 | | -from dateutil.parser import isoparse |
20 | | -import geojson |
21 | | -import logging |
22 | | -import sys |
23 | | -import frost_sta_client.model.ext.entity_list |
24 | | - |
25 | | - |
26 | | -def extract_value(location): |
27 | | - try: |
28 | | - value = int(location[location.find('(')+1: location.find(')')]) |
29 | | - except ValueError: |
30 | | - value = str(location[location.find('(')+2: location.find(')')-1]) |
31 | | - return value |
32 | | - |
33 | | -def transform_entity_to_json_dict(entity): |
34 | | - json_str = jsonpickle.encode(entity, unpicklable=False) |
35 | | - return jsonpickle.decode(json_str) |
36 | | - |
37 | | -def class_from_string(string): |
38 | | - module_name, class_name = string.rsplit(".", 1) |
39 | | - return getattr(sys.modules[module_name], class_name) |
40 | | - |
41 | | -def transform_json_to_entity(json_response, entity_class): |
42 | | - cl = class_from_string(entity_class) |
43 | | - obj = cl() |
44 | | - obj.__setstate__(json_response) |
45 | | - return obj |
46 | | - |
47 | | -def transform_json_to_entity_list(json_response, entity_class): |
48 | | - entity_list = frost_sta_client.model.ext.entity_list.EntityList(entity_class) |
49 | | - result_list = [] |
50 | | - if isinstance(json_response, dict): |
51 | | - try: |
52 | | - response_list = json_response['value'] |
53 | | - entity_list.next_link = json_response.get("@iot.nextLink", None) |
54 | | - entity_list.count = json_response.get("@iot.count", None) |
55 | | - except AttributeError as e: |
56 | | - raise e |
57 | | - elif isinstance(json_response, list): |
58 | | - response_list = json_response |
59 | | - else: |
60 | | - raise ValueError("expected json as a dict or list to transform into entity list") |
61 | | - entity_list.entities = [transform_json_to_entity(item, entity_list.entity_class) for item in response_list] |
62 | | - return entity_list |
63 | | - |
64 | | - |
65 | | -def check_datetime(value, time_entity): |
66 | | - try: |
67 | | - parse_datetime(value) |
68 | | - except ValueError as e: |
69 | | - logging.error(f"error during {time_entity} check") |
70 | | - raise e |
71 | | - return value |
72 | | - |
73 | | - |
74 | | -def parse_datetime(value) -> str: |
75 | | - if value is None: |
76 | | - return value |
77 | | - if isinstance(value, str): |
78 | | - if '/' in value: |
79 | | - try: |
80 | | - times = value.split('/') |
81 | | - if len(times) != 2: |
82 | | - raise ValueError("If the time interval is provided as a string," |
83 | | - " it should be in isoformat") |
84 | | - result = [isoparse(times[0]), |
85 | | - isoparse(times[1])] |
86 | | - except ValueError: |
87 | | - raise ValueError("If the time entity interval is provided as a string," |
88 | | - " it should be in isoformat") |
89 | | - result = result[0].isoformat() + '/' + result[1].isoformat() |
90 | | - return result |
91 | | - else: |
92 | | - try: |
93 | | - result = isoparse(value) |
94 | | - except ValueError: |
95 | | - raise ValueError("If the phenomenon time is provided as string, it should be in isoformat") |
96 | | - result = result.isoformat() |
97 | | - return result |
98 | | - if isinstance(value, datetime.datetime): |
99 | | - return value.isoformat() |
100 | | - if isinstance(value, list) and all(isinstance(v, datetime.datetime) for v in value): |
101 | | - return value[0].isoformat() + value[1].isoformat() |
102 | | - else: |
103 | | - raise ValueError('time entities should consist of one or two datetimes') |
104 | | - |
105 | | - |
106 | | -def process_area(value): |
107 | | - if not isinstance(value, dict): |
108 | | - raise ValueError("geojsons can only be handled as dictionaries!") |
109 | | - if value.get("type", None) is None or value.get("coordinates", None) is None: |
110 | | - raise ValueError("Both type and coordinates need to be specified in the dictionary") |
111 | | - if value["type"] == "Point": |
112 | | - return geojson.geometry.Point(value["coordinates"]) |
113 | | - if value["type"] == "Polygon": |
114 | | - return geojson.geometry.Polygon(value["coordinates"]) |
115 | | - if value["type"] == "Geometry": |
116 | | - return geojson.geometry.Geometry(value["coordinates"]) |
117 | | - if value["type"] == "LineString": |
118 | | - return geojson.geometry.LineString(value["coordinates"]) |
119 | | - raise ValueError("can only handle geojson of type Point, Polygon, Geometry or LineString") |
| 1 | +# Copyright (C) 2021 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131 |
| 2 | +# Karlsruhe, Germany. |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU Lesser General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU Lesser General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU Lesser General Public License |
| 15 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +import jsonpickle |
| 18 | +import datetime |
| 19 | +from dateutil.parser import isoparse |
| 20 | +import geojson |
| 21 | +import logging |
| 22 | +import sys |
| 23 | +import frost_sta_client.model.ext.entity_list |
| 24 | + |
| 25 | + |
| 26 | +def extract_value(location): |
| 27 | + try: |
| 28 | + value = int(location[location.find('(')+1: location.find(')')]) |
| 29 | + except ValueError: |
| 30 | + value = str(location[location.find('(')+2: location.find(')')-1]) |
| 31 | + return value |
| 32 | + |
| 33 | +def transform_entity_to_json_dict(entity): |
| 34 | + try: |
| 35 | + data = entity.__getstate__() |
| 36 | + except AttributeError: |
| 37 | + data = entity.__dict__ |
| 38 | + return data |
| 39 | + |
| 40 | +def class_from_string(string): |
| 41 | + module_name, class_name = string.rsplit(".", 1) |
| 42 | + return getattr(sys.modules[module_name], class_name) |
| 43 | + |
| 44 | +def transform_json_to_entity(json_response, entity_class): |
| 45 | + cl = class_from_string(entity_class) |
| 46 | + obj = cl() |
| 47 | + obj.__setstate__(json_response) |
| 48 | + return obj |
| 49 | + |
| 50 | +def transform_json_to_entity_list(json_response, entity_class): |
| 51 | + entity_list = frost_sta_client.model.ext.entity_list.EntityList(entity_class) |
| 52 | + result_list = [] |
| 53 | + if isinstance(json_response, dict): |
| 54 | + try: |
| 55 | + response_list = json_response['value'] |
| 56 | + entity_list.next_link = json_response.get("@iot.nextLink", None) |
| 57 | + entity_list.count = json_response.get("@iot.count", None) |
| 58 | + except AttributeError as e: |
| 59 | + raise e |
| 60 | + elif isinstance(json_response, list): |
| 61 | + response_list = json_response |
| 62 | + else: |
| 63 | + raise ValueError("expected json as a dict or list to transform into entity list") |
| 64 | + entity_list.entities = [transform_json_to_entity(item, entity_list.entity_class) for item in response_list] |
| 65 | + return entity_list |
| 66 | + |
| 67 | + |
| 68 | +def check_datetime(value, time_entity): |
| 69 | + try: |
| 70 | + parse_datetime(value) |
| 71 | + except ValueError as e: |
| 72 | + logging.error(f"error during {time_entity} check") |
| 73 | + raise e |
| 74 | + return value |
| 75 | + |
| 76 | + |
| 77 | +def parse_datetime(value) -> str: |
| 78 | + if value is None: |
| 79 | + return value |
| 80 | + if isinstance(value, str): |
| 81 | + if '/' in value: |
| 82 | + try: |
| 83 | + times = value.split('/') |
| 84 | + if len(times) != 2: |
| 85 | + raise ValueError("If the time interval is provided as a string," |
| 86 | + " it should be in isoformat") |
| 87 | + result = [isoparse(times[0]), |
| 88 | + isoparse(times[1])] |
| 89 | + except ValueError: |
| 90 | + raise ValueError("If the time entity interval is provided as a string," |
| 91 | + " it should be in isoformat") |
| 92 | + result = result[0].isoformat() + '/' + result[1].isoformat() |
| 93 | + return result |
| 94 | + else: |
| 95 | + try: |
| 96 | + result = isoparse(value) |
| 97 | + except ValueError: |
| 98 | + raise ValueError("If the phenomenon time is provided as string, it should be in isoformat") |
| 99 | + result = result.isoformat() |
| 100 | + return result |
| 101 | + if isinstance(value, datetime.datetime): |
| 102 | + return value.isoformat() |
| 103 | + if isinstance(value, list) and all(isinstance(v, datetime.datetime) for v in value): |
| 104 | + return value[0].isoformat() + value[1].isoformat() |
| 105 | + else: |
| 106 | + raise ValueError('time entities should consist of one or two datetimes') |
| 107 | + |
| 108 | + |
| 109 | +def process_area(value): |
| 110 | + if not isinstance(value, dict): |
| 111 | + raise ValueError("geojsons can only be handled as dictionaries!") |
| 112 | + if value.get("type", None) is None or value.get("coordinates", None) is None: |
| 113 | + raise ValueError("Both type and coordinates need to be specified in the dictionary") |
| 114 | + if value["type"] == "Point": |
| 115 | + return geojson.geometry.Point(value["coordinates"]) |
| 116 | + if value["type"] == "Polygon": |
| 117 | + return geojson.geometry.Polygon(value["coordinates"]) |
| 118 | + if value["type"] == "Geometry": |
| 119 | + return geojson.geometry.Geometry(value["coordinates"]) |
| 120 | + if value["type"] == "LineString": |
| 121 | + return geojson.geometry.LineString(value["coordinates"]) |
| 122 | + raise ValueError("can only handle geojson of type Point, Polygon, Geometry or LineString") |
0 commit comments