1 : /******************************************************************************
2 : * $Id: ogrgeojsonlayer.cpp 23866 2012-02-01 23:51:11Z warmerdam $
3 : *
4 : * Project: OpenGIS Simple Features Reference Implementation
5 : * Purpose: Implementation of OGRGeoJSONLayer class (OGR GeoJSON Driver).
6 : * Author: Mateusz Loskot, mateusz@loskot.net
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2007, Mateusz Loskot
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 : #include <algorithm> // for_each, find_if
30 : #include <jsonc/json.h> // JSON-C
31 : #include "ogr_geojson.h"
32 :
33 : /* Remove annoying warnings Microsoft Visual C++ */
34 : #if defined(_MSC_VER)
35 : # pragma warning(disable:4512)
36 : #endif
37 :
38 : /************************************************************************/
39 : /* STATIC MEMBERS DEFINITION */
40 : /************************************************************************/
41 :
42 : const char* const OGRGeoJSONLayer::DefaultName = "OGRGeoJSON";
43 : const char* const OGRGeoJSONLayer::DefaultFIDColumn = "id";
44 : const OGRwkbGeometryType OGRGeoJSONLayer::DefaultGeometryType = wkbUnknown;
45 :
46 : /************************************************************************/
47 : /* OGRGeoJSONLayer */
48 : /************************************************************************/
49 :
50 82 : OGRGeoJSONLayer::OGRGeoJSONLayer( const char* pszName,
51 : OGRSpatialReference* poSRSIn,
52 : OGRwkbGeometryType eGType,
53 : OGRGeoJSONDataSource* poDS )
54 82 : : iterCurrent_( seqFeatures_.end() ), poDS_( poDS ), poFeatureDefn_(new OGRFeatureDefn( pszName ) ), poSRS_( NULL )
55 : {
56 82 : CPLAssert( NULL != poDS_ );
57 82 : CPLAssert( NULL != poFeatureDefn_ );
58 :
59 82 : poFeatureDefn_->Reference();
60 82 : poFeatureDefn_->SetGeomType( eGType );
61 :
62 82 : if( NULL != poSRSIn )
63 : {
64 0 : SetSpatialRef( poSRSIn );
65 : }
66 82 : }
67 :
68 : /************************************************************************/
69 : /* ~OGRGeoJSONLayer */
70 : /************************************************************************/
71 :
72 82 : OGRGeoJSONLayer::~OGRGeoJSONLayer()
73 : {
74 : std::for_each(seqFeatures_.begin(), seqFeatures_.end(),
75 82 : OGRFeature::DestroyFeature);
76 :
77 82 : if( NULL != poFeatureDefn_ )
78 : {
79 82 : poFeatureDefn_->Release();
80 : }
81 :
82 82 : if( NULL != poSRS_ )
83 : {
84 74 : poSRS_->Release();
85 : }
86 82 : }
87 :
88 : /************************************************************************/
89 : /* GetLayerDefn */
90 : /************************************************************************/
91 :
92 806 : OGRFeatureDefn* OGRGeoJSONLayer::GetLayerDefn()
93 : {
94 806 : return poFeatureDefn_;
95 : }
96 :
97 : /************************************************************************/
98 : /* GetSpatialRef */
99 : /************************************************************************/
100 :
101 8 : OGRSpatialReference* OGRGeoJSONLayer::GetSpatialRef()
102 : {
103 8 : return poSRS_;
104 : }
105 :
106 : /************************************************************************/
107 : /* SetSpatialRef */
108 : /************************************************************************/
109 :
110 74 : void OGRGeoJSONLayer::SetSpatialRef( OGRSpatialReference* poSRSIn )
111 : {
112 74 : if( NULL == poSRSIn )
113 : {
114 0 : poSRS_ = NULL;
115 : // poSRS_ = new OGRSpatialReference();
116 : // if( OGRERR_NONE != poSRS_->importFromEPSG( 4326 ) )
117 : // {
118 : // delete poSRS_;
119 : // poSRS_ = NULL;
120 : // }
121 : }
122 : else
123 : {
124 74 : poSRS_ = poSRSIn->Clone();
125 : }
126 74 : }
127 :
128 : /************************************************************************/
129 : /* GetFeatureCount */
130 : /************************************************************************/
131 :
132 26 : int OGRGeoJSONLayer::GetFeatureCount( int bForce )
133 : {
134 26 : if (m_poFilterGeom == NULL && m_poAttrQuery == NULL)
135 26 : return static_cast<int>( seqFeatures_.size() );
136 : else
137 0 : return OGRLayer::GetFeatureCount(bForce);
138 : }
139 :
140 : /************************************************************************/
141 : /* ResetReading */
142 : /************************************************************************/
143 :
144 182 : void OGRGeoJSONLayer::ResetReading()
145 : {
146 182 : iterCurrent_ = seqFeatures_.begin();
147 182 : }
148 :
149 : /************************************************************************/
150 : /* GetNextFeature */
151 : /************************************************************************/
152 :
153 246 : OGRFeature* OGRGeoJSONLayer::GetNextFeature()
154 : {
155 492 : while ( iterCurrent_ != seqFeatures_.end() )
156 : {
157 168 : OGRFeature* poFeature = (*iterCurrent_);
158 168 : CPLAssert( NULL != poFeature );
159 168 : ++iterCurrent_;
160 :
161 168 : if((m_poFilterGeom == NULL
162 : || FilterGeometry( poFeature->GetGeometryRef() ) )
163 : && (m_poAttrQuery == NULL
164 : || m_poAttrQuery->Evaluate( poFeature )) )
165 : {
166 168 : OGRFeature* poFeatureCopy = poFeature->Clone();
167 168 : CPLAssert( NULL != poFeatureCopy );
168 :
169 168 : if (poFeatureCopy->GetGeometryRef() != NULL && poSRS_ != NULL)
170 : {
171 132 : poFeatureCopy->GetGeometryRef()->assignSpatialReference( poSRS_ );
172 : }
173 :
174 168 : return poFeatureCopy;
175 : }
176 : }
177 :
178 78 : return NULL;
179 : }
180 :
181 : /************************************************************************/
182 : /* TestCapability */
183 : /************************************************************************/
184 :
185 0 : int OGRGeoJSONLayer::TestCapability( const char* pszCap )
186 : {
187 : UNREFERENCED_PARAM(pszCap);
188 :
189 0 : return FALSE;
190 : }
191 :
192 : /************************************************************************/
193 : /* GetFIDColumn */
194 : /************************************************************************/
195 :
196 36 : const char* OGRGeoJSONLayer::GetFIDColumn()
197 : {
198 36 : return sFIDColumn_.c_str();
199 : }
200 :
201 : /************************************************************************/
202 : /* SetFIDColumn */
203 : /************************************************************************/
204 :
205 8 : void OGRGeoJSONLayer::SetFIDColumn( const char* pszFIDColumn )
206 : {
207 8 : sFIDColumn_ = pszFIDColumn;
208 8 : }
209 :
210 : /************************************************************************/
211 : /* AddFeature */
212 : /************************************************************************/
213 :
214 184 : void OGRGeoJSONLayer::AddFeature( OGRFeature* poFeature )
215 : {
216 184 : CPLAssert( NULL != poFeature );
217 :
218 : // NOTE - mloskot:
219 : // Features may not be sorted according to FID values.
220 :
221 : // TODO: Should we check if feature already exists?
222 : // TODO: Think about sync operation, upload, etc.
223 :
224 184 : OGRFeature* poNewFeature = NULL;
225 184 : poNewFeature = poFeature->Clone();
226 :
227 :
228 184 : if( -1 == poNewFeature->GetFID() )
229 : {
230 176 : int nFID = static_cast<int>(seqFeatures_.size());
231 176 : poNewFeature->SetFID( nFID );
232 :
233 : // TODO - mlokot: We need to redesign creation of FID column
234 176 : int nField = poNewFeature->GetFieldIndex( DefaultFIDColumn );
235 176 : if( -1 != nField )
236 : {
237 0 : poNewFeature->SetField( nField, nFID );
238 : }
239 : }
240 :
241 184 : seqFeatures_.push_back( poNewFeature );
242 184 : }
243 :
244 : /************************************************************************/
245 : /* DetectGeometryType */
246 : /************************************************************************/
247 :
248 82 : void OGRGeoJSONLayer::DetectGeometryType()
249 : {
250 82 : if (poFeatureDefn_->GetGeomType() != wkbUnknown)
251 16 : return;
252 :
253 66 : OGRwkbGeometryType featType = wkbUnknown;
254 66 : OGRGeometry* poGeometry = NULL;
255 66 : FeaturesSeq::const_iterator it = seqFeatures_.begin();
256 66 : FeaturesSeq::const_iterator end = seqFeatures_.end();
257 :
258 66 : if( it != end )
259 : {
260 66 : poGeometry = (*it)->GetGeometryRef();
261 66 : if( NULL != poGeometry )
262 : {
263 62 : featType = poGeometry->getGeometryType();
264 62 : if( featType != poFeatureDefn_->GetGeomType() )
265 : {
266 62 : poFeatureDefn_->SetGeomType( featType );
267 : }
268 : }
269 66 : ++it;
270 : }
271 :
272 166 : while( it != end )
273 : {
274 38 : poGeometry = (*it)->GetGeometryRef();
275 38 : if( NULL != poGeometry )
276 : {
277 6 : featType = poGeometry->getGeometryType();
278 6 : if( featType != poFeatureDefn_->GetGeomType() )
279 : {
280 : CPLDebug( "GeoJSON",
281 4 : "Detected layer of mixed-geometry type features." );
282 4 : poFeatureDefn_->SetGeomType( DefaultGeometryType );
283 4 : break;
284 : }
285 : }
286 34 : ++it;
287 : }
288 : }
|