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 : * Permission is hereby granted, free of charge, to any person obtaining a 11 : * copy of this software and associated documentation files (the "Software"), 12 : * to deal in the Software without restriction, including without limitation 13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 : * and/or sell copies of the Software, and to permit persons to whom the 15 : * Software is furnished to do so, subject to the following conditions: 16 : * 17 : * The above copyright notice and this permission notice shall be included 18 : * in all copies or substantial portions of the Software. 19 : * 20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 : * DEALINGS IN THE SOFTWARE. 27 : ****************************************************************************/ 28 : 29 : #include "ogr_pmtiles.h" 30 : 31 : #ifdef HAVE_MVT_WRITE_SUPPORT 32 : 33 : #include "mvtutils.h" 34 : #include "ogrpmtilesfrommbtiles.h" 35 : 36 : /************************************************************************/ 37 : /* ~OGRPMTilesWriterDataset() */ 38 : /************************************************************************/ 39 : 40 68 : OGRPMTilesWriterDataset::~OGRPMTilesWriterDataset() 41 : { 42 34 : OGRPMTilesWriterDataset::Close(); 43 68 : } 44 : 45 : /************************************************************************/ 46 : /* Close() */ 47 : /************************************************************************/ 48 : 49 67 : CPLErr OGRPMTilesWriterDataset::Close() 50 : { 51 67 : CPLErr eErr = CE_None; 52 67 : if (nOpenFlags != OPEN_FLAGS_CLOSED) 53 : { 54 34 : if (m_poMBTilesWriterDataset) 55 : { 56 33 : if (m_poMBTilesWriterDataset->Close() != CE_None) 57 : { 58 0 : eErr = CE_Failure; 59 : } 60 : else 61 : { 62 33 : if (!OGRPMTilesConvertFromMBTiles( 63 33 : GetDescription(), 64 33 : m_poMBTilesWriterDataset->GetDescription())) 65 : { 66 16 : eErr = CE_Failure; 67 : } 68 : } 69 : 70 33 : VSIUnlink(m_poMBTilesWriterDataset->GetDescription()); 71 33 : m_poMBTilesWriterDataset.reset(); 72 : } 73 : 74 34 : if (GDALDataset::Close() != CE_None) 75 0 : eErr = CE_Failure; 76 : } 77 67 : return eErr; 78 : } 79 : 80 : /************************************************************************/ 81 : /* Create() */ 82 : /************************************************************************/ 83 : 84 34 : bool OGRPMTilesWriterDataset::Create(const char *pszFilename, 85 : CSLConstList papszOptions) 86 : { 87 34 : SetDescription(pszFilename); 88 68 : CPLStringList aosOptions(papszOptions); 89 34 : aosOptions.SetNameValue("FORMAT", "MBTILES"); 90 : 91 : // Let's build a temporary file that contains the tile data in 92 : // a way that corresponds to the "clustered" mode, that is 93 : // "offsets are either contiguous with the previous offset+length, or 94 : // refer to a lesser offset, when writing with deduplication." 95 68 : std::string osTmpFile(pszFilename); 96 34 : if (!VSIIsLocal(pszFilename)) 97 : { 98 0 : osTmpFile = CPLGenerateTempFilename(CPLGetFilename(pszFilename)); 99 : } 100 34 : osTmpFile += ".tmp.mbtiles"; 101 : 102 34 : if (!aosOptions.FetchNameValue("NAME")) 103 34 : aosOptions.SetNameValue("NAME", CPLGetBasename(pszFilename)); 104 : 105 34 : m_poMBTilesWriterDataset.reset(OGRMVTWriterDatasetCreate( 106 : osTmpFile.c_str(), 0, 0, 0, GDT_Unknown, aosOptions.List())); 107 : 108 68 : return m_poMBTilesWriterDataset != nullptr; 109 : } 110 : 111 : /************************************************************************/ 112 : /* ICreateLayer() */ 113 : /************************************************************************/ 114 : 115 : OGRLayer * 116 49 : OGRPMTilesWriterDataset::ICreateLayer(const char *pszLayerName, 117 : const OGRGeomFieldDefn *poGeomFieldDefn, 118 : CSLConstList papszOptions) 119 : { 120 49 : return m_poMBTilesWriterDataset->CreateLayer(pszLayerName, poGeomFieldDefn, 121 49 : papszOptions); 122 : } 123 : 124 : /************************************************************************/ 125 : /* TestCapability() */ 126 : /************************************************************************/ 127 : 128 51 : int OGRPMTilesWriterDataset::TestCapability(const char *pszCap) 129 : { 130 51 : return m_poMBTilesWriterDataset->TestCapability(pszCap); 131 : } 132 : 133 : #endif // HAVE_MVT_WRITE_SUPPORT