-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathtest_geojson.py
More file actions
574 lines (493 loc) · 20.2 KB
/
test_geojson.py
File metadata and controls
574 lines (493 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
import json
import os
import shutil
import tempfile
from kili_formats.format.geojson import geojson_feature_collection_to_kili_json_response
def create_mock_geojson_file(feature_collection, temp_dir):
"""Create a mock GeoJSON file for testing purposes."""
file_path = os.path.join(temp_dir, "test.geojson")
with open(file_path, "w", encoding="utf-8") as f:
json.dump(feature_collection, f)
return file_path
def is_almost_equal(a, b, tolerance=1e-14):
"""Due to different versions of coordinate transformations, the coordinates may differ slightly.
This function checks if two coordinates are almost equal within a given tolerance.
"""
return abs(a - b) <= tolerance
def test_point_geojson():
"""Test converting GeoJSON Point features to Kili annotations."""
temp_dir = tempfile.mkdtemp()
# Create mock GeoJSON feature collection with Point geometries
feature_collection = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [9.429123117729949, 54.68002984132896],
},
"properties": {
"kili": {
"job": "points_job",
"type": "marker",
"categories": [{"name": "point_category"}],
}
},
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [9.772235200599381, 54.68970515271516],
},
"properties": {
"kili": {
"job": "points_job",
"type": "marker",
"categories": [{"name": "point_category"}],
}
},
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [9.451439350762108, 54.318792605636354],
},
"properties": {
"kili": {
"job": "points_job",
"type": "marker",
"categories": [{"name": "point_category"}],
}
},
},
],
}
geojson_file_path = create_mock_geojson_file(feature_collection, temp_dir)
response = geojson_feature_collection_to_kili_json_response(feature_collection)
assert "points_job" in response
assert "annotations" in response["points_job"]
annotations = response["points_job"]["annotations"]
assert len(annotations) == 3
expected_coordinates = [
{"x": 9.429123117729949, "y": 54.68002984132896},
{"x": 9.772235200599381, "y": 54.68970515271516},
{"x": 9.451439350762108, "y": 54.318792605636354},
]
for index, annotation in enumerate(annotations):
assert annotation["type"] == "marker"
assert "point" in annotation
assert is_almost_equal(annotation["point"]["x"], expected_coordinates[index]["x"])
assert is_almost_equal(annotation["point"]["y"], expected_coordinates[index]["y"])
assert "categories" in annotation
assert len(annotation["categories"]) == 1
assert annotation["categories"][0]["name"] == "point_category"
shutil.rmtree(temp_dir)
def test_linestring_geojson():
"""Test converting GeoJSON LineString features to Kili annotations."""
temp_dir = tempfile.mkdtemp()
# Create mock GeoJSON feature collection with LineString geometries
feature_collection = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[9.254598688594093, 54.8539201125262],
[9.254598688594093, 54.85101839240259],
[9.259639164462506, 54.84811646354571],
[9.262159402396708, 54.845214325948014],
],
},
"properties": {
"kili": {
"job": "lines_job",
"type": "polyline",
"categories": [{"name": "line_category"}],
}
},
},
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[9.69312008914582, 54.23407519827612],
[9.69312008914582, 54.23996684410168],
[9.698160565014232, 54.244385026482774],
],
},
"properties": {
"kili": {
"job": "lines_job",
"type": "polyline",
"categories": [{"name": "line_category"}],
}
},
},
],
}
geojson_file_path = create_mock_geojson_file(feature_collection, temp_dir)
response = geojson_feature_collection_to_kili_json_response(feature_collection)
assert "lines_job" in response
assert "annotations" in response["lines_job"]
annotations = response["lines_job"]["annotations"]
assert len(annotations) == 2
expected_coordinates = [
[
{"x": 9.254598688594093, "y": 54.8539201125262},
{"x": 9.254598688594093, "y": 54.85101839240259},
{"x": 9.259639164462506, "y": 54.84811646354571},
{"x": 9.262159402396708, "y": 54.845214325948014},
],
[
{"x": 9.69312008914582, "y": 54.23407519827612},
{"x": 9.69312008914582, "y": 54.23996684410168},
{"x": 9.698160565014232, "y": 54.244385026482774},
],
]
for index, annotation in enumerate(annotations):
assert annotation["type"] == "polyline"
assert "polyline" in annotation
for point_index, point in enumerate(annotation["polyline"]):
assert is_almost_equal(point["x"], expected_coordinates[index][point_index]["x"])
assert is_almost_equal(point["y"], expected_coordinates[index][point_index]["y"])
assert "categories" in annotation
assert len(annotation["categories"]) == 1
assert annotation["categories"][0]["name"] == "line_category"
shutil.rmtree(temp_dir)
def test_polygon_geojson():
"""Test converting GeoJSON Polygon features to Kili annotations."""
temp_dir = tempfile.mkdtemp()
# Create mock GeoJSON feature collection with Polygon geometries
feature_collection = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[9.504102244080421, 54.51885219373373],
[9.50914271994883, 54.52031500197589],
[9.514183195817239, 54.52324046127452],
[9.519223671685653, 54.52470311233283],
[9.504102244080421, 54.51885219373373], # Close the polygon
]
],
},
"properties": {
"kili": {
"job": "polygons_job",
"type": "semantic",
"categories": [{"name": "polygon_category"}],
}
},
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[10.05351411373718, 54.316481719000855],
[10.063595065473999, 54.316481719000855],
[10.068635541342411, 54.316481719000855],
[10.076196255145028, 54.31501161884114],
[10.05351411373718, 54.316481719000855], # Close the polygon
]
],
},
"properties": {
"kili": {
"job": "polygons_job",
"type": "semantic",
"categories": [{"name": "polygon_category"}],
}
},
},
],
}
geojson_file_path = create_mock_geojson_file(feature_collection, temp_dir)
response = geojson_feature_collection_to_kili_json_response(feature_collection)
assert "polygons_job" in response
assert "annotations" in response["polygons_job"]
annotations = response["polygons_job"]["annotations"]
assert len(annotations) == 2
# Test the first polygon
first_annotation = annotations[0]
assert first_annotation["type"] == "semantic"
assert "boundingPoly" in first_annotation
assert len(first_annotation["boundingPoly"]) == 1 # Only exterior ring
expected_first_polygon = [
{"x": 9.504102244080421, "y": 54.51885219373373},
{"x": 9.50914271994883, "y": 54.52031500197589},
{"x": 9.514183195817239, "y": 54.52324046127452},
{"x": 9.519223671685653, "y": 54.52470311233283},
]
first_polygon_vertices = first_annotation["boundingPoly"][0][0]["normalizedVertices"]
assert len(first_polygon_vertices) == len(expected_first_polygon)
for i, point in enumerate(first_polygon_vertices):
assert is_almost_equal(point["x"], expected_first_polygon[i]["x"])
assert is_almost_equal(point["y"], expected_first_polygon[i]["y"])
assert "categories" in first_annotation
assert len(first_annotation["categories"]) == 1
assert first_annotation["categories"][0]["name"] == "polygon_category"
shutil.rmtree(temp_dir)
def test_multipolygon_geojson():
"""Test converting GeoJSON MultiPolygon features to Kili annotations."""
temp_dir = tempfile.mkdtemp()
# Create mock GeoJSON feature collection with MultiPolygon geometry
feature_collection = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
# First polygon
[
[
[9.504102244080421, 54.51885219373373],
[9.50914271994883, 54.52031500197589],
[9.514183195817239, 54.52324046127452],
[9.504102244080421, 54.51885219373373],
]
],
# Second polygon
[
[
[10.05351411373718, 54.316481719000855],
[10.063595065473999, 54.316481719000855],
[10.068635541342411, 54.316481719000855],
[10.05351411373718, 54.316481719000855],
]
],
],
},
"properties": {
"kili": {
"job": "multipolygons_job",
"type": "semantic",
"categories": [{"name": "multipolygon_category"}],
}
},
}
],
}
geojson_file_path = create_mock_geojson_file(feature_collection, temp_dir)
response = geojson_feature_collection_to_kili_json_response(feature_collection)
assert "multipolygons_job" in response
assert "annotations" in response["multipolygons_job"]
annotations = response["multipolygons_job"]["annotations"]
# MultiPolygon should create a single annotation with multiple polygons in boundingPoly
assert len(annotations) == 1
annotation = annotations[0]
assert annotation["type"] == "semantic"
assert "boundingPoly" in annotation
assert len(annotation["boundingPoly"]) == 2
assert "categories" in annotation
assert len(annotation["categories"]) == 1
assert annotation["categories"][0]["name"] == "multipolygon_category"
shutil.rmtree(temp_dir)
def test_mixed_geometries_geojson():
"""Test converting GeoJSON with mixed geometry types to Kili annotations."""
temp_dir = tempfile.mkdtemp()
# Create mock GeoJSON feature collection with mixed geometries
feature_collection = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [9.429123117729949, 54.68002984132896],
},
"properties": {
"kili": {
"job": "detection_job",
"type": "marker",
"categories": [{"name": "point_category"}],
}
},
},
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[9.254598688594093, 54.8539201125262],
[9.254598688594093, 54.85101839240259],
[9.259639164462506, 54.84811646354571],
],
},
"properties": {
"kili": {
"job": "tracking_job",
"type": "polyline",
"categories": [{"name": "line_category"}],
}
},
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[9.504102244080421, 54.51885219373373],
[9.50914271994883, 54.52031500197589],
[9.514183195817239, 54.52324046127452],
[9.504102244080421, 54.51885219373373],
]
],
},
"properties": {
"kili": {
"job": "segmentation_job",
"type": "semantic",
"categories": [{"name": "polygon_category"}],
}
},
},
],
}
geojson_file_path = create_mock_geojson_file(feature_collection, temp_dir)
response = geojson_feature_collection_to_kili_json_response(feature_collection)
# Should have three different jobs
assert len(response) == 3
assert "detection_job" in response
assert "tracking_job" in response
assert "segmentation_job" in response
# Check point annotation
assert "annotations" in response["detection_job"]
point_annotations = response["detection_job"]["annotations"]
assert len(point_annotations) == 1
assert point_annotations[0]["type"] == "marker"
assert "point" in point_annotations[0]
# Check line annotation
assert "annotations" in response["tracking_job"]
line_annotations = response["tracking_job"]["annotations"]
assert len(line_annotations) == 1
assert line_annotations[0]["type"] == "polyline"
assert "polyline" in line_annotations[0]
# Check polygon annotation
assert "annotations" in response["segmentation_job"]
polygon_annotations = response["segmentation_job"]["annotations"]
assert len(polygon_annotations) == 1
assert polygon_annotations[0]["type"] == "semantic"
assert "boundingPoly" in polygon_annotations[0]
shutil.rmtree(temp_dir)
def test_multiple_geojson_files():
"""Test merging annotations from multiple GeoJSON files."""
temp_dir = tempfile.mkdtemp()
# Create first GeoJSON file with points
feature_collection_1 = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [9.429123117729949, 54.68002984132896],
},
"properties": {
"kili": {
"job": "detection_job",
"type": "marker",
"categories": [{"name": "point_category"}],
}
},
}
],
}
# Create second GeoJSON file with more points for the same job
feature_collection_2 = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [9.772235200599381, 54.68970515271516],
},
"properties": {
"kili": {
"job": "detection_job",
"type": "marker",
"categories": [{"name": "point_category"}],
}
},
}
],
}
file_path_1 = create_mock_geojson_file(feature_collection_1, temp_dir)
file_path_2 = create_mock_geojson_file(feature_collection_2, temp_dir)
# Test merging logic by manually processing both files
merged_json_response = {}
for feature_collection in [feature_collection_1, feature_collection_2]:
json_response = geojson_feature_collection_to_kili_json_response(feature_collection)
for job_name, job_data in json_response.items():
if job_name not in merged_json_response:
merged_json_response[job_name] = job_data
else:
# Merge job data
for key, value in job_data.items():
if key == "annotations":
merged_json_response[job_name].setdefault("annotations", []).extend(value)
else:
merged_json_response[job_name][key] = value
# Should have merged the annotations from both files
assert "detection_job" in merged_json_response
assert "annotations" in merged_json_response["detection_job"]
annotations = merged_json_response["detection_job"]["annotations"]
assert len(annotations) == 2 # One from each file
shutil.rmtree(temp_dir)
def test_non_localised_features_geojson():
"""Test converting non-localised GeoJSON features (transcription and classification)."""
temp_dir = tempfile.mkdtemp()
# Create mock GeoJSON feature collection with non-localised features
feature_collection = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": None, # Non-localised features have no geometry
"properties": {
"kili": {"job": "transcription_job", "text": "This is a transcription"}
},
},
{
"type": "Feature",
"geometry": None,
"properties": {
"kili": {
"job": "classification_job",
"categories": [{"name": "category_a"}, {"name": "category_b"}],
}
},
},
],
}
geojson_file_path = create_mock_geojson_file(feature_collection, temp_dir)
response = geojson_feature_collection_to_kili_json_response(feature_collection)
# Should have two jobs
assert len(response) == 2
assert "transcription_job" in response
assert "classification_job" in response
# Check transcription job
assert "text" in response["transcription_job"]
assert response["transcription_job"]["text"] == "This is a transcription"
# Check classification job
assert "categories" in response["classification_job"]
categories = response["classification_job"]["categories"]
assert len(categories) == 2
assert categories[0]["name"] == "category_a"
assert categories[1]["name"] == "category_b"
shutil.rmtree(temp_dir)