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 43141 : static int GNMFileDriverIdentify(GDALOpenInfo *poOpenInfo)
36 :
37 : {
38 43141 : if (!poOpenInfo->bIsDirectory)
39 42991 : return FALSE;
40 150 : if ((poOpenInfo->nOpenFlags & GDAL_OF_GNM) == 0)
41 0 : return FALSE;
42 :
43 150 : char **papszFiles = VSIReadDir(poOpenInfo->pszFilename);
44 150 : if (CSLCount(papszFiles) == 0)
45 : {
46 0 : return FALSE;
47 : }
48 :
49 150 : bool bHasMeta(false), bHasGraph(false), bHasFeatures(false);
50 :
51 : // search for base GNM files
52 2960 : for (int i = 0; papszFiles[i] != nullptr; i++)
53 : {
54 2836 : if (EQUAL(papszFiles[i], ".") || EQUAL(papszFiles[i], ".."))
55 52 : continue;
56 :
57 2784 : if (EQUAL(CPLGetBasename(papszFiles[i]), GNM_SYSLAYER_META))
58 26 : bHasMeta = true;
59 2758 : else if (EQUAL(CPLGetBasename(papszFiles[i]), GNM_SYSLAYER_GRAPH))
60 26 : bHasGraph = true;
61 2732 : else if (EQUAL(CPLGetBasename(papszFiles[i]), GNM_SYSLAYER_FEATURES))
62 26 : bHasFeatures = true;
63 :
64 2784 : if (bHasMeta && bHasGraph && bHasFeatures)
65 26 : break;
66 : }
67 :
68 150 : CSLDestroy(papszFiles);
69 :
70 150 : 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 1595 : void RegisterGNMFile()
124 : {
125 1595 : if (GDALGetDriverByName("GNMFile") == nullptr)
126 : {
127 1293 : GDALDriver *poDriver = new GDALDriver();
128 :
129 1293 : poDriver->SetDescription("GNMFile");
130 1293 : poDriver->SetMetadataItem(GDAL_DCAP_GNM, "YES");
131 1293 : poDriver->SetMetadataItem(GDAL_DMD_LONGNAME,
132 : "Geographic Network generic file based "
133 1293 : "model");
134 :
135 1293 : 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 1293 : GNM_MD_DEFAULT_FILE_FORMAT));
154 :
155 1293 : poDriver->SetMetadataItem(GDAL_DS_LAYER_CREATIONOPTIONLIST,
156 1293 : "<LayerCreationOptionList/>");
157 1293 : poDriver->pfnOpen = GNMFileDriverOpen;
158 1293 : poDriver->pfnIdentify = GNMFileDriverIdentify;
159 1293 : poDriver->pfnCreate = GNMFileDriverCreate;
160 1293 : poDriver->pfnDelete = GNMFileDriverDelete;
161 :
162 1293 : GetGDALDriverManager()->RegisterDriver(poDriver);
163 : }
164 1595 : }
|