Ejemplo n.º 1
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()
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
def main(filename, osm_config_file):
    """ Reads the PBF file. """

    # required for OSM data format
    gdal.SetConfigOption('OGR_INTERLEAVED_READING', 'YES')

    # set 'OSM_CONFIG_fILE'
    gdal.SetConfigOption('OSM_CONFIG_FILE', osm_config_file)

    # extract file date
    filedate = ''.join([s for s in filename if s.isdigit()])
    creation_date = date(
        int(filedate[:4]), int(filedate[4:6]), int(filedate[6:8]))

    # find existing file
    existing_file = find_file(creation_date)
    # create new file
    if not existing_file:
        osmfile = OSMfile(creation_date)
        session.add(osmfile)
        session.flush()

    osmfile_id = find_file(creation_date).id

    lyr_read = FeatureReader(filename)

    for layer, feature in lyr_read.readData():

        # extract important attributes
        osm_id = prepare_osm_id(feature, layer)
        osm_version = feature.GetField('osm_version')
        osm_timestamp = feature.GetField('osm_timestamp')
        all_tags = feature.GetField('all_tags')
        geom = feature.GetGeometryRef()

        # detect corrupt geometries
        bad_geom = check_bad_geom(geom, osm_id)
        if bad_geom or bad_geom == 'BONKERS!':
            continue

        # find duplicate features
        duplicate = find_feature(osm_id, osm_version, osm_timestamp)

        # add new data
        if not duplicate:
            new_feature = Feature(osmfile_id, osm_id, osm_version,
                                  osm_timestamp, all_tags, layer)

            if layer == 'points':
                new_feature.point = GeomPoint(geom)
            elif layer == 'lines':
                new_feature.line = GeomLine(geom)
            elif layer == 'multilinestrings':
                new_feature.multiline = GeomMultiLine(geom)
            elif layer == 'multipolygons':
                new_feature.multipolygon = GeomMultiPolygon(geom)
            elif layer == 'other_relations':
                new_feature.other_rel = GeomOtherRelation(geom)

            session.add(new_feature)

        else:
            # reference it to the current file
            duplicate.file_id = osmfile_id

        session.flush()
Ejemplo n.º 4
0
                        help='Find objects in need for revision (out-of-date)')

    parser.add_argument('-occ', '--op-currency-coefficient', required=False,
                        default=argparse.SUPPRESS, nargs='?', const=True,
                        help='Calculate the currency coefficient for each OSM feature')

    parser.add_argument('-ogfo', '--op-geom-from-original', required=False,
                        default=argparse.SUPPRESS, nargs='?', const=True,
                        help='Calculate the distance from the original position (nodes)')

    parser.add_argument('-ogsd', '--op-geom-sum-dist', required=False,
                        default=argparse.SUPPRESS, nargs='?', const=True,
                        help='Calculate cumulative distance across points (nodes)')

    parser.add_argument('-ogad', '--op-geom-area-diffs', required=False,
                        default=argparse.SUPPRESS, nargs='?', const=True,
                        help='Calculate the difference in areas (closed ways)')

    parser.add_argument('-ogld', '--op-geom-length-diffs', required=False,
                        default=argparse.SUPPRESS, nargs='?', const=True,
                        help='Calculate the difference in lengths (ways)')

    args = parser.parse_args()

    main(**vars(args))

    print 'Committing data and closing connection.'
    session.flush()
    session.commit()
    session.close()