def get_tasks_from_json(args,inputMapRouletteChallenge): mr_challenge = inputMapRouletteChallenge with open(args.json_file, 'r') as infile: tasks = json.load(infile) for task in tasks: osmid = task['geometries']['features'][0]['properties']['osmid'] geom = task['geometries'] mr_challenge.addTask(mapRouletteTask(geom,osmid,mr_challenge.slug,mr_challenge.instruction)) return mr_challenge
def get_tasks_from_db(args,inputMapRouletteChallenge): db_user = args.user if not args.user: db_user = getpass.getuser() db_name = args.database if not args.database: db_name = 'osm' db_query = args.query db_string = "dbname={db_name} user={db_user}".format(db_name=db_name, db_user=db_user ) if args.host: db_string += " host={db_host}".format(db_host=args.host) # open a connection, get a cursor conn = psycopg2.connect(db_string) cur = conn.cursor(cursor_factory=DictCursor) register_hstore(cur) # get our results cur.execute(db_query) nodes = cur.fetchall() for node in nodes: osmid = node["id"] geom = { "type": "FeatureCollection", "features": [{ "type": "Feature", "properties": {"osmid": osmid}, "geometry": json.loads(geojson.dumps( wkb.loads(node["geom"].decode("hex")))) }] } mr_challenge.addTask(mapRouletteTask(geom,osmid,mr_challenge.slug,mr_challenge.instruction)) return mr_challenge
import os import json from classmaprouletteloader import mapRouletteChallenge, mapRouletteTask #constructing mapRouletteChallenge object,verifying API service exists testchallenge=mapRouletteChallenge('http://192.168.1.12:5000/','testchallenge',challengeTitle='this is a test',challengeInstruction='test are instructions') #creating challenge via API call testchallenge.initChallenge() #sample upload json objects/tasks with open('example2.json', 'r') as infile: tasks = json.load(infile) for task in tasks: osmid = task['geometries']['features'][0]['properties']['osmid'] geom = task['geometries'] testchallenge.addTask(mapRouletteTask(geom,osmid,testchallenge.slug,testchallenge.instruction)) httpresponse = testchallenge.uploadTasks()