<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Looking at driver code and testing a bit with the following, I
      don't see any issue or need to create all layers before adding
      features to them</p>
    <p>e.g with the following OGR Python script:<br>
    </p>
    <p>from osgeo import ogr, osr<br>
      srs = osr.SpatialReference()<br>
      srs.SetFromUserInput('EPSG:3857')<br>
      ds =
      ogr.GetDriverByName('MBTiles').CreateDataSource('out.mbtiles')<br>
      lyr = ds.CreateLayer('lyr1', srs=srs)<br>
      f = ogr.Feature(lyr.GetLayerDefn())<br>
      f.SetGeometry(ogr.CreateGeometryFromWkt('POINT(2000000 4000000)'))<br>
      lyr.CreateFeature(f)<br>
      lyr = ds.CreateLayer('lyr2', srs=srs)<br>
      f = ogr.Feature(lyr.GetLayerDefn())<br>
      f.SetGeometry(ogr.CreateGeometryFromWkt('POINT(3000000 5000000)'))<br>
      lyr.CreateFeature(f)<br>
      ds = None</p>
    <p>$ ogrinfo out.mbtiles -al -q<br>
      Metadata:<br>
        ZOOM_LEVEL=5<br>
        name=out<br>
        description=<br>
        version=2<br>
        minzoom=0<br>
        maxzoom=5<br>
        center=22.4578821,37.3507523,0<br>
        bounds=17.9663057,33.7852301,26.9494585,40.9162745<br>
        type=overlay<br>
        format=pbf<br>
        scheme=tms<br>
      <br>
      Layer name: lyr1<br>
      OGRFeature(lyr1):401<br>
        MULTIPOINT ((1999898.40805335 4000102.56421984))<br>
      <br>
      <br>
      Layer name: lyr2<br>
      OGRFeature(lyr2):402<br>
        MULTIPOINT ((3000000.4861366 4999898.89418995))</p>
    <p><br>
    </p>
    <p>Note: in your code you need to "delete oFeature" after
      CreateFeature() to avoid memory leaks, but that shouldn't affect
      the creation of the output dataset itself</p>
    <p>I also assume you instantiate a new "geom" object for each
      feature, otherwise as you use SetGeometryDirectly() use-after-free
      would occur.<br>
    </p>
    <p>Even<br>
    </p>
    <div class="moz-cite-prefix">Le 01/07/2022 à 12:16, DELEPINE
      Christophe via gdal-dev a écrit :<br>
    </div>
    <blockquote type="cite"
      cite="mid:5cd13da6909e46d7a4d2a4ff8849f48e@thalesgroup.com">
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <meta name="Generator" content="Microsoft Word 15 (filtered
        medium)">
      <style>@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:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}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:#0563C1;
        text-decoration:underline;}a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}p.msonormal0, li.msonormal0, div.msonormal0
        {mso-style-name:msonormal;
        mso-margin-top-alt:auto;
        margin-right:0cm;
        mso-margin-bottom-alt:auto;
        margin-left:0cm;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}span.EmailStyle18
        {mso-style-type:personal;
        font-family:"Calibri",sans-serif;
        color:windowtext;}span.EmailStyle19
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:#1F497D;}.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}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
            also tried the MVT driver and got the same problem : the
            output metadata.json only lists one layer.<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="color:#1F497D" lang="EN-US">Should
            I create all layers first before adding features to them ?<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
            C++ API documentation does not explain how to write several
            layers in the case of a MBTILES/MVT dataset<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>
        <div>
          <div style="border:none;border-top:solid #E1E1E1
            1.0pt;padding:3.0pt 0cm 0cm 0cm">
            <p class="MsoNormal"><b><span
                  style="mso-fareast-language:FR">De :</span></b><span
                style="mso-fareast-language:FR"> DELEPINE Christophe
                <br>
                <b>Envoyé :</b> jeudi 30 juin 2022 17:17<br>
                <b>À :</b> '<a class="moz-txt-link-abbreviated" href="mailto:gdal-dev@lists.osgeo.org">gdal-dev@lists.osgeo.org</a>'
                <a class="moz-txt-link-rfc2396E" href="mailto:gdal-dev@lists.osgeo.org"><gdal-dev@lists.osgeo.org></a><br>
                <b>Objet :</b> Creating a vector mbtile with several
                layers<o:p></o:p></span></p>
          </div>
        </div>
        <p class="MsoNormal"><o:p> </o:p></p>
        <p class="MsoNormal"><span lang="EN-US">Hello<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">I am trying to create a
            vector mbtile containing several layers (using C++ GDAL API)
            but it does not work. My output mbtile only contains one
            layer (ogrinfo only lists one layer)<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">What I do is the
            following :<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span
            style="font-size:9.5pt;font-family:Consolas;color:#2B91AF"
            lang="EN-US">GDALDriver</span><span
            style="font-size:9.5pt;font-family:Consolas;color:black"
            lang="EN-US">* driver = (</span><span
            style="font-size:9.5pt;font-family:Consolas;color:#2B91AF"
            lang="EN-US">GDALDriver</span><span
            style="font-size:9.5pt;font-family:Consolas;color:black"
            lang="EN-US">*)GDALGetDriverByName(</span><span
            style="font-size:9.5pt;font-family:Consolas;color:#A31515"
            lang="EN-US">"MBTiles"</span><span
            style="font-size:9.5pt;font-family:Consolas;color:black"
            lang="EN-US">);<o:p></o:p></span></p>
        <p class="MsoNormal"><span
            style="font-size:9.5pt;font-family:Consolas;color:#2B91AF"
            lang="EN-US">GDALDataset</span><span
            style="font-size:9.5pt;font-family:Consolas;color:black"
            lang="EN-US">* poDS = driver->Create(mbtiles.c_str(), 0,
            0, 0,
          </span><span
            style="font-size:9.5pt;font-family:Consolas;color:darkslategray"
            lang="EN-US">GDT_Unknown</span><span
            style="font-size:9.5pt;font-family:Consolas;color:black"
            lang="EN-US">, datasetCreationOptions);<o:p></o:p></span></p>
        <p class="MsoNormal" style="text-autospace:none"><span
            style="font-size:9.5pt;font-family:Consolas;color:blue">for</span><span
            style="font-size:9.5pt;font-family:Consolas;color:black"> (</span><span
            style="font-size:9.5pt;font-family:Consolas;color:blue">auto</span><span
            style="font-size:9.5pt;font-family:Consolas;color:black">&
            layer : </span><span
            style="font-size:9.5pt;font-family:Consolas;color:gray">layers</span><span
            style="font-size:9.5pt;font-family:Consolas;color:black">)<o:p></o:p></span></p>
        <p class="MsoNormal" style="text-autospace:none"><span
            style="font-size:9.5pt;font-family:Consolas;color:black">{<o:p></o:p></span></p>
        <p class="MsoNormal" style="text-autospace:none"><span
            style="font-size:9.5pt;font-family:Consolas;color:black">      
          </span><span
            style="font-size:9.5pt;font-family:Consolas;color:#2B91AF"
            lang="EN-US">OGRLayer</span><span
            style="font-size:9.5pt;font-family:Consolas;color:black"
            lang="EN-US">* oLayer =
            poDS->CreateLayer(layer.name_.c_str(), &srs3857_,
          </span><span
            style="font-size:9.5pt;font-family:Consolas;color:darkslategray"
            lang="EN-US">wkbUnknown</span><span
            style="font-size:9.5pt;font-family:Consolas;color:black"
            lang="EN-US">);<o:p></o:p></span></p>
        <p class="MsoNormal"
          style="text-indent:35.4pt;text-autospace:none"><span
            style="font-size:9.5pt;font-family:Consolas;color:#2B91AF"
            lang="EN-US">OGRFeature</span><span
            style="font-size:9.5pt;font-family:Consolas;color:black"
            lang="EN-US"> *oFeature =
          </span><span
            style="font-size:9.5pt;font-family:Consolas;color:#2B91AF"
            lang="EN-US">OGRFeature</span><span
            style="font-size:9.5pt;font-family:Consolas;color:black"
            lang="EN-US">::CreateFeature(oLayer->GetLayerDefn());<o:p></o:p></span></p>
        <p class="MsoNormal"
          style="text-indent:35.4pt;text-autospace:none"><span
            style="font-size:9.5pt;font-family:Consolas;color:black">oFeature->SetGeometryDirectly(geom);<o:p></o:p></span></p>
        <p class="MsoNormal"
          style="text-indent:35.4pt;text-autospace:none"><span
            style="font-size:9.5pt;font-family:Consolas;color:black"
            lang="EN-US">oLayer->CreateFeature(oFeature);<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="EN-US">}<o:p></o:p></span></p>
        <p class="MsoNormal"><span
            style="font-size:9.5pt;font-family:Consolas;color:black"
            lang="EN-US">GDALClose(poDS);<o:p></o:p></span></p>
        <p class="MsoNormal"><span
            style="font-size:9.5pt;font-family:Consolas;color:black"
            lang="EN-US"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span
            style="font-size:9.5pt;font-family:Consolas;color:black"
            lang="EN-US"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span
            style="font-size:9.5pt;font-family:Consolas;color:black"
            lang="EN-US">Any idea what is wrong ?</span><span
            lang="EN-US"><o:p></o:p></span></p>
      </div>
      <br>
      <fieldset class="moz-mime-attachment-header"></fieldset>
      <pre class="moz-quote-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>
    <pre class="moz-signature" cols="72">-- 
<a class="moz-txt-link-freetext" href="http://www.spatialys.com">http://www.spatialys.com</a>
My software is free, but my time generally not.</pre>
  </body>
</html>