1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
/******************************************************************************
* $Id: ogrpdsdatasource.cpp 23042 2011-09-04 15:07:22Z rouault $
*
* Project: PDS Translator
* Purpose: Implements OGRPDSDataSource class
* Author: Even Rouault, even dot rouault at mines dash paris dot org
*
******************************************************************************
* Copyright (c) 2010, Even Rouault <even dot rouault at mines dash paris dot org>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "ogr_pds.h"
#include "cpl_conv.h"
#include "cpl_string.h"
CPL_CVSID("$Id: ogrpdsdatasource.cpp 23042 2011-09-04 15:07:22Z rouault $");
/************************************************************************/
/* OGRPDSDataSource() */
/************************************************************************/
OGRPDSDataSource::OGRPDSDataSource()
{
papoLayers = NULL;
nLayers = 0;
pszName = NULL;
}
/************************************************************************/
/* ~OGRPDSDataSource() */
/************************************************************************/
OGRPDSDataSource::~OGRPDSDataSource()
{
for( int i = 0; i < nLayers; i++ )
delete papoLayers[i];
CPLFree( papoLayers );
CPLFree( pszName );
}
/************************************************************************/
/* TestCapability() */
/************************************************************************/
int OGRPDSDataSource::TestCapability( const char * pszCap )
{
return FALSE;
}
/************************************************************************/
/* GetLayer() */
/************************************************************************/
OGRLayer *OGRPDSDataSource::GetLayer( int iLayer )
{
if( iLayer < 0 || iLayer >= nLayers )
return NULL;
else
return papoLayers[iLayer];
}
/************************************************************************/
/* GetKeywordSub() */
/************************************************************************/
const char * OGRPDSDataSource::GetKeywordSub( const char *pszPath,
int iSubscript,
const char *pszDefault )
{
const char *pszResult = oKeywords.GetKeyword( pszPath, NULL );
if( pszResult == NULL )
return pszDefault;
if( pszResult[0] != '(' )
return pszDefault;
char **papszTokens = CSLTokenizeString2( pszResult, "(,)",
CSLT_HONOURSTRINGS );
if( iSubscript <= CSLCount(papszTokens) )
{
osTempResult = papszTokens[iSubscript-1];
CSLDestroy( papszTokens );
return osTempResult.c_str();
}
else
{
CSLDestroy( papszTokens );
return pszDefault;
}
}
/************************************************************************/
/* CleanString() */
/* */
/* Removes single or double quotes, and converts spaces to underscores. */
/* The change is made in-place to CPLString. */
/************************************************************************/
void OGRPDSDataSource::CleanString( CPLString &osInput )
{
if( ( osInput.size() < 2 ) ||
((osInput.at(0) != '"' || osInput.at(osInput.size()-1) != '"' ) &&
( osInput.at(0) != '\'' || osInput.at(osInput.size()-1) != '\'')) )
return;
char *pszWrk = CPLStrdup(osInput.c_str() + 1);
int i;
pszWrk[strlen(pszWrk)-1] = '\0';
for( i = 0; pszWrk[i] != '\0'; i++ )
{
if( pszWrk[i] == ' ' )
pszWrk[i] = '_';
}
osInput = pszWrk;
CPLFree( pszWrk );
}
/************************************************************************/
/* LoadTable() */
/************************************************************************/
static CPLString MakeAttr(CPLString os1, CPLString os2)
{
return os1 + "." + os2;
}
int OGRPDSDataSource::LoadTable(const char* pszFilename,
int nRecordSize,
CPLString osTableID )
{
CPLString osTableFilename;
int nStartBytes;
CPLString osTableLink = "^";
osTableLink += osTableID;
CPLString osTable = oKeywords.GetKeyword( osTableLink, "" );
if( osTable[0] == '(' )
{
osTableFilename = GetKeywordSub(osTableLink, 1, "");
CPLString osStartRecord = GetKeywordSub(osTableLink, 2, "");
nStartBytes = (atoi(osStartRecord.c_str()) - 1) * nRecordSize;
if (osTableFilename.size() == 0 || osStartRecord.size() == 0 ||
nStartBytes < 0)
{
CPLError(CE_Failure, CPLE_NotSupported,
"Cannot parse %s line", osTableLink.c_str());
return FALSE;
}
CPLString osTPath = CPLGetPath(pszFilename);
CleanString( osTableFilename );
osTableFilename = CPLFormCIFilename( osTPath, osTableFilename, NULL );
}
else
{
osTableFilename = oKeywords.GetKeyword( osTableLink, "" );
if (osTableFilename.size() != 0 && osTableFilename[0] >= '0' &&
osTableFilename[0] <= '9')
{
nStartBytes = atoi(osTableFilename.c_str()) - 1;
if (strstr(osTableFilename.c_str(), "<BYTES>") == NULL)
nStartBytes *= nRecordSize;
osTableFilename = pszFilename;
}
else
{
CPLString osTPath = CPLGetPath(pszFilename);
CleanString( osTableFilename );
osTableFilename = CPLFormCIFilename( osTPath, osTableFilename, NULL );
nStartBytes = 0;
}
}
CPLString osTableName = oKeywords.GetKeyword( MakeAttr(osTableID, "NAME"), "" );
if (osTableName.size() == 0)
{
if (GetLayerByName(osTableID.c_str()) == NULL)
osTableName = osTableID;
else
osTableName = CPLSPrintf("Layer_%d", nLayers+1);
}
CleanString(osTableName);
CPLString osTableInterchangeFormat =
oKeywords.GetKeyword( MakeAttr(osTableID, "INTERCHANGE_FORMAT"), "" );
CPLString osTableRows = oKeywords.GetKeyword( MakeAttr(osTableID, "ROWS"), "" );
int nRecords = atoi(osTableRows);
if (osTableInterchangeFormat.size() == 0 ||
osTableRows.size() == 0 || nRecords < 0)
{
CPLError(CE_Failure, CPLE_NotSupported,
"One of TABLE.INTERCHANGE_FORMAT or TABLE.ROWS is missing");
return FALSE;
}
CleanString(osTableInterchangeFormat);
if (osTableInterchangeFormat.compare("ASCII") != 0 &&
osTableInterchangeFormat.compare("BINARY") != 0)
{
CPLError(CE_Failure, CPLE_NotSupported,
"Only INTERCHANGE_FORMAT=ASCII or BINARY is supported");
return FALSE;
}
VSILFILE* fp = VSIFOpenL(osTableFilename, "rb");
if (fp == NULL)
{
CPLError(CE_Failure, CPLE_AppDefined, "Cannot open %s",
osTableFilename.c_str());
return FALSE;
}
CPLString osTableStructure = oKeywords.GetKeyword( MakeAttr(osTableID, "^STRUCTURE"), "" );
if (osTableStructure.size() != 0)
{
CPLString osTPath = CPLGetPath(pszFilename);
CleanString( osTableStructure );
osTableStructure = CPLFormCIFilename( osTPath, osTableStructure, NULL );
}
GByte* pabyRecord = (GByte*) VSIMalloc(nRecordSize + 1);
if (pabyRecord == NULL)
{
VSIFCloseL(fp);
return FALSE;
}
pabyRecord[nRecordSize] = 0;
papoLayers = (OGRLayer**) CPLRealloc(papoLayers, (nLayers + 1) * sizeof(OGRLayer*));
papoLayers[nLayers] = new OGRPDSLayer(osTableID, osTableName, fp,
pszFilename,
osTableStructure,
nRecords, nStartBytes,
nRecordSize, pabyRecord,
osTableInterchangeFormat.compare("ASCII") == 0);
nLayers++;
return TRUE;
}
/************************************************************************/
/* Open() */
/************************************************************************/
int OGRPDSDataSource::Open( const char * pszFilename, int bUpdateIn)
{
if (bUpdateIn)
{
return FALSE;
}
pszName = CPLStrdup( pszFilename );
// --------------------------------------------------------------------
// Does this appear to be a .PDS table file?
// --------------------------------------------------------------------
VSILFILE* fp = VSIFOpenL(pszFilename, "rb");
if (fp == NULL)
return FALSE;
char szBuffer[512];
int nbRead = (int)VSIFReadL(szBuffer, 1, sizeof(szBuffer) - 1, fp);
szBuffer[nbRead] = '\0';
const char* pszPos = strstr(szBuffer, "PDS_VERSION_ID");
int bIsPDS = (pszPos != NULL);
if (!bIsPDS)
{
VSIFCloseL(fp);
return FALSE;
}
if (!oKeywords.Ingest(fp, pszPos - szBuffer))
{
VSIFCloseL(fp);
return FALSE;
}
VSIFCloseL(fp);
CPLString osRecordType = oKeywords.GetKeyword( "RECORD_TYPE", "" );
CPLString osFileRecords = oKeywords.GetKeyword( "FILE_RECORDS", "" );
CPLString osRecordBytes = oKeywords.GetKeyword( "RECORD_BYTES", "" );
int nRecordSize = atoi(osRecordBytes);
if (osRecordType.size() == 0 || osFileRecords.size() == 0 ||
osRecordBytes.size() == 0 || nRecordSize <= 0)
{
CPLError(CE_Failure, CPLE_NotSupported,
"One of RECORD_TYPE, FILE_RECORDS or RECORD_BYTES is missing");
return FALSE;
}
CleanString(osRecordType);
if (osRecordType.compare("FIXED_LENGTH") != 0)
{
CPLError(CE_Failure, CPLE_NotSupported,
"Only RECORD_TYPE=FIXED_LENGTH is supported");
return FALSE;
}
CPLString osTable = oKeywords.GetKeyword( "^TABLE", "" );
if (osTable.size() != 0)
LoadTable(pszFilename, nRecordSize, "TABLE");
else
{
VSILFILE* fp = VSIFOpenL(pszFilename, "rb");
if (fp == NULL)
return FALSE;
while(TRUE)
{
CPLPushErrorHandler(CPLQuietErrorHandler);
const char* pszLine = CPLReadLine2L(fp, 256, NULL);
CPLPopErrorHandler();
CPLErrorReset();
if (pszLine == NULL)
break;
char** papszTokens =
CSLTokenizeString2( pszLine, " =", CSLT_HONOURSTRINGS );
int nTokens = CSLCount(papszTokens);
if (nTokens == 2 &&
papszTokens[0][0] == '^' &&
strstr(papszTokens[0], "TABLE") != NULL)
{
LoadTable(pszFilename, nRecordSize, papszTokens[0] + 1);
}
CSLDestroy(papszTokens);
papszTokens = NULL;
}
VSIFCloseL(fp);
}
return nLayers != 0;
}
|