Skip to content

Commit c98f3dc

Browse files
authored
Merge pull request #2 from mapswipe/refactor-python-workers
add test for group and task creation for tile classification project …
2 parents c1d8eb4 + bc655a2 commit c98f3dc

File tree

6 files changed

+335
-4
lines changed

6 files changed

+335
-4
lines changed

mapswipe_workers/mapswipe_workers/project_types/tile_classification/group.py

Whitespace-only changes.

mapswipe_workers/mapswipe_workers/project_types/tile_classification/project.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from mapswipe_workers.firebase.firebase import Firebase
22
from mapswipe_workers.project_types.base.project import BaseProject
33
from mapswipe_workers.project_types.base.tile_server import BaseTileServer
4+
from mapswipe_workers.utils import tile_grouping_functions
45
from mapswipe_workers.utils.validate_input import (
56
save_geojson_to_file,
67
validate_geometries,
@@ -16,14 +17,12 @@ def __init__(self, project_draft: dict):
1617
self.tileServer = vars(BaseTileServer(project_draft["tileServer"]))
1718

1819
def validate_geometries(self):
20+
# TODO rename attribute validInputGeometries, it is a path to a geojson.
1921
self.validInputGeometries = save_geojson_to_file(self.projectId, self.geometry)
2022
wkt_geometry, self.validInputGeometries = validate_geometries(
2123
self.projectId, self.validInputGeometries, self.zoomLevel
2224
)
2325
return wkt_geometry
24-
25-
def create_groups(self):
26-
pass
2726

2827
def save_project_to_firebase(self, project):
2928
firebase = Firebase()
@@ -36,4 +35,15 @@ def save_groups_to_firebase(self, projectId: str, groups: list):
3635
def save_tasks_to_firebase(self, projectId: str, tasks: list):
3736
pass
3837

39-
38+
def create_groups(self):
39+
"""
40+
The function to create groups from the project extent
41+
"""
42+
# first step get properties of each group from extent
43+
raw_groups = tile_grouping_functions.extent_to_groups(
44+
self.validInputGeometries, self.zoomLevel, self.groupSize
45+
)
46+
self.create_tasks(raw_groups)
47+
48+
def create_tasks(self, raw_groups):
49+
tile_grouping_functions.groups_to_tasks(raw_groups)

mapswipe_workers/mapswipe_workers/utils/tile_grouping_functions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@
77
from mapswipe_workers.utils import tile_functions as t
88

99

10+
def groups_to_tasks(raw_groups, zoomLevel):
11+
12+
for group_id, slice in raw_groups.items():
13+
tasks = []
14+
for TileX in range(int(slice["xMin"]), int(slice["xMax"]) + 1):
15+
for TileY in range(int(slice["yMin"]), int(slice["yMax"]) + 1):
16+
task = {
17+
"taskId": "{}-{}-{}".format(zoomLevel, TileX, TileY),
18+
"taskX": str(TileX),
19+
"taskY": str(TileY),
20+
"geometry": t.geometry_from_tile_coords(TileX, TileY, zoomLevel),
21+
}
22+
tasks.append(task)
23+
24+
logger.info("create_groups - created groups dictionary")
25+
26+
1027
def get_geometry_from_file(infile: str):
1128
"""
1229
The function to load input geometries from an .shp, .kml or .geojson file
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
{
2+
"g101" : {
3+
"finishedCount" : 0,
4+
"groupId" : "g101",
5+
"numberOfTasks" : 252,
6+
"progress" : 0,
7+
"projectId" : "test_build_area",
8+
"requiredCount" : 3,
9+
"xMax" : "156623",
10+
"xMin" : "156540",
11+
"yMax" : "142743",
12+
"yMin" : "142741"
13+
},
14+
"g102" : {
15+
"finishedCount" : 0,
16+
"groupId" : "g102",
17+
"numberOfTasks" : 252,
18+
"progress" : 0,
19+
"projectId" : "test_build_area",
20+
"requiredCount" : 3,
21+
"xMax" : "156623",
22+
"xMin" : "156540",
23+
"yMax" : "142746",
24+
"yMin" : "142744"
25+
},
26+
"g103" : {
27+
"finishedCount" : 0,
28+
"groupId" : "g103",
29+
"numberOfTasks" : 252,
30+
"progress" : 0,
31+
"projectId" : "test_build_area",
32+
"requiredCount" : 3,
33+
"xMax" : "156623",
34+
"xMin" : "156540",
35+
"yMax" : "142749",
36+
"yMin" : "142747"
37+
},
38+
"g104" : {
39+
"finishedCount" : 0,
40+
"groupId" : "g104",
41+
"numberOfTasks" : 252,
42+
"progress" : 0,
43+
"projectId" : "test_build_area",
44+
"requiredCount" : 3,
45+
"xMax" : "156623",
46+
"xMin" : "156540",
47+
"yMax" : "142752",
48+
"yMin" : "142750"
49+
},
50+
"g105" : {
51+
"finishedCount" : 0,
52+
"groupId" : "g105",
53+
"numberOfTasks" : 252,
54+
"progress" : 0,
55+
"projectId" : "test_build_area",
56+
"requiredCount" : 3,
57+
"xMax" : "156623",
58+
"xMin" : "156540",
59+
"yMax" : "142755",
60+
"yMin" : "142753"
61+
},
62+
"g106" : {
63+
"finishedCount" : 0,
64+
"groupId" : "g106",
65+
"numberOfTasks" : 252,
66+
"progress" : 0,
67+
"projectId" : "test_build_area",
68+
"requiredCount" : 3,
69+
"xMax" : "156623",
70+
"xMin" : "156540",
71+
"yMax" : "142758",
72+
"yMin" : "142756"
73+
},
74+
"g107" : {
75+
"finishedCount" : 0,
76+
"groupId" : "g107",
77+
"numberOfTasks" : 252,
78+
"progress" : 0,
79+
"projectId" : "test_build_area",
80+
"requiredCount" : 3,
81+
"xMax" : "156623",
82+
"xMin" : "156540",
83+
"yMax" : "142761",
84+
"yMin" : "142759"
85+
},
86+
"g108" : {
87+
"finishedCount" : 0,
88+
"groupId" : "g108",
89+
"numberOfTasks" : 252,
90+
"progress" : 0,
91+
"projectId" : "test_build_area",
92+
"requiredCount" : 3,
93+
"xMax" : "156623",
94+
"xMin" : "156540",
95+
"yMax" : "142764",
96+
"yMin" : "142762"
97+
},
98+
"g109" : {
99+
"finishedCount" : 0,
100+
"groupId" : "g109",
101+
"numberOfTasks" : 252,
102+
"progress" : 0,
103+
"projectId" : "test_build_area",
104+
"requiredCount" : 3,
105+
"xMax" : "156623",
106+
"xMin" : "156540",
107+
"yMax" : "142767",
108+
"yMin" : "142765"
109+
},
110+
"g110" : {
111+
"finishedCount" : 0,
112+
"groupId" : "g110",
113+
"numberOfTasks" : 252,
114+
"progress" : 0,
115+
"projectId" : "test_build_area",
116+
"requiredCount" : 3,
117+
"xMax" : "156623",
118+
"xMin" : "156540",
119+
"yMax" : "142770",
120+
"yMin" : "142768"
121+
},
122+
"g111" : {
123+
"finishedCount" : 0,
124+
"groupId" : "g111",
125+
"numberOfTasks" : 252,
126+
"progress" : 0,
127+
"projectId" : "test_build_area",
128+
"requiredCount" : 3,
129+
"xMax" : "156623",
130+
"xMin" : "156540",
131+
"yMax" : "142773",
132+
"yMin" : "142771"
133+
},
134+
"g112" : {
135+
"finishedCount" : 0,
136+
"groupId" : "g112",
137+
"numberOfTasks" : 252,
138+
"progress" : 0,
139+
"projectId" : "test_build_area",
140+
"requiredCount" : 3,
141+
"xMax" : "156623",
142+
"xMin" : "156540",
143+
"yMax" : "142776",
144+
"yMin" : "142774"
145+
},
146+
"g113" : {
147+
"finishedCount" : 0,
148+
"groupId" : "g113",
149+
"numberOfTasks" : 252,
150+
"progress" : 0,
151+
"projectId" : "test_build_area",
152+
"requiredCount" : 3,
153+
"xMax" : "156623",
154+
"xMin" : "156540",
155+
"yMax" : "142779",
156+
"yMin" : "142777"
157+
},
158+
"g114" : {
159+
"finishedCount" : 0,
160+
"groupId" : "g114",
161+
"numberOfTasks" : 252,
162+
"progress" : 0,
163+
"projectId" : "test_build_area",
164+
"requiredCount" : 3,
165+
"xMax" : "156623",
166+
"xMin" : "156540",
167+
"yMax" : "142782",
168+
"yMin" : "142780"
169+
},
170+
"g115" : {
171+
"finishedCount" : 0,
172+
"groupId" : "g115",
173+
"numberOfTasks" : 252,
174+
"progress" : 0,
175+
"projectId" : "test_build_area",
176+
"requiredCount" : 3,
177+
"xMax" : "156623",
178+
"xMin" : "156540",
179+
"yMax" : "142785",
180+
"yMin" : "142783"
181+
},
182+
"g116" : {
183+
"finishedCount" : 0,
184+
"groupId" : "g116",
185+
"numberOfTasks" : 252,
186+
"progress" : 0,
187+
"projectId" : "test_build_area",
188+
"requiredCount" : 3,
189+
"xMax" : "156623",
190+
"xMin" : "156540",
191+
"yMax" : "142788",
192+
"yMin" : "142786"
193+
},
194+
"g117" : {
195+
"finishedCount" : 0,
196+
"groupId" : "g117",
197+
"numberOfTasks" : 252,
198+
"progress" : 0,
199+
"projectId" : "test_build_area",
200+
"requiredCount" : 3,
201+
"xMax" : "156623",
202+
"xMin" : "156540",
203+
"yMax" : "142791",
204+
"yMin" : "142789"
205+
},
206+
"g118" : {
207+
"finishedCount" : 0,
208+
"groupId" : "g118",
209+
"numberOfTasks" : 252,
210+
"progress" : 0,
211+
"projectId" : "test_build_area",
212+
"requiredCount" : 3,
213+
"xMax" : "156623",
214+
"xMin" : "156540",
215+
"yMax" : "142794",
216+
"yMin" : "142792"
217+
},
218+
"g119" : {
219+
"finishedCount" : 0,
220+
"groupId" : "g119",
221+
"numberOfTasks" : 252,
222+
"progress" : 0,
223+
"projectId" : "test_build_area",
224+
"requiredCount" : 3,
225+
"xMax" : "156623",
226+
"xMin" : "156540",
227+
"yMax" : "142797",
228+
"yMin" : "142795"
229+
},
230+
"g120" : {
231+
"finishedCount" : 0,
232+
"groupId" : "g120",
233+
"numberOfTasks" : 252,
234+
"progress" : 0,
235+
"projectId" : "test_build_area",
236+
"requiredCount" : 3,
237+
"xMax" : "156623",
238+
"xMin" : "156540",
239+
"yMax" : "142800",
240+
"yMin" : "142798"
241+
}
242+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"createdBy": "test",
3+
"geometry": {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[34.975833892822266,-15.899098066386088],[35.089302062988274,-15.899098066386088],[35.089302062988274,-15.820002241903946],[34.975833892822266,-15.820002241903946],[34.975833892822266,-15.899098066386088]]]},"properties":{}}]},
4+
"image": "",
5+
"lookFor": "buildings",
6+
"name": "test - Malawi (1)\ntest",
7+
"projectDetails": "test",
8+
"verificationNumber": 3,
9+
"groupSize": 120,
10+
"tileServer": {
11+
"name": "bing",
12+
"credits": "© 2019 Microsoft Corporation, Earthstar Geographics SIO"
13+
},
14+
"projectType": 1
15+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import json
2+
import os
3+
import unittest
4+
5+
from mapswipe_workers.utils import tile_grouping_functions
6+
from mapswipe_workers.utils.validate_input import save_geojson_to_file
7+
8+
9+
class TestTileGroupingFunctions(unittest.TestCase):
10+
def test_extent_to_group(self):
11+
12+
project_draft_path = (
13+
"fixtures/tile_map_service_grid/projectDrafts/build_area.json"
14+
)
15+
groups_path = "fixtures/tile_map_service_grid/groups/build_area.json"
16+
17+
test_dir = os.path.dirname(os.path.abspath(__file__))
18+
19+
with open(os.path.join(test_dir, project_draft_path)) as json_file:
20+
project_draft = json.load(json_file)
21+
path_to_geometries = save_geojson_to_file(1, project_draft["geometry"])
22+
23+
created_groups = tile_grouping_functions.extent_to_groups(
24+
path_to_geometries, 18, project_draft["groupSize"]
25+
)
26+
27+
with open(os.path.join(test_dir, groups_path)) as json_file:
28+
test_groups = json.load(json_file)
29+
30+
self.assertEqual(len(test_groups), len(created_groups))
31+
32+
def test_groups_to_tasks(self):
33+
34+
project_draft_path = (
35+
"fixtures/tile_map_service_grid/projectDrafts/build_area.json"
36+
)
37+
38+
test_dir = os.path.dirname(os.path.abspath(__file__))
39+
with open(os.path.join(test_dir, project_draft_path)) as json_file:
40+
project_draft = json.load(json_file)
41+
path_to_geometries = save_geojson_to_file(1, project_draft["geometry"])
42+
raw_groups = tile_grouping_functions.extent_to_groups(
43+
path_to_geometries, 18, project_draft["groupSize"]
44+
)
45+
46+
tasks = tile_grouping_functions.groups_to_tasks(raw_groups, zoomLevel=18)
47+
self.assertTrue(tasks)

0 commit comments

Comments
 (0)