~ubuntu-branches/debian/sid/gdal/sid

« back to all changes in this revision

Viewing changes to ogr/ogrsf_frmts/pgdump/ogrpgdumpdatasource.cpp

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2012-05-07 15:04:42 UTC
  • mfrom: (5.5.16 experimental)
  • Revision ID: package-import@ubuntu.com-20120507150442-2eks97loeh6rq005
Tags: 1.9.0-1
* Ready for sid, starting transition.
* All symfiles updated to latest builds.
* Added dh_numpy call in debian/rules to depend on numpy ABI.
* Policy bumped to 3.9.3, no changes required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 * $Id: ogrpgdumpdatasource.cpp 22821 2011-07-28 17:54:47Z rouault $
 
3
 *
 
4
 * Project:  OpenGIS Simple Features Reference Implementation
 
5
 * Purpose:  Implements OGRPGDumpDataSource class.
 
6
 * Author:   Even Rouault, <even dot rouault at mines dash paris dot org>
 
7
 *
 
8
 ******************************************************************************
 
9
 * Copyright (c) 2010, Even Rouault
 
10
 *
 
11
 * Permission is hereby granted, free of charge, to any person obtaining a
 
12
 * copy of this software and associated documentation files (the "Software"),
 
13
 * to deal in the Software without restriction, including without limitation
 
14
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
15
 * and/or sell copies of the Software, and to permit persons to whom the
 
16
 * Software is furnished to do so, subject to the following conditions:
 
17
 *
 
18
 * The above copyright notice and this permission notice shall be included
 
19
 * in all copies or substantial portions of the Software.
 
20
 *
 
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
22
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 
24
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
26
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
27
 * DEALINGS IN THE SOFTWARE.
 
28
 ****************************************************************************/
 
29
 
 
30
#include <string.h>
 
31
#include "ogr_pgdump.h"
 
32
#include "cpl_conv.h"
 
33
#include "cpl_string.h"
 
34
 
 
35
CPL_CVSID("$Id: ogrpgdumpdatasource.cpp 22821 2011-07-28 17:54:47Z rouault $");
 
36
 
 
37
/************************************************************************/
 
38
/*                      OGRPGDumpDataSource()                           */
 
39
/************************************************************************/
 
40
 
 
41
OGRPGDumpDataSource::OGRPGDumpDataSource(const char* pszName,
 
42
                                         char** papszOptions)
 
43
 
 
44
{
 
45
    nLayers = 0;
 
46
    papoLayers = NULL;
 
47
    this->pszName = CPLStrdup(pszName);
 
48
    bTriedOpen = FALSE;
 
49
    fp = NULL;
 
50
    bInTransaction = FALSE;
 
51
    poLayerInCopyMode = NULL;
 
52
 
 
53
    const char *pszCRLFFormat = CSLFetchNameValue( papszOptions, "LINEFORMAT");
 
54
 
 
55
    int bUseCRLF;
 
56
    if( pszCRLFFormat == NULL )
 
57
    {
 
58
#ifdef WIN32
 
59
        bUseCRLF = TRUE;
 
60
#else
 
61
        bUseCRLF = FALSE;
 
62
#endif
 
63
    }
 
64
    else if( EQUAL(pszCRLFFormat,"CRLF") )
 
65
        bUseCRLF = TRUE;
 
66
    else if( EQUAL(pszCRLFFormat,"LF") )
 
67
        bUseCRLF = FALSE;
 
68
    else
 
69
    {
 
70
        CPLError( CE_Warning, CPLE_AppDefined, 
 
71
                  "LINEFORMAT=%s not understood, use one of CRLF or LF.",
 
72
                  pszCRLFFormat );
 
73
#ifdef WIN32
 
74
        bUseCRLF = TRUE;
 
75
#else
 
76
        bUseCRLF = FALSE;
 
77
#endif
 
78
    }
 
79
    pszEOL = (bUseCRLF) ? "\r\n" : "\n";
 
80
}
 
