Skip to content

Commit 9fe20d6

Browse files
committed
feat: add conflation project type
1 parent a7c1863 commit 9fe20d6

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

mapswipe_workers/mapswipe_workers/project_types/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .arbitrary_geometry.conflation.project import ConflationProject
12
from .arbitrary_geometry.digitization.project import DigitizationProject
23
from .arbitrary_geometry.footprint.project import FootprintProject
34
from .arbitrary_geometry.footprint.tutorial import FootprintTutorial
@@ -24,4 +25,5 @@
2425
"DigitizationProject",
2526
"StreetProject",
2627
"StreetTutorial",
28+
"ConflationProject",
2729
]

mapswipe_workers/mapswipe_workers/project_types/arbitrary_geometry/conflation/__init__.py

Whitespace-only changes.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import json
2+
import urllib
3+
4+
from mapswipe_workers.definitions import logger
5+
from mapswipe_workers.firebase.firebase import Firebase
6+
from mapswipe_workers.firebase_to_postgres.transfer_results import (
7+
results_to_file,
8+
save_results_to_postgres,
9+
truncate_temp_results,
10+
)
11+
from mapswipe_workers.generate_stats.project_stats import (
12+
get_statistics_for_integer_result_project,
13+
)
14+
from mapswipe_workers.project_types.arbitrary_geometry.project import (
15+
ArbitraryGeometryProject,
16+
)
17+
from mapswipe_workers.utils.api_calls import geojsonToFeatureCollection, ohsome
18+
19+
20+
class ConflationProject(ArbitraryGeometryProject):
21+
def __init__(self, project_draft):
22+
super().__init__(project_draft)
23+
self.inputType = project_draft["inputType"]
24+
25+
def handle_input_type(self, raw_input_file: str):
26+
"""
27+
Handle different input types.
28+
29+
Input (self.geometry) can be:
30+
'aoi_file' -> query ohsome with aoi from geometry then write
31+
result to raw_input_file
32+
a Link (str) -> download geojson from link and write to raw_input_file
33+
a TMId -> get project info from geometry and query ohsome
34+
for objects, then write to raw_input_file.
35+
"""
36+
if not isinstance(self.geometry, str):
37+
self.geometry = geojsonToFeatureCollection(self.geometry)
38+
self.geometry = json.dumps(self.geometry)
39+
40+
if self.inputType == "aoi_file":
41+
logger.info("aoi file detected")
42+
# write string to geom file
43+
ohsome_request = {"endpoint": "elements/geometry", "filter": self.filter}
44+
45+
result = ohsome(ohsome_request, self.geometry, properties="tags, metadata")
46+
with open(raw_input_file, "w") as geom_file:
47+
json.dump(result, geom_file)
48+
elif self.inputType == "TMId":
49+
logger.info("TMId detected")
50+
hot_tm_project_id = int(self.TMId)
51+
ohsome_request = {"endpoint": "elements/geometry", "filter": self.filter}
52+
result = ohsome(ohsome_request, self.geometry, properties="tags, metadata")
53+
result["properties"] = {}
54+
result["properties"]["hot_tm_project_id"] = hot_tm_project_id
55+
with open(raw_input_file, "w") as geom_file:
56+
json.dump(result, geom_file)
57+
elif self.inputType == "link":
58+
logger.info("link detected")
59+
urllib.request.urlretrieve(self.geometry, raw_input_file)
60+
61+
def save_tasks_to_firebase(self, projectId: str, tasks: dict):
62+
firebase = Firebase()
63+
firebase.save_tasks_to_firebase(projectId, tasks, useCompression=True)
64+
65+
@staticmethod
66+
def results_to_postgres(results: dict, project_id: str, filter_mode: bool):
67+
"""How to move the result data from firebase to postgres."""
68+
results_file, user_group_results_file = results_to_file(results, project_id)
69+
truncate_temp_results()
70+
save_results_to_postgres(results_file, project_id, filter_mode)
71+
return user_group_results_file
72+
73+
@staticmethod
74+
def get_per_project_statistics(project_id, project_info):
75+
"""How to aggregate the project results."""
76+
return get_statistics_for_integer_result_project(
77+
project_id, project_info, generate_hot_tm_geometries=False
78+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"createdBy" : "Sample Admin",
3+
"filter" : "building=* and geometry:polygon",
4+
"geometry" : {
5+
"type": "FeatureCollection",
6+
"features": [
7+
{
8+
"type": "Feature",
9+
"properties": {},
10+
"geometry": {
11+
"type": "Polygon",
12+
"coordinates": [[[9.18032169342041, 48.790552471542284],[9.187102317810059,48.790552471542284],[9.187102317810059,48.79407236257656],[9.18032169342041,48.79407236257656],[9.18032169342041,48.790552471542284]]]}
13+
}
14+
]
15+
},
16+
"groupSize" : 25,
17+
"lookFor": "Buildings",
18+
"image": "http://www.fragosus.com/test/Javita.jpg",
19+
"projectDetails": "This is a template for a GeoJSON AOI project. We use Bing as the tile server.",
20+
"inputType" : "aoi_file",
21+
"name" : "Test Footprint GeoJSON AOI",
22+
"projectTopic" : "Test Footprint GeoJSON AOI",
23+
"projectType" : 2,
24+
"verificationNumber": 3,
25+
"tileServer" : {
26+
"credits" : "© 2019 Microsoft Corporation, Earthstar Geographics SIO",
27+
"name" : "bing",
28+
"url" : "",
29+
"wmtsLayerName" : ""
30+
}
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
import unittest
3+
4+
from mapswipe_workers.project_types import ConflationProject
5+
from tests import fixtures
6+
7+
8+
class TestCreateConflationProject(unittest.TestCase):
9+
def setUp(self) -> None:
10+
project_draft = fixtures.get_fixture(
11+
os.path.join(
12+
"projectDrafts",
13+
"conflation.json",
14+
)
15+
)
16+
project_draft["projectDraftId"] = "foo"
17+
self.project = ConflationProject(project_draft)
18+
19+
def test_init(self):
20+
self.assertEqual(self.project.geometry["type"], "FeatureCollection")
21+
self.assertEqual(self.project.inputType, "aoi_file")
22+
23+
24+
if __name__ == "__main__":
25+
unittest.main()

0 commit comments

Comments
 (0)