LCOV - code coverage report
Current view: top level - gnm/gnm_frmts/file - gnmfiledriver.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 54 61 88.5 %
Date: 2024-05-04 12:52:34 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL/OGR Geography Network support (Geographic Network Model)
       4             :  * Purpose:  GNM generic driver.
       5             :  * Authors:  Mikhail Gusev (gusevmihs at gmail dot com)
       6             :  *           Dmitry Baryshnikov, polimax@mail.ru
       7             :  *
       8             :  ******************************************************************************
       9             :  * Copyright (c) 2014, Mikhail Gusev
      10             :  * Copyright (c) 2014-2015, NextGIS <info@nextgis.com>
      11             :  *
      12             :  * Permission is hereby granted, free of charge, to any person obtaining a
      13             :  * copy of this software and associated documentation files (the "Software"),
      14             :  * to deal in the Software without restriction, including without limitation
      15             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      16             :  * and/or sell copies of the Software, and to permit persons to whom the
      17             :  * Software is furnished to do so, subject to the following conditions:
      18             :  *
      19             :  * The above copyright notice and this permission notice shall be included
      20             :  * in all copies or substantial portions of the Software.
      21             :  *
      22             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      23             :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      24             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      25             :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      26             :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      27             :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      28             :  * DEALINGS IN THE SOFTWARE.
      29             :  ****************************************************************************/
      30             : 
      31             : #include "gnmfile.h"
      32             : #include "gnm_frmts.h"
      33             : #include "gnm_priv.h"
      34             : 
      35       40669 : static int GNMFileDriverIdentify(GDALOpenInfo *poOpenInfo)
      36             : 
      37             : {
      38       40669 :     if (!poOpenInfo->bIsDirectory)
      39       40528 :         return FALSE;
      40         141 :     if ((poOpenInfo->nOpenFlags & GDAL_OF_GNM) == 0)
      41          14 :         return FALSE;
      42             : 
      43         127 :     char **papszFiles = VSIReadDir(poOpenInfo->pszFilename);
      44         127 :     if (CSLCount(papszFiles) == 0)
      45             :     {
      46           0 :         return FALSE;
      47             :     }
      48             : 
      49         127 :     bool bHasMeta(false), bHasGraph(false), bHasFeatures(false);
      50             : 
      51             :     // search for base GNM files
      52        1698 :     for (int i = 0; papszFiles[i] != nullptr; i++)
      53             :     {
      54        1597 :         if (EQUAL(papszFiles[i], ".") || EQUAL(papszFiles[i], ".."))
      55          56 :             continue;
      56             : 
      57        1541 :         if (EQUAL(CPLGetBasename(papszFiles[i]), GNM_SYSLAYER_META))
      58          26 :             bHasMeta = true;
      59        1515 :         else if (EQUAL(CPLGetBasename(papszFiles[i]), GNM_SYSLAYER_GRAPH))
      60          26 :             bHasGraph = true;
      61        1489 :         else if (EQUAL(CPLGetBasename(papszFiles[i]), GNM_SYSLAYER_FEATURES))
      62          26 :             bHasFeatures = true;
      63             : 
      64        1541 :         if (bHasMeta && bHasGraph && bHasFeatures)
      65          26 :             break;
      66             :     }
      67             : 
      68         127 :     CSLDestroy(papszFiles);
      69             : 
      70         127 :     return bHasMeta && bHasGraph && bHasFeatures;
      71             : }
      72             : 
      73          13 : static GDALDataset *GNMFileDriverOpen(GDALOpenInfo *poOpenInfo)
      74             : 
      75             : {
      76          13 :     if (!GNMFileDriverIdentify(poOpenInfo))
      77           0 :         return nullptr;
      78             : 
      79          13 :     GNMFileNetwork *poFN = new GNMFileNetwork();
      80             : 
      81          13 :     if (poFN->Open(poOpenInfo) != CE_None)
      82             :     {
      83           0 :         delete poFN;
      84           0 :         poFN = nullptr;
      85             :     }
      86             : 
      87          13 :     return poFN;
      88             : }
      89             : 
      90             : static GDALDataset *
      91           2 : GNMFileDriverCreate(const char *pszName, CPL_UNUSED int nBands,
      92             :                     CPL_UNUSED int nXSize, CPL_UNUSED int nYSize,
      93             :                     CPL_UNUSED GDALDataType eDT, char **papszOptions)
      94             : {
      95           2 :     CPLAssert(nullptr != pszName);
      96           2 :     CPLDebug("GNM", "Attempt to create network at: %s", pszName);
      97             : 
      98           2 :     GNMFileNetwork *poFN = new GNMFileNetwork();
      99             : 
     100           2 :     if (poFN->Create(pszName, papszOptions) != CE_None)
     101             :     {
     102           0 :         delete poFN;
     103           0 :         poFN = nullptr;
     104             :     }
     105             : 
     106           2 :     return poFN;
     107             : }
     108             : 
     109           1 : static CPLErr GNMFileDriverDelete(const char *pszDataSource)
     110             : 
     111             : {
     112           2 :     GDALOpenInfo oOpenInfo(pszDataSource, GA_Update);
     113           2 :     GNMFileNetwork oFN;
     114             : 
     115           1 :     if (oFN.Open(&oOpenInfo) != CE_None)
     116             :     {
     117           0 :         return CE_Failure;
     118             :     }
     119             : 
     120           1 :     return oFN.Delete();
     121             : }
     122             : 
     123        1520 : void RegisterGNMFile()
     124             : {
     125        1520 :     if (GDALGetDriverByName("GNMFile") == nullptr)
     126             :     {
     127        1219 :         GDALDriver *poDriver = new GDALDriver();
     128             : 
     129        1219 :         poDriver->SetDescription("GNMFile");
     130        1219 :         poDriver->SetMetadataItem(GDAL_DCAP_GNM, "YES");
     131        1219 :         poDriver->SetMetadataItem(GDAL_DMD_LONGNAME,
     132             :                                   "Geographic Network generic file based "
     133        1219 :                                   "model");
     134             : 
     135        1219 :         poDriver->SetMetadataItem(
     136             :             GDAL_DMD_CREATIONOPTIONLIST,
     137             :             CPLSPrintf(
     138             :                 "<CreationOptionList>"
     139             :                 "  <Option name='%s' type='string' description='The network "
     140             :                 "name. Also it will be a folder name, so the limits for folder "
     141             :                 "name distribute on network name'/>"
     142             :                 "  <Option name='%s' type='string' description='The network "
     143             :                 "description. Any text describes the network'/>"
     144             :                 "  <Option name='%s' type='string' description='The network "
     145             :                 "Spatial reference. All network features will reproject to "
     146             :                 "this spatial reference. May be a WKT text or EPSG code'/>"
     147             :                 "  <Option name='FORMAT' type='string' description='The OGR "
     148             :                 "format to store network data.' default='%s'/>"
     149             :                 "  <Option name='OVERWRITE' type='boolean' "
     150             :                 "description='Overwrite exist network or not' default='NO'/>"
     151             :                 "</CreationOptionList>",
     152             :                 GNM_MD_NAME, GNM_MD_DESCR, GNM_MD_SRS,
     153        1219 :                 GNM_MD_DEFAULT_FILE_FORMAT));
     154             : 
     155        1219 :         poDriver->SetMetadataItem(GDAL_DS_LAYER_CREATIONOPTIONLIST,
     156        1219 :                                   "<LayerCreationOptionList/>");
     157        1219 :         poDriver->pfnOpen = GNMFileDriverOpen;
     158        1219 :         poDriver->pfnIdentify = GNMFileDriverIdentify;
     159        1219 :         poDriver->pfnCreate = GNMFileDriverCreate;
     160        1219 :         poDriver->pfnDelete = GNMFileDriverDelete;
     161             : 
     162        1219 :         GetGDALDriverManager()->RegisterDriver(poDriver);
     163             :     }
     164        1520 : }

Generated by: LCOV version 1.14