Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: OGR 4 : * Purpose: OGRNASDriver implementation 5 : * Author: Frank Warmerdam, warmerdam@pobox.com 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2002, Frank Warmerdam <warmerdam@pobox.com> 9 : * 10 : * Permission is hereby granted, free of charge, to any person obtaining a 11 : * copy of this software and associated documentation files (the "Software"), 12 : * to deal in the Software without restriction, including without limitation 13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 : * and/or sell copies of the Software, and to permit persons to whom the 15 : * Software is furnished to do so, subject to the following conditions: 16 : * 17 : * The above copyright notice and this permission notice shall be included 18 : * in all copies or substantial portions of the Software. 19 : * 20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 : * DEALINGS IN THE SOFTWARE. 27 : ****************************************************************************/ 28 : 29 : #include "cpl_conv.h" 30 : #include "cpl_multiproc.h" 31 : #include "nasreaderp.h" 32 : #include "ogr_nas.h" 33 : 34 : /************************************************************************/ 35 : /* OGRNASDriverIdentify() */ 36 : /************************************************************************/ 37 : 38 45218 : static int OGRNASDriverIdentify(GDALOpenInfo *poOpenInfo) 39 : 40 : { 41 45218 : if (poOpenInfo->fpL == nullptr) 42 40984 : return FALSE; 43 : 44 : /* -------------------------------------------------------------------- */ 45 : /* Check for a UTF-8 BOM and skip if found */ 46 : /* */ 47 : /* TODO: BOM is variable-length parameter and depends on encoding. */ 48 : /* Add BOM detection for other encodings. */ 49 : /* -------------------------------------------------------------------- */ 50 : 51 : // Used to skip to actual beginning of XML data 52 : // const char* szPtr = (const char*)poOpenInfo->pabyHeader; 53 4234 : const char *szPtr = reinterpret_cast<char *>(poOpenInfo->pabyHeader); 54 : 55 4234 : if (((unsigned char)szPtr[0] == 0xEF) && 56 6 : ((unsigned char)szPtr[1] == 0xBB) && ((unsigned char)szPtr[2] == 0xBF)) 57 : { 58 6 : szPtr += 3; 59 : } 60 : 61 : /* -------------------------------------------------------------------- */ 62 : /* Here, we expect the opening chevrons of NAS tree root element */ 63 : /* -------------------------------------------------------------------- */ 64 4234 : if (szPtr[0] != '<') 65 3584 : return FALSE; 66 : 67 650 : if (!poOpenInfo->TryToIngest(8192)) 68 0 : return FALSE; 69 650 : szPtr = (const char *)poOpenInfo->pabyHeader; 70 : 71 650 : if (strstr(szPtr, "opengis.net/gml") == nullptr) 72 244 : return FALSE; 73 : 74 406 : char **papszIndicators = CSLTokenizeStringComplex( 75 : CPLGetConfigOption("NAS_INDICATOR", 76 : "NAS-Operationen;AAA-Fachschema;aaa.xsd;aaa-suite"), 77 : ";", 0, 0); 78 : 79 406 : bool bFound = false; 80 2012 : for (int i = 0; papszIndicators[i] && !bFound; i++) 81 : { 82 1606 : bFound = strstr(szPtr, papszIndicators[i]) != nullptr; 83 : } 84 : 85 406 : CSLDestroy(papszIndicators); 86 : 87 : // Require NAS_GFS_TEMPLATE to be defined 88 406 : if (bFound && !CPLGetConfigOption("NAS_GFS_TEMPLATE", nullptr)) 89 : { 90 0 : CPLDebug("NAS", 91 : "This file could be recognized by the NAS driver. " 92 : "If this is desired, you need to define the NAS_GFS_TEMPLATE " 93 : "configuration option."); 94 0 : return FALSE; 95 : } 96 : 97 406 : return bFound; 98 : } 99 : 100 : /************************************************************************/ 101 : /* Open() */ 102 : /************************************************************************/ 103 : 104 3 : static GDALDataset *OGRNASDriverOpen(GDALOpenInfo *poOpenInfo) 105 : 106 : { 107 3 : if (poOpenInfo->eAccess == GA_Update || !OGRNASDriverIdentify(poOpenInfo)) 108 0 : return nullptr; 109 : 110 3 : VSIFCloseL(poOpenInfo->fpL); 111 3 : poOpenInfo->fpL = nullptr; 112 : 113 3 : OGRNASDataSource *poDS = new OGRNASDataSource(); 114 : 115 3 : if (!poDS->Open(poOpenInfo->pszFilename)) 116 : { 117 0 : delete poDS; 118 0 : return nullptr; 119 : } 120 : 121 3 : return poDS; 122 : } 123 : 124 : /************************************************************************/ 125 : /* RegisterOGRNAS() */ 126 : /************************************************************************/ 127 : 128 1511 : void RegisterOGRNAS() 129 : 130 : { 131 1511 : if (GDALGetDriverByName("NAS") != nullptr) 132 295 : return; 133 : 134 1216 : GDALDriver *poDriver = new GDALDriver(); 135 : 136 1216 : poDriver->SetDescription("NAS"); 137 1216 : poDriver->SetMetadataItem(GDAL_DCAP_VECTOR, "YES"); 138 1216 : poDriver->SetMetadataItem(GDAL_DMD_LONGNAME, "NAS - ALKIS"); 139 1216 : poDriver->SetMetadataItem(GDAL_DMD_EXTENSION, "xml"); 140 1216 : poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/vector/nas.html"); 141 1216 : poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES"); 142 1216 : poDriver->SetMetadataItem(GDAL_DMD_SUPPORTED_SQL_DIALECTS, "OGRSQL SQLITE"); 143 : 144 1216 : poDriver->pfnOpen = OGRNASDriverOpen; 145 1216 : poDriver->pfnIdentify = OGRNASDriverIdentify; 146 : 147 1216 : GetGDALDriverManager()->RegisterDriver(poDriver); 148 : }