Пример #1
0
def current_data(**kwargs):
    """ Get current data from the Feature table. """

    # find the ID of the latest file
    latest_file = session.query(OSMfile).order_by(
        OSMfile.creation_date).all()[-1]

    # get current features (features from the last file)
    list_current = session.query(Feature).filter(and_(
        Feature.all_tags[kwargs['fkey']] == kwargs['fvalue']),
        Feature.file_id == latest_file.id,
        Feature.osm_version >= kwargs['osm_version']).order_by(
        Feature.osm_version).all()

    return list_current
Пример #2
0
def count_inserts(**kwargs):
    """ Counts the number of tag insertions. """

    # get current data
    list_current = current_data(**kwargs)

    for i, current in enumerate(list_current):

        # select all version of the feature
        features = session.query(Feature).filter(
            Feature.osm_id == current.osm_id).order_by(
            Feature.osm_version).all()

        # initialize counter
        total = 0

        if len(features) >= 2:
            for i, feature in enumerate(features):

                if i != 0:
                    previous = features[i - 1]

                    if feature.all_tags != previous.all_tags:
                        # go through all tags
                        for key in feature.all_tags:
                            if not previous.all_tags.has_key(key):
                                total += 1

            # save results to the DB
            save_result(kwargs['job_op_id'], current.osm_id, total)
Пример #3
0
def find_feature(osm_id, osm_version, osm_timestamp):
    """ Fetches the existing feature. """

    return session.query(Feature).filter(and_(
        Feature.osm_id == osm_id,
        Feature.osm_version == osm_version,
        Feature.osm_timestamp == osm_timestamp)).first()
Пример #4
0
def count_geom_changes(**kwargs):
    """ Counts the number of geometric changes. """

    # get current data
    list_current = current_data(**kwargs)

    for i, current in enumerate(list_current):

        if current.osm_version >= kwargs['osm_version']:
            # select features based on the osm_id
            features = session.query(Feature).filter(
                Feature.osm_id == current.osm_id).order_by(
                Feature.osm_version).all()

            # initialize counter
            total = 0

            if len(features) >= 2:
                for i, feature in enumerate(features):

                    if i != 0:
                        previous_geom = find_geom(features[i - 1])
                        current_geom = find_geom(feature)

                        if not previous_geom.almost_equals(current_geom):
                            total += 1

            # save results to the DB
            save_result(kwargs['job_op_id'], current.osm_id, total)
Пример #5
0
def time_boundary(**kwargs):
    """ Sets the time boundary between up-to-date features and outdated
    features. """

    # find the ID of the latest file
    latest_file = session.query(OSMfile).order_by(OSMfile.id).all()[-1]

    # get current features (features from the last file)
    features = session.query(Feature).filter(and_(
        Feature.all_tags[kwargs['fkey']] == kwargs['fvalue']),
        Feature.file_id == latest_file.id).order_by(
        Feature.osm_timestamp).all()

    # find the index of the median timestamp
    idx = int(len(features) / 2)

    return convert_timestamp(features[idx].osm_timestamp)
Пример #6
0
def count_updates(**kwargs):
    """ Counts the number of modified tags or the number of modifications to a
    certain tag of interest. """

    # get current data
    list_current = current_data(**kwargs)

    # find TOI
    try:
        tag = kwargs['tag_of_interest']
    except KeyError:
        tag = None

    for i, current in enumerate(list_current):

        # select features based on the osm_id
        features = session.query(Feature).filter(
            Feature.osm_id == current.osm_id).order_by(
            Feature.osm_version).all()

        # initialize counter
        total = 0

        if len(features) >= 2:
            for i, feature in enumerate(features):

                if i != 0:
                    previous = features[i - 1]

                    # if no tag was specified
                    if not tag:
                        for key in feature.all_tags:
                            # go through all tags
                            if previous.all_tags.has_key(key) and \
                                    feature.all_tags.has_key(key) and \
                                    previous.all_tags[key] != feature.all_tags[key]:
                                total += 1

                    # if a tag was specified
                    else:
                        # check the specified tag
                        if previous.all_tags.has_key(tag) and \
                                feature.all_tags.has_key(tag) and \
                                previous.all_tags[tag] != feature.all_tags[tag]:
                            total += 1

            # save results to the DB
            save_result(kwargs['job_op_id'], current.osm_id, total)
