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