1 : /******************************************************************************
2 : * $Id: ogr_geojson.h 23662 2011-12-30 11:16:59Z rouault $
3 : *
4 : * Project: OpenGIS Simple Features Reference Implementation
5 : * Purpose: Definitions of OGR OGRGeoJSON driver types.
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 : #ifndef OGR_GEOJSON_H_INCLUDED
30 : #define OGR_GEOJSON_H_INCLUDED
31 :
32 : #include <ogrsf_frmts.h>
33 : #include <cstdio>
34 : #include <vector> // used by OGRGeoJSONLayer
35 :
36 : #define SPACE_FOR_BBOX 80
37 :
38 : class OGRGeoJSONDataSource;
39 :
40 : /************************************************************************/
41 : /* OGRGeoJSONLayer */
42 : /************************************************************************/
43 :
44 : class OGRGeoJSONLayer : public OGRLayer
45 : {
46 : public:
47 :
48 : static const char* const DefaultName;
49 : static const char* const DefaultFIDColumn;
50 : static const OGRwkbGeometryType DefaultGeometryType;
51 :
52 : OGRGeoJSONLayer( const char* pszName,
53 : OGRSpatialReference* poSRS,
54 : OGRwkbGeometryType eGType,
55 : OGRGeoJSONDataSource* poDS );
56 : ~OGRGeoJSONLayer();
57 :
58 : //
59 : // OGRLayer Interface
60 : //
61 : OGRFeatureDefn* GetLayerDefn();
62 : OGRSpatialReference* GetSpatialRef();
63 :
64 : int GetFeatureCount( int bForce = TRUE );
65 : void ResetReading();
66 : OGRFeature* GetNextFeature();
67 : int TestCapability( const char* pszCap );
68 : const char* GetFIDColumn();
69 : void SetFIDColumn( const char* pszFIDColumn );
70 :
71 : //
72 : // OGRGeoJSONLayer Interface
73 : //
74 : void AddFeature( OGRFeature* poFeature );
75 : void SetSpatialRef( OGRSpatialReference* poSRS );
76 : void DetectGeometryType();
77 :
78 : private:
79 :
80 : typedef std::vector<OGRFeature*> FeaturesSeq;
81 : FeaturesSeq seqFeatures_;
82 : FeaturesSeq::iterator iterCurrent_;
83 :
84 : OGRGeoJSONDataSource* poDS_;
85 : OGRFeatureDefn* poFeatureDefn_;
86 : OGRSpatialReference* poSRS_;
87 : CPLString sFIDColumn_;
88 : };
89 :
90 : /************************************************************************/
91 : /* OGRGeoJSONWriteLayer */
92 : /************************************************************************/
93 :
94 : class OGRGeoJSONWriteLayer : public OGRLayer
95 : {
96 : public:
97 : OGRGeoJSONWriteLayer( const char* pszName,
98 : OGRwkbGeometryType eGType,
99 : char** papszOptions,
100 : OGRGeoJSONDataSource* poDS );
101 : ~OGRGeoJSONWriteLayer();
102 :
103 : //
104 : // OGRLayer Interface
105 : //
106 27 : OGRFeatureDefn* GetLayerDefn() { return poFeatureDefn_; }
107 0 : OGRSpatialReference* GetSpatialRef() { return NULL; }
108 :
109 0 : void ResetReading() { }
110 0 : OGRFeature* GetNextFeature() { return NULL; }
111 : OGRErr CreateFeature( OGRFeature* poFeature );
112 : OGRErr CreateField(OGRFieldDefn* poField, int bApproxOK);
113 : int TestCapability( const char* pszCap );
114 :
115 : private:
116 :
117 : OGRGeoJSONDataSource* poDS_;
118 : OGRFeatureDefn* poFeatureDefn_;
119 : int nOutCounter_;
120 :
121 : int bWriteBBOX;
122 : int bBBOX3D;
123 : OGREnvelope3D sEnvelopeLayer;
124 :
125 : int nCoordPrecision;
126 : };
127 :
128 : /************************************************************************/
129 : /* OGRGeoJSONDataSource */
130 : /************************************************************************/
131 :
132 : class OGRGeoJSONDataSource : public OGRDataSource
133 : {
134 : public:
135 :
136 : OGRGeoJSONDataSource();
137 : ~OGRGeoJSONDataSource();
138 :
139 : //
140 : // OGRDataSource Interface
141 : //
142 : int Open( const char* pszSource );
143 : const char* GetName();
144 : int GetLayerCount();
145 : OGRLayer* GetLayer( int nLayer );
146 : OGRLayer* CreateLayer( const char* pszName,
147 : OGRSpatialReference* poSRS = NULL,
148 : OGRwkbGeometryType eGType = wkbUnknown,
149 : char** papszOptions = NULL );
150 : int TestCapability( const char* pszCap );
151 :
152 : //
153 : // OGRGeoJSONDataSource Interface
154 : //
155 : int Create( const char* pszName, char** papszOptions );
156 42 : VSILFILE* GetOutputFile() const { return fpOut_; }
157 :
158 : enum GeometryTranslation
159 : {
160 : eGeometryPreserve,
161 : eGeometryAsCollection,
162 : };
163 :
164 : void SetGeometryTranslation( GeometryTranslation type );
165 :
166 : enum AttributesTranslation
167 : {
168 : eAtributesPreserve,
169 : eAtributesSkip
170 : };
171 :
172 : void SetAttributesTranslation( AttributesTranslation type );
173 :
174 1 : int GetFpOutputIsSeekable() const { return bFpOutputIsSeekable_; }
175 1 : int GetBBOXInsertLocation() const { return nBBOXInsertLocation_; }
176 :
177 : private:
178 :
179 : //
180 : // Private data members
181 : //
182 : char* pszName_;
183 : char* pszGeoData_;
184 : OGRLayer** papoLayers_;
185 : int nLayers_;
186 : VSILFILE* fpOut_;
187 :
188 : //
189 : // Translation/Creation control flags
190 : //
191 : GeometryTranslation flTransGeom_;
192 : AttributesTranslation flTransAttrs_;
193 :
194 : int bFpOutputIsSeekable_;
195 : int nBBOXInsertLocation_;
196 :
197 : //
198 : // Priavte utility functions
199 : //
200 : void Clear();
201 : int ReadFromFile( const char* pszSource );
202 : int ReadFromService( const char* pszSource );
203 : OGRGeoJSONLayer* LoadLayer();
204 : };
205 :
206 :
207 : /************************************************************************/
208 : /* OGRGeoJSONDriver */
209 : /************************************************************************/
210 :
211 : class OGRGeoJSONDriver : public OGRSFDriver
212 : {
213 : public:
214 :
215 : OGRGeoJSONDriver();
216 : ~OGRGeoJSONDriver();
217 :
218 : //
219 : // OGRSFDriver Interface
220 : //
221 : const char* GetName();
222 : OGRDataSource* Open( const char* pszName, int bUpdate );
223 : OGRDataSource* CreateDataSource( const char* pszName, char** papszOptions );
224 : OGRErr DeleteDataSource( const char* pszName );
225 : int TestCapability( const char* pszCap );
226 :
227 : //
228 : // OGRGeoJSONDriver Interface
229 : //
230 : // NOTE: New version of Open() based on Andrey's RFC 10.
231 : OGRDataSource* Open( const char* pszName, int bUpdate,
232 : char** papszOptions );
233 :
234 : };
235 :
236 : #endif /* OGR_GEOJSON_H_INCLUDED */
237 :
|