Ejemplo n.º 1
0
    def _aggregate(self, myImpactLayer, myExpectedResults):
        myAggregationLayer = QgsVectorLayer(
            os.path.join(BOUNDDATA, 'kabupaten_jakarta.shp'),
            'test aggregation',
            'ogr')
        # create a copy of aggregation layer
        myGeoExtent = extent_to_geo_array(
            myAggregationLayer.extent(),
            myAggregationLayer.crs())

        myAggrAttribute = self.keywordIO.read_keywords(
            myAggregationLayer, self.defaults['AGGR_ATTR_KEY'])
        # noinspection PyArgumentEqualDefault
        myAggregationLayer = clip_layer(
            layer=myAggregationLayer,
            extent=myGeoExtent,
            explode_flag=True,
            explode_attribute=myAggrAttribute)

        myAggregator = Aggregator(None, myAggregationLayer)
        # setting up
        myAggregator.isValid = True
        myAggregator.layer = myAggregationLayer
        myAggregator.safeLayer = safe_read_layer(
            str(myAggregator.layer.source()))
        myAggregator.aoiMode = False
        myAggregator.aggregate(myImpactLayer)

        myProvider = myAggregator.layer.dataProvider()
        myProvider.select(myProvider.attributeIndexes())
        myFeature = QgsFeature()
        myResults = []

        while myProvider.nextFeature(myFeature):
            myFeatureResults = {}
            myAtMap = myFeature.attributeMap()
            for (k, attr) in myAtMap.iteritems():
                myFeatureResults[k] = str(attr.toString())
            myResults.append(myFeatureResults)

        self.assertEqual(myExpectedResults, myResults)
Ejemplo n.º 2
0
    def _aggregate(self,
                   myImpactLayer,
                   myExpectedResults,
                   useNativeZonalStats=False):
        """Helper to calculate aggregation.

        Expected results is split into two lists - one list contains numeric
        attributes, the other strings. This is done so that we can use numpy
        .testing.assert_allclose which doesn't work on strings
        """

        myExpectedStringResults = []
        myExpectedNumericResults = []

        for item in myExpectedResults:
            myItemNumResults = []
            myItemStrResults = []
            for field in item:
                try:
                    value = float(field)
                    myItemNumResults.append(value)
                except ValueError:
                    myItemStrResults.append(str(field))

            myExpectedNumericResults.append(myItemNumResults)
            myExpectedStringResults.append(myItemStrResults)

        myAggregationLayer = QgsVectorLayer(
            os.path.join(BOUNDDATA, 'kabupaten_jakarta.shp'),
            'test aggregation',
            'ogr')
        # create a copy of aggregation layer
        myGeoExtent = extent_to_geo_array(
            myAggregationLayer.extent(),
            myAggregationLayer.crs())

        myAggrAttribute = self.keywordIO.read_keywords(
            myAggregationLayer, self.defaults['AGGR_ATTR_KEY'])
        # noinspection PyArgumentEqualDefault
        myAggregationLayer = clip_layer(
            layer=myAggregationLayer,
            extent=myGeoExtent,
            explode_flag=True,
            explode_attribute=myAggrAttribute)

        myAggregator = Aggregator(None, myAggregationLayer)
        # setting up
        myAggregator.isValid = True
        myAggregator.layer = myAggregationLayer
        myAggregator.safeLayer = safe_read_layer(
            str(myAggregator.layer.source()))
        myAggregator.aoiMode = False
        myAggregator.useNativeZonalStats = useNativeZonalStats
        myAggregator.aggregate(myImpactLayer)

        myProvider = myAggregator.layer.dataProvider()
        myNumericResults = []
        myStringResults = []

        for myFeature in myProvider.getFeatures():
            myFeatureNumResults = []
            myFeatureStrResults = []
            myAttrs = myFeature.attributes()
            for attr in myAttrs:
                if isinstance(attr, (int, float)):
                    myFeatureNumResults.append(attr)
                else:
                    myFeatureStrResults.append(attr)

            myNumericResults.append(myFeatureNumResults)
            myStringResults.append(myFeatureStrResults)

        # check string attributes
        self.assertEqual(myExpectedStringResults, myStringResults)
        # check numeric attributes with a 0.01% tolerance compared to the
        # native QGIS stats
        numpy.testing.assert_allclose(myExpectedNumericResults,
                                      myNumericResults,
                                      rtol=0.01)
