示例#1
0
def run_seed(projects):
    logging.basicConfig(filename='export.log',
                        level=logging.WARNING,
                        format='%(asctime)s %(levelname)-8s %(message)s',
                        datefmt='%m-%d %H:%M:%S',
                        filemode='a')

    # get geoserver config
    g = geoserver_config()

    responses = []

    for project_id in projects:
        # get layer name
        logging.warning('start seed layer for: %s' % project_id)
        layer = 'final_{}'.format(project_id)

        # set the type (seed, reseed, truncate)
        type = 'reseed'

        # check if there are seeding tasks are pending for layer
        if check_task_queue(g, layer) is True:
            # abort seeding
            logging.warning('tasks are running and or pending')
            aborted_tasks = abort_pending_tasks(g, layer)
            logging.warning('aborted tasks and response: %s' % aborted_tasks)
        # seed layer for the different styles
        for style in g['styles']:
            r = seed_layer(g, layer, type, style)
        if r.status_code == 200:
            logging.warning('sucessfully seeded layer: %s' % layer)
        else:
            logging.warning('failed to seed layer: %s' % layer)
            logging.warning('response status code: %s' % r.status_code)
            logging.warning('response status text: %s' % r.text)

        responses.append([project_id, r.status_code])
        logging.warning('finished seed layer for: %s' % project_id)

    return responses
def create_projects_layers(project_table_name):
    # creates either two layers: one layer with centroids and another one with extents for all mapswipe projects

    # get geoserver config
    g = geoserver_config()

    responses = []
    # define world wide bbox since projects potentiall span the whole globe
    bbox_world = [-180.0, 180.0, -85.0, 85.0]
    # define layer names
    layer_names = ['{}_centroids'.format(project_table_name), '{}_extents'.format(project_table_name)]

    for layer_name in layer_names:
        # check if the layer already exists and skip if so
        if check_if_layer_exists(g, layer_name):
            responses.append([layer_name, 'layer already exists'])
            logging.warning('layer already exists: %s' % layer_name)
            print('layer already exists: %s' % layer_name)
        else:
            logging.warning('start create layer for: %s' % layer_name)
            print('start create layer for: %s' % layer_name)

            # call function to create centroid layer @ geoserver
            r, xml = create_layer(g, project_table_name, layer_name, bbox_world)

            # check if error appeared
            if r.status_code == 201:
                logging.warning('successfully created layer: %s' % layer_name)
            else:
                logging.warning('failed to create layer: %s' % layer_name)
                logging.warning('response status code: %s' % r.status_code)
                logging.warning('response status text: %s' % r.text)

            responses.append([layer_name, r.status_code])
            logging.warning('finished create layer for: %s' % layer_name)
            print('finished create layer for: %s' % layer_name)

    return responses
def run_create(projects, project_table_name):
    # this function will create the geoserver layers for specified projects
    # this functions does not create a layer for project extents of all projects

    logging.basicConfig(filename='export.log',
                        level=logging.WARNING,
                        format='%(asctime)s %(levelname)-8s %(message)s',
                        datefmt='%m-%d %H:%M:%S',
                        filemode='a'
                        )

    # get geoserver config
    g = geoserver_config()
    responses = []

    # first step create the layers for all projects specified:
    if projects:
        for project_id in projects:
            response = create_final_layer(g, project_id, project_table_name)
            responses.append(response)
    else:
        print('no projects given for create layer')

    return responses
