def get_map_layer(request):
    context = {}
    if request.is_ajax() and request.method == 'POST':
        post_params = request.POST
        # If 'Display map' is clicked, load layers
        map_layers = load_tiff_ly(post_params)
        if map_layers:
            load_layer = map_layers[0]['options']['params']['LAYERS']
        else:
            load_layer = None

        context = {
            'load_layer': load_layer,
            'geoserver_url': get_geoserver_url()
        }

    return JsonResponse(context)
Beispiel #2
0
def load_tiff_ly(post_params):
    """
    This function returns the previously loaded map or the new map layer
    if the button on the page was clicked
    """
    map_layers = None

    if post_params.get('plotTime'):
        plot_time = post_params['plotTime']
    else:
        plot_time = None

    if post_params.get('model'):
        model = post_params['model']
    else:
        model = None

    if post_params.get('variable'):
        variable = post_params['variable']
    else:
        variable = None

    if model and variable and plot_time:
        # Geoserver parameters
        geo_eng = get_spatial_dataset_engine(name='default')
        # Data rods parameters
        latlonbox = [post_params['lonW'], post_params['latS'], post_params['lonE'], post_params['latN']]
        time_st = plot_time + ':00:00Z/' + plot_time + ':00:30Z'
        zip_file, store_name, store_id = get_raster_zip(latlonbox, time_st, model, variable)
        # Create raster in geoserver
        flag_add_layer = False
        response = geo_eng.create_coverage_resource(store_id=store_id,
                                                    coverage_file=zip_file,
                                                    coverage_type='worldimage',
                                                    overwrite=True,
                                                    )
        if not response['success']:
            result = geo_eng.create_workspace(workspace_id=get_workspace(),
                                              uri='tethys_app-%s' % get_workspace())
            if result['success']:
                response = geo_eng.create_coverage_resource(store_id=store_id,
                                                            coverage_file=zip_file,
                                                            coverage_type='worldimage',
                                                            overwrite=True,
                                                            )
                if response['success']:
                    flag_add_layer = True
        else:
            flag_add_layer = True

        if flag_add_layer:
            # Add raster to map
            title = '{0} {1}'.format(variable, plot_time)
            geoserver_layer = MVLayer(source='ImageWMS',
                                      options={'url': get_geoserver_url(),
                                               'params': {'LAYERS': store_id},
                                               'serverType': 'geoserver'},
                                      legend_title=title,
                                      )
            map_layers = [geoserver_layer]

    return map_layers