LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/parquet - ogrparquetwriterdataset.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 41 44 93.2 %
Date: 2024-05-14 23:54:21 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  Parquet Translator
       4             :  * Purpose:  Implements OGRParquetDriver.
       5             :  * Author:   Even Rouault, <even.rouault at spatialys.com>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2022, 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_parquet.h"
      30             : 
      31             : #include "../arrow_common/ograrrowwriterlayer.hpp"
      32             : 
      33             : /************************************************************************/
      34             : /*                       OGRParquetWriterDataset()                      */
      35             : /************************************************************************/
      36             : 
      37         198 : OGRParquetWriterDataset::OGRParquetWriterDataset(
      38         198 :     const std::shared_ptr<arrow::io::OutputStream> &poOutputStream)
      39             :     : m_poMemoryPool(arrow::MemoryPool::CreateDefault()),
      40         198 :       m_poOutputStream(poOutputStream)
      41             : {
      42         198 : }
      43             : 
      44             : /************************************************************************/
      45             : /*                                Close()                               */
      46             : /************************************************************************/
      47             : 
      48         198 : CPLErr OGRParquetWriterDataset::Close()
      49             : {
      50         198 :     CPLErr eErr = CE_None;
      51         198 :     if (nOpenFlags != OPEN_FLAGS_CLOSED)
      52             :     {
      53         198 :         if (m_poLayer && !m_poLayer->Close())
      54             :         {
      55           0 :             eErr = CE_Failure;
      56             :         }
      57             : 
      58         198 :         if (GDALPamDataset::Close() != CE_None)
      59             :         {
      60           0 :             eErr = CE_Failure;
      61             :         }
      62             :     }
      63             : 
      64         198 :     return eErr;
      65             : }
      66             : 
      67             : /************************************************************************/
      68             : /*                           GetLayerCount()                            */
      69             : /************************************************************************/
      70             : 
      71          23 : int OGRParquetWriterDataset::GetLayerCount()
      72             : {
      73          23 :     return m_poLayer ? 1 : 0;
      74             : }
      75             : 
      76             : /************************************************************************/
      77             : /*                             GetLayer()                               */
      78             : /************************************************************************/
      79             : 
      80           9 : OGRLayer *OGRParquetWriterDataset::GetLayer(int idx)
      81             : {
      82           9 :     return idx == 0 ? m_poLayer.get() : nullptr;
      83             : }
      84             : 
      85             : /************************************************************************/
      86             : /*                         TestCapability()                             */
      87             : /************************************************************************/
      88             : 
      89          76 : int OGRParquetWriterDataset::TestCapability(const char *pszCap)
      90             : {
      91          76 :     if (EQUAL(pszCap, ODsCCreateLayer))
      92          41 :         return m_poLayer == nullptr;
      93          35 :     if (EQUAL(pszCap, ODsCAddFieldDomain))
      94           5 :         return m_poLayer != nullptr;
      95          30 :     return false;
      96             : }
      97             : 
      98             : /************************************************************************/
      99             : /*                          ICreateLayer()                              */
     100             : /************************************************************************/
     101             : 
     102             : OGRLayer *
     103         217 : OGRParquetWriterDataset::ICreateLayer(const char *pszName,
     104             :                                       const OGRGeomFieldDefn *poGeomFieldDefn,
     105             :                                       CSLConstList papszOptions)
     106             : {
     107         217 :     if (m_poLayer)
     108             :     {
     109          17 :         CPLError(CE_Failure, CPLE_NotSupported,
     110             :                  "Can write only one layer in a Parquet file");
     111          17 :         return nullptr;
     112             :     }
     113             : 
     114         200 :     const auto eGType = poGeomFieldDefn ? poGeomFieldDefn->GetType() : wkbNone;
     115             :     const auto poSpatialRef =
     116         200 :         poGeomFieldDefn ? poGeomFieldDefn->GetSpatialRef() : nullptr;
     117             : 
     118         200 :     m_poLayer = std::make_unique<OGRParquetWriterLayer>(
     119         200 :         this, m_poMemoryPool.get(), m_poOutputStream, pszName);
     120         200 :     if (!m_poLayer->SetOptions(papszOptions, poSpatialRef, eGType))
     121             :     {
     122           3 :         m_poLayer.reset();
     123           3 :         return nullptr;
     124             :     }
     125         197 :     return m_poLayer.get();
     126             : }
     127             : 
     128             : /************************************************************************/
     129             : /*                          AddFieldDomain()                            */
     130             : /************************************************************************/
     131             : 
     132           4 : bool OGRParquetWriterDataset::AddFieldDomain(
     133             :     std::unique_ptr<OGRFieldDomain> &&domain, std::string &failureReason)
     134             : {
     135           4 :     if (m_poLayer == nullptr)
     136             :     {
     137           1 :         failureReason = "Layer must be created";
     138           1 :         return false;
     139             :     }
     140           3 :     return m_poLayer->AddFieldDomain(std::move(domain), failureReason);
     141             : }
     142             : 
     143             : /************************************************************************/
     144             : /*                          GetFieldDomainNames()                       */
     145             : /************************************************************************/
     146             : 
     147             : std::vector<std::string>
     148           1 : OGRParquetWriterDataset::GetFieldDomainNames(CSLConstList) const
     149             : {
     150           0 :     return m_poLayer ? m_poLayer->GetFieldDomainNames()
     151           1 :                      : std::vector<std::string>();
     152             : }
     153             : 
     154             : /************************************************************************/
     155             : /*                          GetFieldDomain()                            */
     156             : /************************************************************************/
     157             : 
     158             : const OGRFieldDomain *
     159           5 : OGRParquetWriterDataset::GetFieldDomain(const std::string &name) const
     160             : {
     161           5 :     return m_poLayer ? m_poLayer->GetFieldDomain(name) : nullptr;
     162             : }

Generated by: LCOV version 1.14