24 static std::expected<Shapefile,std::string>
convert(
const GeoTiff & geoTiff,
bool maskZero =
true,
bool showProgress=
false) noexcept{
26 GDALDataset *src = (GDALDataset *) GDALOpen(geoTiff.
getPath().c_str(), GA_ReadOnly);
28 return std::unexpected(
"Could not open Geotiff-Dataset: "+geoTiff.
getPath().string());
29 auto driver = GetGDALDriverManager()->GetDriverByName(
"ESRI Shapefile");
31 return std::unexpected(
"No suitable ESRI Shapefile driver detected");
32 std::filesystem::path conversionOutputDir = std::filesystem::current_path() /
"conversion_output";
33 std::filesystem::create_directories(conversionOutputDir);
34 std::filesystem::path destPath = conversionOutputDir / geoTiff.
getPath().stem().replace_extension(
".shp");
35 GDALDataset *dest = driver->Create(destPath.c_str(), 0,0, 0, GDT_Unknown,
nullptr);
36 auto spatialReference = src->GetSpatialRef();
37 OGRLayer * layer = dest->CreateLayer(destPath.stem().c_str(),spatialReference->Clone(),wkbPolygon,
nullptr);
38 const char *fieldName =
"pixel_val";
39 OGRFieldDefn fieldDefn = OGRFieldDefn(fieldName, OFTInteger);
40 if(layer->CreateField(&fieldDefn)!=OGRERR_NONE)
41 return std::unexpected(
"Could not create field for pixel values");
42 int fieldID = layer->GetLayerDefn()->GetFieldIndex(fieldName);
43 char ** papszOptions =
nullptr;
44 papszOptions = CSLSetNameValue(papszOptions,
"8CONNECTED",
"8");
46 src->GetRasterBand(1),
47 maskZero?src->GetRasterBand(1):
nullptr,
48 OGRLayer::ToHandle(layer),
51 showProgress?GDALTermProgress:
nullptr,