Exemplo n.º 1
0
def geojson_to_task(slug, feature):
    """converts one geojson feature to a task.
    This will only work in a limited number of cases, where:
    * The geometry is one single Point or Linestring feature,
    * The OSM ID of the feature is in the id field of the JSON,
    and you are OK with the following:
    * The description cannot be set at the task level
    * The identifier will be slug-osmid"""
    # check for id and geometries
    if 'id' not in feature:
        app.logger.debug('no id in this feature, skipping')
        return None
    if 'geometry' not in feature:
        app.logger.debug('no geometries in feature, skipping')
        return None
    # generate an identifier
    osmid = feature['id']
    identifier = '{slug}-{osmid}'.format(
        slug=slug,
        osmid=osmid)
    # find, or create a task
    task = Task.query.filter(
        Task.challenge_slug == slug).filter(
        Task.identifier == identifier).first()
    if task is None:
        task = Task(slug, identifier)
        app.logger.debug('creating task {identifier}'.format(
            identifier=identifier))
    else:
        app.logger.debug('updating task {identifier}'.format(
            identifier=identifier))
        # clear existing geometries
        task.geometries = []
    # get the geometry
    geom = feature['geometry']
    shape = asShape(geom)
    try:
        g = TaskGeometry(osmid, shape)
        task.geometries.append(g)
        return task
    except Exception as e:
        app.logger.debug("task could not be created, {}".format(e))
        return None
Exemplo n.º 2
0
def json_to_task(slug, data, task=None, identifier=None):
    """Parse task json coming in through the admin api"""

    app.logger.debug(data)

    if identifier is None and task is None:
        if 'identifier' not in data:
            raise Exception('no identifier given')
        identifier = data['identifier']

    if task is None:
        # create the task if none was passed in
        try:
            task = Task(slug, identifier)
        except Exception as e:
            app.logger.warn(e)
            raise e
    else:
        # delete existing task geometries
        task.geometries = []

    # check for instruction
    if 'instruction' in data:
        task.instruction = data['instruction']
    # check for status
    if 'status' in data:
        task.status = data['status']

    # extract the task geometries
    if 'geometries' in data:
        geometries = data.pop('geometries')
        # parse the geometries
        app.logger.debug(geometries)
        for feature in geometries['features']:
            osmid = None
            if 'properties' in feature and feature['properties'].get('osmid'):
                osmid = feature['properties'].get('osmid')
            app.logger.debug(feature)
            shape = asShape(feature['geometry'])
            g = TaskGeometry(shape, osmid)
            task.geometries.append(g)
    return task
Exemplo n.º 3
0
def json_to_task(slug, data, task=None, identifier=None):
    """Parse task json coming in through the admin api"""

    app.logger.debug(data)

    if identifier is None and task is None:
        if 'identifier' not in data:
            raise Exception('no identifier given')
        identifier = data['identifier']

    if task is None:
        # create the task if none was passed in
        try:
            task = Task(slug, identifier)
        except Exception as e:
            app.logger.warn(e)
            raise e
    else:
        # delete existing task geometries
        task.geometries = []

    # check for instruction
    if 'instruction' in data:
        task.instruction = data['instruction']
    # check for status
    if 'status' in data:
        task.status = data['status']

    # extract the task geometries
    if 'geometries' in data:
        geometries = data.pop('geometries')
        # parse the geometries
        app.logger.debug(geometries)
        for feature in geometries['features']:
            osmid = None
            if 'properties' in feature and feature['properties'].get('osmid'):
                osmid = feature['properties'].get('osmid')
            app.logger.debug(feature)
            shape = asShape(feature['geometry'])
            g = TaskGeometry(shape, osmid)
            task.geometries.append(g)
    return task
Exemplo n.º 4
0
def geojson_to_task(slug, feature):
    """converts one geojson feature to a task.
    This will only work in a limited number of cases, where:
    * The geometry is one single Point or Linestring feature,
    * The OSM ID of the feature is in the id field of the JSON,
    and you are OK with the following:
    * The description cannot be set at the task level
    * The identifier will be slug-osmid"""
    # check for id and geometries
    if 'id' not in feature:
        app.logger.debug('no id in this feature, skipping')
        return None
    if 'geometry' not in feature:
        app.logger.debug('no geometries in feature, skipping')
        return None
    # generate an identifier
    osmid = feature['id']
    identifier = '{slug}-{osmid}'.format(slug=slug, osmid=osmid)
    # find, or create a task
    task = Task.query.filter(Task.challenge_slug == slug).filter(
        Task.identifier == identifier).first()
    if task is None:
        task = Task(slug, identifier)
        app.logger.debug(
            'creating task {identifier}'.format(identifier=identifier))
    else:
        app.logger.debug(
            'updating task {identifier}'.format(identifier=identifier))
        # clear existing geometries
        task.geometries = []
    # get the geometry
    geom = feature['geometry']
    shape = asShape(geom)
    try:
        g = TaskGeometry(osmid, shape)
        task.geometries.append(g)
        return task
    except Exception, e:
        app.logger.debug("task could not be created, {}".format(e))
        return None