81
 
 
82
/************************************************************************/
 
83
/*                          ~OGRPGDumpDataSource()                          */
 
84
/************************************************************************/
 
85
 
 
86
OGRPGDumpDataSource::~OGRPGDumpDataSource()
 
87
 
 
88
{
 
89
    int i;
 
90
 
 
91
    if (fp)
 
92
    {
 
93
        Commit();
 
94
        VSIFCloseL(fp);
 
95
        fp = NULL;
 
96
    }
 
97
 
 
98
    for(i=0;i<nLayers;i++)
 
99
        delete papoLayers[i];
 
100
    CPLFree(papoLayers);
 
101
    CPLFree(pszName);
 
102
}
 
103
 
 
104
/************************************************************************/
 
105
/*                         StartTransaction()                           */
 
106
/************************************************************************/
 
107
 
 
108
void OGRPGDumpDataSource::StartTransaction()
 
109
{
 
110
    if (bInTransaction)
 
111
        return;
 
112
    bInTransaction = TRUE;
 
113
    Log("BEGIN");
 
114
}
 
115
 
 
116
/************************************************************************/
 
117
/*                              Commit()                                */
 
118
/************************************************************************/
 
119
 
 
120
void OGRPGDumpDataSource::Commit()
 
121
{
 
122
    EndCopy();
 
123
 
 
124
    if (!bInTransaction)
 
125
        return;
 
126
    bInTransaction = FALSE;
 
127
    Log("COMMIT");
 
128
}
 
129
 
 
130
/************************************************************************/
 
131
/*                            LaunderName()                             */
 
132
/************************************************************************/
 
133
 
 
134
char *OGRPGDumpDataSource::LaunderName( const char *pszSrcName )
 
135
 
 
136
{
 
137
    char    *pszSafeName = CPLStrdup( pszSrcName );
 
138
 
 
139
    for( int i = 0; pszSafeName[i] != '\0'; i++ )
 
140
    {
 
141
        pszSafeName[i] = (char) tolower( pszSafeName[i] );
 
142
        if( pszSafeName[i] == '\'' || pszSafeName[i] == '-' || pszSafeName[i] == '#' )
 
143
            pszSafeName[i] = '_';
 
144
    }
 
145
 
 
146
    if( strcmp(pszSrcName,pszSafeName) != 0 )
 
147
        CPLDebug("PG","LaunderName('%s') -> '%s'", 
 
148
                 pszSrcName, pszSafeName);
 
149
 
 
150
    return pszSafeName;
 
151
}
 
152
 
 
153
/************************************************************************/
 
154
/*                            CreateLayer()                             */
 
155
/************************************************************************/
 
156
 
 
157
OGRLayer *
 
158
OGRPGDumpDataSource::CreateLayer( const char * pszLayerName,
 
159
                                  OGRSpatialReference *poSRS,
 
160
                                  OGRwkbGeometryType eType,
 
161
                                  char ** papszOptions )
 
