LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/pmtiles - ogrpmtileswriterdataset.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 32 35 91.4 %
Date: 2024-11-21 22:18:42 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  OpenGIS Simple Features Reference Implementation
       4             :  * Purpose:  Implementation of PMTiles
       5             :  * Author:   Even Rouault <even.rouault at spatialys.com>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2023, Planet Labs
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #include "ogr_pmtiles.h"
      14             : 
      15             : #ifdef HAVE_MVT_WRITE_SUPPORT
      16             : 
      17             : #include "mvtutils.h"
      18             : #include "ogrpmtilesfrommbtiles.h"
      19             : 
      20             : /************************************************************************/
      21             : /*                     ~OGRPMTilesWriterDataset()                       */
      22             : /************************************************************************/
      23             : 
      24          68 : OGRPMTilesWriterDataset::~OGRPMTilesWriterDataset()
      25             : {
      26          34 :     OGRPMTilesWriterDataset::Close();
      27          68 : }
      28             : 
      29             : /************************************************************************/
      30             : /*                             Close()                                  */
      31             : /************************************************************************/
      32             : 
      33          67 : CPLErr OGRPMTilesWriterDataset::Close()
      34             : {
      35          67 :     CPLErr eErr = CE_None;
      36          67 :     if (nOpenFlags != OPEN_FLAGS_CLOSED)
      37             :     {
      38          34 :         if (m_poMBTilesWriterDataset)
      39             :         {
      40          33 :             if (m_poMBTilesWriterDataset->Close() != CE_None)
      41             :             {
      42           0 :                 eErr = CE_Failure;
      43             :             }
      44             :             else
      45             :             {
      46          33 :                 if (!OGRPMTilesConvertFromMBTiles(
      47          33 :                         GetDescription(),
      48          33 :                         m_poMBTilesWriterDataset->GetDescription()))
      49             :                 {
      50          16 :                     eErr = CE_Failure;
      51             :                 }
      52             :             }
      53             : 
      54          33 :             VSIUnlink(m_poMBTilesWriterDataset->GetDescription());
      55          33 :             m_poMBTilesWriterDataset.reset();
      56             :         }
      57             : 
      58          34 :         if (GDALDataset::Close() != CE_None)
      59           0 :             eErr = CE_Failure;
      60             :     }
      61          67 :     return eErr;
      62             : }
      63             : 
      64             : /************************************************************************/
      65             : /*                             Create()                                 */
      66             : /************************************************************************/
      67             : 
      68          34 : bool OGRPMTilesWriterDataset::Create(const char *pszFilename,
      69             :                                      CSLConstList papszOptions)
      70             : {
      71          34 :     SetDescription(pszFilename);
      72          68 :     CPLStringList aosOptions(papszOptions);
      73          34 :     aosOptions.SetNameValue("FORMAT", "MBTILES");
      74             : 
      75             :     // Let's build a temporary file that contains the tile data in
      76             :     // a way that corresponds to the "clustered" mode, that is
      77             :     // "offsets are either contiguous with the previous offset+length, or
      78             :     // refer to a lesser offset, when writing with deduplication."
      79          68 :     std::string osTmpFile(pszFilename);
      80          34 :     if (!VSIIsLocal(pszFilename))
      81             :     {
      82           0 :         osTmpFile = CPLGenerateTempFilename(CPLGetFilename(pszFilename));
      83             :     }
      84          34 :     osTmpFile += ".tmp.mbtiles";
      85             : 
      86          34 :     if (!aosOptions.FetchNameValue("NAME"))
      87          34 :         aosOptions.SetNameValue("NAME", CPLGetBasename(pszFilename));
      88             : 
      89          34 :     m_poMBTilesWriterDataset.reset(OGRMVTWriterDatasetCreate(
      90             :         osTmpFile.c_str(), 0, 0, 0, GDT_Unknown, aosOptions.List()));
      91             : 
      92          68 :     return m_poMBTilesWriterDataset != nullptr;
      93             : }
      94             : 
      95             : /************************************************************************/
      96             : /*                           ICreateLayer()                             */
      97             : /************************************************************************/
      98             : 
      99             : OGRLayer *
     100          49 : OGRPMTilesWriterDataset::ICreateLayer(const char *pszLayerName,
     101             :                                       const OGRGeomFieldDefn *poGeomFieldDefn,
     102             :                                       CSLConstList papszOptions)
     103             : {
     104          49 :     return m_poMBTilesWriterDataset->CreateLayer(pszLayerName, poGeomFieldDefn,
     105          49 :                                                  papszOptions);
     106             : }
     107             : 
     108             : /************************************************************************/
     109             : /*                            TestCapability()                          */
     110             : /************************************************************************/
     111             : 
     112          51 : int OGRPMTilesWriterDataset::TestCapability(const char *pszCap)
     113             : {
     114          51 :     return m_poMBTilesWriterDataset->TestCapability(pszCap);
     115             : }
     116             : 
     117             : #endif  // HAVE_MVT_WRITE_SUPPORT

Generated by: LCOV version 1.14