-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_api_buffer.py
More file actions
163 lines (137 loc) Β· 5.67 KB
/
test_api_buffer.py
File metadata and controls
163 lines (137 loc) Β· 5.67 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
#!/usr/bin/env python3
"""
Simple API test for buffer functionality to verify the Flask endpoints work.
"""
import json
import requests
from shapely.geometry import Point
from shapely.wkt import dumps as wkt_dumps
BASE_URL = "http://127.0.0.1:5000"
def test_api_buffer():
"""Test buffer functionality through API endpoints"""
print("π Testing Buffer API Endpoints")
print("=" * 35)
# Test 1: Check if server is running
print("\n1οΈβ£ Checking API server...")
try:
response = requests.get(f"{BASE_URL}/get_map_layers", timeout=5)
if response.status_code == 200:
print(" β
API server is running")
else:
print(f" β API server returned {response.status_code}")
return False
except requests.exceptions.RequestException as e:
print(f" β Could not connect to API server: {e}")
print(" π‘ Make sure Flask server is running: python src/app.py")
return False
# Test 2: Test buffer creation
print("\n2οΈβ£ Testing buffer creation...")
test_point = Point(32.8, 3.16) # Point in Lamwo, Uganda
test_wkt = wkt_dumps(test_point)
print(f" π Test point: {test_wkt}")
buffer_data = {"geometry_wkt": test_wkt, "radius_m": 1000}
try:
response = requests.post(
f"{BASE_URL}/api/buffer-feature", json=buffer_data, timeout=10
)
if response.status_code == 200:
result = response.json()
print(" β
Buffer creation successful!")
print(
f" π Buffer radius: {result['buffer_geojson']['properties']['radius_m']}m"
)
# Store the buffered geometry for subsequent tests
buffered_wkt = result.get("buffered_geometry_wkt")
# Test 3: Query features within buffer
print("\n3οΈβ£ Testing buffer queries...")
# Test buildings
query_data = {
"query_type": "buildings",
"geometry_wkt": buffered_wkt,
"radius_m": 1000,
}
response = requests.post(
f"{BASE_URL}/api/query-buffer", json=query_data, timeout=10
)
if response.status_code == 200:
result = response.json()
building_count = result["result"]["count"]
print(f" π Buildings in buffer: {building_count}")
else:
print(
f" β οΈ Building query returned {response.status_code}: {response.text}"
)
# Test minigrids
query_data = {
"query_type": "minigrids",
"geometry_wkt": buffered_wkt,
"radius_m": 1000,
}
response = requests.post(
f"{BASE_URL}/api/query-buffer", json=query_data, timeout=10
)
if response.status_code == 200:
result = response.json()
minigrid_count = result["result"]["count"]
print(f" β‘ Minigrids in buffer: {minigrid_count}")
else:
print(
f" β οΈ Minigrid query returned {response.status_code}: {response.text}"
)
# Test NDVI
query_data = {
"query_type": "ndvi",
"geometry_wkt": buffered_wkt,
"radius_m": 1000,
}
try:
response = requests.post(
f"{BASE_URL}/api/query-buffer", json=query_data, timeout=10
)
if response.status_code == 200:
try:
result = response.json()
avg_ndvi = result["result"]["avg_ndvi"]
print(f" π± Average NDVI: {avg_ndvi:.3f}")
print("\nπ All API tests passed!")
return True
except json.JSONDecodeError as e:
print(f" β JSON decode error for NDVI: {e}")
print(f" Raw response: {response.text}")
return False
else:
print(
f" β οΈ NDVI query returned {response.status_code}: {response.text}"
)
return False
except requests.exceptions.RequestException as e:
print(f" β NDVI request failed: {e}")
return False
except requests.exceptions.RequestException as e:
print(f" β API request failed: {e}")
return False
return True
def main():
success = test_api_buffer()
if success:
print("\nπ Buffer API Functionality is Working!")
print("\n" + "=" * 50)
print("β
Core buffer_geometry method works")
print("β
API endpoints for buffer operations work")
print("β
Session management works")
print("β
Spatial queries on buffered areas work")
print("\nπ Ready for Frontend Integration!")
print("\nNext steps:")
print("1. Add feature selection UI to map")
print("2. Add buffer visualization")
print("3. Connect buffer controls to chat interface")
print("4. Test complete user workflow")
print("\nπ‘ Demo usage:")
print("- Open http://127.0.0.1:5000 in your browser")
print("- Draw a polygon and ask: 'Create a 2km buffer'")
print("- Then ask: 'How many buildings are in this buffer?'")
else:
print("\nβ API buffer functionality needs fixes")
print("Make sure the Flask server is running: python src/app.py")
if __name__ == "__main__":
main()