|
| 1 | +# ================================================================= |
| 2 | +# |
| 3 | +# Authors: Francesco Bartoli <xbartolone@gmail.com> |
| 4 | +# |
| 5 | +# Copyright (c) 2025 Francesco Bartoli |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person |
| 8 | +# obtaining a copy of this software and associated documentation |
| 9 | +# files (the "Software"), to deal in the Software without |
| 10 | +# restriction, including without limitation the rights to use, |
| 11 | +# copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +# copies of the Software, and to permit persons to whom the |
| 13 | +# Software is furnished to do so, subject to the following |
| 14 | +# conditions: |
| 15 | +# |
| 16 | +# The above copyright notice and this permission notice shall be |
| 17 | +# included in all copies or substantial portions of the Software. |
| 18 | +# |
| 19 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 21 | +# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 23 | +# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 24 | +# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 25 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 26 | +# OTHER DEALINGS IN THE SOFTWARE. |
| 27 | +# |
| 28 | +# ================================================================= |
| 29 | +"""JSON-FG capabilities |
| 30 | +Returns content as JSON-FG representations |
| 31 | +""" |
| 32 | + |
| 33 | +import json |
| 34 | +import logging |
| 35 | +import uuid |
| 36 | +from typing import Union |
| 37 | + |
| 38 | +from osgeo import gdal |
| 39 | + |
| 40 | +from pygeoapi.formatter.base import BaseFormatter, FormatterSerializationError |
| 41 | + |
| 42 | +LOGGER = logging.getLogger(__name__) |
| 43 | + |
| 44 | + |
| 45 | +class JSONFGFormatter(BaseFormatter): |
| 46 | + """JSON-FG formatter""" |
| 47 | + |
| 48 | + def __init__(self, formatter_def: dict): |
| 49 | + """ |
| 50 | + Initialize object |
| 51 | +
|
| 52 | + :param formatter_def: formatter definition |
| 53 | +
|
| 54 | + :returns: `pygeoapi.formatter.jsonfg.JSONFGFormatter` |
| 55 | + """ |
| 56 | + |
| 57 | + geom = False |
| 58 | + if "geom" in formatter_def: |
| 59 | + geom = formatter_def["geom"] |
| 60 | + |
| 61 | + super().__init__({"name": "jsonfg", "geom": geom}) |
| 62 | + self.mimetype = "application/vnd.ogc.fg+json" |
| 63 | + |
| 64 | + def write(self, data: dict, options: dict = {}) -> str: |
| 65 | + """ |
| 66 | + Generate data in JSON-FG format |
| 67 | +
|
| 68 | + :param options: JSON-FG formatting options |
| 69 | + :param data: dict of GeoJSON data |
| 70 | +
|
| 71 | + :returns: string representation of format |
| 72 | + """ |
| 73 | + |
| 74 | + try: |
| 75 | + fields = list(data["features"][0]["properties"].keys()) |
| 76 | + except IndexError: |
| 77 | + LOGGER.error("no features") |
| 78 | + return str() |
| 79 | + |
| 80 | + LOGGER.debug(f"JSONFG fields: {fields}") |
| 81 | + |
| 82 | + try: |
| 83 | + output = geojson2jsonfg(data=data, dataset="items") |
| 84 | + return output |
| 85 | + except ValueError as err: |
| 86 | + LOGGER.error(err) |
| 87 | + raise FormatterSerializationError("Error writing JSONFG output") |
| 88 | + |
| 89 | + def __repr__(self): |
| 90 | + return f"<JSONFGFormatter> {self.name}" |
| 91 | + |
| 92 | + |
| 93 | +def geojson2jsonfg( |
| 94 | + data: dict, |
| 95 | + dataset: str, |
| 96 | + identifier: Union[str, None] = None, |
| 97 | + id_field: str = "id", |
| 98 | +) -> str: |
| 99 | + """ |
| 100 | + Return JSON-FG from a GeoJSON content. |
| 101 | +
|
| 102 | + :param cls: API object |
| 103 | + :param data: dict of data: |
| 104 | +
|
| 105 | + :returns: string of rendered JSON (JSON-FG) |
| 106 | + """ |
| 107 | + gdal.UseExceptions() |
| 108 | + LOGGER.debug("Dump GeoJSON content into a data source") |
| 109 | + # breakpoint() |
| 110 | + try: |
| 111 | + with gdal.OpenEx(json.dumps(data)) as srcDS: |
| 112 | + tmpfile = f"/vsimem/{uuid.uuid1()}.json" |
| 113 | + LOGGER.debug("Translate GeoJSON into a JSONFG memory file") |
| 114 | + gdal.VectorTranslate(tmpfile, srcDS, format="JSONFG") |
| 115 | + LOGGER.debug("Read JSONFG content from a memory file") |
| 116 | + data = gdal.VSIFOpenL(tmpfile, "rb") |
| 117 | + if not data: |
| 118 | + raise ValueError("Failed to read JSONFG content") |
| 119 | + gdal.VSIFSeekL(data, 0, 2) |
| 120 | + length = gdal.VSIFTellL(data) |
| 121 | + gdal.VSIFSeekL(data, 0, 0) |
| 122 | + jsonfg = json.loads(gdal.VSIFReadL(1, length, data).decode()) |
| 123 | + return jsonfg |
| 124 | + except Exception as e: |
| 125 | + LOGGER.error(f"Failed to convert GeoJSON to JSON-FG: {e}") |
| 126 | + raise |
| 127 | + finally: |
| 128 | + gdal.VSIFCloseL(data) |
0 commit comments