diff --git a/formats/geojson_format.py b/formats/geojson_format.py index 111288f1009e1783279b0238762636849776954a..a4ed2a928e090a42f2c716e39776e85f3396ec61 100644 --- a/formats/geojson_format.py +++ b/formats/geojson_format.py @@ -1,6 +1,4 @@ -import geopandas -from pyogrio.errors import DataSourceError -from shapely.errors import GEOSException +import geojson def is_valid(resource, file): @@ -8,9 +6,11 @@ def is_valid(resource, file): with open(file.name, "rb") as f: try: - geopandas.read_file(f) - return True - except DataSourceError: - return False - except GEOSException: - return False + geojson_data = geojson.load(f) + if isinstance(geojson_data, dict) and "type" in geojson_data: + return True + else: + resource["error"] = "JSON is not GeoJSON." + return False + except Exception as e: + resource["error"] = str(e) diff --git a/poetry.lock b/poetry.lock index edac2a2f7c95425967b0d7cdbe7995dda81e460f..edfcc0e0df6a25d58dc54a6982f4ec60e6a7e66d 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. [[package]] name = "annotated-types" @@ -402,6 +402,17 @@ visidata = ["visidata (>=2.10)"] wkt = ["tatsu (>=5.8.3)"] zenodo = ["pyzenodo3 (>=1.0)"] +[[package]] +name = "geojson" +version = "3.2.0" +description = "Python bindings and utilities for GeoJSON" +optional = false +python-versions = ">=3.7" +files = [ + {file = "geojson-3.2.0-py3-none-any.whl", hash = "sha256:69d14156469e13c79479672eafae7b37e2dcd19bdfd77b53f74fa8fe29910b52"}, + {file = "geojson-3.2.0.tar.gz", hash = "sha256:b860baba1e8c6f71f8f5f6e3949a694daccf40820fa8f138b3f712bd85804903"}, +] + [[package]] name = "geopandas" version = "1.0.1" @@ -1784,4 +1795,4 @@ crypto-eth-addresses = ["eth-hash[pycryptodome] (>=0.7.0)"] [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "5623eece30a81343a4c45540ab21121ad4059f42e96aea0941eb630dc659e791" +content-hash = "4ba78fa3eb7d714a01d408c49df85bc74c63fb4bdb627dba2a21c0dfa8e126a6" diff --git a/pyproject.toml b/pyproject.toml index a17f94b91c4a074bf571a8453351983fe57e0411..469fac86cf3e94a2c5a3dc4be82ebd9cec783a31 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,6 +21,7 @@ pypdf = "^5.1.0" pillow = "^11.0.0" fiona = "^1.10.1" pyarrow = "^18.1.0" +geojson = "^3.2.0" [tool.poetry.group.dev.dependencies] coverage = "^7.6.1" diff --git a/tests/test_geojson_format.py b/tests/test_geojson_format.py index de63ebcf67b190ca41c3cbc721744336d770a729..39239a13275e5b9bea750aca2fe74be90fb8a97e 100644 --- a/tests/test_geojson_format.py +++ b/tests/test_geojson_format.py @@ -2,16 +2,18 @@ import unittest from formats.geojson_format import is_valid -class TestShpFormat(unittest.TestCase): +class TestGeoJsonFormat(unittest.TestCase): def test_is_valid__valid(self): resource = {} with open("tests/data/bermuda.geojson", "r") as file: self.assertTrue(is_valid(resource, file)) + self.assertIsNone(resource.get("error")) def test_is_valid__invalid(self): resource = {} with open("tests/data/correct.json", "r") as file: self.assertFalse(is_valid(resource, file)) + self.assertIsNotNone(resource.get("error")) if __name__ == "__main__":