Пример #7
0
def count_deletions(**kwargs):
    """ Counts the number of deleted tags. """

    # get current data
    list_current = current_data(**kwargs)

    # check if any specific tag was specified
    try:
        tag = kwargs['tag_of_interest']
    except KeyError:
        tag = None

    for i, current in enumerate(list_current):

        # select features based on the osm_id
        features = session.query(Feature).filter(
            Feature.osm_id == current.osm_id).order_by(
            Feature.osm_version).all()

        # initialize counter
        total = 0

        if len(features) >= 2:

            for i, feature in enumerate(features):

                if i != 0:
                    previous = features[i - 1]

                    # if no tag was specified
                    if tag == None:
                        # go through all tags
                        for key in previous.all_tags:
                            if previous.all_tags.has_key(key) == True and \
                                    feature.all_tags.has_key(key) == False:
                                total += 1

                    # if a tag was specified
                    else:
                        # check the specified tag
                        if previous.all_tags.has_key(tag) == True and \
                                feature.all_tags.has_key(tag) == False:
                            total += 1

            # save results to the DB
            save_result(kwargs['job_op_id'], current.osm_id, total)
Пример #8
0
def save_result(job_operation_id, osm_id, value):
    """ Save the result into an Hstore.
    For more info visit: http://stackoverflow.com/questions/28331046/\
    why-does-sqlalchemy-initialize-hstore-field-as-null """

    # checks the existence of the job->operation combination
    exists = session.query(Result).filter(
        Result.job_operation_id == job_operation_id).first()

    # saves new results
    if not exists:
        new_result = Result()
        new_result.job_operation_id = job_operation_id
        new_result.results[osm_id] = str(value)

        session.add(new_result)

    # updates existing results
    else:
        exists.results[osm_id] = str(value)

    session.flush()
Пример #9
0
def find_file(date):
    """ Fetches the existing file. """
    return session.query(OSMfile).filter(OSMfile.creation_date == date).first()
Пример #10
0
def main(**kwargs):

    # create a new job
    job = Job(kwargs['new_job'])

    for key in kwargs:
        # find operations
        if key[:3] == 'op_':

            # check for an existing operation
            existing_operation = session.query(Operation).filter(
                Operation.func_name == key[3:]).first()

            # define JobOperation parameters
            job_oper = JobOperation(';'.join([str(kwargs[a]) for a in kwargs
                                              if kwargs[a] != True]))

            # define new operation
            if not existing_operation:
                job_oper.operation = Operation(key[3:])
            # use existing operation
            else:
                job_oper.operation = existing_operation

            # add job and operatios to DB
            job.operations.append(job_oper)
            session.add_all([job, job_oper])
            session.flush()

    for assoc in job.operations:
        # ignore output function
        if assoc.operation.func_name != 'geojson':

            # initialize counter functions
            if assoc.operation.func_name[:5] == 'count':
                operation = getattr(operations.counters,
                                    assoc.operation.func_name)

            # initialize change functions
            elif assoc.operation.func_name[:6] == 'change':
                operation = getattr(operations.changes,
                                    assoc.operation.func_name)

            # initialize currency functions
            elif assoc.operation.func_name[:8] == 'currency':
                operation = getattr(operations.currency,
                                    assoc.operation.func_name)

            # initialize geometry functions
            elif assoc.operation.func_name[:4] == 'geom':
                operation = getattr(operations.geometry,
                                    assoc.operation.func_name)

            # test the existence of a tag of interest (TOI)
            try:
                operation(
                    job_op_id=assoc.id,
                    fkey=kwargs['fkey'],
                    fvalue=kwargs['fvalue'],
                    osm_version=kwargs['osm_version'],
                    tag_of_interest=kwargs['tag_of_interest'])

            except KeyError:
                operation(
                    job_op_id=assoc.id,
                    fkey=kwargs['fkey'],
                    fvalue=kwargs['fvalue'],
                    osm_version=kwargs['osm_version'])

            session.flush()

    # create a GeoJSON output file
    if kwargs['op_geojson']:
        create_GeoJSON(job, kwargs)