Ejemplo n.º 3
0
    def _aggregate(self,
                   impact_layer,
                   expected_results,
                   use_native_zonal_stats=False):
        """Helper to calculate aggregation.

        Expected results is split into two lists - one list contains numeric
        attributes, the other strings. This is done so that we can use numpy
        .testing.assert_allclose which doesn't work on strings
        """

        expected_string_results = []
        expected_numeric_results = []

        for item in expected_results:
            string_results = []
            numeric_results = []
            for field in item:
                try:
                    value = float(field)
                    numeric_results.append(value)
                except ValueError:
                    string_results.append(str(field))

            expected_numeric_results.append(numeric_results)
            expected_string_results.append(string_results)

        aggregation_layer = QgsVectorLayer(
            os.path.join(BOUNDDATA, 'kabupaten_jakarta.shp'),
            'test aggregation', 'ogr')
        # create a copy of aggregation layer
        geo_extent = extent_to_geo_array(aggregation_layer.extent(),
                                         aggregation_layer.crs())

        aggregation_attribute = self.keywordIO.read_keywords(
            aggregation_layer, self.defaults['AGGR_ATTR_KEY'])
        # noinspection PyArgumentEqualDefault
        aggregation_layer = clip_layer(layer=aggregation_layer,
                                       extent=geo_extent,
                                       explode_flag=True,
                                       explode_attribute=aggregation_attribute)

        aggregator = Aggregator(None, aggregation_layer)
        # setting up
        aggregator.is_valid = True
        aggregator.layer = aggregation_layer
        aggregator.safe_layer = safe_read_layer(str(aggregator.layer.source()))
        aggregator.aoi_mode = False
        aggregator.use_native_zonal_stats = use_native_zonal_stats
        aggregator.aggregate(impact_layer)

        provider = aggregator.layer.dataProvider()
        string_results = []
        numeric_results = []

        for feature in provider.getFeatures():
            feature_string_results = []
            feature_numeric_results = []
            attributes = feature.attributes()
            for attr in attributes:
                if isinstance(attr, (int, float)):
                    feature_numeric_results.append(attr)
                else:
                    feature_string_results.append(attr)

            numeric_results.append(feature_numeric_results)
            string_results.append(feature_string_results)

        # check string attributes
        self.assertEqual(expected_string_results, string_results)
        # check numeric attributes with a 0.01% tolerance compared to the
        # native QGIS stats
        numpy.testing.assert_allclose(expected_numeric_results,
                                      numeric_results,
                                      rtol=0.01)