162
 
 
163
{
 
164
    CPLString            osCommand;
 
165
    const char          *pszGeomType = NULL;
 
166
    char                *pszTableName = NULL;
 
167
    char                *pszSchemaName = NULL;
 
168
    int                  nDimension = 3;
 
169
    int                  bHavePostGIS = TRUE;
 
170
 
 
171
    const char* pszFIDColumnName = CSLFetchNameValue(papszOptions, "FID");
 
172
    CPLString osFIDColumnName;
 
173
    if (pszFIDColumnName == NULL)
 
174
        osFIDColumnName = "OGC_FID";
 
175
    else
 
176
    {
 
177
        if( CSLFetchBoolean(papszOptions,"LAUNDER", TRUE) )
 
178
        {
 
179
            char* pszLaunderedFid = LaunderName(pszFIDColumnName);
 
180
            osFIDColumnName += OGRPGDumpEscapeColumnName(pszLaunderedFid);
 
181
            CPLFree(pszLaunderedFid);
 
182
        }
 
183
        else
 
184
            osFIDColumnName += OGRPGDumpEscapeColumnName(pszFIDColumnName);
 
185
    }
 
186
    pszFIDColumnName = osFIDColumnName.c_str();
 
187
 
 
188
    if (strncmp(pszLayerName, "pg", 2) == 0)
 
189
    {
 
190
        CPLError(CE_Warning, CPLE_AppDefined,
 
191
                 "The layer name should not begin by 'pg' as it is a reserved prefix");
 
192
    }
 
193
    
 
194
    //bHavePostGIS = CSLFetchBoolean(papszOptions,"POSTGIS", TRUE);
 
195
 
 
196
    int bCreateTable = CSLFetchBoolean(papszOptions,"CREATE_TABLE", TRUE);
 
197
    int bCreateSchema = CSLFetchBoolean(papszOptions,"CREATE_SCHEMA", TRUE);
 
198
    const char* pszDropTable = CSLFetchNameValueDef(papszOptions,"DROP_TABLE", "YES");
 
199
 
 
200
    if( wkbFlatten(eType) == eType )
 
201
        nDimension = 2;
 
202
 
 
203
    if( CSLFetchNameValue( papszOptions, "DIM") != NULL )
 
204
        nDimension = atoi(CSLFetchNameValue( papszOptions, "DIM"));
 
205
 
 
206
    /* Should we turn layers with None geometry type as Unknown/GEOMETRY */
 
207
    /* so they are still recorded in geometry_columns table ? (#4012) */
 
208
    int bNoneAsUnknown = CSLTestBoolean(CSLFetchNameValueDef(
 
209
                                    papszOptions, "NONE_AS_UNKNOWN", "NO"));
 
210
    if (bNoneAsUnknown && eType == wkbNone)
 
211
        eType = wkbUnknown;
 
212
    else if (eType == wkbNone)
 
213
        bHavePostGIS = FALSE;
 
214
 
 
215
    int bExtractSchemaFromLayerName = CSLTestBoolean(CSLFetchNameValueDef(
 
216
                                    papszOptions, "EXTRACT_SCHEMA_FROM_LAYER_NAME", "YES"));
 
217
 
 
218
    /* Postgres Schema handling:
 
219
       Extract schema name from input layer name or passed with -lco SCHEMA.
 
220
       Set layer name to "schema.table" or to "table" if schema == current_schema()
 
221
       Usage without schema name is backwards compatible
 
222
    */
 
223
    const char* pszDotPos = strstr(pszLayerName,".");
 
224
    if ( pszDotPos != NULL && bExtractSchemaFromLayerName )
 
225
    {
 
226
      int length = pszDotPos - pszLayerName;
 
227
      pszSchemaName = (char*)CPLMalloc(length+1);
 
228
      strncpy(pszSchemaName, pszLayerName, length);
 
229
      pszSchemaName[length] = '\0';
 
230
 
 
231
      if( CSLFetchBoolean(papszOptions,"LAUNDER", TRUE) )
 
232
          pszTableName = LaunderName( pszDotPos + 1 ); //skip "."
 
233
      else
 
234
          pszTableName = CPLStrdup( pszDotPos + 1 ); //skip "."
 
235
    }
 
236
    else
 
237
    {
 
238
      pszSchemaName = NULL;
 
239
      if( CSLFetchBoolean(papszOptions,"LAUNDER", TRUE) )
 
240
          pszTableName = LaunderName( pszLayerName ); //skip "."
 
241
      else
 
242
          pszTableName = CPLStrdup( pszLayerName ); //skip "."
 
243
    }
 
244
 
 
245
    Commit();
 
246
 
 
247
/* -------------------------------------------------------------------- */
 
248
/*      Set the default schema for the layers.                          */
 
249
/* -------------------------------------------------------------------- */
 
250
    if( CSLFetchNameValue( papszOptions, "SCHEMA" ) != NULL )
 
251
    {
 
252
        CPLFree(pszSchemaName);
 
253
        pszSchemaName = CPLStrdup(CSLFetchNameValue( papszOptions, "SCHEMA" ));
 
254
        if (bCreateSchema)
 
255
        {
 
256
            osCommand.Printf("CREATE SCHEMA \"%s\"", pszSchemaName);
 
257
            Log(osCommand);
 
258
        }
 
259
    }
 
260
 
 
261
    if ( pszSchemaName == NULL)
 
262
    {
 
263
        pszSchemaName = CPLStrdup("public");
 
264
    }
 
265
 
 
266
/* -------------------------------------------------------------------- */
 
267
/*      Do we already have this layer?                                  */
 
268
/* -------------------------------------------------------------------- */
 
269
    int iLayer;
 
270
 
 
271
    for( iLayer = 0; iLayer < nLayers; iLayer++ )
 
272
    {
 
273
        if( EQUAL(pszLayerName,papoLayers[iLayer]->GetLayerDefn()->GetName()) )
 
274
        {
 
275
            CPLError( CE_Failure, CPLE_AppDefined,
 
276
                      "Layer %s already exists, CreateLayer failed.\n", 
 
277
                      pszLayerName );
 
278
            CPLFree( pszTableName );
 
279
            CPLFree( pszSchemaName );
 
280
            return NULL;
 
281
        }
 
282
    }
 
283
 
 
284
 
 
285
    if (bCreateTable && (EQUAL(pszDropTable, "YES") ||
 
286
                         EQUAL(pszDropTable, "ON") ||
 
287
                         EQUAL(pszDropTable, "TRUE") ||
 
288
                         EQUAL(pszDropTable, "IF_EXISTS")))
 
289
    {
 
290
        if (EQUAL(pszDropTable, "IF_EXISTS"))
 
291
            osCommand.Printf("DROP TABLE IF EXISTS \"%s\".\"%s\" CASCADE", pszSchemaName, pszTableName );
 
292
        else
 
293
            osCommand.Printf("DROP TABLE \"%s\".\"%s\" CASCADE", pszSchemaName, pszTableName );
 
294
        Log(osCommand);
 
295
    }
 
296
    
 
297
/* -------------------------------------------------------------------- */
 
298
/*      Handle the GEOM_TYPE option.                                    */
 
299
/* -------------------------------------------------------------------- */
 
300
    pszGeomType = CSLFetchNameValue( papszOptions, "GEOM_TYPE" );
 
301
    if( pszGeomType == NULL )
 
302
    {
 
303
        pszGeomType = "geometry";
 
304
    }
 
305
 
 
306
    if( !EQUAL(pszGeomType,"geometry") && !EQUAL(pszGeomType, "geography"))
 
307
    {
 
308
        CPLError( CE_Failure, CPLE_AppDefined,
 
309
                    "GEOM_TYPE in PostGIS enabled databases must be 'geometry' or 'geography'.\n"
 
310
                    "Creation of layer %s with GEOM_TYPE %s has failed.",
 
311
                    pszLayerName, pszGeomType );
 
312
        CPLFree( pszTableName );
 
313
        CPLFree( pszSchemaName );
 
314
        return NULL;
 
315
    }
 
316
 
 
317
/* -------------------------------------------------------------------- */
 
318
/*      Try to get the SRS Id of this spatial reference system,         */
 
319
/*      adding tot the srs table if needed.                             */
 
320
/* -------------------------------------------------------------------- */
 
321
    int nSRSId = -1;
 
322
 
 
323
    if( CSLFetchNameValue( papszOptions, "SRID") != NULL )
 
324
        nSRSId = atoi(CSLFetchNameValue( papszOptions, "SRID"));
 
325
    else
 
326
    {
 
327
        if (poSRS)
 
328
        {
 
329
            const char* pszAuthorityName = poSRS->GetAuthorityName(NULL);
 
330
            if( pszAuthorityName != NULL && EQUAL( pszAuthorityName, "EPSG" ) )
 
331
            {
 
332
                /* Assume the EPSG Id is the SRS ID. Might be a wrong guess ! */
 
333
                nSRSId = atoi( poSRS->GetAuthorityCode(NULL) );
 
334
            }
 
335
            else
 
336
            {
 
337
                const char* pszGeogCSName = poSRS->GetAttrValue("GEOGCS");
 
338
                if (pszGeogCSName != NULL && EQUAL(pszGeogCSName, "GCS_WGS_1984"))
 
339
                    nSRSId = 4326;
 
340
            }
 
341
        }
 
342
    }
 
343
 
 
344
    CPLString osEscapedTableNameSingleQuote = OGRPGDumpEscapeString(pszTableName, -1, "");
 
345
    const char* pszEscapedTableNameSingleQuote = osEscapedTableNameSingleQuote.c_str();
 
346
 
 
347
    const char *pszGeometryType = OGRToOGCGeomType(eType);
 
348
 
 
349
    const char *pszGFldName = NULL;
 
350
    if( bHavePostGIS && !EQUAL(pszGeomType, "geography"))
 
351
    {
 
352
        if( CSLFetchNameValue( papszOptions, "GEOMETRY_NAME") != NULL )
 
353
            pszGFldName = CSLFetchNameValue( papszOptions, "GEOMETRY_NAME");
 
354
        else
 
355
            pszGFldName = "wkb_geometry";
 
356
 
 
357
        /* Sometimes there is an old cruft entry in the geometry_columns
 
358
        * table if things were not properly cleaned up before.  We make
 
359
        * an effort to clean out such cruft.
 
360
        */
 
361
        osCommand.Printf(
 
362
                "DELETE FROM geometry_columns WHERE f_table_name = %s AND f_table_schema = '%s'",
 
363
                pszEscapedTableNameSingleQuote, pszSchemaName );
 
364
        if (bCreateTable)
 
365
            Log(osCommand);
 
366
    }
 
367
 
 
368
 
 
369
    StartTransaction();
 
370
 
 
371
/* -------------------------------------------------------------------- */
 
372
/*      Create a basic table with the FID.  Also include the            */
 
373
/*      geometry if this is not a PostGIS enabled table.                */
 
374
/* -------------------------------------------------------------------- */
 
375
    
 
376
    CPLString osCreateTable;
 
377
    int bTemporary = CSLFetchNameValue( papszOptions, "TEMPORARY" ) != NULL &&
 
378
                     CSLTestBoolean(CSLFetchNameValue( papszOptions, "TEMPORARY" ));
 
379
    if (bTemporary)
 
380
    {
 
381
        CPLFree(pszSchemaName);
 
382
        pszSchemaName = CPLStrdup("pg_temp_1");
 
383
        osCreateTable.Printf("CREATE TEMPORARY TABLE \"%s\"", pszTableName);
 
384
    }
 
385
    else
 
386
        osCreateTable.Printf("CREATE TABLE \"%s\".\"%s\"", pszSchemaName, pszTableName);
 
387
 
 
388
    if( !bHavePostGIS )
 
389
    {
 
390
        if (eType == wkbNone)
 
391
            osCommand.Printf(
 
392
                    "%s ( "
 
393
                    "   %s SERIAL, "
 
394
                    "   CONSTRAINT \"%s_pk\" PRIMARY KEY (%s) )",
 
395
                    osCreateTable.c_str(), pszFIDColumnName, pszTableName, pszFIDColumnName );
 
396
        else
 
397
            osCommand.Printf(
 
398
                    "%s ( "
 
399
                    "   %s SERIAL, "
 
400
                    "   WKB_GEOMETRY %s, "
 
401
                    "   CONSTRAINT \"%s_pk\" PRIMARY KEY (%s) )",
 
402
                    osCreateTable.c_str(), pszFIDColumnName, pszGeomType, pszTableName, pszFIDColumnName );
 
403
    }
 
404
    else if ( EQUAL(pszGeomType, "geography") )
 
405
    {
 
406
        if( CSLFetchNameValue( papszOptions, "GEOMETRY_NAME") != NULL )
 
407
            pszGFldName = CSLFetchNameValue( papszOptions, "GEOMETRY_NAME");
 
408
        else
 
409
            pszGFldName = "the_geog";
 
410
 
 
411
        if (nSRSId)
 
412
            osCommand.Printf(
 
413
                     "%s ( %s SERIAL, \"%s\" geography(%s%s,%d), CONSTRAINT \"%s_pk\" PRIMARY KEY (%s) )",
 
414
                     osCreateTable.c_str(), pszFIDColumnName, pszGFldName, pszGeometryType, nDimension == 2 ? "" : "Z", nSRSId, pszTableName, pszFIDColumnName );
 
415
        else
 
416
            osCommand.Printf(
 
417
                     "%s ( %s SERIAL, \"%s\" geography(%s%s), CONSTRAINT \"%s_pk\" PRIMARY KEY (%s) )",
 
418
                     osCreateTable.c_str(), pszFIDColumnName, pszGFldName, pszGeometryType, nDimension == 2 ? "" : "Z", pszTableName, pszFIDColumnName );
 
419
    }
 
420
    else
 
421
    {
 
422
        osCommand.Printf(
 
423
                 "%s ( %s SERIAL, CONSTRAINT \"%s_pk\" PRIMARY KEY (%s) )",
 
424
                 osCreateTable.c_str(), pszFIDColumnName, pszTableName, pszFIDColumnName );
 
425
    }
 
426
 
 
427
    if (bCreateTable)
 
428
        Log(osCommand);
 
429
 
 
430
/* -------------------------------------------------------------------- */
 
431
/*      Eventually we should be adding this table to a table of         */
 
432
/*      "geometric layers", capturing the WKT projection, and           */
 
433
/*      perhaps some other housekeeping.                                */
 
434
/* -------------------------------------------------------------------- */
 
435
    if( bCreateTable && bHavePostGIS && !EQUAL(pszGeomType, "geography"))
 
436
    {
 
437
        osCommand.Printf(
 
438
                "SELECT AddGeometryColumn('%s',%s,'%s',%d,'%s',%d)",
 
439
                pszSchemaName, pszEscapedTableNameSingleQuote, pszGFldName,
 
440
                nSRSId, pszGeometryType, nDimension );
 
441
        Log(osCommand);
 
442
    }
 
443
 
 
444
    if( bCreateTable && bHavePostGIS )
 
445
    {
 
446
/* -------------------------------------------------------------------- */
 
447
/*      Create the spatial index.                                       */
 
448
/*                                                                      */
 
449
/*      We're doing this before we add geometry and record to the table */
 
450
/*      so this may not be exactly the best way to do it.               */
 
451
/* -------------------------------------------------------------------- */
 
452
        const char *pszSI = CSLFetchNameValue( papszOptions, "SPATIAL_INDEX" );
 
453
        if( pszSI == NULL || CSLTestBoolean(pszSI) )
 
454
        {
 
455
            osCommand.Printf("CREATE INDEX \"%s_geom_idx\" "
 
456
                            "ON \"%s\".\"%s\" "
 
457
                            "USING GIST (\"%s\")",
 
458
                    pszTableName, pszSchemaName, pszTableName, pszGFldName);
 
459
 
 
460
            Log(osCommand);
 
461
        }
 
462
    }
 
463
 
 
464
/* -------------------------------------------------------------------- */
 
465
/*      Create the layer object.                                        */
 
466
/* -------------------------------------------------------------------- */
 
467
    OGRPGDumpLayer     *poLayer;
 
468
 
 
469
    int bWriteAsHex = !CSLFetchBoolean(papszOptions,"WRITE_EWKT_GEOM",FALSE);
 
470
 
 
471
    poLayer = new OGRPGDumpLayer( this, pszSchemaName, pszTableName, pszGFldName,
 
472
                                  pszFIDColumnName, nDimension, nSRSId, bWriteAsHex, bCreateTable );
 
473
    poLayer->SetLaunderFlag( CSLFetchBoolean(papszOptions,"LAUNDER",TRUE) );
 
474
    poLayer->SetPrecisionFlag( CSLFetchBoolean(papszOptions,"PRECISION",TRUE));
 
475
 
 
476
/* -------------------------------------------------------------------- */
 
477
/*      Add layer to data source layer list.                            */
 
478
/* -------------------------------------------------------------------- */
 
479
    papoLayers = (OGRPGDumpLayer **)
 
480
        CPLRealloc( papoLayers,  sizeof(OGRPGDumpLayer *) * (nLayers+1) );
 
481
 
 
482
    papoLayers[nLayers++] = poLayer;
 
483
 
 
484
    CPLFree( pszTableName );
 
485
    CPLFree( pszSchemaName );
 
486
 
 
487
    return poLayer;
 
488
}
 