示例#4
0
def run_delete(projects, flag_delete_initial_layers, project_table_name):

    print(projects)

    logging.basicConfig(filename='export.log',
                        level=logging.WARNING,
                        format='%(asctime)s %(levelname)-8s %(message)s',
                        datefmt='%m-%d %H:%M:%S',
                        filemode='a'
                        )

    # get geoserver config
    g = geoserver_config()
    responses = []

    if flag_delete_initial_layers:

        layer_names = ['{}_centroids'.format(project_table_name), '{}_extents'.format(project_table_name)]
        # define layer names
        for layer_name in layer_names:

            logging.warning('start delete layer for: %s' % layer_name)
            print('start delete layer for: %s' % layer_name)

            # call function to delete centroid layer @ geoserver
            ri, ri_2 = delete_layer(g, layer_name)

            # check response
            if ri.status_code == 200:
                logging.warning('sucessfully deleted layer: %s' % layer_name)
            else:
                logging.warning('failed to delete layer: %s' % layer_name)
                logging.warning('response status code: %s' % ri.status_code)
                logging.warning('response status text: %s' % ri.text)

            if ri_2.status_code == 200:
                logging.warning('sucessfully deleted featuretype for layer: %s' % layer_name)
            else:
                logging.warning('failed to delete featuretype for layer: %s' % layer_name)
                logging.warning('response status code: %s' % ri_2.status_code)
                logging.warning('response status text: %s' % ri_2.text)

            responses.append([layer_name, ri.status_code, ri_2.status_code])
            logging.warning('finish delete layer for: %s' % layer_name)
            print('finished delete layer for: %s' % layer_name)



    if projects:
        for project_id in projects:
            # get layer name
            logging.warning('start delete layer for: %s' % project_id)
            print('start delete layer for: %s' % project_id)
            layer = 'final_{}'.format(project_id)

            # delete layer
            r, r_2 = delete_layer(g, layer)

            # check response
            if r.status_code == 200:
                logging.warning('sucessfully deleted layer: %s' % layer)
            else:
                logging.warning('failed to delete layer: %s' % layer)
                logging.warning('response status code: %s' % r.status_code)
                logging.warning('response status text: %s' % r.text)

            if r_2.status_code == 200:
                logging.warning('sucessfully deleted featuretype for layer: %s' % layer)
            else:
                logging.warning('failed to delete featuretype for layer: %s' % layer)
                logging.warning('response status code: %s' % r_2.status_code)
                logging.warning('response status text: %s' % r_2.text)

            responses.append([project_id, r.status_code, r_2.status_code])
            logging.warning('finish delete layer for: %s' % project_id)
            print('finished delete layer for: %s' % project_id)
    else:
        print('no projects given for delete layer')
    return responses
示例#5
0
def run_wfs_export(layer, path):

    logging.basicConfig(filename='export.log',
                        level=logging.WARNING,
                        format='%(asctime)s %(levelname)-8s %(message)s',
                        datefmt='%m-%d %H:%M:%S',
                        filemode='a')

    g = geoserver_config()

    if check_if_layer_exists(g, layer):
        logging.warning('Export of Layer: %s started' % layer)

        # seperate location from file
        try:
            head, tail = ntpath.split(path)
        except:
            print('slicing of path/file failed')
            print('path: %s' % path)
            print('head: %s' % head)
            print('tail: %s' % tail)
        # separate file extension from filename
        file_ext = tail.split('.')[1]

        # check if fileextension is available @ geoserver
        format = get_out_format(file_ext)

        # check if format is of type shape zip to overwrite filextension to .zip format
        '''if format == 'shape-zip':
            tail = tail.split('.')[0] + '.zip'
        '''

        if not format:
            logging.warning(
                'Export format not found, requested filextension: %s' %
                file_ext)
            sys.exit()

        logging.warning('Export format found, using %s' % format)
        # build url from layer geoserver config and output format
        url = build_wfs_url(g, layer, format)

        logging.warning('Url built: %s' % url)

        # check if path is provided
        '''if head:
            print(head)
            # convert path to absolute path regardless if it alread is an absolute path
            head = os.path.abspath(head)
            print(head)
            os.chdir(head)
            print('changed dir')
        '''

        # request wfs
        wfs = requests.get(url)
        logging.warning(
            'Export successfull. WFS: %s written to location/file: %s ' %
            (layer, path))
        with open(path, "w") as fo:
            fo.write(wfs.text)

        return wfs.status_code

    else:
        logging.warning(
            'There is no layer: {layer} registered at geoserver with url {g_url}'
            .format(layer=layer, g_url=g['baseurl']))