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
|
/******************************************************************************
* $Id: ogrili1datasource.cpp 23890 2012-02-03 16:30:23Z pka $
*
* Project: Interlis 1 Translator
* Purpose: Implements OGRILI1DataSource class.
* Author: Pirmin Kalberer, Sourcepole AG
*
******************************************************************************
* Copyright (c) 2004, Pirmin Kalberer, Sourcepole AG
*
* 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_ili1.h"
#include "cpl_conv.h"
#include "cpl_string.h"
#include "ili1reader.h"
#include "iomhelper.h"
#include "iom/iom.h"
#include <string>
CPL_CVSID("$Id: ogrili1datasource.cpp 23890 2012-02-03 16:30:23Z pka $");
/************************************************************************/
/* OGRILI1DataSource() */
/************************************************************************/
OGRILI1DataSource::OGRILI1DataSource()
{
pszName = NULL;
poReader = NULL;
fpTransfer = NULL;
pszTopic = NULL;
nLayers = 0;
papoLayers = NULL;
}
/************************************************************************/
/* ~OGRILI1DataSource() */
/************************************************************************/
OGRILI1DataSource::~OGRILI1DataSource()
{
int i;
for(i=0;i<nLayers;i++)
{
delete papoLayers[i];
}
CPLFree( papoLayers );
CPLFree( pszName );
CPLFree( pszTopic );
DestroyILI1Reader( poReader );
if( fpTransfer )
{
VSIFPrintf( fpTransfer, "ETAB\n" );
VSIFPrintf( fpTransfer, "ETOP\n" );
VSIFPrintf( fpTransfer, "EMOD\n" );
VSIFPrintf( fpTransfer, "ENDE\n" );
fclose(fpTransfer);
}
}
/************************************************************************/
/* Open() */
/************************************************************************/
int OGRILI1DataSource::Open( const char * pszNewName, int bTestOpen )
{
FILE *fp;
char szHeader[1000];
std::string osBasename, osModelFilename;
if (strlen(pszNewName) == 0)
{
return FALSE;
}
char **filenames = CSLTokenizeString2( pszNewName, ",", 0 );
osBasename = filenames[0];
if( CSLCount(filenames) > 1 )
osModelFilename = filenames[1];
CSLDestroy( filenames );
/* -------------------------------------------------------------------- */
/* Open the source file. */
/* -------------------------------------------------------------------- */
fp = VSIFOpen( osBasename.c_str(), "r" );
if( fp == NULL )
{
if( !bTestOpen )
CPLError( CE_Failure, CPLE_OpenFailed,
"Failed to open ILI1 file `%s'.",
pszNewName );
return FALSE;
}
/* -------------------------------------------------------------------- */
/* If we aren't sure it is ILI1, load a header chunk and check */
/* for signs it is ILI1 */
/* -------------------------------------------------------------------- */
if( bTestOpen )
{
int nLen = (int)VSIFRead( szHeader, 1, sizeof(szHeader), fp );
if (nLen == sizeof(szHeader))
szHeader[sizeof(szHeader)-1] = '\0';
else
szHeader[nLen] = '\0';
if( strstr(szHeader,"SCNT") == NULL )
{
VSIFClose( fp );
return FALSE;
}
}
/* -------------------------------------------------------------------- */
/* We assume now that it is ILI1. Close and instantiate a */
/* ILI1Reader on it. */
/* -------------------------------------------------------------------- */
VSIFClose( fp );
poReader = CreateILI1Reader();
if( poReader == NULL )
{
CPLError( CE_Failure, CPLE_AppDefined,
"File %s appears to be ILI1 but the ILI1 reader can't\n"
"be instantiated, likely because Xerces support wasn't\n"
"configured in.",
pszNewName );
return FALSE;
}
poReader->OpenFile( osBasename.c_str() );
pszName = CPLStrdup( osBasename.c_str() );
if (osModelFilename.length() > 0 )
poReader->ReadModel( osModelFilename.c_str() );
if( getenv( "ARC_DEGREES" ) != NULL ) {
//No better way to pass arguments to the reader (it could even be an -lco arg)
poReader->SetArcDegrees( atof( getenv("ARC_DEGREES") ) );
}
//Parse model and read data - without surface joing and polygonizing
poReader->ReadFeatures();
return TRUE;
}
/************************************************************************/
/* Create() */
/************************************************************************/
int OGRILI1DataSource::Create( const char *pszFilename,
char **papszOptions )
{
std::string osBasename, osModelFilename;
char **filenames = CSLTokenizeString2( pszFilename, ",", 0 );
osBasename = filenames[0];
if( CSLCount(filenames) > 1 )
osModelFilename = filenames[1];
CSLDestroy( filenames );
if( osModelFilename.length() == 0 )
{
//TODO: create automatic model
}
/* -------------------------------------------------------------------- */
/* Create the empty file. */
/* -------------------------------------------------------------------- */
fpTransfer = VSIFOpen( osBasename.c_str(), "w+b" );
if( fpTransfer == NULL )
{
CPLError( CE_Failure, CPLE_OpenFailed,
"Failed to create %s:\n%s",
osBasename.c_str(), VSIStrerror( errno ) );
return FALSE;
}
/* -------------------------------------------------------------------- */
/* Parse model */
/* -------------------------------------------------------------------- */
iom_init();
// set error listener to a iom provided one, that just
// dumps all errors to stderr
iom_seterrlistener(iom_stderrlistener);
IOM_BASKET model = 0;
if( osModelFilename.length() != 0 ) {
// compile ili model
char *iliFiles[1] = {(char *)osModelFilename.c_str()};
model=iom_compileIli(1,iliFiles);
if(!model){
CPLError( CE_Warning, CPLE_OpenFailed,
"iom_compileIli %s, %s.",
pszName, VSIStrerror( errno ) );
iom_end();
return FALSE;
}
}
pszTopic = CPLStrdup(model ?
GetAttrObjName(model, "iom04.metamodel.Topic") :
CPLGetBasename(osBasename.c_str()));
/* -------------------------------------------------------------------- */
/* Write headers */
/* -------------------------------------------------------------------- */
VSIFPrintf( fpTransfer, "SCNT\n" );
VSIFPrintf( fpTransfer, "OGR/GDAL %s, INTERLIS Driver\n", GDAL_RELEASE_NAME );
VSIFPrintf( fpTransfer, "////\n" );
VSIFPrintf( fpTransfer, "MTID INTERLIS1\n" );
const char* modelname = model ?
GetAttrObjName(model, "iom04.metamodel.DataModel") :
CPLGetBasename(osBasename.c_str());
VSIFPrintf( fpTransfer, "MODL %s\n", modelname );
return TRUE;
}
static char *ExtractTopic(const char * pszLayerName)
{
const char *table = strchr(pszLayerName, '_');
while (table && table[1] != '_') table = strchr(table+1, '_');
return (table) ? CPLScanString(pszLayerName, table-pszLayerName, FALSE, FALSE) : NULL;
}
/************************************************************************/
/* CreateLayer() */
/************************************************************************/
OGRLayer *
OGRILI1DataSource::CreateLayer( const char * pszLayerName,
OGRSpatialReference *poSRS,
OGRwkbGeometryType eType,
char ** papszOptions )
{
const char *table = pszLayerName;
char * topic = ExtractTopic(pszLayerName);
if (nLayers) VSIFPrintf( fpTransfer, "ETAB\n" );
if (topic)
{
table = pszLayerName+strlen(topic)+2; //after "__"
if (pszTopic == NULL || !EQUAL(topic, pszTopic))
{
if (pszTopic)
{
VSIFPrintf( fpTransfer, "ETOP\n" );
CPLFree(pszTopic);
}
pszTopic = topic;
VSIFPrintf( fpTransfer, "TOPI %s\n", pszTopic );
}
else
{
CPLFree(topic);
}
}
else
{
if (pszTopic == NULL) pszTopic = CPLStrdup("Unknown");
VSIFPrintf( fpTransfer, "TOPI %s\n", pszTopic );
}
VSIFPrintf( fpTransfer, "TABL %s\n", table );
OGRILI1Layer *poLayer = new OGRILI1Layer(table, poSRS, TRUE, eType, this);
nLayers ++;
papoLayers = (OGRILI1Layer**)CPLRealloc(papoLayers, sizeof(OGRILI1Layer*) * nLayers);
papoLayers[nLayers-1] = poLayer;
return poLayer;
}
/************************************************************************/
/* TestCapability() */
/************************************************************************/
int OGRILI1DataSource::TestCapability( const char * pszCap )
{
if( EQUAL(pszCap,ODsCCreateLayer) )
return TRUE;
else
return FALSE;
}
/************************************************************************/
/* GetLayer() */
/************************************************************************/
OGRLayer *OGRILI1DataSource::GetLayer( int iLayer )
{
return poReader->GetLayer( iLayer );
}
|