<div dir="ltr"><div class="gmail_default" style="font-size:small">Thank you ,</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">I already worked on that code. But clipping was not succesful. output.tif is blank on qgis.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, May 30, 2023 at 3:24 PM Norman Vine <<a href="mailto:nhv@meganet.net">nhv@meganet.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">perhaps this will help<br>
<a href="https://stackoverflow.com/questions/70664043/clip-raster-with-polygon-with-gdal-c" rel="noreferrer" target="_blank">https://stackoverflow.com/questions/70664043/clip-raster-with-polygon-with-gdal-c</a><br>
<br>
> On May 30, 2023, at 8:12 AM, Ahmet Temiz <<a href="mailto:ahmettemiz88@gmail.com" target="_blank">ahmettemiz88@gmail.com</a>> wrote:<br>
> <br>
> Hi,<br>
> I am trying to clip a tif file with a polygon. But resulting output.tif file shows nothing.<br>
> <br>
> Where am I doing wrong ?<br>
> <br>
> " #include <gdal/gdal.h><br>
> #include <gdal/gdal_priv.h><br>
> #include <gdal/gdalwarper.h><br>
> #include <gdal/ogrsf_frmts.h><br>
> #include <gdal/gdalwarper.h><br>
> #include <string> // for string class<br>
> using namespace std;<br>
> <br>
> int main() {<br>
> const char *inputPath = "input.tif";<br>
> const char *outputPath = "output.tif";<br>
> <br>
> // clipper Polygon<br>
> // THIS FILE MUST BE IN PIXEL/LINE COORDINATES or otherwise one should<br>
> // copy the function gdalwarp_lib.cpp:TransformCutlineToSource()<br>
> // from GDAL's sources<br>
> // It is expected that it contains a single polygon feature<br>
> const char *read_filenamePoly = "input.geojson";<br>
> <br>
> GDALDataset *hSrcDS;<br>
> GDALDataset *hDstDS;<br>
> <br>
> GDALAllRegister();<br>
> auto poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");<br>
> hSrcDS = (GDALDataset *)GDALOpen(inputPath, GA_ReadOnly);<br>
> <br>
> hDstDS = (GDALDataset *)poDriver->CreateCopy(<br>
> outputPath, hSrcDS, 0, nullptr, nullptr, nullptr);<br>
> // Without this step the cutline is useless - because the background<br>
> // will be carried over from the original image<br>
> hDstDS->GetRasterBand(1)->Fill(0);<br>
> <br>
> hDstDS->SetProjection(hSrcDS->GetProjectionRef());<br>
> <br>
> const char *src_srs = hSrcDS->GetProjectionRef();<br>
> const char *dst_srs = hDstDS->GetProjectionRef();<br>
> printf("%s ",src_srs);<br>
> // clipper Layer<br>
> GDALDataset *poDSClipper;<br>
> poDSClipper = (GDALDataset *)GDALOpenEx(<br>
> read_filenamePoly, GDAL_OF_UPDATE, NULL, NULL, NULL);<br>
> <br>
> auto poLayerClipper = poDSClipper->GetLayer(0);<br>
> auto geom = poLayerClipper->GetNextFeature()->GetGeometryRef();<br>
> //char* wkt = nullptr;<br>
> if (geom != nullptr && geom->IsValid())<br>
> { <br>
> printf("\n geom is valid \n");<br>
> }<br>
> // setup warp options<br>
> GDALWarpOptions *psWarpOptions = GDALCreateWarpOptions();<br>
> psWarpOptions->hSrcDS = hSrcDS;<br>
> psWarpOptions->hDstDS = hDstDS;<br>
> psWarpOptions->nBandCount = 1;<br>
> psWarpOptions->panSrcBands =<br>
> (int *)CPLMalloc(sizeof(int) * psWarpOptions->nBandCount);<br>
> psWarpOptions->panSrcBands[0] = 1;<br>
> psWarpOptions->panDstBands =<br>
> (int *)CPLMalloc(sizeof(int) * psWarpOptions->nBandCount);<br>
> psWarpOptions->panDstBands[0] = 1;<br>
> psWarpOptions->pfnProgress = GDALTermProgress;<br>
> psWarpOptions->hCutline = geom;<br>
> <br>
> // Establish reprojection transformer.<br>
> psWarpOptions->pTransformerArg = GDALCreateGenImgProjTransformer(<br>
> hSrcDS, src_srs, hDstDS, dst_srs, TRUE, 1000, 1);<br>
> psWarpOptions->pfnTransformer = GDALGenImgProjTransform;<br>
> GDALWarpOperation oOperation;<br>
> oOperation.Initialize(psWarpOptions);<br>
> oOperation.ChunkAndWarpImage(<br>
> 0, 0, GDALGetRasterXSize(hDstDS), GDALGetRasterYSize(hDstDS));<br>
> GDALDestroyGenImgProjTransformer(psWarpOptions->pTransformerArg);<br>
> GDALDestroyWarpOptions(psWarpOptions);<br>
> GDALClose(hDstDS);<br>
> GDALClose(hSrcDS);<br>
> }<br>
> "<br>
> <br>
> -- <br>
> Ahmet Temiz<br>
> Jeoloji Müh.<br>
> Afet ve Acil Durum Yönetimi Başkanlığı<br>
> Deprem Dairesi Başkanlığı<br>
> <br>
> <br>
> ________________________<br>
> <br>
> Ahmet Temiz<br>
> Geological Eng.<br>
> <br>
> Disaster and Emergency Management<br>
> of Presidency<br>
> _______________________________________________<br>
> gdal-dev mailing list<br>
> <a href="mailto:gdal-dev@lists.osgeo.org" target="_blank">gdal-dev@lists.osgeo.org</a><br>
> <a href="https://lists.osgeo.org/mailman/listinfo/gdal-dev" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/gdal-dev</a><br>
<br>
<br>
</blockquote></div><br clear="all"><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div>Ahmet Temiz<br>Jeoloji Müh.<br>Afet ve Acil Durum Yönetimi Başkanlığı<br>Deprem Dairesi Başkanlığı<br><br><br>________________________<br><br>Ahmet Temiz<br>Geological Eng.<br><br>Disaster and Emergency Management<br>of Presidency</div></div></div></div></div></div></div>