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

« back to all changes in this revision

Viewing changes to ogr/ogrsf_frmts/geojson/ogrgeojsonlayer.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
1
/******************************************************************************
2
 
 * $Id$
 
2
 * $Id: ogrgeojsonlayer.cpp 23367 2011-11-12 22:46:13Z rouault $
3
3
 *
4
4
 * Project:  OpenGIS Simple Features Reference Implementation
5
5
 * Purpose:  Implementation of OGRGeoJSONLayer class (OGR GeoJSON Driver).
55
55
                                  OGRGeoJSONDataSource* poDS )
56
56
    : iterCurrent_( seqFeatures_.end() ), poDS_( poDS ), poFeatureDefn_(new OGRFeatureDefn( pszName ) ), poSRS_( NULL ), nOutCounter_( 0 )
57
57
{
58
 
    UNREFERENCED_PARAM(papszOptions);
 
58
    bWriteBBOX = CSLTestBoolean(CSLFetchNameValueDef(papszOptions, "WRITE_BBOX", "FALSE"));
 
59
    bBBOX3D = FALSE;
59
60
 
60
61
    CPLAssert( NULL != poDS_ );
61
62
    CPLAssert( NULL != poFeatureDefn_ );
67
68
    {
68
69
        SetSpatialRef( poSRSIn );
69
70
    }
 
71
 
 
72
    nCoordPrecision = atoi(CSLFetchNameValueDef(papszOptions, "COORDINATE_PRECISION", "-1"));
70
73
}
71
74
 
72
75
/************************************************************************/
75
78
 
76
79
OGRGeoJSONLayer::~OGRGeoJSONLayer()
77
80
{
78
 
    FILE* fp = poDS_->GetOutputFile();
 
81
    VSILFILE* fp = poDS_->GetOutputFile();
79
82
    if( NULL != fp )
80
83
    {
81
 
        VSIFPrintfL( fp, "\n]\n}\n" );
 
84
        VSIFPrintfL( fp, "\n]" );
 
85
 
 
86
        if( bWriteBBOX && sEnvelopeLayer.IsInit() )
 
87
        {
 
88
            json_object* poObjBBOX = json_object_new_array();
 
89
            json_object_array_add(poObjBBOX,
 
90
                            json_object_new_double_with_precision(sEnvelopeLayer.MinX, nCoordPrecision));
 
91
            json_object_array_add(poObjBBOX,
 
92
                            json_object_new_double_with_precision(sEnvelopeLayer.MinY, nCoordPrecision));
 
93
            if( bBBOX3D )
 
94
                json_object_array_add(poObjBBOX,
 
95
                            json_object_new_double_with_precision(sEnvelopeLayer.MinZ, nCoordPrecision));
 
96
            json_object_array_add(poObjBBOX,
 
97
                            json_object_new_double_with_precision(sEnvelopeLayer.MaxX, nCoordPrecision));
 
98
            json_object_array_add(poObjBBOX,
 
99
                            json_object_new_double_with_precision(sEnvelopeLayer.MaxY, nCoordPrecision));
 
100
            if( bBBOX3D )
 
101
                json_object_array_add(poObjBBOX,
 
102
                            json_object_new_double_with_precision(sEnvelopeLayer.MaxZ, nCoordPrecision));
 
103
 
 
104
            const char* pszBBOX = json_object_to_json_string( poObjBBOX );
 
105
            if( poDS_->GetFpOutputIsSeekable() )
 
106
            {
 
107
                VSIFSeekL(fp, poDS_->GetBBOXInsertLocation(), SEEK_SET);
 
108
                if (strlen(pszBBOX) + 9 < SPACE_FOR_BBOX)
 
109
                    VSIFPrintfL( fp, "\"bbox\": %s,", pszBBOX );
 
110
                VSIFSeekL(fp, 0, SEEK_END);
 
111
            }
 
112
            else
 
113
            {
 
114
                VSIFPrintfL( fp, ",\n\"bbox\": %s", pszBBOX );
 
115
            }
 
116
 
 
117
            json_object_put( poObjBBOX );
 
118
        }
 
119
 
 
120
        VSIFPrintfL( fp, "\n}\n" );
82
121
    }
83
122
 
84
123
    std::for_each(seqFeatures_.begin(), seqFeatures_.end(),
202
241
 
203
242
OGRErr OGRGeoJSONLayer::CreateFeature( OGRFeature* poFeature )
204
243
{
205
 
    FILE* fp = poDS_->GetOutputFile();
206
 
    if( NULL == poFeature )
 
244
    VSILFILE* fp = poDS_->GetOutputFile();
 
245
    if( NULL == fp )
207
246
    {
208
247
        CPLDebug( "GeoJSON", "Target datasource file is invalid." );
209
248
        return CE_Failure;
215
254
        return OGRERR_INVALID_HANDLE;
216
255
    }
217
256
 
218
 
    json_object* poObj = OGRGeoJSONWriteFeature( poFeature );
 
257
    json_object* poObj = OGRGeoJSONWriteFeature( poFeature, bWriteBBOX, nCoordPrecision );
219
258
    CPLAssert( NULL != poObj );
220
259
 
221
260
    if( nOutCounter_ > 0 )
229
268
 
230
269
    ++nOutCounter_;
231
270
 
 
271
    OGRGeometry* poGeometry = poFeature->GetGeometryRef();
 
272
    if ( bWriteBBOX && !poGeometry->IsEmpty() )
 
273
    {
 
274
        OGREnvelope3D sEnvelope;
 
275
        poGeometry->getEnvelope(&sEnvelope);
 
276
 
 
277
        if( poGeometry->getCoordinateDimension() == 3 )
 
278
            bBBOX3D = TRUE;
 
279
 
 
280
        sEnvelopeLayer.Merge(sEnvelope);
 
281
    }
 
282
 
232
283
    return OGRERR_NONE;
233
284
}
234
285
 
325
376
 
326
377
void OGRGeoJSONLayer::DetectGeometryType()
327
378
{
 
379
    if (poFeatureDefn_->GetGeomType() != wkbUnknown)
 
380
        return;
 
381
 
328
382
    OGRwkbGeometryType featType = wkbUnknown;
329
383
    OGRGeometry* poGeometry = NULL;
330
384
    FeaturesSeq::const_iterator it = seqFeatures_.begin();