<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Wingdings;
        panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0cm;
        margin-right:0cm;
        margin-bottom:0cm;
        margin-left:36.0pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
span.gmailsignatureprefix
        {mso-style-name:gmail_signature_prefix;}
span.Shkpostityyli19
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:70.85pt 2.0cm 70.85pt 2.0cm;}
div.WordSection1
        {page:WordSection1;}
/* List Definitions */
@list l0
        {mso-list-id:1633242266;
        mso-list-type:hybrid;
        mso-list-template-ids:-808007790 67829777 67829785 67829787 67829775 67829785 67829787 67829775 67829785 67829787;}
@list l0:level1
        {mso-level-text:"%1\)";
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level2
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level3
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level4
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level5
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level6
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level7
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level8
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level9
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l1
        {mso-list-id:2116514611;
        mso-list-type:hybrid;
        mso-list-template-ids:2101134956 940052182 67829763 67829765 67829761 67829763 67829765 67829761 67829763 67829765;}
@list l1:level1
        {mso-level-number-format:bullet;
        mso-level-text:-;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        font-family:"Calibri",sans-serif;
        mso-fareast-font-family:Calibri;}
@list l1:level2
        {mso-level-number-format:bullet;
        mso-level-text:o;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        font-family:"Courier New";}
@list l1:level3
        {mso-level-number-format:bullet;
        mso-level-text:;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        font-family:Wingdings;}
@list l1:level4
        {mso-level-number-format:bullet;
        mso-level-text:;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        font-family:Symbol;}
@list l1:level5
        {mso-level-number-format:bullet;
        mso-level-text:o;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        font-family:"Courier New";}
@list l1:level6
        {mso-level-number-format:bullet;
        mso-level-text:;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        font-family:Wingdings;}
@list l1:level7
        {mso-level-number-format:bullet;
        mso-level-text:;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        font-family:Symbol;}
@list l1:level8
        {mso-level-number-format:bullet;
        mso-level-text:o;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        font-family:"Courier New";}
@list l1:level9
        {mso-level-number-format:bullet;
        mso-level-text:;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        font-family:Wingdings;}
ol
        {margin-bottom:0cm;}
ul
        {margin-bottom:0cm;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="FI" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">Hi,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US">I can’t really read the code, but based on what happens, maybe your cutline is outside the image. So you
<o:p></o:p></span></p>
<ul style="margin-top:0cm" type="disc">
<li class="MsoListParagraph" style="margin-left:0cm;mso-list:l1 level1 lfo2"><span lang="EN-US" style="mso-fareast-language:EN-US">create an output band<o:p></o:p></span></li><li class="MsoListParagraph" style="margin-left:0cm;mso-list:l1 level1 lfo2"><span lang="EN-US" style="mso-fareast-language:EN-US">fill it with zeroes<o:p></o:p></span></li><li class="MsoListParagraph" style="margin-left:0cm;mso-list:l1 level1 lfo2"><span lang="EN-US" style="mso-fareast-language:EN-US">and then you write pixels within the cutline into the output<o:p></o:p></span></li></ul>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language: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.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US">Maybe printing the WKT out from “geom” could prove or disprove my guess.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US">-Jukka Rahkonen-<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="mso-fareast-language:EN-US"><o:p> </o:p></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 <gdal-dev-bounces@lists.osgeo.org>
<b>Puolesta </b>Ahmet Temiz<br>
<b>Lähetetty:</b> tiistai 30. toukokuuta 2023 15.57<br>
<b>Vastaanottaja:</b> Norman Vine <nhv@meganet.net><br>
<b>Kopio:</b> gdal-dev <gdal-dev@lists.osgeo.org><br>
<b>Aihe:</b> Re: [gdal-dev] wrong with clipping a tif file with a polygon<o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt">Thank you ,<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt"><o:p> </o:p></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.<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Tue, May 30, 2023 at 3:24 PM Norman Vine <<a href="mailto:nhv@meganet.net">nhv@meganet.net</a>> wrote:<o:p></o:p></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">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" target="_blank">https://lists.osgeo.org/mailman/listinfo/gdal-dev</a><br>
<br>
<o:p></o:p></p>
</blockquote>
</div>
<p class="MsoNormal"><br clear="all">
<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal"><span class="gmailsignatureprefix">-- </span><o:p></o:p></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<o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>