11"""This module contains the methods that the user will use directly to interact with MapRoulette challenges"""
22
3+ import geojson
34from maproulette .api .maproulette_server import MapRouletteServer
45from 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+ """
0 commit comments