1 : /******************************************************************************
2 : * $Id: ogrshapedriver.cpp 15755 2008-11-17 23:13:50Z rouault $
3 : *
4 : * Project: OpenGIS Simple Features Reference Implementation
5 : * Purpose: Implements OGRShapeDriver class.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 1999, Les Technologies SoftMap Inc.
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 :
30 : #include "ogrshape.h"
31 : #include "cpl_conv.h"
32 : #include "cpl_string.h"
33 :
34 : CPL_CVSID("$Id: ogrshapedriver.cpp 15755 2008-11-17 23:13:50Z rouault $");
35 :
36 : /************************************************************************/
37 : /* ~OGRShapeDriver() */
38 : /************************************************************************/
39 :
40 96 : OGRShapeDriver::~OGRShapeDriver()
41 :
42 : {
43 96 : }
44 :
45 : /************************************************************************/
46 : /* GetName() */
47 : /************************************************************************/
48 :
49 2201 : const char *OGRShapeDriver::GetName()
50 :
51 : {
52 2201 : return "ESRI Shapefile";
53 : }
54 :
55 : /************************************************************************/
56 : /* Open() */
57 : /************************************************************************/
58 :
59 385 : OGRDataSource *OGRShapeDriver::Open( const char * pszFilename,
60 : int bUpdate )
61 :
62 : {
63 : OGRShapeDataSource *poDS;
64 :
65 385 : poDS = new OGRShapeDataSource();
66 :
67 : /* If no layer was found, return NULL otherwise how will we allow any other driver */
68 : /* that is directory oriented to work? I just don't see how we can know the */
69 : /* directory is for shapefiles if there aren't any in it (#2686) */
70 570 : if( !poDS->Open( pszFilename, bUpdate, TRUE )
71 185 : || poDS->GetLayerCount() == 0 )
72 : {
73 204 : delete poDS;
74 204 : return NULL;
75 : }
76 : else
77 181 : return poDS;
78 : }
79 :
80 : /************************************************************************/
81 : /* CreateDataSource() */
82 : /************************************************************************/
83 :
84 35 : OGRDataSource *OGRShapeDriver::CreateDataSource( const char * pszName,
85 : char **papszOptions )
86 :
87 : {
88 : VSIStatBuf stat;
89 35 : int bSingleNewFile = FALSE;
90 :
91 : /* -------------------------------------------------------------------- */
92 : /* Is the target a valid existing directory? */
93 : /* -------------------------------------------------------------------- */
94 35 : if( CPLStat( pszName, &stat ) == 0 )
95 : {
96 6 : if( !VSI_ISDIR(stat.st_mode) )
97 : {
98 : CPLError( CE_Failure, CPLE_AppDefined,
99 : "%s is not a directory.\n",
100 0 : pszName );
101 :
102 0 : return NULL;
103 : }
104 : }
105 :
106 : /* -------------------------------------------------------------------- */
107 : /* Does it end in the extension .shp indicating the user likely */
108 : /* wants to create a single file set? */
109 : /* -------------------------------------------------------------------- */
110 29 : else if( EQUAL(CPLGetExtension(pszName),"shp")
111 : || EQUAL(CPLGetExtension(pszName),"dbf") )
112 : {
113 29 : bSingleNewFile = TRUE;
114 : }
115 :
116 : /* -------------------------------------------------------------------- */
117 : /* Otherwise try to create a new directory. */
118 : /* -------------------------------------------------------------------- */
119 : else
120 : {
121 0 : if( VSIMkdir( pszName, 0755 ) != 0 )
122 : {
123 : CPLError( CE_Failure, CPLE_AppDefined,
124 : "Failed to create directory %s\n"
125 : "for shapefile datastore.\n",
126 0 : pszName );
127 :
128 0 : return NULL;
129 : }
130 : }
131 :
132 : /* -------------------------------------------------------------------- */
133 : /* Return a new OGRDataSource() */
134 : /* -------------------------------------------------------------------- */
135 35 : OGRShapeDataSource *poDS = NULL;
136 :
137 35 : poDS = new OGRShapeDataSource();
138 :
139 35 : if( !poDS->Open( pszName, TRUE, FALSE, bSingleNewFile ) )
140 : {
141 0 : delete poDS;
142 0 : return NULL;
143 : }
144 : else
145 35 : return poDS;
146 : }
147 :
148 : /************************************************************************/
149 : /* DeleteDataSource() */
150 : /************************************************************************/
151 :
152 38 : OGRErr OGRShapeDriver::DeleteDataSource( const char *pszDataSource )
153 :
154 : {
155 : int iExt;
156 : VSIStatBuf sStatBuf;
157 : static const char *apszExtensions[] =
158 : { "shp", "shx", "dbf", "sbn", "sbx", "prj", "idm", "ind",
159 : "qix", NULL };
160 :
161 38 : if( VSIStat( pszDataSource, &sStatBuf ) != 0 )
162 : {
163 : CPLError( CE_Failure, CPLE_AppDefined,
164 : "%s does not appear to be a file or directory.",
165 1 : pszDataSource );
166 :
167 1 : return OGRERR_FAILURE;
168 : }
169 :
170 37 : if( VSI_ISREG(sStatBuf.st_mode)
171 : && (EQUAL(CPLGetExtension(pszDataSource),"shp")
172 : || EQUAL(CPLGetExtension(pszDataSource),"shx")
173 : || EQUAL(CPLGetExtension(pszDataSource),"dbf")) )
174 : {
175 300 : for( iExt=0; apszExtensions[iExt] != NULL; iExt++ )
176 : {
177 : const char *pszFile = CPLResetExtension(pszDataSource,
178 270 : apszExtensions[iExt] );
179 270 : if( VSIStat( pszFile, &sStatBuf ) == 0 )
180 114 : VSIUnlink( pszFile );
181 : }
182 : }
183 7 : else if( VSI_ISDIR(sStatBuf.st_mode) )
184 : {
185 7 : char **papszDirEntries = CPLReadDir( pszDataSource );
186 : int iFile;
187 :
188 392 : for( iFile = 0;
189 196 : papszDirEntries != NULL && papszDirEntries[iFile] != NULL;
190 : iFile++ )
191 : {
192 378 : if( CSLFindString( (char **) apszExtensions,
193 189 : CPLGetExtension(papszDirEntries[iFile])) != -1)
194 : {
195 : VSIUnlink( CPLFormFilename( pszDataSource,
196 158 : papszDirEntries[iFile],
197 316 : NULL ) );
198 : }
199 : }
200 :
201 7 : CSLDestroy( papszDirEntries );
202 :
203 7 : VSIRmdir( pszDataSource );
204 : }
205 :
206 37 : return OGRERR_NONE;
207 : }
208 :
209 :
210 : /************************************************************************/
211 : /* TestCapability() */
212 : /************************************************************************/
213 :
214 20 : int OGRShapeDriver::TestCapability( const char * pszCap )
215 :
216 : {
217 20 : if( EQUAL(pszCap,ODrCCreateDataSource) )
218 20 : return TRUE;
219 0 : else if( EQUAL(pszCap,ODrCDeleteDataSource) )
220 0 : return TRUE;
221 : else
222 0 : return FALSE;
223 : }
224 :
225 : /************************************************************************/
226 : /* RegisterOGRShape() */
227 : /************************************************************************/
228 :
229 64 : void RegisterOGRShape()
230 :
231 : {
232 64 : OGRSFDriverRegistrar::GetRegistrar()->RegisterDriver( new OGRShapeDriver );
233 64 : }
234 :
|