Пример #1
0
    def accept(self):
        """Process the layer for multi buffering and generate a new layer.

        .. note:: This is called on OK click.
        """
        # set parameter from dialog
        input_layer = self.layer.currentLayer()
        output_path = self.output_form.text()
        radius = self.get_classification()
        # monkey patch keywords so layer works on multi buffering function
        input_layer.keywords = {'inasafe_fields': {}}

        # run multi buffering
        self.output_layer = multi_buffering(input_layer, radius)

        # save output layer to data store and check whether user
        # provide the output path.
        if output_path:
            self.output_directory, self.output_filename = (
                os.path.split(output_path))
            self.output_filename, self.output_extension = (
                os.path.splitext(self.output_filename))

        # if user do not provide the output path, create a temporary file.
        else:
            self.output_directory = temp_dir(sub_dir='work')
            self.output_filename = (
                unique_filename(
                    prefix='hazard_layer',
                    suffix='.geojson',
                    dir=self.output_directory))
            self.output_filename = os.path.split(self.output_filename)[1]
            self.output_filename, self.output_extension = (
                os.path.splitext(self.output_filename))

        self.data_store = Folder(self.output_directory)
        if self.output_extension == '.shp':
            self.data_store.default_vector_format = 'shp'
        elif self.output_extension == '.geojson':
            self.data_store.default_vector_format = 'geojson'
        self.data_store.add_layer(self.output_layer, self.output_filename)

        # add output layer to map canvas
        self.output_layer = self.data_store.layer(self.output_filename)

        QgsMapLayerRegistry.instance().addMapLayers(
            [self.output_layer])
        self.iface.setActiveLayer(self.output_layer)
        self.iface.zoomToActiveLayer()
        self.done(QtGui.QDialog.Accepted)

        if self.keyword_wizard_checkbox.isChecked():
            self.launch_keyword_wizard()
Пример #2
0
    def accept(self):
        """Process the layer for multi buffering and generate a new layer.

        .. note:: This is called on OK click.
        """
        # set parameter from dialog
        input_layer = self.layer.currentLayer()
        output_path = self.output_form.text()
        radius = self.get_classification()
        # monkey patch keywords so layer works on multi buffering function
        input_layer.keywords = {'inasafe_fields': {}}

        # run multi buffering
        self.output_layer = multi_buffering(input_layer, radius)

        # save output layer to data store and check whether user
        # provide the output path.
        if output_path:
            self.output_directory, self.output_filename = (
                os.path.split(output_path))
            self.output_filename, self.output_extension = (
                os.path.splitext(self.output_filename))

        # if user do not provide the output path, create a temporary file.
        else:
            self.output_directory = temp_dir(sub_dir='work')
            self.output_filename = (
                unique_filename(
                    prefix='hazard_layer',
                    suffix='.geojson',
                    dir=self.output_directory))
            self.output_filename = os.path.split(self.output_filename)[1]
            self.output_filename, self.output_extension = (
                os.path.splitext(self.output_filename))

        self.data_store = Folder(self.output_directory)
        if self.output_extension == '.shp':
            self.data_store.default_vector_format = 'shp'
        elif self.output_extension == '.geojson':
            self.data_store.default_vector_format = 'geojson'
        self.data_store.add_layer(self.output_layer, self.output_filename)

        # add output layer to map canvas
        self.output_layer = self.data_store.layer(self.output_filename)

        QgsMapLayerRegistry.instance().addMapLayers(
            [self.output_layer])
        self.iface.setActiveLayer(self.output_layer)
        self.iface.zoomToActiveLayer()
        self.done(QtGui.QDialog.Accepted)

        if self.keyword_wizard_checkbox.isChecked():
            self.launch_keyword_wizard()
Пример #3
0
    def test_multi_buffer_points(self):
        """Test we can multi buffer points such as volcano points."""
        radii = OrderedDict()
        radii[500] = 'high'
        radii[1000] = 'medium'
        radii[2000] = 'low'

        # Let's add a vector layer.
        layer = load_test_vector_layer('hazard', 'volcano_point.geojson')
        keywords = layer.keywords
        self.assertEqual(layer.keywords['layer_geometry'], 'point')

        expected_keywords = keywords.copy()
        expected_keywords['layer_geometry'] = 'polygon'
        expected_name_field = hazard_class_field['field_name']
        expected_keywords['inasafe_fields'][hazard_class_field['key']] = (
            expected_name_field)
        result = multi_buffering(
            layer=layer,
            radii=radii)

        self.assertDictEqual(result.keywords, expected_keywords)
        self.assertEqual(result.geometryType(), QGis.Polygon)
        expected_feature_count = layer.featureCount() * len(radii)
        self.assertEqual(result.featureCount(), expected_feature_count)

        # We can check the new fields added about the hazard class name
        # and the buffer distances.
        expected_fields_count = layer.fields().count() + 2

        self.assertEqual(result.fields().count(), expected_fields_count)

        # Check if the field name is correct.
        expected_fields_name = [
            hazard_class_field['field_name'],
            buffer_distance_field['field_name']]

        actual_field_names = [field.name() for field in result.pendingFields()]
        new_field_names = actual_field_names[-2:]

        self.assertEqual(expected_fields_name, new_field_names)
Пример #4
0
    def test_multi_buffer_points(self):
        """Test we can multi buffer points such as volcano points."""
        radii = OrderedDict()
        radii[500] = 'high'
        radii[1000] = 'medium'
        radii[2000] = 'low'

        # Let's add a vector layer.
        layer = load_test_vector_layer('hazard', 'volcano_point.geojson')
        keywords = layer.keywords
        self.assertEqual(layer.keywords['layer_geometry'], 'point')

        expected_keywords = keywords.copy()
        expected_keywords['layer_geometry'] = 'polygon'
        expected_name_field = hazard_class_field['field_name']
        expected_keywords['inasafe_fields'][hazard_class_field['key']] = (
            expected_name_field)
        result = multi_buffering(
            layer=layer,
            radii=radii)

        self.assertDictEqual(result.keywords, expected_keywords)
        self.assertEqual(result.geometryType(), QGis.Polygon)
        expected_feature_count = layer.featureCount() * len(radii)
        self.assertEqual(result.featureCount(), expected_feature_count)

        # We can check the new fields added about the hazard class name
        # and the buffer distances.
        expected_fields_count = layer.fields().count() + 2

        self.assertEqual(result.fields().count(), expected_fields_count)

        # Check if the field name is correct.
        expected_fields_name = [
            hazard_class_field['field_name'],
            buffer_distance_field['field_name']]

        actual_field_names = [field.name() for field in result.pendingFields()]
        new_field_names = actual_field_names[-2:]

        self.assertEqual(expected_fields_name, new_field_names)