[gdal-dev] Creating a vector mbtile with several layers

Even Rouault even.rouault at spatialys.com
Fri Jul 1 03:31:32 PDT 2022


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

e.g with the following OGR Python script:

from osgeo import ogr, osr
srs = osr.SpatialReference()
srs.SetFromUserInput('EPSG:3857')
ds = ogr.GetDriverByName('MBTiles').CreateDataSource('out.mbtiles')
lyr = ds.CreateLayer('lyr1', srs=srs)
f = ogr.Feature(lyr.GetLayerDefn())
f.SetGeometry(ogr.CreateGeometryFromWkt('POINT(2000000 4000000)'))
lyr.CreateFeature(f)
lyr = ds.CreateLayer('lyr2', srs=srs)
f = ogr.Feature(lyr.GetLayerDefn())
f.SetGeometry(ogr.CreateGeometryFromWkt('POINT(3000000 5000000)'))
lyr.CreateFeature(f)
ds = None

$ ogrinfo out.mbtiles -al -q
Metadata:
   ZOOM_LEVEL=5
   name=out
   description=
   version=2
   minzoom=0
   maxzoom=5
   center=22.4578821,37.3507523,0
   bounds=17.9663057,33.7852301,26.9494585,40.9162745
   type=overlay
   format=pbf
   scheme=tms

Layer name: lyr1
OGRFeature(lyr1):401
   MULTIPOINT ((1999898.40805335 4000102.56421984))


Layer name: lyr2
OGRFeature(lyr2):402
   MULTIPOINT ((3000000.4861366 4999898.89418995))


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

I also assume you instantiate a new "geom" object for each feature, 
otherwise as you use SetGeometryDirectly() use-after-free would occur.

Even

Le 01/07/2022 à 12:16, DELEPINE Christophe via gdal-dev a écrit :
>
> I also tried the MVT driver and got the same problem : the output 
> metadata.json only lists one layer.
>
> Should I create all layers first before adding features to them ?
>
> The C++ API documentation does not explain how to write several layers 
> in the case of a MBTILES/MVT dataset
>
> *De :*DELEPINE Christophe
> *Envoyé :* jeudi 30 juin 2022 17:17
> *À :* 'gdal-dev at lists.osgeo.org' <gdal-dev at lists.osgeo.org>
> *Objet :* Creating a vector mbtile with several layers
>
> Hello
>
> 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)
>
> What I do is the following :
>
> GDALDriver* driver = (GDALDriver*)GDALGetDriverByName("MBTiles");
>
> GDALDataset* poDS = driver->Create(mbtiles.c_str(), 0, 0, 0, 
> GDT_Unknown, datasetCreationOptions);
>
> for(auto& layer : layers)
>
> {
>
> OGRLayer* oLayer = poDS->CreateLayer(layer.name_.c_str(), &srs3857_, 
> wkbUnknown);
>
> OGRFeature*oFeature = OGRFeature::CreateFeature(oLayer->GetLayerDefn());
>
> oFeature->SetGeometryDirectly(geom);
>
> oLayer->CreateFeature(oFeature);
>
> }
>
> GDALClose(poDS);
>
> Any idea what is wrong ?
>
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/gdal-dev

-- 
http://www.spatialys.com
My software is free, but my time generally not.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20220701/6820e029/attachment.htm>


More information about the gdal-dev mailing list