|
| 1 | +import maproulette |
| 2 | +import json |
| 3 | +import base64 |
| 4 | + |
| 5 | +# Create a configuration object for MapRoulette using your API key: |
| 6 | +config = maproulette.Configuration(api_key="API_KEY") |
| 7 | + |
| 8 | +# Create an API objects with the above config object: |
| 9 | +api = maproulette.Task(config) |
| 10 | + |
| 11 | +# Setting a challenge ID in which we'll place our cooperative task |
| 12 | +challenge_id = 14452 |
| 13 | + |
| 14 | +# We'll start by creating some 'child' operations to apply to the target objects add them to a list: |
| 15 | +child_operations_list = [maproulette.ChildOperationModel(operation="setTags", |
| 16 | + data={"test_tag_1": "True", |
| 17 | + "test_tag_2": "True", |
| 18 | + "test_tag_3": "True"}).to_dict(), |
| 19 | + maproulette.ChildOperationModel(operation="setTags", |
| 20 | + data={"test_tag_4": "True"}).to_dict(), |
| 21 | + maproulette.ChildOperationModel(operation="setTags", |
| 22 | + data={"test_tag_5": "True"}).to_dict()] |
| 23 | + |
| 24 | +# Now we'll pass these operations into a 'parent' operation list to specify the objects to which the changes |
| 25 | +# will be applied: |
| 26 | +test_parent_relation = [maproulette.ParentOperationModel(operation_type="modifyElement", |
| 27 | + element_type="way", |
| 28 | + osm_id="175208404", |
| 29 | + child_operations=child_operations_list).to_dict()] |
| 30 | + |
| 31 | +# The below flags error when handling is in the constructor, but not when in the setter: |
| 32 | +test_2 = maproulette.ParentOperationModel(operation_type="modifyElement", |
| 33 | + element_type="way", |
| 34 | + osm_id="175208404", |
| 35 | + child_operations=child_operations_list) |
| 36 | + |
| 37 | + |
| 38 | +# Now that we have a Parent Operation containing the Child Operations we'd like to implement, we |
| 39 | +# can pass this into our Cooperative Work model: |
| 40 | + |
| 41 | +test_cooperative_work = maproulette.CooperativeWorkModel(version=2, |
| 42 | + type=1, |
| 43 | + parent_operations=test_parent_relation).to_dict() |
| 44 | + |
| 45 | +# Now we can create a basic task to apply these suggested changes to: |
| 46 | +with open('data/Example_Geometry.geojson', 'r') as data_file: |
| 47 | + data = json.loads(data_file.read()) |
| 48 | + |
| 49 | +test_task = maproulette.TaskModel(name="Test_Coop_Task_Kastellet", |
| 50 | + parent=challenge_id, |
| 51 | + geometries=data, |
| 52 | + cooperative_work=test_cooperative_work).to_dict() |
| 53 | + |
| 54 | + |
| 55 | +# Finally, we'll pass our task object to into the create_task method to call the /task |
| 56 | +# endpoint, creating this new task with our cooperative work model applied |
| 57 | +print(json.dumps(api.create_task(test_task), indent=4, sort_keys=True)) |
| 58 | + |
| 59 | + |
| 60 | +# Alternatively, cooperative work can be populated as in-progress edits via an OSM changefile (osc file) |
| 61 | +# as 'type 2' cooperative work: |
| 62 | +with open('data/ExampleChangefile.osc', 'rb') as data_file: |
| 63 | + osc_file = base64.b64encode(data_file.read()).decode('ascii') |
| 64 | + |
| 65 | +test_osc_cooperative_work = maproulette.CooperativeWorkModel(type=2, |
| 66 | + content=osc_file).to_dict() |
| 67 | + |
| 68 | +test_osc_task = maproulette.TaskModel(name="Test_Coop_Task_Kastellet_OSC_2", |
| 69 | + parent=challenge_id, |
| 70 | + geometries=data, |
| 71 | + cooperative_work=test_osc_cooperative_work).to_dict() |
| 72 | + |
| 73 | +print(json.dumps(api.create_task(test_osc_task), indent=4, sort_keys=True)) |
| 74 | + |
0 commit comments