<div dir="auto">Thank you..<div dir="auto">I solved the problem. Cutting polygon has to be converted to pixel coordinates. I have missed that point. now it works well ..</div><div dir="auto"><br></div><div dir="auto">Regards </div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">30 May 2023 Sal 16:20 tarihinde Rahkonen Jukka <<a href="mailto:jukka.rahkonen@maanmittauslaitos.fi">jukka.rahkonen@maanmittauslaitos.fi</a>> şunu yazdı:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div lang="FI" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="m_-8159072151342521610WordSection1">
<p class="MsoNormal"><span>Hi,<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">I can’t really read the code, but based on what happens, maybe your cutline is outside the image. So you
<u></u><u></u></span></p>
<ul style="margin-top:0cm" type="disc">
<li class="m_-8159072151342521610MsoListParagraph" style="margin-left:0cm"><span lang="EN-US">create an output band<u></u><u></u></span></li><li class="m_-8159072151342521610MsoListParagraph" style="margin-left:0cm"><span lang="EN-US">fill it with zeroes<u></u><u></u></span></li><li class="m_-8159072151342521610MsoListParagraph" style="margin-left:0cm"><span lang="EN-US">and then you write pixels within the cutline into the output<u></u><u></u></span></li></ul>
<p class="MsoNormal"><span lang="EN-US">Because the output stays all zeroes, and there are no errors, then I guess that the pixels within your cutline are all nodata.<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">Maybe printing the WKT out from “geom” could prove or disprove my guess.<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">-Jukka Rahkonen-<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<div style="border:none;border-top:solid #e1e1e1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b>Lähettäjä:</b> gdal-dev <<a href="mailto:gdal-dev-bounces@lists.osgeo.org" target="_blank" rel="noreferrer">gdal-dev-bounces@lists.osgeo.org</a>>
<b>Puolesta </b>Ahmet Temiz<br>
<b>Lähetetty:</b> tiistai 30. toukokuuta 2023 15.57<br>
<b>Vastaanottaja:</b> Norman Vine <<a href="mailto:nhv@meganet.net" target="_blank" rel="noreferrer">nhv@meganet.net</a>><br>
<b>Kopio:</b> gdal-dev <<a href="mailto:gdal-dev@lists.osgeo.org" target="_blank" rel="noreferrer">gdal-dev@lists.osgeo.org</a>><br>
<b>Aihe:</b> Re: [gdal-dev] wrong with clipping a tif file with a polygon<u></u><u></u></p>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt">Thank you ,<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt">I already worked on that code. But clipping was not succesful. output.tif is blank on qgis.<u></u><u></u></span></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">On Tue, May 30, 2023 at 3:24 PM Norman Vine <<a href="mailto:nhv@meganet.net" target="_blank" rel="noreferrer">nhv@meganet.net</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<p class="MsoNormal" style="margin-bottom:12.0pt">perhaps this will help<br>
<a href="https://stackoverflow.com/questions/70664043/clip-raster-with-polygon-with-gdal-c" target="_blank" rel="noreferrer">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" rel="noreferrer">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" rel="noreferrer">gdal-dev@lists.osgeo.org</a><br>
> <a href="https://lists.osgeo.org/mailman/listinfo/gdal-dev" target="_blank" rel="noreferrer">https://lists.osgeo.org/mailman/listinfo/gdal-dev</a><br>
<br>
<u></u><u></u></p>
</blockquote>
</div>
<p class="MsoNormal"><br clear="all">
<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<p class="MsoNormal"><span class="m_-8159072151342521610gmailsignatureprefix">-- </span><u></u><u></u></p>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">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<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote></div>