def _create_aggregator(self, use_aoi_mode, use_native_zonal_stats): """Helper to create aggregator""" 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 return aggregator
def _create_aggregator(self, use_aoi_mode, use_native_zonal_stats): """Helper to create aggregator""" 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 return aggregator
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)
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)
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)