LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/dgn - ogrdgndriver.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 50 55 90.9 %
Date: 2024-05-08 14:54:11 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  OpenGIS Simple Features Reference Implementation
       4             :  * Purpose:  Implements OGRDGNDriver class.
       5             :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2000, Frank Warmerdam (warmerdam@pobox.com)
       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_dgn.h"
      30             : #include "cpl_conv.h"
      31             : 
      32             : /************************************************************************/
      33             : /*                                Open()                                */
      34             : /************************************************************************/
      35             : 
      36       46244 : static int OGRDGNDriverIdentify(GDALOpenInfo *poOpenInfo)
      37             : 
      38             : {
      39       49810 :     if (poOpenInfo->fpL != nullptr && poOpenInfo->nHeaderBytes >= 512 &&
      40        3566 :         DGNTestOpen(poOpenInfo->pabyHeader, poOpenInfo->nHeaderBytes))
      41             :     {
      42          80 :         return TRUE;
      43             :     }
      44             : 
      45             :     // Is this is a DGNv8 file ? If so, and if the DGNV8 driver is not
      46             :     // available, and we are called from GDALError(), emit an explicit
      47             :     // error.
      48             :     VSIStatBuf sStat;
      49       99342 :     if ((poOpenInfo->nOpenFlags & GDAL_OF_FROM_GDALOPEN) != 0 &&
      50        7014 :         poOpenInfo->papszAllowedDrivers == nullptr &&
      51        7014 :         poOpenInfo->fpL != nullptr && poOpenInfo->nHeaderBytes >= 512 &&
      52        3312 :         memcmp(poOpenInfo->pabyHeader, "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1", 8) ==
      53           6 :             0 &&
      54           6 :         EQUAL(CPLGetExtension(poOpenInfo->pszFilename), "DGN") &&
      55       53179 :         VSIStat(poOpenInfo->pszFilename, &sStat) == 0 &&
      56           1 :         GDALGetDriverByName("DGNV8") == nullptr)
      57             :     {
      58           1 :         CPLError(CE_Failure, CPLE_AppDefined,
      59             :                  "`%s' recognized as a DGNv8 dataset, but the DGNv8 driver is "
      60             :                  "not available in this GDAL build. Consult "
      61             :                  "https://gdal.org/drivers/vector/dgnv8.html",
      62             :                  poOpenInfo->pszFilename);
      63             :     }
      64       46164 :     return FALSE;
      65             : }
      66             : 
      67             : /************************************************************************/
      68             : /*                                Open()                                */
      69             : /************************************************************************/
      70             : 
      71          40 : static GDALDataset *OGRDGNDriverOpen(GDALOpenInfo *poOpenInfo)
      72             : 
      73             : {
      74          40 :     if (!OGRDGNDriverIdentify(poOpenInfo))
      75           0 :         return nullptr;
      76             : 
      77          40 :     OGRDGNDataSource *poDS = new OGRDGNDataSource();
      78             : 
      79         120 :     if (!poDS->Open(poOpenInfo->pszFilename, TRUE,
      80          80 :                     (poOpenInfo->eAccess == GA_Update)) ||
      81          40 :         poDS->GetLayerCount() == 0)
      82             :     {
      83           0 :         delete poDS;
      84           0 :         return nullptr;
      85             :     }
      86             : 
      87          40 :     return poDS;
      88             : }
      89             : 
      90             : /************************************************************************/
      91             : /*                              Create()                                */
      92             : /************************************************************************/
      93             : 
      94          34 : static GDALDataset *OGRDGNDriverCreate(const char *pszName, int /* nBands */,
      95             :                                        int /* nXSize */, int /* nYSize */,
      96             :                                        GDALDataType /* eDT */,
      97             :                                        char **papszOptions)
      98             : {
      99             :     /* -------------------------------------------------------------------- */
     100             :     /*      Return a new OGRDataSource()                                    */
     101             :     /* -------------------------------------------------------------------- */
     102          34 :     OGRDGNDataSource *poDS = new OGRDGNDataSource();
     103             : 
     104          34 :     if (!poDS->PreCreate(pszName, papszOptions))
     105             :     {
     106           0 :         delete poDS;
     107           0 :         return nullptr;
     108             :     }
     109             : 
     110          34 :     return poDS;
     111             : }
     112             : 
     113             : /************************************************************************/
     114             : /*                          RegisterOGRDGN()                            */
     115             : /************************************************************************/
     116             : 
     117        1520 : void RegisterOGRDGN()
     118             : 
     119             : {
     120        1520 :     if (GDALGetDriverByName("DGN") != nullptr)
     121         301 :         return;
     122             : 
     123        1219 :     GDALDriver *poDriver = new GDALDriver();
     124             : 
     125        1219 :     poDriver->SetDescription("DGN");
     126        1219 :     poDriver->SetMetadataItem(GDAL_DCAP_VECTOR, "YES");
     127        1219 :     poDriver->SetMetadataItem(GDAL_DCAP_CREATE_LAYER, "YES");
     128        1219 :     poDriver->SetMetadataItem(GDAL_DMD_LONGNAME, "Microstation DGN");
     129        1219 :     poDriver->SetMetadataItem(GDAL_DMD_EXTENSION, "dgn");
     130        1219 :     poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/vector/dgn.html");
     131        1219 :     poDriver->SetMetadataItem(GDAL_DCAP_Z_GEOMETRIES, "YES");
     132        1219 :     poDriver->SetMetadataItem(GDAL_DMD_SUPPORTED_SQL_DIALECTS, "OGRSQL SQLITE");
     133             : 
     134        1219 :     poDriver->SetMetadataItem(
     135             :         GDAL_DMD_CREATIONOPTIONLIST,
     136             :         "<CreationOptionList>"
     137             :         "  <Option name='3D' type='boolean' description='whether 2D "
     138             :         "(seed_2d.dgn) or 3D (seed_3d.dgn) seed file should be used. This "
     139             :         "option is ignored if the SEED option is provided'/>"
     140             :         "  <Option name='SEED' type='string' description='Filename of seed "
     141             :         "file to use'/>"
     142             :         "  <Option name='COPY_WHOLE_SEED_FILE' type='boolean' "
     143             :         "description='whether the whole seed file should be copied. If not, "
     144             :         "only the first three elements (and potentially the color table) will "
     145             :         "be copied.' default='NO'/>"
     146             :         "  <Option name='COPY_SEED_FILE_COLOR_TABLE' type='boolean' "
     147             :         "description='whether the color table should be copied from the seed "
     148             :         "file.' default='NO'/>"
     149             :         "  <Option name='MASTER_UNIT_NAME' type='string' description='Override "
     150             :         "the master unit name from the seed file with the provided one or two "
     151             :         "character unit name.'/>"
     152             :         "  <Option name='SUB_UNIT_NAME' type='string' description='Override "
     153             :         "the master unit name from the seed file with the provided one or two "
     154             :         "character unit name.'/>"
     155             :         "  <Option name='MASTER_UNIT_NAME' type='string' description='Override "
     156             :         "the master unit name from the seed file with the provided one or two "
     157             :         "character unit name.'/>"
     158             :         "  <Option name='SUB_UNIT_NAME' type='string' description='Override "
     159             :         "the sub unit name from the seed file with the provided one or two "
     160             :         "character unit name.'/>"
     161             :         "  <Option name='SUB_UNITS_PER_MASTER_UNIT' type='int' "
     162             :         "description='Override the number of subunits per master unit. By "
     163             :         "default the seed file value is used.'/>"
     164             :         "  <Option name='UOR_PER_SUB_UNIT' type='int' description='Override "
     165             :         "the number of UORs (Units of Resolution) per sub unit. By default the "
     166             :         "seed file value is used.'/>"
     167             :         "  <Option name='ORIGIN' type='string' description='Value as x,y,z. "
     168             :         "Override the origin of the design plane. By default the origin from "
     169             :         "the seed file is used.'/>"
     170        1219 :         "</CreationOptionList>");
     171             : 
     172        1219 :     poDriver->SetMetadataItem(GDAL_DS_LAYER_CREATIONOPTIONLIST,
     173        1219 :                               "<LayerCreationOptionList/>");
     174        1219 :     poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES");
     175        1219 :     poDriver->SetMetadataItem(GDAL_DCAP_FEATURE_STYLES, "YES");
     176        1219 :     poDriver->SetMetadataItem(GDAL_DCAP_FEATURE_STYLES_READ, "YES");
     177        1219 :     poDriver->SetMetadataItem(GDAL_DCAP_FEATURE_STYLES_WRITE, "YES");
     178        1219 :     poDriver->SetMetadataItem(GDAL_DCAP_MULTIPLE_VECTOR_LAYERS, "YES");
     179             : 
     180        1219 :     poDriver->pfnOpen = OGRDGNDriverOpen;
     181        1219 :     poDriver->pfnIdentify = OGRDGNDriverIdentify;
     182        1219 :     poDriver->pfnCreate = OGRDGNDriverCreate;
     183             : 
     184        1219 :     GetGDALDriverManager()->RegisterDriver(poDriver);
     185             : }

Generated by: LCOV version 1.14