489
 
 
490
/************************************************************************/
 
491
/*                           TestCapability()                           */
 
492
/************************************************************************/
 
493
 
 
494
int OGRPGDumpDataSource::TestCapability( const char * pszCap )
 
495
 
 
496
{
 
497
    if( EQUAL(pszCap,ODsCCreateLayer) )
 
498
        return TRUE;
 
499
    else
 
500
        return FALSE;
 
501
}
 
502
 
 
503
/************************************************************************/
 
504
/*                              GetLayer()                              */
 
505
/************************************************************************/
 
506
 
 
507
OGRLayer *OGRPGDumpDataSource::GetLayer( int iLayer )
 
508
 
 
509
{
 
510
    if( iLayer < 0 || iLayer >= nLayers )
 
511
        return NULL;
 
512
    else
 
513
        return papoLayers[iLayer];
 
514
}
 
515
 
 
516
/************************************************************************/
 
517
/*                                  Log()                               */
 
518
/************************************************************************/
 
519
 
 
520
void  OGRPGDumpDataSource::Log(const char* pszStr, int bAddSemiColumn)
 
521
{
 
522
    if (fp == NULL)
 
523
    {
 
524
        if (bTriedOpen)
 
525
            return;
 
526
        bTriedOpen = TRUE;
 
527
        fp = VSIFOpenL(pszName, "wb");
 
528
        if (fp == NULL)
 
529
        {
 
530
            CPLError(CE_Failure, CPLE_FileIO, "Cannot create %s", pszName);
 
531
            return;
 
532
        }
 
533
    }
 
534
 
 
535
    if (bAddSemiColumn)
 
536
        VSIFPrintfL(fp, "%s;%s", pszStr, pszEOL);
 
537
    else
 
538
        VSIFPrintfL(fp, "%s%s", pszStr, pszEOL);
 
539
}
 
540
 
 
541
/************************************************************************/
 
542
/*                             StartCopy()                              */
 
543
/************************************************************************/
 
544
void OGRPGDumpDataSource::StartCopy( OGRPGDumpLayer *poPGLayer )
 
545
{
 
546
    EndCopy();
 
547
    poLayerInCopyMode = poPGLayer;
 
548
}
 
549
 
 
550
/************************************************************************/
 
551
/*                              EndCopy()                               */
 
552
/************************************************************************/
 
553
OGRErr OGRPGDumpDataSource::EndCopy( )
 
554
{
 
555
    if( poLayerInCopyMode != NULL )
 
556
    {
 
557
        OGRErr result = poLayerInCopyMode->EndCopy();
 
558
        poLayerInCopyMode = NULL;
 
559
 
 
560
        return result;
 
561
    }
 
562
    else
 
563
        return OGRERR_NONE;
 
564
}