|
| 1 | +import json |
1 | 2 | import os
|
2 | 3 | import unittest
|
3 |
| -import json |
| 4 | + |
| 5 | +from osgeo import ogr |
4 | 6 |
|
5 | 7 | from mapswipe_workers.definitions import CustomError
|
6 |
| -from mapswipe_workers.utils.validate_input import validate_geometries |
| 8 | +from mapswipe_workers.utils.validate_input import ( |
| 9 | + save_geojson_to_file, |
| 10 | + validate_geometries, |
| 11 | +) |
7 | 12 |
|
8 | 13 |
|
9 |
| -class TestValidateGeometries(unittest.TestCase): |
10 |
| - def test_multiple_geom_validation(self): |
11 |
| - pass |
| 14 | +def get_project_draft(path): |
| 15 | + # pre steps for outputting result of function |
| 16 | + test_dir = os.path.dirname(os.path.abspath(__file__)) |
| 17 | + with open(os.path.join(test_dir, path)) as json_file: |
| 18 | + project_draft = json.load(json_file) |
| 19 | + |
| 20 | + path_to_geometries = save_geojson_to_file(1, project_draft["geometry"]) |
| 21 | + return project_draft, path_to_geometries |
12 | 22 |
|
13 |
| - # todo: all the other tests |
14 | 23 |
|
| 24 | +class TestValidateGeometries(unittest.TestCase): |
15 | 25 | def test_area_is_too_large(self):
|
16 | 26 | """Test if validate_geometries throws an error
|
17 | 27 | if the provided geojson covers a too large area."""
|
18 | 28 |
|
19 | 29 | path = (
|
20 | 30 | "fixtures/tile_map_service_grid/projects/projectDraft_area_too_large.json"
|
21 | 31 | )
|
22 |
| - test_dir = os.path.dirname(os.path.abspath(__file__)) |
23 |
| - with open(os.path.join(test_dir, path)) as json_file: |
24 |
| - project_draft = json.load(json_file) |
25 |
| - geometry = project_draft["geometry"] |
| 32 | + project_draft, path_to_geometries = get_project_draft(path) |
| 33 | + self.assertRaises(CustomError, validate_geometries, 1, 18, path_to_geometries) |
| 34 | + |
| 35 | + def test_broken_geojson_string(self): |
| 36 | + """Test if validate_geometries throws an error |
| 37 | + if the provided geojson string is broken. |
| 38 | + This means we can't create the geo data layer with ogr.""" |
| 39 | + |
| 40 | + path = ( |
| 41 | + "fixtures/tile_map_service_grid/projects/projectDraft_broken_geojson.json" |
| 42 | + ) |
| 43 | + project_draft, path_to_geometries = get_project_draft(path) |
| 44 | + self.assertRaises(CustomError, validate_geometries, 1, 18, path_to_geometries) |
| 45 | + |
| 46 | + def test_feature_is_none(self): |
| 47 | + """Test if validate_geometries throws an error |
| 48 | + if the provided geojson contains a not defined feature.""" |
| 49 | + |
| 50 | + path = ( |
| 51 | + "fixtures/tile_map_service_grid/projects/projectDraft_feature_is_none.json" |
| 52 | + ) |
| 53 | + project_draft, path_to_geometries = get_project_draft(path) |
| 54 | + self.assertRaises(CustomError, validate_geometries, 1, 18, path_to_geometries) |
| 55 | + |
| 56 | + def test_no_features(self): |
| 57 | + """Test if validate_geometries throws an error |
| 58 | + if the provided geojson contains no features.""" |
| 59 | + |
| 60 | + path = "fixtures/tile_map_service_grid/projects/projectDraft_no_features.json" |
| 61 | + project_draft, path_to_geometries = get_project_draft(path) |
| 62 | + self.assertRaises(CustomError, validate_geometries, 1, 18, path_to_geometries) |
| 63 | + |
| 64 | + def test_single_geom_validation(self): |
| 65 | + path = "fixtures/completeness/projectDraft_single.json" |
| 66 | + project_draft, path_to_geometries = get_project_draft(path) |
| 67 | + |
| 68 | + path = "fixtures/completeness/single_polygon.geojson" |
| 69 | + driver = ogr.GetDriverByName("GeoJSON") |
| 70 | + datasource = driver.Open(path, 0) |
| 71 | + |
| 72 | + geometry_collection = ogr.Geometry(ogr.wkbMultiPolygon) |
| 73 | + # Get the data layer |
| 74 | + layer = datasource.GetLayer() |
| 75 | + for feature in layer: |
| 76 | + feat_geom = feature.GetGeometryRef() |
| 77 | + geom_name = feat_geom.GetGeometryName() |
| 78 | + # add geometry to geometry collection |
| 79 | + if geom_name == "MULTIPOLYGON": |
| 80 | + for multi in feat_geom: |
| 81 | + geometry_collection.AddGeometry(multi) |
| 82 | + if geom_name == "POLYGON": |
| 83 | + geometry_collection.AddGeometry(feat_geom) |
| 84 | + |
| 85 | + dissolved_geometry = geometry_collection.UnionCascaded() |
| 86 | + wkt_geometry_collection = dissolved_geometry.ExportToWkt() |
| 87 | + |
| 88 | + # results coming from the validate_geometries function |
| 89 | + wkt = validate_geometries(1, 18, path_to_geometries) |
| 90 | + # Test that sequence first contains the same elements as second |
| 91 | + self.assertCountEqual(wkt, wkt_geometry_collection) |
| 92 | + |
| 93 | + def test_multiple_geom_validation(self): |
| 94 | + path = "fixtures/completeness/projectDraft_overlappingGeom.json" |
| 95 | + project_draft, path_to_geometries = get_project_draft(path) |
26 | 96 |
|
27 |
| - self.assertRaises(CustomError, validate_geometries, 1, geometry, 18) |
| 97 | + # prepare data that is expected |
| 98 | + path = "fixtures/completeness/overlappingGeoms.geojson" |
| 99 | + driver = ogr.GetDriverByName("GeoJSON") |
| 100 | + datasource = driver.Open(path, 0) |
28 | 101 |
|
29 |
| - """ |
30 |
| - project = create_project(path) |
| 102 | + geometry_collection = ogr.Geometry(ogr.wkbMultiPolygon) |
| 103 | + # Get the data layer |
| 104 | + layer = datasource.GetLayer() |
| 105 | + for feature in layer: |
| 106 | + feat_geom = feature.GetGeometryRef() |
| 107 | + geom_name = feat_geom.GetGeometryName() |
| 108 | + # add geometry to geometry collection |
| 109 | + # check if input geom is multipolygon or polygon |
| 110 | + if geom_name == "MULTIPOLYGON": |
| 111 | + for multi in feat_geom: |
| 112 | + geometry_collection.AddGeometry(multi) |
| 113 | + # apply union function if multiple geoms of polygons overlap |
| 114 | + dissolved_geometry = geometry_collection.UnionCascaded() |
| 115 | + wkt_geometry_collection = dissolved_geometry.ExportToWkt() |
| 116 | + if geom_name == "POLYGON": |
| 117 | + geometry_collection.AddGeometry(feat_geom) |
| 118 | + wkt_geometry_collection = geometry_collection.ExportToWkt() |
31 | 119 |
|
| 120 | + # results coming from the validate_geometries function |
| 121 | + wkt = validate_geometries(1, 18, path_to_geometries) |
| 122 | + # Test that sequence first contains the same elements as second |
| 123 | + self.assertEqual(wkt, wkt_geometry_collection) |
32 | 124 |
|
33 |
| - |
34 |
| - # we expect that the function raises a CustomError due to the fact |
35 |
| - # that the area is too large |
36 |
| - self.assertRaises(CustomError, project.validate_geometries) |
37 |
| -""" |
38 | 125 |
|
39 | 126 | if __name__ == "__main__":
|
40 | 127 | unittest.main()
|
0 commit comments