Exemplo n.º 1
0
    def test_union_error(self):
        """Test we can union two layers like hazard and aggregation (2)."""

        union_a = clean_layer(
            load_test_vector_layer('gisv4', 'hazard',
                                   'union_check_hazard.geojson'))
        union_a.keywords['inasafe_fields'][hazard_class_field['key']] = (
            union_a.keywords['inasafe_fields'][hazard_value_field['key']])

        union_b = clean_layer(
            load_test_vector_layer('gisv4', 'aggregation',
                                   'union_check_aggregation.geojson'))

        layer = union(union_a, union_b)

        self.assertEqual(layer.featureCount(), 11)
        self.assertEqual(union_a.fields().count() + union_b.fields().count(),
                         layer.fields().count())
Exemplo n.º 2
0
    def test_union_error(self):
        """Test we can union two layers like hazard and aggregation (2)."""

        union_a = clean_layer(load_test_vector_layer(
            'gisv4', 'hazard', 'union_check_hazard.geojson'))
        union_a.keywords['inasafe_fields'][hazard_class_field['key']] = (
            union_a.keywords['inasafe_fields'][hazard_value_field['key']])

        union_b = clean_layer(load_test_vector_layer(
            'gisv4', 'aggregation', 'union_check_aggregation.geojson'))

        layer = union(union_a, union_b)

        self.assertEqual(layer.featureCount(), 11)
        self.assertEqual(
            union_a.fields().count() + union_b.fields().count(),
            layer.fields().count()
        )
Exemplo n.º 3
0
def create_spatial_index(layer):
    """Helper function to create the spatial index on a vector layer.

    This function is mainly used to see the processing time with the decorator.

    :param layer: The vector layer.
    :type layer: QgsVectorLayer

    :return: The index.
    :rtype: QgsSpatialIndex
    """
    request = QgsFeatureRequest().setSubsetOfAttributes([])
    try:
        spatial_index = QgsSpatialIndex(layer.getFeatures(request))
    except BaseException:
        # Spatial index is creating an unknown exception.
        # https://github.com/inasafe/inasafe/issues/4304
        # or https://gitter.im/inasafe/inasafe?at=5a2903d487680e6230e0359a
        LOGGER.warning(
            'An Exception has been raised from the spatial index creation. '
            'We will clean your layer and try again.')
        new_layer = clean_layer(layer)
        try:
            spatial_index = QgsSpatialIndex(new_layer.getFeatures())
        except BaseException:
            # We got another exception.
            # We try now to insert feature by feature.
            # It's slower than the using the feature iterator.
            spatial_index = QgsSpatialIndex()
            for feature in new_layer.getFeatures(request):
                try:
                    spatial_index.insertFeature(feature)
                except BaseException:
                    LOGGER.critical(
                        'A feature has been removed from the spatial index.')

            # # We tried one time to clean the layer, we can't do more.
            # LOGGER.critical(
            #     'An Exception has been raised from the spatial index '
            #     'creation. Unfortunately, we already try to clean your '
            #     'layer. We will stop here the process.')
            # raise SpatialIndexCreationError
    return spatial_index
Exemplo n.º 4
0
def create_spatial_index(layer):
    """Helper function to create the spatial index on a vector layer.

    This function is mainly used to see the processing time with the decorator.

    :param layer: The vector layer.
    :type layer: QgsVectorLayer

    :return: The index.
    :rtype: QgsSpatialIndex
    """
    request = QgsFeatureRequest().setSubsetOfAttributes([])
    try:
        spatial_index = QgsSpatialIndex(layer.getFeatures(request))
    except BaseException:
        # Spatial index is creating an unknown exception.
        # https://github.com/inasafe/inasafe/issues/4304
        # or https://gitter.im/inasafe/inasafe?at=5a2903d487680e6230e0359a
        LOGGER.warning(
            'An Exception has been raised from the spatial index creation. '
            'We will clean your layer and try again.')
        new_layer = clean_layer(layer)
        try:
            spatial_index = QgsSpatialIndex(new_layer.getFeatures())
        except BaseException:
            # We got another exception.
            # We try now to insert feature by feature.
            # It's slower than the using the feature iterator.
            spatial_index = QgsSpatialIndex()
            for feature in new_layer.getFeatures(request):
                try:
                    spatial_index.insertFeature(feature)
                except BaseException:
                    LOGGER.critical(
                        'A feature has been removed from the spatial index.')

            # # We tried one time to clean the layer, we can't do more.
            # LOGGER.critical(
            #     'An Exception has been raised from the spatial index '
            #     'creation. Unfortunately, we already try to clean your '
            #     'layer. We will stop here the process.')
            # raise SpatialIndexCreationError
    return spatial_index