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 42 : OGRGeoJSONLayer::OGRGeoJSONLayer( const char* pszName,
51 : OGRSpatialReference* poSRSIn,
52 : OGRwkbGeometryType eGType,
53 : OGRGeoJSONDataSource* poDS )
54 42 : : iterCurrent_( seqFeatures_.end() ), poDS_( poDS ), poFeatureDefn_(new OGRFeatureDefn( pszName ) ), poSRS_( NULL )
55 : {
56 42 : CPLAssert( NULL != poDS_ );
57 42 : CPLAssert( NULL != poFeatureDefn_ );
58 :
59 42 : poFeatureDefn_->Reference();
60 42 : poFeatureDefn_->SetGeomType( eGType );
61 :
62 42 : if( NULL != poSRSIn )
63 : {
64 0 : SetSpatialRef( poSRSIn );
65 : }
66 42 : }
67 :
68 : /************************************************************************/
69 : /* ~OGRGeoJSONLayer */
70 : /************************************************************************/
71 :
72 42 : OGRGeoJSONLayer::~OGRGeoJSONLayer()
73 : {
74 : std::for_each(seqFeatures_.begin(), seqFeatures_.end(),
75 42 : OGRFeature::DestroyFeature);
76 :
77 42 : if( NULL != poFeatureDefn_ )
78 : {
79 42 : poFeatureDefn_->Release();
80 : }
81 :
82 42 : if( NULL != poSRS_ )
83 : {
84 38 : poSRS_->Release();
85 : }
86 42 : }
87 :
88 : /************************************************************************/
89 : /* GetLayerDefn */
90 : /************************************************************************/
91 :
92 443 : OGRFeatureDefn* OGRGeoJSONLayer::GetLayerDefn()
93 : {
94 443 : return poFeatureDefn_;
95 : }
96 :
97 : /************************************************************************/
98 : /* GetSpatialRef */
99 : /************************************************************************/
100 :
101 4 : OGRSpatialReference* OGRGeoJSONLayer::GetSpatialRef()
102 : {
103 4 : return poSRS_;
104 : }
105 :
106 : /************************************************************************/
107 : /* SetSpatialRef */
108 : /************************************************************************/
109 :
110 38 : void OGRGeoJSONLayer::SetSpatialRef( OGRSpatialReference* poSRSIn )
111 : {
112 38 : 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 38 : poSRS_ = poSRSIn->Clone();
125 : }
126 38 : }
127 :
128 : /************************************************************************/
129 : /* GetFeatureCount */
130 : /************************************************************************/
131 :
132 13 : int OGRGeoJSONLayer::GetFeatureCount( int bForce )
133 : {
134 13 : if (m_poFilterGeom == NULL && m_poAttrQuery == NULL)
135 13 : return static_cast<int>( seqFeatures_.size() );
136 : else
137 0 : return OGRLayer::GetFeatureCount(bForce);
138 : }
139 :
140 : /************************************************************************/
141 : /* ResetReading */
142 : /************************************************************************/
143 :
144 93 : void OGRGeoJSONLayer::ResetReading()
145 : {
146 93 : iterCurrent_ = seqFeatures_.begin();
147 93 : }
148 :
149 : /************************************************************************/
150 : /* GetNextFeature */
151 : /************************************************************************/
152 :
153 124 : OGRFeature* OGRGeoJSONLayer::GetNextFeature()
154 : {
155 248 : while ( iterCurrent_ != seqFeatures_.end() )
156 : {
157 85 : OGRFeature* poFeature = (*iterCurrent_);
158 85 : CPLAssert( NULL != poFeature );
159 85 : ++iterCurrent_;
160 :
161 85 : if((m_poFilterGeom == NULL
162 : || FilterGeometry( poFeature->GetGeometryRef() ) )
163 : && (m_poAttrQuery == NULL
164 : || m_poAttrQuery->Evaluate( poFeature )) )
165 : {
166 85 : OGRFeature* poFeatureCopy = poFeature->Clone();
167 85 : CPLAssert( NULL != poFeatureCopy );
168 :
169 85 : if (poFeatureCopy->GetGeometryRef() != NULL && poSRS_ != NULL)
170 : {
171 67 : poFeatureCopy->GetGeometryRef()->assignSpatialReference( poSRS_ );
172 : }
173 :
174 85 : return poFeatureCopy;
175 : }
176 : }
177 :
178 39 : 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 28 : const char* OGRGeoJSONLayer::GetFIDColumn()
197 : {
198 28 : return sFIDColumn_.c_str();
199 : }
200 :
201 : /************************************************************************/
202 : /* SetFIDColumn */
203 : /************************************************************************/
204 :
205 4 : void OGRGeoJSONLayer::SetFIDColumn( const char* pszFIDColumn )
206 : {
207 4 : sFIDColumn_ = pszFIDColumn;
208 4 : }
209 :
210 : /************************************************************************/
211 : /* AddFeature */
212 : /************************************************************************/
213 :
214 102 : void OGRGeoJSONLayer::AddFeature( OGRFeature* poFeature )
215 : {
216 102 : 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 102 : OGRFeature* poNewFeature = NULL;
225 102 : poNewFeature = poFeature->Clone();
226 :
227 :
228 102 : if( -1 == poNewFeature->GetFID() )
229 : {
230 98 : int nFID = static_cast<int>(seqFeatures_.size());
231 98 : poNewFeature->SetFID( nFID );
232 :
233 : // TODO - mlokot: We need to redesign creation of FID column
234 98 : int nField = poNewFeature->GetFieldIndex( DefaultFIDColumn );
235 98 : if( -1 != nField )
236 : {
237 0 : poNewFeature->SetField( nField, nFID );
238 : }
239 : }
240 :
241 102 : seqFeatures_.push_back( poNewFeature );
242 102 : }
243 :
244 : /************************************************************************/
245 : /* DetectGeometryType */
246 : /************************************************************************/
247 :
248 42 : void OGRGeoJSONLayer::DetectGeometryType()
249 : {
250 42 : if (poFeatureDefn_->GetGeomType() != wkbUnknown)
251 8 : return;
252 :
253 34 : OGRwkbGeometryType featType = wkbUnknown;
254 34 : OGRGeometry* poGeometry = NULL;
255 34 : FeaturesSeq::const_iterator it = seqFeatures_.begin();
256 34 : FeaturesSeq::const_iterator end = seqFeatures_.end();
257 :
258 34 : if( it != end )
259 : {
260 34 : poGeometry = (*it)->GetGeometryRef();
261 34 : if( NULL != poGeometry )
262 : {
263 32 : featType = poGeometry->getGeometryType();
264 32 : if( featType != poFeatureDefn_->GetGeomType() )
265 : {
266 32 : poFeatureDefn_->SetGeomType( featType );
267 : }
268 : }
269 34 : ++it;
270 : }
271 :
272 94 : while( it != end )
273 : {
274 28 : poGeometry = (*it)->GetGeometryRef();
275 28 : if( NULL != poGeometry )
276 : {
277 12 : featType = poGeometry->getGeometryType();
278 12 : if( featType != poFeatureDefn_->GetGeomType() )
279 : {
280 : CPLDebug( "GeoJSON",
281 2 : "Detected layer of mixed-geometry type features." );
282 2 : poFeatureDefn_->SetGeomType( DefaultGeometryType );
283 2 : break;
284 : }
285 : }
286 26 : ++it;
287 : }
288 : }
|