|
| 1 | +# ================================================================= |
| 2 | +# |
| 3 | +# Authors: Benjamin Webb <bwebb@lincolninst.edu> |
| 4 | +# |
| 5 | +# Copyright (c) 2025 Benjamin Webb |
| 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 | + |
| 30 | +from copy import deepcopy |
| 31 | +import json |
| 32 | +import logging |
| 33 | + |
| 34 | +import pytest |
| 35 | +from shapely.geometry import (Point, MultiPoint, Polygon, |
| 36 | + MultiPolygon, LineString, MultiLineString) |
| 37 | + |
| 38 | +from pygeoapi.linked_data import ( |
| 39 | + geojson2jsonld, |
| 40 | + geom2schemageo, |
| 41 | + jsonldify_geometry |
| 42 | +) |
| 43 | + |
| 44 | + |
| 45 | +LOGGER = logging.getLogger(__name__) |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture |
| 49 | +def feature(): |
| 50 | + return { |
| 51 | + 'type': 'Feature', |
| 52 | + 'geometry': { |
| 53 | + 'type': 'Point', |
| 54 | + 'coordinates': [125.6, 10.1] |
| 55 | + }, |
| 56 | + 'properties': { |
| 57 | + 'name': 'Test Point' |
| 58 | + }, |
| 59 | + 'id': 'test1', |
| 60 | + 'links': [] |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | +def test_geojson2jsonld_single_feature(api_, feature): |
| 65 | + """Test conversion of single GeoJSON feature to JSON-LD""" |
| 66 | + |
| 67 | + result = geojson2jsonld(api_, feature, |
| 68 | + 'obs', 'http://example.org/feature/1') |
| 69 | + result_dict = json.loads(result) |
| 70 | + |
| 71 | + assert '@context' in result_dict |
| 72 | + assert result_dict['@id'] == 'http://example.org/feature/1' |
| 73 | + assert 'schema:geo' in result_dict |
| 74 | + |
| 75 | + |
| 76 | +def test_geom2schemageo(): |
| 77 | + """Test conversion of various geometry types to schema.org geometry""" |
| 78 | + |
| 79 | + # Test Point |
| 80 | + point = Point(125.6, 10.1) |
| 81 | + point_result = geom2schemageo(point) |
| 82 | + assert point_result['@type'] == 'schema:GeoCoordinates' |
| 83 | + assert point_result['schema:longitude'] == 125.6 |
| 84 | + assert point_result['schema:latitude'] == 10.1 |
| 85 | + |
| 86 | + # Test LineString |
| 87 | + line = LineString([(0, 0), (1, 1)]) |
| 88 | + line_result = geom2schemageo(line) |
| 89 | + assert line_result['@type'] == 'schema:GeoShape' |
| 90 | + assert 'schema:line' in line_result |
| 91 | + |
| 92 | + # Test Polygon |
| 93 | + polygon = Polygon([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]) |
| 94 | + poly_result = geom2schemageo(polygon) |
| 95 | + assert poly_result['@type'] == 'schema:GeoShape' |
| 96 | + assert 'schema:polygon' in poly_result |
| 97 | + |
| 98 | + |
| 99 | +def test_jsonldify_geometry(feature): |
| 100 | + """Test addition of multiple geometry encodings to a feature""" |
| 101 | + |
| 102 | + jsonldify_geometry(feature) |
| 103 | + |
| 104 | + assert feature['type'] == 'schema:Place' |
| 105 | + assert 'gsp:hasGeometry' in feature |
| 106 | + assert 'schema:geo' in feature |
| 107 | + assert feature['schema:geo']['@type'] == 'schema:GeoCoordinates' |
| 108 | + |
| 109 | + |
| 110 | +def test_jsonldify_invalid_geometry(feature): |
| 111 | + """Test invalid geometry encodings to a feature""" |
| 112 | + feature['geometry']['type'] = 'MultiPolygon' |
| 113 | + feature['geometry']['coordinates'] = [] |
| 114 | + jsonldify_geometry(feature) |
| 115 | + |
| 116 | + assert feature['type'] == 'schema:Place' |
| 117 | + assert 'schema:geo' not in feature |
| 118 | + |
| 119 | + |
| 120 | +@pytest.mark.parametrize('geom_type,coords', [ |
| 121 | + ('Point', [125.6, 10.1]), |
| 122 | + ('LineString', [(0, 0), (1, 1)]), |
| 123 | + ('Polygon', [(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]), |
| 124 | + ('MultiPoint', [(0, 0), (1, 1)]), |
| 125 | + ('MultiLineString', [[(0, 0), (1, 1)], [(2, 2), (3, 3)]]), |
| 126 | + ('MultiPolygon', [[(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)], |
| 127 | + [(2, 2), (3, 2), (3, 3), (2, 3), (2, 2)]]) |
| 128 | +]) |
| 129 | +def test_geometry_conversions(geom_type, coords): |
| 130 | + """Test conversion of different geometry types""" |
| 131 | + if geom_type == 'Point': |
| 132 | + geom = Point(coords) |
| 133 | + elif geom_type == 'LineString': |
| 134 | + geom = LineString(coords) |
| 135 | + elif geom_type == 'Polygon': |
| 136 | + geom = Polygon(coords) |
| 137 | + elif geom_type == 'MultiPoint': |
| 138 | + geom = MultiPoint(coords) |
| 139 | + elif geom_type == 'MultiLineString': |
| 140 | + geom = MultiLineString(coords) |
| 141 | + elif geom_type == 'MultiPolygon': |
| 142 | + geom = MultiPolygon([Polygon(poly) for poly in coords]) |
| 143 | + |
| 144 | + result = geom2schemageo(geom) |
| 145 | + assert result['@type'] in ['schema:GeoCoordinates', 'schema:GeoShape'] |
| 146 | + |
| 147 | + |
| 148 | +def test_render_item_template(api_, feature): |
| 149 | + """Test conversion rendering of item template""" |
| 150 | + |
| 151 | + # Use 'objects' collection which has item json-ld template |
| 152 | + result = geojson2jsonld(api_, deepcopy(feature), |
| 153 | + 'objects', 'http://example.org/feature/1') |
| 154 | + |
| 155 | + # Ensure item template is renderable |
| 156 | + assert json.loads(result) |
| 157 | + |
| 158 | + |
| 159 | +def test_render_items_template(api_, feature): |
| 160 | + """Test conversion rendering of items template""" |
| 161 | + |
| 162 | + fc = { |
| 163 | + 'features': [deepcopy(feature) for _ in range(5)], |
| 164 | + 'links': [] |
| 165 | + } |
| 166 | + |
| 167 | + result = geojson2jsonld(api_, fc, 'objects') |
| 168 | + feature_list = json.loads(result) |
| 169 | + |
| 170 | + assert len(feature_list['features']) == len(fc['features']) |
| 171 | + |
| 172 | + for fld, f in zip(feature_list['features'], fc['features']): |
| 173 | + assert ['@type', '@id'] == list(fld.keys()) |
| 174 | + assert f['id'] in fld['@id'] |
0 commit comments