Coverage Report

Created: 2025-06-13 06:29

/src/gdal/gnm/gnm_frmts/db/gnmdbdriver.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 "gnm_frmts.h"
32
#include "gnm_priv.h"
33
#include "gnmdb.h"
34
35
static int GNMDBDriverIdentify(GDALOpenInfo *poOpenInfo)
36
37
0
{
38
0
    if (!STARTS_WITH_CI(poOpenInfo->pszFilename, "PGB:") &&
39
0
        !STARTS_WITH_CI(poOpenInfo->pszFilename, "PG:"))
40
0
        return FALSE;
41
0
    if ((poOpenInfo->nOpenFlags & GDAL_OF_GNM) == 0)
42
0
        return FALSE;
43
44
    // TODO: do we need to open datasource end check tables/layer exist?
45
46
0
    auto poDriver = GetGDALDriverManager()->GetDriverByName("PostgreSQL");
47
0
    return poDriver && !poDriver->GetMetadataItem("MISSING_PLUGIN_FILENAME");
48
0
}
49
50
static GDALDataset *GNMDBDriverOpen(GDALOpenInfo *poOpenInfo)
51
52
0
{
53
0
    if (!GNMDBDriverIdentify(poOpenInfo))
54
0
        return nullptr;
55
56
0
    GNMDatabaseNetwork *poFN = new GNMDatabaseNetwork();
57
58
0
    if (poFN->Open(poOpenInfo) != CE_None)
59
0
    {
60
0
        delete poFN;
61
0
        poFN = nullptr;
62
0
    }
63
64
0
    return poFN;
65
0
}
66
67
static GDALDataset *
68
GNMDBDriverCreate(const char *pszName, CPL_UNUSED int nBands,
69
                  CPL_UNUSED int nXSize, CPL_UNUSED int nYSize,
70
                  CPL_UNUSED GDALDataType eDT, char **papszOptions)
71
0
{
72
0
    CPLAssert(nullptr != pszName);
73
0
    CPLDebug("GNM", "Attempt to create network at: %s", pszName);
74
75
0
    GNMDatabaseNetwork *poFN = new GNMDatabaseNetwork();
76
77
0
    if (poFN->Create(pszName, papszOptions) != CE_None)
78
0
    {
79
0
        delete poFN;
80
0
        poFN = nullptr;
81
0
    }
82
83
0
    return poFN;
84
0
}
85
86
static CPLErr GNMDBDriverDelete(const char *pszDataSource)
87
88
0
{
89
0
    GDALOpenInfo oOpenInfo(pszDataSource, GA_Update);
90
0
    GNMDatabaseNetwork *poFN = new GNMDatabaseNetwork();
91
92
0
    if (poFN->Open(&oOpenInfo) != CE_None)
93
0
    {
94
0
        delete poFN;
95
0
        poFN = nullptr;
96
97
0
        return CE_Failure;
98
0
    }
99
100
0
    return poFN->Delete();
101
0
}
102
103
void RegisterGNMDatabase()
104
0
{
105
0
    if (GDALGetDriverByName("GNMDatabase") == nullptr)
106
0
    {
107
0
        GDALDriver *poDriver = new GDALDriver();
108
109
0
        poDriver->SetDescription("GNMDatabase");
110
0
        poDriver->SetMetadataItem(GDAL_DCAP_GNM, "YES");
111
0
        poDriver->SetMetadataItem(GDAL_DMD_LONGNAME,
112
0
                                  "Geographic Network generic DB based "
113
0
                                  "model");
114
115
0
        poDriver->SetMetadataItem(
116
0
            GDAL_DMD_CREATIONOPTIONLIST,
117
0
            CPLSPrintf(
118
0
                "<CreationOptionList>"
119
0
                "  <Option name='%s' type='string' description='The network "
120
0
                "name. Also it will be a folder name, so the limits for folder "
121
0
                "name distribute on network name'/>"
122
0
                "  <Option name='%s' type='string' description='The network "
123
0
                "description. Any text describes the network'/>"
124
0
                "  <Option name='%s' type='string' description='The network "
125
0
                "Spatial reference. All network features will reproject to "
126
0
                "this spatial reference. May be a WKT text or EPSG code'/>"
127
0
                "  <Option name='FORMAT' type='string' description='The OGR "
128
0
                "format to store network data.'/>"
129
0
                "  <Option name='OVERWRITE' type='boolean' "
130
0
                "description='Overwrite exist network or not' default='NO'/>"
131
0
                "</CreationOptionList>",
132
0
                GNM_MD_NAME, GNM_MD_DESCR, GNM_MD_SRS));
133
134
0
        poDriver->SetMetadataItem(GDAL_DS_LAYER_CREATIONOPTIONLIST,
135
0
                                  "<LayerCreationOptionList/>");
136
0
        poDriver->pfnOpen = GNMDBDriverOpen;
137
0
        poDriver->pfnIdentify = GNMDBDriverIdentify;
138
0
        poDriver->pfnCreate = GNMDBDriverCreate;
139
0
        poDriver->pfnDelete = GNMDBDriverDelete;
140
141
0
        GetGDALDriverManager()->RegisterDriver(poDriver);
142
0
    }
143
0
}