-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_worlddiff.py
More file actions
103 lines (88 loc) · 3.65 KB
/
Copy pathtest_worlddiff.py
File metadata and controls
103 lines (88 loc) · 3.65 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
#!/usr/bin/env python3
import json
import tempfile
from pathlib import Path
from worlddiff import run
def collection(features):
return {"type": "FeatureCollection", "features": features}
def feature(feature_id, x, name):
return {
"type": "Feature",
"id": feature_id,
"properties": {"name": name},
"geometry": {"type": "Point", "coordinates": [x, 0]},
}
def polygon(feature_id, x, y, name):
return {
"type": "Feature",
"id": feature_id,
"properties": {"name": name},
"geometry": {
"type": "Polygon",
"coordinates": [[[x, y], [x + 0.01, y], [x + 0.01, y + 0.01], [x, y + 0.01], [x, y]]]
},
}
def linestring(feature_id, x, y, name):
return {
"type": "Feature",
"id": feature_id,
"properties": {"name": name},
"geometry": {"type": "LineString", "coordinates": [[x, y], [x + 0.1, y + 0.1]]},
}
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
(root / "old.json").write_text(
json.dumps(collection([feature("a", 0, "Alpha"), feature("b", 1, "Beta")])),
encoding="utf-8",
)
(root / "new.json").write_text(
json.dumps(collection([feature("a", 2, "Alpha 2"), feature("c", 3, "Gamma")])),
encoding="utf-8",
)
result = run(root / "old.json", root / "new.json", "id", root / "report")
assert result["added"] == ["c"]
assert result["removed"] == ["b"]
assert result["changed"] == [
{"id": "a", "geometry_changed": True, "properties_changed": ["name"]}
]
layer = json.loads((root / "report/changes.geojson").read_text(encoding="utf-8"))
assert {f["properties"]["_worlddiff_change"] for f in layer["features"]} == {
"added",
"removed",
"changed",
}
# Test SVG generation with demo data (polygons and linestrings)
old_demo = collection([
polygon("p1", -118.24, 34.05, "Parcel 1"),
polygon("p2", -118.25, 34.06, "Parcel 2"),
linestring("r1", -118.24, 34.05, "Road 1"),
linestring("r2", -118.26, 34.07, "Road 2"),
])
new_demo = collection([
polygon("p1", -118.24, 34.05, "Parcel 1 Updated"),
polygon("p3", -118.27, 34.08, "Parcel 3"),
linestring("r1", -118.24, 34.05, "Road 1"),
linestring("r3", -118.28, 34.09, "Road 3"),
])
(root / "old_demo.json").write_text(json.dumps(old_demo), encoding="utf-8")
(root / "new_demo.json").write_text(json.dumps(new_demo), encoding="utf-8")
svg_file = root / "demo.svg"
result = run(root / "old_demo.json", root / "new_demo.json", "id", root / "report_svg", svg_path=svg_file)
assert result["added"] == ["p3", "r3"]
assert result["removed"] == ["p2", "r2"]
assert result["changed"] == [{"id": "p1", "geometry_changed": False, "properties_changed": ["name"]}]
# Verify SVG was created and contains expected classes
assert svg_file.exists(), f"SVG file not created at {svg_file}"
svg_content = svg_file.read_text(encoding="utf-8")
assert '<svg xmlns="http://www.w3.org/2000/svg"' in svg_content
assert 'class="added"' in svg_content
assert 'class="removed"' in svg_content
assert 'class="changed"' in svg_content
# Count class occurrences
added_count = svg_content.count('class="added"')
removed_count = svg_content.count('class="removed"')
changed_count = svg_content.count('class="changed"')
assert added_count == 2, f"Expected 2 added features, got {added_count}"
assert removed_count == 2, f"Expected 2 removed features, got {removed_count}"
assert changed_count == 1, f"Expected 1 changed feature, got {changed_count}"
print("WorldDiff check passed")