Ejemplo n.º 4
0
    def _aggregate(
            self,
            impact_layer,
            expected_results,
            use_native_zonal_stats=False):
        """Helper to calculate aggregation.

        Expected results is split into two lists - one list contains numeric
        attributes, the other strings. This is done so that we can use numpy
        .testing.assert_allclose which doesn't work on strings
        """

        expected_string_results = []
        expected_numeric_results = []

        for item in expected_results:
            string_results = []
            numeric_results = []
            for field in item:
                try:
                    value = float(field)
                    numeric_results.append(value)
                except ValueError:
                    string_results.append(str(field))

            expected_numeric_results.append(numeric_results)
            expected_string_results.append(string_results)

        aggregation_layer = QgsVectorLayer(
            os.path.join(BOUNDDATA, 'kabupaten_jakarta.shp'),
            'test aggregation',
            'ogr')
        # create a copy of aggregation layer
        geo_extent = extent_to_geo_array(
            aggregation_layer.extent(),
            aggregation_layer.crs())

        aggregation_attribute = self._keywordIO.read_keywords(
            aggregation_layer, self._defaults['AGGR_ATTR_KEY'])
        # noinspection PyArgumentEqualDefault
        aggregation_layer = clip_layer(
            layer=aggregation_layer,
            extent=geo_extent,
            explode_flag=True,
            explode_attribute=aggregation_attribute)

        aggregator = Aggregator(None, aggregation_layer)
        # setting up
        aggregator.is_valid = True
        aggregator.layer = aggregation_layer
        aggregator.safe_layer = safe_read_layer(
            str(aggregator.layer.source()))
        aggregator.aoi_mode = False
        aggregator.use_native_zonal_stats = use_native_zonal_stats
        aggregator.aggregate(impact_layer)

        provider = aggregator.layer.dataProvider()
        string_results = []
        numeric_results = []

        for feature in provider.getFeatures():
            feature_string_results = []
            feature_numeric_results = []
            attributes = feature.attributes()
            for attr in attributes:
                if isinstance(attr, (int, float)):
                    feature_numeric_results.append(attr)
                else:
                    feature_string_results.append(attr)

            numeric_results.append(feature_numeric_results)
            string_results.append(feature_string_results)

        # check string attributes
        self.assertEqual(expected_string_results, string_results)
        # check numeric attributes with a 0.01% tolerance compared to the
        # native QGIS stats
        numpy.testing.assert_allclose(expected_numeric_results,
                                      numeric_results,
                                      rtol=0.01)
Ejemplo n.º 5
0
    def _aggregate(
            self,
            impact_layer,
            expected_results,
            use_native_zonal_stats=False,
            use_aoi_mode=False):
        """Helper to calculate aggregation.

        Expected results is split into two lists - one list contains numeric
        attributes, the other strings. This is done so that we can use numpy
        .testing.assert_allclose which doesn't work on strings
        """

        expected_string_results = []
        expected_numeric_results = []

        for item in expected_results:
            string_results = []
            numeric_results = []
            for field in item:
                try:
                    value = float(field)
                    numeric_results.append(value)
                except ValueError:
                    string_results.append(str(field))

            expected_numeric_results.append(numeric_results)
            expected_string_results.append(string_results)

        aggregation_layer = QgsVectorLayer(
            os.path.join(BOUNDDATA, 'kabupaten_jakarta.shp'),
            'test aggregation',
            'ogr')

        # Dummy layers. Them are used in aggregator._prepare_layer
        # The extent of the layers must be equal to aggregator.extent
        hazard_layer = exposure_layer = aggregation_layer

        # setting up
        if not use_aoi_mode:
            aggregator = Aggregator(self.extent, aggregation_layer)
        else:
            aggregator = Aggregator(self.extent, None)
        aggregator.set_layers(hazard_layer, exposure_layer)
        aggregator.validate_keywords()
        aggregator.use_native_zonal_stats = use_native_zonal_stats

        aggregator.aggregate(impact_layer)

        provider = aggregator.layer.dataProvider()
        string_results = []
        numeric_results = []

        for feature in provider.getFeatures():
            feature_string_results = []
            feature_numeric_results = []
            attributes = feature.attributes()
            for attr in attributes:
                try:
                    value = float(attr)
                    feature_numeric_results.append(value)
                except ValueError:
                    feature_string_results.append(str(attr))

            numeric_results.append(feature_numeric_results)
            string_results.append(feature_string_results)

        # check string attributes
        self.assertEqual(expected_string_results, string_results)
        # check numeric attributes with a 0.01% tolerance compared to the
        # native QGIS stats
        numpy.testing.assert_allclose(expected_numeric_results,
                                      numeric_results,
                                      rtol=0.01)