def test_polygonize(self): """Test if we can polygonize a raster using GDAL.""" raster_path = standard_data_path( 'hazard', 'classified_flood_20_20.asc') driver = ogr.GetDriverByName('ESRI Shapefile') expected_field_name = 'my_field' shapefile = polygonize(raster_path, 1, expected_field_name) data_source = driver.Open(shapefile, 0) layer = data_source.GetLayer() layer_definition = layer.GetLayerDefn() field_name = layer_definition.GetFieldDefn(0).GetName() self.assertEqual(field_name, expected_field_name) self.assertEquals(layer.GetFeatureCount(), 400) layer.SetAttributeFilter('%s = 1' % expected_field_name) self.assertEquals(layer.GetFeatureCount(), 133) layer.SetAttributeFilter('%s = 2' % expected_field_name) self.assertEquals(layer.GetFeatureCount(), 134) layer.SetAttributeFilter('%s = 3' % expected_field_name) self.assertEquals(layer.GetFeatureCount(), 133)
def test_polygonize(self): """Test if we can polygonize a raster using GDAL.""" raster_path = test_data_path('hazard', 'classified_flood_20_20.asc') driver = ogr.GetDriverByName('ESRI Shapefile') expected_field_name = 'my_field' shapefile = polygonize(raster_path, 1, expected_field_name) data_source = driver.Open(shapefile, 0) layer = data_source.GetLayer() layer_definition = layer.GetLayerDefn() field_name = layer_definition.GetFieldDefn(0).GetName() self.assertEqual(field_name, expected_field_name) self.assertEquals(layer.GetFeatureCount(), 400) layer.SetAttributeFilter('%s = 1' % expected_field_name) self.assertEquals(layer.GetFeatureCount(), 133) layer.SetAttributeFilter('%s = 2' % expected_field_name) self.assertEquals(layer.GetFeatureCount(), 134) layer.SetAttributeFilter('%s = 3' % expected_field_name) self.assertEquals(layer.GetFeatureCount(), 133)
def reclassify_polygonize(input_raster, ranges, name_field='DN'): """Reclassify and polygonize a raster according to some ranges. .. note:: Delegates to reclassify() and safe.gis.gdal_ogr_tools.polygonize() .. versionadded:: 3.4 :param input_raster: The file path to the raster to reclassify. :type input_raster: str :param ranges: The ranges as a OrderedDict. :type ranges: OrderedDict :param name_field: The name of the field with the cell value. :type name_field: str :return: The file path to shapefile. :rtype: str """ return polygonize(reclassify(input_raster, ranges), name_field=name_field)