Skip to content

Commit 8d23207

Browse files
committed
Starting to add batch functionality
1 parent 4e642ee commit 8d23207

4 files changed

Lines changed: 50 additions & 2 deletions

File tree

examples/challenge_examples.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,5 @@
5858
data = json.loads(data_file.read())
5959

6060
# Printing response:
61-
print(api.add_tasks_to_challenge(data, challenge_id))
61+
response = api.add_tasks_to_challenge(data, challenge_id)
62+

maproulette/api/challenge.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""This module contains the methods that the user will use directly to interact with MapRoulette challenges"""
22

3+
import geojson
34
from maproulette.api.maproulette_server import MapRouletteServer
45
from maproulette.models.challenge import ChallengeModel
56

@@ -250,7 +251,7 @@ def reset_task_instructions(self, challenge_id):
250251
def add_tasks_to_challenge(self, data, challenge_id):
251252
"""Method to add tasks to an existing challenge
252253
253-
:param data: a geojson containing geometry of tasks to be added to a challenge
254+
:param data: a GeoJSON containing geometry of tasks to be added to a challenge
254255
:param challenge_id: the ID corresponding to the challenge that tasks will be added to
255256
:returns: the API response from the PUT request
256257
"""
@@ -259,6 +260,42 @@ def add_tasks_to_challenge(self, data, challenge_id):
259260
body=data)
260261
return response
261262

263+
def add_tasks_to_challenge_batch(self, data, challenge_id, batch_size=100):
264+
"""Method to add tasks to an existing challenge
265+
266+
:param data: a GeoJSON containing geometry of tasks to be added to a challenge
267+
:param challenge_id: the ID corresponding to the challenge that tasks will be added to
268+
:param batch_size: the number of features within the GeoJSON to send to the API at a time. Default is 100.
269+
:returns: the API response from the PUT request
270+
"""
271+
if isinstance(data, str):
272+
try:
273+
geojson.loads(data)
274+
except ValueError
275+
features = data['features']
276+
feature_collection = {'type': 'FeatureCollection'}
277+
remaining_features = len(features)
278+
bundle = []
279+
count = 0
280+
for index, feature in enumerate(features):
281+
bundle.append(feature)
282+
count += 1
283+
if count == batch_size:
284+
feature_collection['features'] = bundle
285+
self.add_tasks_to_challenge(
286+
challenge_id=challenge_id,
287+
data=feature_collection
288+
)
289+
remaining_features -= count
290+
bundle = []
291+
count = 0
292+
if remaining_features > 0:
293+
feature_collection['features'] = bundle
294+
self.add_tasks_to_challenge(
295+
challenge_id=challenge_id,
296+
data=feature_collection
297+
)
298+
262299
def delete_challenge(self, challenge_id, immediate="false"):
263300
"""Method to delete a challenge by using the corresponding challenge ID
264301
@@ -314,3 +351,11 @@ def is_challenge_model(input_object):
314351
:returns: True if instance of model
315352
"""
316353
return bool(isinstance(input_object, ChallengeModel))
354+
355+
@staticmethod
356+
def string_to_geojson(input_object):
357+
"""Converts string to GeoJSON object using the geojson package
358+
359+
:param input_object: string representing the GeoJSON object
360+
:return: a valid
361+
"""

maproulette/api/maproulette_server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def __check_health(self, retries=3, delay=5):
4343
return True
4444
except requests.exceptions.ConnectionError:
4545
print(f"Connection not available. Attempt {str(i+1)} out of {str(retries)}")
46+
time.sleep(delay)
4647

4748
raise ConnectionUnavailableError(
4849
message='Specified server unavailable'

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ requests==2.23.0
22
tox==3.14.5
33
sphinx==2.4.4
44
sphinx_rtd_theme
5+
geojson

0 commit comments

Comments
 (0)