|
6 | 6 | # John A Stevenson <jostev@bgs.ac.uk> |
7 | 7 | # Colin Blackburn <colb@bgs.ac.uk> |
8 | 8 | # Francesco Bartoli <xbartolone@gmail.com> |
| 9 | +# Bernhard Mallinger <bernhard.mallinger@eox.at> |
9 | 10 | # |
10 | 11 | # Copyright (c) 2018 Jorge Samuel Mendes de Jesus |
11 | 12 | # Copyright (c) 2024 Tom Kralidis |
12 | 13 | # Copyright (c) 2022 John A Stevenson and Colin Blackburn |
13 | 14 | # Copyright (c) 2023 Francesco Bartoli |
| 15 | +# Copyright (c) 2024 Bernhard Mallinger |
14 | 16 | # |
15 | 17 | # Permission is hereby granted, free of charge, to any person |
16 | 18 | # obtaining a copy of this software and associated documentation |
|
56 | 58 |
|
57 | 59 | from geoalchemy2 import Geometry # noqa - this isn't used explicitly but is needed to process Geometry columns |
58 | 60 | from geoalchemy2.functions import ST_MakeEnvelope |
59 | | -from geoalchemy2.shape import to_shape |
| 61 | +from geoalchemy2.shape import to_shape, from_shape |
60 | 62 | from pygeofilter.backends.sqlalchemy.evaluate import to_filter |
61 | 63 | import pyproj |
62 | 64 | import shapely |
63 | | -from sqlalchemy import create_engine, MetaData, PrimaryKeyConstraint, asc, desc |
| 65 | +from sqlalchemy import create_engine, MetaData, PrimaryKeyConstraint, asc, \ |
| 66 | + desc, delete |
64 | 67 | from sqlalchemy.engine import URL |
65 | 68 | from sqlalchemy.exc import ConstraintColumnNotFoundError, \ |
66 | 69 | InvalidRequestError, OperationalError |
|
69 | 72 | from sqlalchemy.sql.expression import and_ |
70 | 73 |
|
71 | 74 | from pygeoapi.provider.base import BaseProvider, \ |
72 | | - ProviderConnectionError, ProviderQueryError, ProviderItemNotFoundError |
| 75 | + ProviderConnectionError, ProviderInvalidDataError, ProviderQueryError, \ |
| 76 | + ProviderItemNotFoundError |
73 | 77 | from pygeoapi.util import get_transform_from_crs |
74 | 78 |
|
75 | 79 |
|
@@ -307,6 +311,65 @@ def get(self, identifier, crs_transform_spec=None, **kwargs): |
307 | 311 |
|
308 | 312 | return feature |
309 | 313 |
|
| 314 | + def create(self, item): |
| 315 | + """ |
| 316 | + Create a new item |
| 317 | +
|
| 318 | + :param item: `dict` of new item |
| 319 | +
|
| 320 | + :returns: identifier of created item |
| 321 | + """ |
| 322 | + |
| 323 | + identifier, json_data = self._load_and_prepare_item( |
| 324 | + item, accept_missing_identifier=True) |
| 325 | + |
| 326 | + new_instance = self._feature_to_sqlalchemy(json_data, identifier) |
| 327 | + with Session(self._engine) as session: |
| 328 | + session.add(new_instance) |
| 329 | + session.commit() |
| 330 | + result_id = getattr(new_instance, self.id_field) |
| 331 | + |
| 332 | + # NOTE: need to use id from instance in case it's generated |
| 333 | + return result_id |
| 334 | + |
| 335 | + def update(self, identifier, item): |
| 336 | + """ |
| 337 | + Updates an existing item |
| 338 | +
|
| 339 | + :param identifier: feature id |
| 340 | + :param item: `dict` of partial or full item |
| 341 | +
|
| 342 | + :returns: `bool` of update result |
| 343 | + """ |
| 344 | + |
| 345 | + identifier, json_data = self._load_and_prepare_item( |
| 346 | + item, raise_if_exists=False) |
| 347 | + |
| 348 | + new_instance = self._feature_to_sqlalchemy(json_data, identifier) |
| 349 | + with Session(self._engine) as session: |
| 350 | + session.merge(new_instance) |
| 351 | + session.commit() |
| 352 | + |
| 353 | + return True |
| 354 | + |
| 355 | + def delete(self, identifier): |
| 356 | + """ |
| 357 | + Deletes an existing item |
| 358 | +
|
| 359 | + :param identifier: item id |
| 360 | +
|
| 361 | + :returns: `bool` of deletion result |
| 362 | + """ |
| 363 | + with Session(self._engine) as session: |
| 364 | + id_column = getattr(self.table_model, self.id_field) |
| 365 | + result = session.execute( |
| 366 | + delete(self.table_model) |
| 367 | + .where(id_column == identifier) |
| 368 | + ) |
| 369 | + session.commit() |
| 370 | + |
| 371 | + return result.rowcount > 0 |
| 372 | + |
310 | 373 | def _store_db_parameters(self, parameters, options): |
311 | 374 | self.db_user = parameters.get('user') |
312 | 375 | self.db_host = parameters.get('host') |
@@ -343,6 +406,26 @@ def _sqlalchemy_to_feature(self, item, crs_transform_out=None): |
343 | 406 |
|
344 | 407 | return feature |
345 | 408 |
|
| 409 | + def _feature_to_sqlalchemy(self, json_data, identifier=None): |
| 410 | + attributes = {**json_data['properties']} |
| 411 | + # 'identifier' key maybe be present in geojson properties, but might |
| 412 | + # not be a valid db field |
| 413 | + attributes.pop('identifier', None) |
| 414 | + attributes[self.geom] = from_shape( |
| 415 | + shapely.geometry.shape(json_data['geometry']), |
| 416 | + # NOTE: for some reason, postgis in the github action requires |
| 417 | + # explicit crs information. i think it's valid to assume 4326: |
| 418 | + # https://portal.ogc.org/files/108198#feature-crs |
| 419 | + srid=4326 |
| 420 | + ) |
| 421 | + attributes[self.id_field] = identifier |
| 422 | + |
| 423 | + try: |
| 424 | + return self.table_model(**attributes) |
| 425 | + except Exception as e: |
| 426 | + LOGGER.exception('Failed to create db model') |
| 427 | + raise ProviderInvalidDataError(str(e)) |
| 428 | + |
346 | 429 | def _get_order_by_clauses(self, sort_by, table_model): |
347 | 430 | # Build sort_by clauses if provided |
348 | 431 | clauses = [] |
|
0 commit comments