Coverage Report

Created: 2025-06-13 06:18

/src/gdal/gnm/gnm_frmts/file/gnmfiledriver.cpp
Line
Count
Source (jump to first uncovered line)
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
static int GNMFileDriverIdentify(GDALOpenInfo *poOpenInfo)
36
37
0
{
38
0
    if (!poOpenInfo->bIsDirectory)
39
0
        return FALSE;
40
0
    if ((poOpenInfo->nOpenFlags & GDAL_OF_GNM) == 0)
41
0
        return FALSE;
42
43
0
    char **papszFiles = VSIReadDir(poOpenInfo->pszFilename);
44
0
    if (CSLCount(papszFiles) == 0)
45
0
    {
46
0
        return FALSE;
47
0
    }
48
49
0
    bool bHasMeta(false), bHasGraph(false), bHasFeatures(false);
50
51
    // search for base GNM files
52
0
    for (int i = 0; papszFiles[i] != nullptr; i++)
53
0
    {
54
0
        if (EQUAL(papszFiles[i], ".") || EQUAL(papszFiles[i], ".."))
55
0
            continue;
56
57
0
        const CPLString osBasename = CPLGetBasenameSafe(papszFiles[i]);
58
0
        if (EQUAL(osBasename, GNM_SYSLAYER_META))
59
0
            bHasMeta = true;
60
0
        else if (EQUAL(osBasename, GNM_SYSLAYER_GRAPH))
61
0
            bHasGraph = true;
62
0
        else if (EQUAL(osBasename, GNM_SYSLAYER_FEATURES))
63
0
            bHasFeatures = true;
64
65
0
        if (bHasMeta && bHasGraph && bHasFeatures)
66
0
            break;
67
0
    }
68
69
0
    CSLDestroy(papszFiles);
70
71
0
    return bHasMeta && bHasGraph && bHasFeatures;
72
0
}
73
74
static GDALDataset *GNMFileDriverOpen(GDALOpenInfo *poOpenInfo)
75
76
0
{
77
0
    if (!GNMFileDriverIdentify(poOpenInfo))
78
0
        return nullptr;
79
80
0
    GNMFileNetwork *poFN = new GNMFileNetwork();
81
82
0
    if (poFN->Open(poOpenInfo) != CE_None)
83
0
    {
84
0
        delete poFN;
85
0
        poFN = nullptr;
86
0
    }
87
88
0
    return poFN;
89
0
}
90
91
static GDALDataset *
92
GNMFileDriverCreate(const char *pszName, CPL_UNUSED int nBands,
93
                    CPL_UNUSED int nXSize, CPL_UNUSED int nYSize,
94
                    CPL_UNUSED GDALDataType eDT, char **papszOptions)
95
0
{
96
0
    CPLAssert(nullptr != pszName);
97
0
    CPLDebug("GNM", "Attempt to create network at: %s", pszName);
98
99
0
    GNMFileNetwork *poFN = new GNMFileNetwork();
100
101
0
    if (poFN->Create(pszName, papszOptions) != CE_None)
102
0
    {
103
0
        delete poFN;
104
0
        poFN = nullptr;
105
0
    }
106
107
0
    return poFN;
108
0
}
109
110
static CPLErr GNMFileDriverDelete(const char *pszDataSource)
111
112
0
{
113
0
    GDALOpenInfo oOpenInfo(pszDataSource, GA_Update);
114
0
    GNMFileNetwork oFN;
115
116
0
    if (oFN.Open(&oOpenInfo) != CE_None)
117
0
    {
118
0
        return CE_Failure;
119
0
    }
120
121
0
    return oFN.Delete();
122
0
}
123
124
void RegisterGNMFile()
125
0
{
126
0
    if (GDALGetDriverByName("GNMFile") == nullptr)
127
0
    {
128
0
        GDALDriver *poDriver = new GDALDriver();
129
130
0
        poDriver->SetDescription("GNMFile");
131
0
        poDriver->SetMetadataItem(GDAL_DCAP_GNM, "YES");
132
0
        poDriver->SetMetadataItem(GDAL_DMD_LONGNAME,
133
0
                                  "Geographic Network generic file based "
134
0
                                  "model");
135
136
0
        poDriver->SetMetadataItem(
137
0
            GDAL_DMD_CREATIONOPTIONLIST,
138
0
            CPLSPrintf(
139
0
                "<CreationOptionList>"
140
0
                "  <Option name='%s' type='string' description='The network "
141
0
                "name. Also it will be a folder name, so the limits for folder "
142
0
                "name distribute on network name'/>"
143
0
                "  <Option name='%s' type='string' description='The network "
144
0
                "description. Any text describes the network'/>"
145
0
                "  <Option name='%s' type='string' description='The network "
146
0
                "Spatial reference. All network features will reproject to "
147
0
                "this spatial reference. May be a WKT text or EPSG code'/>"
148
0
                "  <Option name='FORMAT' type='string' description='The OGR "
149
0
                "format to store network data.' default='%s'/>"
150
0
                "  <Option name='OVERWRITE' type='boolean' "
151
0
                "description='Overwrite exist network or not' default='NO'/>"
152
0
                "</CreationOptionList>",
153
0
                GNM_MD_NAME, GNM_MD_DESCR, GNM_MD_SRS,
154
0
                GNM_MD_DEFAULT_FILE_FORMAT));
155
156
0
        poDriver->SetMetadataItem(GDAL_DS_LAYER_CREATIONOPTIONLIST,
157
0
                                  "<LayerCreationOptionList/>");
158
0
        poDriver->pfnOpen = GNMFileDriverOpen;
159
0
        poDriver->pfnIdentify = GNMFileDriverIdentify;
160
0
        poDriver->pfnCreate = GNMFileDriverCreate;
161
0
        poDriver->pfnDelete = GNMFileDriverDelete;
162
163
0
        GetGDALDriverManager()->RegisterDriver(poDriver);
164
0
    }
165
0
}