1 : /******************************************************************************
2 : * $Id$
3 : *
4 : * Project: OpenGIS Simple Features Reference Implementation
5 : * Purpose: Implementation of OGRGeoJSONDriver 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 "ogr_geojson.h"
30 : #include <cpl_conv.h>
31 :
32 : /************************************************************************/
33 : /* OGRGeoJSONDriver() */
34 : /************************************************************************/
35 :
36 64 : OGRGeoJSONDriver::OGRGeoJSONDriver()
37 : {
38 64 : }
39 :
40 : /************************************************************************/
41 : /* ~OGRGeoJSONDriver() */
42 : /************************************************************************/
43 :
44 96 : OGRGeoJSONDriver::~OGRGeoJSONDriver()
45 : {
46 96 : }
47 :
48 : /************************************************************************/
49 : /* GetName() */
50 : /************************************************************************/
51 :
52 1923 : const char* OGRGeoJSONDriver::GetName()
53 : {
54 1923 : return "GeoJSON";
55 : }
56 :
57 : /************************************************************************/
58 : /* Open() */
59 : /************************************************************************/
60 :
61 117 : OGRDataSource* OGRGeoJSONDriver::Open( const char* pszName, int bUpdate )
62 : {
63 117 : return Open( pszName, bUpdate, NULL );
64 : }
65 :
66 : /************************************************************************/
67 : /* Open() */
68 : /************************************************************************/
69 :
70 117 : OGRDataSource* OGRGeoJSONDriver::Open( const char* pszName, int bUpdate,
71 : char** papszOptions )
72 : {
73 : UNREFERENCED_PARAM(papszOptions);
74 :
75 117 : OGRGeoJSONDataSource* poDS = NULL;
76 117 : poDS = new OGRGeoJSONDataSource();
77 :
78 : /* -------------------------------------------------------------------- */
79 : /* Processing configuration options. */
80 : /* -------------------------------------------------------------------- */
81 :
82 : // TODO: Currently, options are based on environment variables.
83 : // This is workaround for not yet implemented Andrey's concept
84 : // described in document 'RFC 10: OGR Open Parameters'.
85 :
86 117 : poDS->SetGeometryTranslation( OGRGeoJSONDataSource::eGeometryPreserve );
87 117 : const char* pszOpt = CPLGetConfigOption("GEOMETRY_AS_COLLECTION", NULL);
88 117 : if( NULL != pszOpt && EQUALN(pszOpt, "YES", 3) )
89 : {
90 : poDS->SetGeometryTranslation(
91 0 : OGRGeoJSONDataSource::eGeometryAsCollection );
92 : }
93 :
94 117 : poDS->SetAttributesTranslation( OGRGeoJSONDataSource::eAtributesPreserve );
95 117 : pszOpt = CPLGetConfigOption("ATTRIBUTES_SKIP", NULL);
96 117 : if( NULL != pszOpt && EQUALN(pszOpt, "YES", 3) )
97 : {
98 : poDS->SetAttributesTranslation(
99 0 : OGRGeoJSONDataSource::eAtributesSkip );
100 : }
101 :
102 : /* -------------------------------------------------------------------- */
103 : /* Open and start processing GeoJSON datasoruce to OGR objects. */
104 : /* -------------------------------------------------------------------- */
105 117 : if( !poDS->Open( pszName ) )
106 : {
107 103 : delete poDS;
108 103 : poDS= NULL;
109 : }
110 :
111 117 : if( NULL != poDS && bUpdate )
112 : {
113 : CPLError( CE_Failure, CPLE_OpenFailed,
114 0 : "GeoJSON Driver doesn't support update." );
115 0 : delete poDS;
116 0 : return NULL;
117 : }
118 :
119 117 : return poDS;
120 : }
121 :
122 : /************************************************************************/
123 : /* CreateDataSource() */
124 : /************************************************************************/
125 :
126 6 : OGRDataSource* OGRGeoJSONDriver::CreateDataSource( const char* pszName,
127 : char** papszOptions )
128 : {
129 6 : OGRGeoJSONDataSource* poDS = new OGRGeoJSONDataSource();
130 :
131 6 : if( !poDS->Create( pszName, papszOptions ) )
132 : {
133 0 : delete poDS;
134 0 : poDS = NULL;
135 : }
136 :
137 6 : return poDS;
138 : }
139 :
140 : /************************************************************************/
141 : /* DeleteDataSource() */
142 : /************************************************************************/
143 :
144 6 : OGRErr OGRGeoJSONDriver::DeleteDataSource( const char* pszName )
145 : {
146 6 : if( VSIUnlink( pszName ) == 0 )
147 : {
148 6 : return OGRERR_NONE;
149 : }
150 :
151 0 : CPLDebug( "GeoJSON", "Failed to delete \'%s\'", pszName);
152 :
153 0 : return OGRERR_FAILURE;
154 : }
155 :
156 : /************************************************************************/
157 : /* TestCapability() */
158 : /************************************************************************/
159 :
160 0 : int OGRGeoJSONDriver::TestCapability( const char* pszCap )
161 : {
162 0 : if( EQUAL( pszCap, ODrCCreateDataSource ) )
163 0 : return TRUE;
164 0 : else if( EQUAL(pszCap, ODrCDeleteDataSource) )
165 0 : return TRUE;
166 : else
167 0 : return FALSE;
168 : }
169 :
170 : /************************************************************************/
171 : /* RegisterOGRGeoJSON() */
172 : /************************************************************************/
173 :
174 64 : void RegisterOGRGeoJSON()
175 : {
176 64 : if( GDAL_CHECK_VERSION("OGR/GeoJSON driver") )
177 : {
178 : OGRSFDriverRegistrar::GetRegistrar()->RegisterDriver(
179 64 : new OGRGeoJSONDriver );
180 : }
181 64 : }
182 :
|