1 : /******************************************************************************
2 : *
3 : * Project: KML Translator
4 : * Purpose: Implements OGRLIBKMLDriver
5 : * Author: Brian Case, rush at winkey dot org
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 2010, Brian Case
9 : *
10 : * Permission is hereby granted, free of charge, to any person obtaining a
11 : * copy of this software and associated documentation files (the "Software"),
12 : * to deal in the Software without restriction, including without limitation
13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 : * and/or sell copies of the Software, and to permit persons to whom the
15 : * Software is furnished to do so, subject to the following conditions:
16 : *
17 : * The above copyright notice and this permission notice shall be included
18 : * in all copies or substantial portions of the Software.
19 : *
20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 : * DEALINGS IN THE SOFTWARE.
27 : *****************************************************************************/
28 :
29 : #include "ogr_libkml.h"
30 : #include "cpl_conv.h"
31 : #include "cpl_error.h"
32 :
33 : #include <kml/dom.h>
34 :
35 : using kmldom::KmlFactory;
36 :
37 : /******************************************************************************
38 : OGRLIBKMLDriver()
39 : ******************************************************************************/
40 :
41 80 : OGRLIBKMLDriver::OGRLIBKMLDriver ( )
42 : {
43 80 : m_poKmlFactory = KmlFactory::GetFactory ( );
44 :
45 80 : }
46 :
47 : /******************************************************************************
48 : ~OGRLIBKMLDriver()
49 : ******************************************************************************/
50 :
51 72 : OGRLIBKMLDriver::~OGRLIBKMLDriver ( )
52 : {
53 72 : delete m_poKmlFactory;
54 :
55 72 : }
56 :
57 : /******************************************************************************
58 : GetName()
59 : ******************************************************************************/
60 :
61 : const char *OGRLIBKMLDriver::GetName (
62 3256 : )
63 : {
64 :
65 3256 : return "LIBKML";
66 : }
67 :
68 : /******************************************************************************
69 : Open()
70 : ******************************************************************************/
71 :
72 : OGRDataSource *OGRLIBKMLDriver::Open (
73 : const char *pszFilename,
74 15 : int bUpdate )
75 : {
76 15 : OGRLIBKMLDataSource *poDS = new OGRLIBKMLDataSource ( m_poKmlFactory );
77 :
78 15 : if ( !poDS->Open ( pszFilename, bUpdate ) ) {
79 12 : delete poDS;
80 :
81 12 : poDS = NULL;
82 : }
83 :
84 15 : return poDS;
85 : }
86 :
87 : /******************************************************************************
88 : CreateDataSource()
89 : ******************************************************************************/
90 :
91 : OGRDataSource *OGRLIBKMLDriver::CreateDataSource (
92 : const char *pszName,
93 1 : char **papszOptions )
94 : {
95 1 : CPLAssert ( NULL != pszName );
96 1 : CPLDebug ( "LIBKML", "Attempt to create: %s", pszName );
97 :
98 1 : OGRLIBKMLDataSource *poDS = new OGRLIBKMLDataSource ( m_poKmlFactory );
99 :
100 1 : if ( !poDS->Create ( pszName, papszOptions ) ) {
101 0 : delete poDS;
102 :
103 0 : poDS = NULL;
104 : }
105 :
106 1 : return poDS;
107 : }
108 :
109 : /******************************************************************************
110 : DeleteDataSource()
111 :
112 : note: this method recursivly deletes an entire dir if the datasource is a dir
113 : and all the files are kml or kmz
114 :
115 : ******************************************************************************/
116 :
117 : OGRErr OGRLIBKMLDriver::DeleteDataSource (
118 0 : const char *pszName )
119 : {
120 :
121 : /***** dir *****/
122 :
123 0 : VSIStatBufL sStatBuf = { };
124 0 : if ( !VSIStatL ( pszName, &sStatBuf ) && VSI_ISDIR ( sStatBuf.st_mode ) ) {
125 :
126 0 : char **papszDirList = NULL;
127 :
128 0 : if ( !( papszDirList = VSIReadDir ( pszName ) ) ) {
129 0 : if ( VSIRmdir ( pszName ) < 0 )
130 0 : return OGRERR_FAILURE;
131 : }
132 :
133 0 : int nFiles = CSLCount ( papszDirList );
134 : int iFile;
135 :
136 0 : for ( iFile = 0; iFile < nFiles; iFile++ ) {
137 0 : if ( OGRERR_FAILURE ==
138 : this->DeleteDataSource ( papszDirList[iFile] ) ) {
139 0 : CSLDestroy ( papszDirList );
140 0 : return OGRERR_FAILURE;
141 : }
142 : }
143 :
144 0 : if ( VSIRmdir ( pszName ) < 0 ) {
145 0 : CSLDestroy ( papszDirList );
146 0 : return OGRERR_FAILURE;
147 : }
148 :
149 0 : CSLDestroy ( papszDirList );
150 : }
151 :
152 : /***** kml *****/
153 :
154 0 : else if ( EQUAL ( CPLGetExtension ( pszName ), "kml" ) ) {
155 0 : if ( VSIUnlink ( pszName ) < 0 )
156 0 : return OGRERR_FAILURE;
157 : }
158 :
159 : /***** kmz *****/
160 :
161 0 : else if ( EQUAL ( CPLGetExtension ( pszName ), "kmz" ) ) {
162 0 : if ( VSIUnlink ( pszName ) < 0 )
163 0 : return OGRERR_FAILURE;
164 : }
165 :
166 : /***** do not delete other types of files *****/
167 :
168 : else
169 0 : return OGRERR_FAILURE;
170 :
171 0 : return OGRERR_NONE;
172 : }
173 :
174 : /******************************************************************************
175 : TestCapability()
176 : ******************************************************************************/
177 :
178 : int OGRLIBKMLDriver::TestCapability (
179 0 : const char *pszCap )
180 : {
181 0 : if ( EQUAL ( pszCap, ODrCCreateDataSource ) )
182 0 : return TRUE;
183 :
184 0 : else if ( EQUAL ( pszCap, ODrCDeleteDataSource ) )
185 0 : return bUpdate;
186 :
187 0 : return FALSE;
188 : }
189 :
190 : /******************************************************************************
191 : RegisterOGRLIBKML()
192 : ******************************************************************************/
193 :
194 : void RegisterOGRLIBKML (
195 80 : )
196 : {
197 : OGRSFDriverRegistrar::GetRegistrar ( )->
198 80 : RegisterDriver ( new OGRLIBKMLDriver );
199 :
200 80 : }
|