Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: S-57 Translator 4 : * Purpose: Implements OGRS57Driver 5 : * Author: Frank Warmerdam, warmerdam@pobox.com 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 1999, Frank Warmerdam 9 : * Copyright (c) 2013, Even Rouault <even dot rouault at spatialys.com> 10 : * 11 : * SPDX-License-Identifier: MIT 12 : ****************************************************************************/ 13 : 14 : #include "ogr_s57.h" 15 : #include "cpl_conv.h" 16 : #include "cpl_multiproc.h" 17 : 18 : S57ClassRegistrar *OGRS57Driver::poRegistrar = nullptr; 19 : static CPLMutex *hS57RegistrarMutex = nullptr; 20 : 21 : /************************************************************************/ 22 : /* OGRS57Driver() */ 23 : /************************************************************************/ 24 : 25 1776 : OGRS57Driver::OGRS57Driver() 26 : { 27 1776 : } 28 : 29 : /************************************************************************/ 30 : /* ~OGRS57Driver() */ 31 : /************************************************************************/ 32 : 33 2252 : OGRS57Driver::~OGRS57Driver() 34 : 35 : { 36 1126 : if (poRegistrar != nullptr) 37 : { 38 2 : delete poRegistrar; 39 2 : poRegistrar = nullptr; 40 : } 41 : 42 1126 : if (hS57RegistrarMutex != nullptr) 43 : { 44 2 : CPLDestroyMutex(hS57RegistrarMutex); 45 2 : hS57RegistrarMutex = nullptr; 46 : } 47 2252 : } 48 : 49 : /************************************************************************/ 50 : /* OGRS57DriverIdentify() */ 51 : /************************************************************************/ 52 : 53 59518 : static int OGRS57DriverIdentify(GDALOpenInfo *poOpenInfo) 54 : 55 : { 56 59518 : if (poOpenInfo->nHeaderBytes < 10) 57 53102 : return false; 58 6416 : const char *pachLeader = reinterpret_cast<char *>(poOpenInfo->pabyHeader); 59 6416 : if ((pachLeader[5] != '1' && pachLeader[5] != '2' && 60 5871 : pachLeader[5] != '3') || 61 619 : pachLeader[6] != 'L' || (pachLeader[8] != '1' && pachLeader[8] != ' ')) 62 : { 63 6342 : return false; 64 : } 65 : // Test for S-57 DSID field structure (to distinguish it from S-101) 66 148 : return strstr(pachLeader, "DSID") != nullptr && 67 74 : (strstr(pachLeader, 68 : "RCNM!RCID!EXPP!INTU!DSNM!EDTN!UPDN!UADT!ISDT!" 69 4 : "STED!PRSP!PSDN!PRED!PROF!AGEN!COMT") != nullptr || 70 : // Below is for autotest/ogr/data/s57/fake_s57.000 fake dataset that has a shortened structure 71 78 : strstr(pachLeader, "RCNM!RCID!EXPP!xxxx") != nullptr); 72 : } 73 : 74 : /************************************************************************/ 75 : /* Open() */ 76 : /************************************************************************/ 77 : 78 37 : GDALDataset *OGRS57Driver::Open(GDALOpenInfo *poOpenInfo) 79 : 80 : { 81 37 : if (!OGRS57DriverIdentify(poOpenInfo)) 82 0 : return nullptr; 83 : 84 37 : OGRS57DataSource *poDS = new OGRS57DataSource(poOpenInfo->papszOpenOptions); 85 37 : if (!poDS->Open(poOpenInfo->pszFilename)) 86 : { 87 0 : delete poDS; 88 0 : poDS = nullptr; 89 : } 90 : 91 37 : if (poDS && poOpenInfo->eAccess == GA_Update) 92 : { 93 0 : delete poDS; 94 0 : CPLError(CE_Failure, CPLE_OpenFailed, 95 : "S57 Driver doesn't support update."); 96 0 : return nullptr; 97 : } 98 : 99 37 : return poDS; 100 : } 101 : 102 : /************************************************************************/ 103 : /* Create() */ 104 : /************************************************************************/ 105 : 106 19 : GDALDataset *OGRS57Driver::Create(const char *pszName, int /* nBands */, 107 : int /* nXSize */, int /* nYSize */, 108 : GDALDataType /* eDT */, 109 : CSLConstList papszOptions) 110 : { 111 19 : OGRS57DataSource *poDS = new OGRS57DataSource(); 112 : 113 19 : if (poDS->Create(pszName, papszOptions)) 114 18 : return poDS; 115 : 116 1 : delete poDS; 117 1 : return nullptr; 118 : } 119 : 120 : /************************************************************************/ 121 : /* GetS57Registrar() */ 122 : /************************************************************************/ 123 : 124 5447 : S57ClassRegistrar *OGRS57Driver::GetS57Registrar() 125 : 126 : { 127 : /* -------------------------------------------------------------------- */ 128 : /* Instantiate the class registrar if possible. */ 129 : /* -------------------------------------------------------------------- */ 130 5447 : CPLMutexHolderD(&hS57RegistrarMutex); 131 : 132 5447 : if (poRegistrar == nullptr) 133 : { 134 3 : poRegistrar = new S57ClassRegistrar(); 135 : 136 3 : if (!poRegistrar->LoadInfo(nullptr, nullptr, false)) 137 : { 138 0 : delete poRegistrar; 139 0 : poRegistrar = nullptr; 140 : } 141 : } 142 : 143 10894 : return poRegistrar; 144 : } 145 : 146 : /************************************************************************/ 147 : /* RegisterOGRS57() */ 148 : /************************************************************************/ 149 : 150 2059 : void RegisterOGRS57() 151 : 152 : { 153 2059 : if (GDALGetDriverByName("S57") != nullptr) 154 283 : return; 155 : 156 1776 : GDALDriver *poDriver = new OGRS57Driver(); 157 : 158 1776 : poDriver->SetDescription("S57"); 159 1776 : poDriver->SetMetadataItem(GDAL_DCAP_VECTOR, "YES"); 160 1776 : poDriver->SetMetadataItem(GDAL_DMD_LONGNAME, "IHO S-57 (ENC)"); 161 1776 : poDriver->SetMetadataItem(GDAL_DMD_EXTENSION, "000"); 162 1776 : poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/vector/s57.html"); 163 1776 : poDriver->SetMetadataItem(GDAL_DMD_SUPPORTED_SQL_DIALECTS, "OGRSQL SQLITE"); 164 : 165 1776 : poDriver->SetMetadataItem( 166 : GDAL_DMD_OPENOPTIONLIST, 167 : "<OpenOptionList>" 168 : " <Option name='" S57O_UPDATES 169 : "' type='string-select' description='Should update files be " 170 : "incorporated into the base data on the fly' default='APPLY'>" 171 : " <Value>APPLY</Value>" 172 : " <Value>IGNORE</Value>" 173 : " </Option>" 174 : " <Option name='" S57O_SPLIT_MULTIPOINT 175 : "' type='boolean' description='Should multipoint soundings be split " 176 : "into many single point sounding features' default='NO'/>" 177 : " <Option name='" S57O_ADD_SOUNDG_DEPTH 178 : "' type='boolean' description='Should a DEPTH attribute be added on " 179 : "SOUNDG features and assign the depth of the sounding' default='NO'/>" 180 : " <Option name='" S57O_RETURN_PRIMITIVES 181 : "' type='boolean' description='Should all the low level geometry " 182 : "primitives be returned as special IsolatedNode, ConnectedNode, Edge " 183 : "and Face layers' default='NO'/>" 184 : " <Option name='" S57O_PRESERVE_EMPTY_NUMBERS 185 : "' type='boolean' description='If enabled, numeric attributes assigned " 186 : "an empty string as a value will be preserved as a special numeric " 187 : "value' default='NO'/>" 188 : " <Option name='" S57O_LNAM_REFS 189 : "' type='boolean' description='Should LNAM and LNAM_REFS fields be " 190 : "attached to features capturing the feature to feature relationships " 191 : "in the FFPT group of the S-57 file' default='NO'/>" 192 : " <Option name='" S57O_RETURN_LINKAGES 193 : "' type='boolean' description='Should additional attributes relating " 194 : "features to their underlying geometric primitives be attached' " 195 : "default='NO'/>" 196 : " <Option name='" S57O_RECODE_BY_DSSI 197 : "' type='boolean' description='Should attribute values be recoded to " 198 : "UTF-8 from the character encoding specified in the S57 DSSI record.' " 199 : "default='YES'/>" 200 : " <Option name='" S57O_LIST_AS_STRING 201 : "' type='boolean' description='Whether attributes tagged as list in " 202 : "S57 dictionaries should be reported as a String field' default='NO'/>" 203 1776 : "</OpenOptionList>"); 204 1776 : poDriver->SetMetadataItem( 205 : GDAL_DMD_CREATIONOPTIONLIST, 206 : "<CreationOptionList>" 207 : " <Option name='S57_EXPP' type='int' description='Exchange purpose' " 208 : "default='1'/>" 209 : " <Option name='S57_INTU' type='int' description='Intended usage' " 210 : "default='4'/>" 211 : " <Option name='S57_EDTN' type='string' description='Edition number' " 212 : "default='2'/>" 213 : " <Option name='S57_UPDN' type='string' description='Update number' " 214 : "default='0'/>" 215 : " <Option name='S57_UADT' type='string' description='Update " 216 : "application date' default='20030801'/>" 217 : " <Option name='S57_ISDT' type='string' description='Issue date' " 218 : "default='20030801'/>" 219 : " <Option name='S57_STED' type='string' description='Edition number " 220 : "of S-57' default='03.1'/>" 221 : " <Option name='S57_AGEN' type='int' description='Producing agency' " 222 : "default='540'/>" 223 : " <Option name='S57_COMT' type='string' description='Comment' " 224 : "default=''/>" 225 : " <Option name='S57_AALL' type='int' description='Lexical level used " 226 : "for the ATTF fields' default='0'/>" 227 : " <Option name='S57_NALL' type='int' description='Lexical level used " 228 : "for the NATF fields' default='0'/>" 229 : " <Option name='S57_NOMR' type='int' description='Number of meta " 230 : "records (objects with acronym starting with \"M_\")' default='0'/>" 231 : " <Option name='S57_NOGR' type='int' description='Number of geo " 232 : "records' default='0'/>" 233 : " <Option name='S57_NOLR' type='int' description='Number of " 234 : "collection records' default='0'/>" 235 : " <Option name='S57_NOIN' type='int' description='Number of isolated " 236 : "node records' default='0'/>" 237 : " <Option name='S57_NOCN' type='int' description='Number of " 238 : "connected node records' default='0'/>" 239 : " <Option name='S57_NOED' type='int' description='Number of edge " 240 : "records' default='0'/>" 241 : " <Option name='S57_HDAT' type='int' description='Horizontal " 242 : "geodetic datum' default='2'/>" 243 : " <Option name='S57_VDAT' type='int' description='Vertical datum' " 244 : "default='17'/>" 245 : " <Option name='S57_SDAT' type='int' description='Sounding datum' " 246 : "default='23'/>" 247 : " <Option name='S57_CSCL' type='int' description='Compilation scale " 248 : "of data (1:X)' default='52000'/>" 249 : " <Option name='S57_COMF' type='int' description='Floating-point to " 250 : "integer multiplication factor for coordinate values' " 251 : "default='10000000'/>" 252 : " <Option name='S57_SOMF' type='int' description='Floating point to " 253 : "integer multiplication factor for 3-D (sounding) values' " 254 : "default='10'/>" 255 1776 : "</CreationOptionList>"); 256 : 257 1776 : poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES"); 258 1776 : poDriver->SetMetadataItem(GDAL_DCAP_MULTIPLE_VECTOR_LAYERS, "YES"); 259 1776 : poDriver->SetMetadataItem(GDAL_DCAP_Z_GEOMETRIES, "YES"); 260 : 261 1776 : poDriver->pfnOpen = OGRS57Driver::Open; 262 1776 : poDriver->pfnIdentify = OGRS57DriverIdentify; 263 1776 : poDriver->pfnCreate = OGRS57Driver::Create; 264 : 265 1776 : GetGDALDriverManager()->RegisterDriver(poDriver); 266 : }