def impact_function_filter(request): """Ajax Request for filtered available IF """ if request.method != 'GET': return HttpResponseBadRequest() exposure_id = request.GET.get('exposure_id') hazard_id = request.GET.get('hazard_id') if not (exposure_id and hazard_id): return HttpResponse(json.dumps([]), content_type="application/json") try: exposure_layer = Layer.objects.get(id=exposure_id) hazard_layer = Layer.objects.get(id=hazard_id) hazard_url = Analysis.get_layer_url(hazard_layer) exposure_url = Analysis.get_layer_url(exposure_layer) async_result = filter_impact_function.delay(hazard_url, exposure_url) impact_functions = async_result.get() return HttpResponse(json.dumps(impact_functions), content_type="application/json") except Exception as e: LOGGER.exception(e) raise HttpResponseServerError()
def layer_panel(request, bbox=None, **kwargs): if request.method != 'GET': return HttpResponseBadRequest() try: # both authorized_objects and sections are obtained from decorator authorized_objects = kwargs['authorized_objects'] sections = kwargs['sections'] form = AnalysisCreationForm( user=request.user, exposure_layer=retrieve_layers( 'exposure', bbox=bbox, authorized_objects=authorized_objects)[0], hazard_layer=retrieve_layers( 'hazard', bbox=bbox, authorized_objects=authorized_objects)[0], impact_functions=Analysis.impact_function_list()) context = { 'sections': sections, 'form': form, 'user': request.user, } return render(request, "geosafe/analysis/options_panel.html", context) except Exception as e: LOGGER.exception(e) return HttpResponseServerError()
def layer_panel(request, bbox=None): if request.method != 'GET': return HttpResponseBadRequest() try: authorized_objects = get_objects_for_user( request.user, 'base.view_resourcebase').values('id') sections = AnalysisCreateView.options_panel_dict( authorized_objects=authorized_objects, bbox=bbox) form = AnalysisCreationForm( user=request.user, exposure_layer=retrieve_layers( 'exposure', bbox=bbox, authorized_objects=authorized_objects)[0], hazard_layer=retrieve_layers( 'hazard', bbox=bbox, authorized_objects=authorized_objects)[0], impact_functions=Analysis.impact_function_list()) context = { 'sections': sections, 'form': form, 'user': request.user, } return render(request, "geosafe/analysis/options_panel.html", context) except Exception as e: LOGGER.exception(e) return HttpResponseServerError()
def get_layer_path(layer): """Helper function to get path for InaSAFE worker. :param layer: geonode layer :type layer: geonode.layers.models.Layer :p :return: Layer path or url :rtype: str """ using_direct_access = settings.USE_LAYER_FILE_ACCESS if using_direct_access and not layer.is_remote: base_layer_path = Analysis.get_base_layer_path(layer) layers_base_dir = settings.INASAFE_LAYER_DIRECTORY_BASE_PATH relative_path = os.path.relpath(base_layer_path, layers_base_dir) layer_url = os.path.join(settings.INASAFE_LAYER_DIRECTORY, relative_path) layer_url = urlparse.urljoin('file://', layer_url) else: layer_url = Analysis.get_layer_url(layer) return layer_url
def get_layer_path(layer, base=None): """Helper function to get path for InaSAFE worker. :param layer: geonode layer or simply the layer path :type layer: geonode.layers.models.Layer | basestring :param base: Base path :type base: basestring :return: Layer path or url :rtype: str """ using_direct_access = True if ( settings.USE_LAYER_FILE_ACCESS and settings.INASAFE_LAYER_DIRECTORY_BASE_PATH and settings.INASAFE_LAYER_DIRECTORY ) else False if using_direct_access: layers_base_dir = settings.INASAFE_LAYER_DIRECTORY_BASE_PATH if isinstance(layer, Layer) and not layer.remote_service: base_layer_path = Analysis.get_base_layer_path(layer) elif isinstance(layer, basestring): # Validate if it is actually resolvable by InaSAFE Headless base_layer_path = layer if not base_layer_path.startswith(layers_base_dir): raise AttributeError( 'Layer path possibly not reachable by InaSAFE Headless') else: raise AttributeError('Unexpected layer type') relative_path = os.path.relpath(base_layer_path, layers_base_dir) layer_url = os.path.join( settings.INASAFE_LAYER_DIRECTORY, relative_path) layer_url = urlparse.urljoin(base, layer_url) if isinstance(layer, Layer) and ( not using_direct_access or layer.remote_service): layer_url = Analysis.get_layer_url(layer) return layer_url
def test_download_file_url(self): """Test downloading file directly using url.""" data_helper = InaSAFETestData() filename = data_helper.hazard('flood_data.geojson') hazard = file_upload(filename) wait_metadata(hazard) # download zipped layer hazard_layer_url = Analysis.get_layer_url(hazard) downloaded_file = download_file(hazard_layer_url) # check that file is downloaded self.assertTrue(os.path.exists(downloaded_file)) basename, ext = os.path.splitext(downloaded_file) # check that file has .zip extension self.assertEqual(ext, '.zip') # delete zipfile os.remove(downloaded_file) # download metadata xml file of geosafe hazard_layer_xml = reverse('geosafe:layer-metadata', kwargs={'layer_id': hazard.id}) hazard_layer_xml = urlparse.urljoin(settings.GEONODE_BASE_URL, hazard_layer_xml) downloaded_file = download_file(hazard_layer_xml) # check that file is donwloaded self.assertTrue(os.path.exists(downloaded_file)) basename, ext = os.path.splitext(downloaded_file) # check that file has .xml extension self.assertEqual(ext, '.xml') # delete xmlfile os.remove(downloaded_file) # delete layer hazard.delete()
def get_form(self, form_class): kwargs = self.get_form_kwargs() kwargs.update({'impact_functions': Analysis.impact_function_list()}) logger.error(kwargs) return form_class(**kwargs)