def test_run(self): function = FloodVectorRoadsExperimentalFunction.instance() hazard_path = test_data_path('hazard', 'flood_multipart_polygons.shp') exposure_path = test_data_path('exposure', 'roads.shp') # noinspection PyCallingNonCallable hazard_layer = QgsVectorLayer(hazard_path, 'Flood', 'ogr') # noinspection PyCallingNonCallable exposure_layer = QgsVectorLayer(exposure_path, 'Roads', 'ogr') # Let's set the extent to the hazard extent extent = hazard_layer.extent() rect_extent = [ extent.xMinimum(), extent.yMaximum(), extent.xMaximum(), extent.yMinimum() ] function.hazard = QgisWrapper(hazard_layer) function.exposure = QgisWrapper(exposure_layer) function.requested_extent = rect_extent function.parameters['affected_field'] = 'FLOODPRONE' function.parameters['affected_value'] = 'YES' function.run() impact = function.impact # Count of flooded objects is calculated "by the hands" # the count = 69 expected_feature_total = 69 count = sum(impact.get_data(attribute='FLOODED')) message = 'Expecting %s, but it returns %s' % (expected_feature_total, count) self.assertEquals(count, expected_feature_total, message)
def test_run(self): function = FloodRasterRoadsQGISFunction.instance() hazard_path = test_data_path('hazard', 'continuous_flood_20_20.asc') exposure_path = test_data_path('exposure', 'roads.shp') # noinspection PyCallingNonCallable hazard_layer = QgsRasterLayer(hazard_path, 'Flood') # noinspection PyCallingNonCallable exposure_layer = QgsVectorLayer(exposure_path, 'Roads', 'ogr') # Let's set the extent to the hazard extent extent = hazard_layer.extent() rect_extent = [ extent.xMinimum(), extent.yMaximum(), extent.xMaximum(), extent.yMinimum() ] function.hazard = QgisWrapper(hazard_layer) function.exposure = QgisWrapper(exposure_layer) function.requested_extent = rect_extent function.run() impact = function.impact keywords = impact.get_keywords() self.assertEquals('flooded', keywords['target_field']) expected_inundated_feature = 193 count = sum(impact.get_data(attribute=keywords['target_field'])) self.assertEquals(count, expected_inundated_feature)
def _get_layer(self, layer): """Analyze style of self._function and return appropriate class of the layer. :param layer: A layer. :type layer: QgsMapLayer or SAFE layer. :returns: The layer of appropriate type :rtype: SAFE or QgisWrapper :raises: InsufficientParametersError if self._function is not set, InvalidParameterError if style of self._function is not in ('old-style', 'qgis2.0') Any exceptions raised by other libraries will be propogated. """ if self._function is None or self._function == '': message = self.tr('Error: Function not set.') raise InsufficientParametersError(message) # Get type of the impact function (old-style or new-style) try: func_type = get_safe_impact_function_type(self._function) if func_type == 'old-style': return convert_to_safe_layer(layer) elif func_type == 'qgis2.0': # convert for new style impact function return QgisWrapper(layer) else: message = self.tr('Error: Function has unknown style.') raise InvalidParameterError(message) except: raise
def setUp(self): data_path = test_data_path('impact', 'aggregation_test_roads.shp') self.layer = QgsVectorLayer(data_path, 'test vector', 'ogr') self.wrapper = QgisWrapper(self.layer)