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 "gnm_frmts.h" 32 : #include "gnm_priv.h" 33 : #include "gnmdb.h" 34 : 35 49658 : static int GNMDBDriverIdentify(GDALOpenInfo *poOpenInfo) 36 : 37 : { 38 49658 : if (!STARTS_WITH_CI(poOpenInfo->pszFilename, "PGB:") && 39 49658 : !STARTS_WITH_CI(poOpenInfo->pszFilename, "PG:")) 40 49658 : 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 : } 49 : 50 0 : static GDALDataset *GNMDBDriverOpen(GDALOpenInfo *poOpenInfo) 51 : 52 : { 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 : { 60 0 : delete poFN; 61 0 : poFN = nullptr; 62 : } 63 : 64 0 : return poFN; 65 : } 66 : 67 : static GDALDataset * 68 0 : 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 : { 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 : { 79 0 : delete poFN; 80 0 : poFN = nullptr; 81 : } 82 : 83 0 : return poFN; 84 : } 85 : 86 0 : static CPLErr GNMDBDriverDelete(const char *pszDataSource) 87 : 88 : { 89 0 : GDALOpenInfo oOpenInfo(pszDataSource, GA_Update); 90 0 : GNMDatabaseNetwork *poFN = new GNMDatabaseNetwork(); 91 : 92 0 : if (poFN->Open(&oOpenInfo) != CE_None) 93 : { 94 0 : delete poFN; 95 0 : poFN = nullptr; 96 : 97 0 : return CE_Failure; 98 : } 99 : 100 0 : return poFN->Delete(); 101 : } 102 : 103 1935 : void RegisterGNMDatabase() 104 : { 105 1935 : if (GDALGetDriverByName("GNMDatabase") == nullptr) 106 : { 107 1653 : GDALDriver *poDriver = new GDALDriver(); 108 : 109 1653 : poDriver->SetDescription("GNMDatabase"); 110 1653 : poDriver->SetMetadataItem(GDAL_DCAP_GNM, "YES"); 111 1653 : poDriver->SetMetadataItem(GDAL_DMD_LONGNAME, 112 : "Geographic Network generic DB based " 113 1653 : "model"); 114 : 115 1653 : poDriver->SetMetadataItem( 116 : GDAL_DMD_CREATIONOPTIONLIST, 117 : CPLSPrintf( 118 : "<CreationOptionList>" 119 : " <Option name='%s' type='string' description='The network " 120 : "name. Also it will be a folder name, so the limits for folder " 121 : "name distribute on network name'/>" 122 : " <Option name='%s' type='string' description='The network " 123 : "description. Any text describes the network'/>" 124 : " <Option name='%s' type='string' description='The network " 125 : "Spatial reference. All network features will reproject to " 126 : "this spatial reference. May be a WKT text or EPSG code'/>" 127 : " <Option name='FORMAT' type='string' description='The OGR " 128 : "format to store network data.'/>" 129 : " <Option name='OVERWRITE' type='boolean' " 130 : "description='Overwrite exist network or not' default='NO'/>" 131 : "</CreationOptionList>", 132 1653 : GNM_MD_NAME, GNM_MD_DESCR, GNM_MD_SRS)); 133 : 134 1653 : poDriver->SetMetadataItem(GDAL_DS_LAYER_CREATIONOPTIONLIST, 135 1653 : "<LayerCreationOptionList/>"); 136 1653 : poDriver->pfnOpen = GNMDBDriverOpen; 137 1653 : poDriver->pfnIdentify = GNMDBDriverIdentify; 138 1653 : poDriver->pfnCreate = GNMDBDriverCreate; 139 1653 : poDriver->pfnDelete = GNMDBDriverDelete; 140 : 141 1653 : GetGDALDriverManager()->RegisterDriver(poDriver); 142 : } 143 1935 : }