<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p>Casper,</p>
<p>The polygonizer can be used in 4 or 8 connected modes. I'm not
sure which one you used. My results are the following. (I used
GeoTransform (0,1,0,3,0,-1) for the rasters.<br>
</p>
<p>4-connected:</p>
<p>1:<br>
POLYGON ((0 3,0 0,2 0,2 1,1 1,1 2,2 2,2 1,3 1,3 3,0 3))<br>
POLYGON ((1 2,1 1,2 1,2 2,1 2))<br>
POLYGON ((2 1,2 0,3 0,3 1,2 1))<br>
2:<br>
POLYGON ((0 3,0 0,3 0,3 2,2 2,2 1,1 1,1 2,2 2,2 3,0 3))<br>
POLYGON ((2 3,2 2,3 2,3 3,2 3))<br>
POLYGON ((1 2,1 1,2 1,2 2,1 2))<br>
3:<br>
POLYGON ((0 3,0 2,1 2,1 3,0 3))<br>
POLYGON ((1 3,1 2,0 2,0 0,2 0,3 0,3 3,1 3),(1 2,1 1,2 1,2 2,1 2))<br>
POLYGON ((1 2,1 1,2 1,2 2,1 2))<br>
4:<br>
POLYGON ((1 2,1 1,2 1,2 2,1 2))<br>
POLYGON ((0 1,0 0,1 0,1 1,0 1))<br>
POLYGON ((0 3,0 1,1 1,1 0,3 0,3 3,0 3),(1 2,2 2,2 1,1 1,1 2))<br>
3+4:<br>
POLYGON ((0 4,0 3,1 3,1 4,0 4))<br>
POLYGON ((0 3,0 1,1 1,1 3,0 3))<br>
POLYGON ((1 3,1 1,2 1,2 3,1 3))<br>
POLYGON ((0 1,0 0,1 0,1 1,0 1))<br>
POLYGON ((1 4,1 3,2 3,2 1,1 1,1 0,2 0,3 0,3 4,1 4))<br>
</p>
<p>8-connected:</p>
<p>1:<br>
POLYGON ((0 3,0 0,2 0,2 1,1 1,1 2,2 2,2 1,3 1,3 3,0 3))<br>
POLYGON ((1 2,1 1,2 1,2 0,3 0,3 1,2 1,2 2,1 2))<br>
2:<br>
POLYGON ((0 3,0 0,3 0,3 2,2 2,2 1,1 1,1 2,2 2,2 3,0 3))<br>
POLYGON ((2 3,2 2,1 2,1 1,2 1,2 2,3 2,3 3,2 3))<br>
3:<br>
POLYGON ((0 3,0 2,1 2,1 1,2 1,2 2,1 2,1 3,0 3))<br>
POLYGON ((1 3,1 2,0 2,0 0,2 0,3 0,3 3,1 3),(1 2,1 1,2 1,2 2,1 2))<br>
4:<br>
POLYGON ((0 3,0 1,1 1,1 0,3 0,3 3,0 3),(1 2,2 2,2 1,1 1,1 2))<br>
POLYGON ((1 2,1 1,0 1,0 0,1 0,1 1,2 1,2 2,1 2))<br>
3+4:<br>
POLYGON ((0 4,0 3,1 3,1 1,0 1,0 0,1 0,1 1,2 1,2 3,1 3,1 4,0 4))<br>
POLYGON ((1 4,1 3,0 3,0 1,1 1,1 0,2 0,3 0,3 4,1 4),(1 3,1 1,2 1,2
3,1 3))<br>
</p>
<p>To me the results seem to be correct by manual checking with
QGIS.</p>
<p>Can you show the code that you used?</p>
<p>Best,<br>
</p>
<p>Ari</p>
<p>the code that I used to generate these results:</p>
<p>use Modern::Perl;<br>
use Geo::GDAL;<br>
<br>
sub polygonize {<br>
my ($b, $name) = @_;<br>
my $l2 = Geo::OGR::Driver('ESRI
Shapefile')->Create('.')->CreateLayer($name);<br>
my $options = {'8CONNECTED' => 1};<br>
my $l = $b->Polygonize(Options => $options);<br>
$l->ResetReading();<br>
while (my $f = $l->GetNextFeature()) {<br>
$l2->CreateFeature($f);<br>
my $g = $f->GetGeometry();<br>
say $g->As(Format => 'WKT');<br>
}<br>
}<br>
<br>
system "rm test*";<br>
<br>
my $b = Geo::GDAL::Driver('MEM')->Create(Width => 3, Height
=> 3)->Band;<br>
$b->Dataset->GeoTransform(0,1,0,3,0,-1);<br>
$b->Dataset->SpatialReference(Geo::OSR::SpatialReference->new(EPSG=>3067));<br>
my $d = $b->ReadTile;<br>
<br>
# 1:<br>
<br>
$d->[0] = [1,1,1];<br>
$d->[1] = [1,0,1];<br>
$d->[2] = [1,1,0];<br>
$b->WriteTile($d);<br>
$b->Dataset->Translate('test1.tiff');<br>
say "1:";<br>
polygonize($b, 'test1');<br>
<br>
$d->[0] = [1,1,0];<br>
$d->[1] = [1,0,1];<br>
$d->[2] = [1,1,1];<br>
$b->WriteTile($d);<br>
$b->Dataset->Translate('test2.tiff');<br>
say "2:";<br>
polygonize($b, 'test2');<br>
<br>
$d->[0] = [0,1,1];<br>
$d->[1] = [1,0,1];<br>
$d->[2] = [1,1,1];<br>
$b->WriteTile($d);<br>
$b->Dataset->Translate('test3.tiff');<br>
say "3:";<br>
polygonize($b, 'test3');<br>
<br>
$d->[0] = [1,1,1];<br>
$d->[1] = [1,0,1];<br>
$d->[2] = [0,1,1];<br>
$b->WriteTile($d);<br>
$b->Dataset->Translate('test4.tiff');<br>
say "4:";<br>
polygonize($b, 'test4');<br>
<br>
$b = Geo::GDAL::Driver('MEM')->Create(Width => 3, Height
=> 4)->Band;<br>
$b->Dataset->GeoTransform(0,1,0,4,0,-1);<br>
$b->Dataset->SpatialReference(Geo::OSR::SpatialReference->new(EPSG=>3067));<br>
$d = $b->ReadTile;<br>
<br>
$d->[0] = [0,1,1];<br>
$d->[1] = [1,0,1];<br>
$d->[2] = [1,0,1];<br>
$d->[3] = [0,1,1];<br>
$b->WriteTile($d);<br>
$b->Dataset->Translate('test3+4.tiff');<br>
say "3+4:";<br>
polygonize($b, 'test3+4');<br>
<br>
</p>
<br>
<div class="moz-cite-prefix">20.01.2017, 10:31, Casper Børgesen
(CABO) kirjoitti:<br>
</div>
<blockquote
cite="mid:BCF6B63AE0B25941A7F56567B58A2805C581C6FF@allkexa1.NIRAS.INT"
type="cite">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 14 (filtered
medium)">
<style><!--
/* Font Definitions */
@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;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal;
font-family:"Calibri","sans-serif";
color:windowtext;}
span.EmailStyle18
{mso-style-type:personal-reply;
font-family:"Calibri","sans-serif";
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:3.0cm 2.0cm 3.0cm 2.0cm;}
div.WordSection1
{page:WordSection1;}
--></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]-->
<div class="WordSection1">
<p class="MsoNormal"><span style="color:#1F497D" lang="EN-US">I
have looked at the special case of a raster combining
situation 3 and 4 below:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="EN-US">3
+ 4:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="EN-US">
# #<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="EN-US">#
#<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="EN-US">#
#<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="EN-US">
# #<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="EN-US">The
polygoniser handles this fine by return a result with two
separate polygons.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="EN-US">For
easier reference, I have included the WKT of the 4 + 1
situations and their results:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV">1:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV">Input:
POLYGON ((1 1,3 1,3 2,2 2,2 3,3 3,3 2,4 2,4 4,1 4,1 1))<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV">Result:
POLYGON ((1 4,1 1,3 1,3 2,2 2,2 3,3 3,3 2,4 2,4 4,1 4))<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV">2:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV">Input:
POLYGON ((1 1,4 1,4 3,3 3,3 2,2 2,2 3,3 3,3 4,1 4,1 1))<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV">Result:
POLYGON ((1 4,1 1,4 1,4 3,3 3,3 2,2 2,2 3,3 3,3 4,1 4))<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV">3:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV">Input:
POLYGON ((1 1,4 1,4 4,2 4,2 3,3 3,3 2,2 2,2 3,1 3,1 1))<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV">Result:
POLYGON ((2 4,2 3,1 3,1 1,3 1,4 1,4 4,2 4),(2 3,2 2,3 2,3
3,2 3))<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV">4:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV">Input:
POLYGON ((1 4,1 2,2 2,2 3,3 3,3 2,2 2,2 1,4 1,4 4,1 4))<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV">Result:
POLYGON ((1 4,1 2,2 2,2 1,4 1,4 4,1 4),(2 3,3 3,3 2,2 2,2
3))<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV">3 +
4:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV">Input:
MULTIPOLYGON (((1 2,2 2,2 4,1 4,1 2)),((2 1,4 1,4 5,2 5,2
4,3 4,3 2,2 2,2 1)))<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV">Result:
POLYGON ((1 4,1 2,2 2,2 4,1 4)) + POLYGON ((2 5,2 4,3 4,3
2,2 2,2 1,3 1,4 1,4 5,2 5))<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="SV"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="EN-US">I
have performed the tests on both GDAL 1.11 and GDAL 2.1 and
both return the same results.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="EN-US">Can
anyone confirm my observations?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><i><span style="color:#1F497D" lang="EN-US">Background:
The 4 situations are very simplified versions of much more
complicated polygons, where the problem occurs. If I save
the results from situation 3 or 4 to a shape file with
ogr, the hole in the results are converted to an exterior
ring in a separate polygon thus creating overlapping
results.<o:p></o:p></span></i></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="EN-US">Regards,
Casper<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#1F497D" lang="EN-US"><o:p> </o:p></span></p>
<div>
<div style="border:none;border-top:solid #B5C4DF
1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span
style="font-size:10.0pt;font-family:"Tahoma","sans-serif";mso-fareast-language:DA"
lang="EN-US">From:</span></b><span
style="font-size:10.0pt;font-family:"Tahoma","sans-serif";mso-fareast-language:DA"
lang="EN-US"> gdal-dev
[<a class="moz-txt-link-freetext" href="mailto:gdal-dev-bounces@lists.osgeo.org">mailto:gdal-dev-bounces@lists.osgeo.org</a>]
<b>On Behalf Of </b>Casper Børgesen (CABO)<br>
<b>Sent:</b> 19. januar 2017 13:26<br>
<b>To:</b> <a class="moz-txt-link-abbreviated" href="mailto:gdal-dev@lists.osgeo.org">gdal-dev@lists.osgeo.org</a><br>
<b>Subject:</b> [gdal-dev] Strange results of simple
polygonising<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal">Hi,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span lang="EN-US">I have four simple
rasters that I would like to polygonise:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">1:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"># # #<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"># #<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"># #<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">2: <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"># #<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"># #<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"># # #<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">3:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> # #<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"># #<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"># # #<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">4:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"># # #<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"># #<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"> # #<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">It’s the same shape,
just rotated 90, 180, 270 degrees. The resulting polygons
for 1 and 2 are simple polygons without holes, where the
results for 3 and 4 are polygons with a hole intersecting
the exterior ring. Thus 3 and 4 results in invalid
geometries with self-intersection.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Is this the intended
behavior of the polygoniser?<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Regards, Casper<o:p></o:p></span></p>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
gdal-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:gdal-dev@lists.osgeo.org">gdal-dev@lists.osgeo.org</a>
<a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/gdal-dev">https://lists.osgeo.org/mailman/listinfo/gdal-dev</a></pre>
</blockquote>
<br>
</body>
</html>