예제 #1
0
파일: views.py 프로젝트: sabman/riab
def get_servers(user):
    """ Gets the list of servers for a given user
    """

    theuser = get_guaranteed_valid_user(user)
    try:
        workspace = Workspace.objects.get(user=theuser)
    except Workspace.DoesNotExist:
        workspace = Workspace.objects.get(user__username='******')
    servers = workspace.servers.all()
    geoservers = [{'url': settings.GEOSERVER_BASE_URL + 'ows',
                   'name': 'Local Geoserver',
                   'version': '1.0.0', 'id':0}]
    for server in servers:
        # TODO for the moment assume version 1.0.0
        geoservers.append({'url': server.url,
                           'name': server.name,
                           'id': server.id,
                           'version': '1.0.0'})

    return geoservers
예제 #2
0
파일: views.py 프로젝트: sabman/riab
def calculate(request, save_output=dummy_save):
    start = datetime.datetime.now()

    if request.method == 'GET':
        # FIXME: Add a basic form here to be able to generate the POST request.
        return HttpResponse('This should be accessed by robots, not humans.'
                            'In other words using HTTP POST instead of GET.')
    elif request.method == 'POST':
        data = request.POST
        impact_function_name = data['impact_function']
        hazard_server = data['hazard_server']
        hazard_layer = data['hazard']
        exposure_server = data['exposure_server']
        exposure_layer = data['exposure']
        bbox = data['bbox']
        keywords = data['keywords']


    theuser = get_guaranteed_valid_user(request.user)

    plugin_list = get_plugins(impact_function_name)
    _, impact_function = plugin_list[0].items()[0]
    impact_function_source = inspect.getsource(impact_function)

    # Create entry in database
    calculation = Calculation(user=theuser,
                              run_date=start,
                              hazard_server=hazard_server,
                              hazard_layer=hazard_layer,
                              exposure_server='exposure_server',
                              exposure_layer='exposure_layer',
                              impact_function=impact_function_name,
                              impact_function_source=impact_function_source,
                              bbox=bbox,
                              success=False)
    calculation.save()

    logger.info('Performing requested calculation')
    # Download selected layer objects
    logger.info('- Downloading hazard layer %s from %s' % (hazard_layer,
                                                           hazard_server))
    H = download(hazard_server, hazard_layer, bbox)
    logger.info('- Downloading exposure layer %s from %s' % (exposure_layer,
                                                             exposure_server))
    E = download(exposure_server, exposure_layer, bbox)

    # Calculate result using specified impact function
    logger.info('- Calculating impact using %s' % impact_function)

    impact_filename = calculate_impact(layers=[H, E],
                                       impact_function=impact_function)

    # Upload result to internal GeoServer
    logger.info('- Uploading impact layer %s' % impact_filename)
    result = save_output(impact_filename,
                         title='output_%s' % start.isoformat(),
                         user=theuser)
    logger.info('- Result available at %s.' % result.get_absolute_url())

    calculation.layer = result.get_absolute_url()
    calculation.success = True
    calculation.save()

    output = calculation.__dict__

    # json.dumps does not like datetime objects,
    # let's make it a json string ourselves
    output['run_date'] = 'new Date("%s")' % calculation.run_date

    # FIXME:This should not be needed in an ideal world
    ows_server_url = settings.GEOSERVER_BASE_URL + 'ows',
    output['ows_server_url'] = ows_server_url

    # json.dumps does not like django users
    output['user'] = calculation.user.username

    # Delete _state and _user_cache item from the dict,
    # they were created automatically by Django
    del output['_user_cache']
    del output['_state']
    jsondata = json.dumps(output)
    return HttpResponse(jsondata, mimetype='application/json')