1 : /******************************************************************************
2 : * $Id: ogrshapedriver.cpp 23963 2012-02-12 20:51:41Z 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 23963 2012-02-12 20:51:41Z rouault $");
35 :
36 : /************************************************************************/
37 : /* ~OGRShapeDriver() */
38 : /************************************************************************/
39 :
40 214 : OGRShapeDriver::~OGRShapeDriver()
41 :
42 : {
43 214 : }
44 :
45 : /************************************************************************/
46 : /* GetName() */
47 : /************************************************************************/
48 :
49 15664 : const char *OGRShapeDriver::GetName()
50 :
51 : {
52 15664 : return "ESRI Shapefile";
53 : }
54 :
55 : /************************************************************************/
56 : /* Open() */
57 : /************************************************************************/
58 :
59 1528 : OGRDataSource *OGRShapeDriver::Open( const char * pszFilename,
60 : int bUpdate )
61 :
62 : {
63 : OGRShapeDataSource *poDS;
64 :
65 1528 : poDS = new OGRShapeDataSource();
66 :
67 1528 : if( !poDS->Open( pszFilename, bUpdate, TRUE ) )
68 : {
69 954 : delete poDS;
70 954 : return NULL;
71 : }
72 : else
73 574 : return poDS;
74 : }
75 :
76 : /************************************************************************/
77 : /* CreateDataSource() */
78 : /************************************************************************/
79 :
80 135 : OGRDataSource *OGRShapeDriver::CreateDataSource( const char * pszName,
81 : char **papszOptions )
82 :
83 : {
84 : VSIStatBuf stat;
85 135 : int bSingleNewFile = FALSE;
86 :
87 : /* -------------------------------------------------------------------- */
88 : /* Is the target a valid existing directory? */
89 : /* -------------------------------------------------------------------- */
90 135 : if( CPLStat( pszName, &stat ) == 0 )
91 : {
92 10 : if( !VSI_ISDIR(stat.st_mode) )
93 : {
94 : CPLError( CE_Failure, CPLE_AppDefined,
95 : "%s is not a directory.\n",
96 0 : pszName );
97 :
98 0 : return NULL;
99 : }
100 : }
101 :
102 : /* -------------------------------------------------------------------- */
103 : /* Does it end in the extension .shp indicating the user likely */
104 : /* wants to create a single file set? */
105 : /* -------------------------------------------------------------------- */
106 125 : else if( EQUAL(CPLGetExtension(pszName),"shp")
107 : || EQUAL(CPLGetExtension(pszName),"dbf") )
108 : {
109 108 : bSingleNewFile = TRUE;
110 : }
111 :
112 : /* -------------------------------------------------------------------- */
113 : /* Otherwise try to create a new directory. */
114 : /* -------------------------------------------------------------------- */
115 : else
116 : {
117 17 : if( VSIMkdir( pszName, 0755 ) != 0 )
118 : {
119 : CPLError( CE_Failure, CPLE_AppDefined,
120 : "Failed to create directory %s\n"
121 : "for shapefile datastore.\n",
122 0 : pszName );
123 :
124 0 : return NULL;
125 : }
126 : }
127 :
128 : /* -------------------------------------------------------------------- */
129 : /* Return a new OGRDataSource() */
130 : /* -------------------------------------------------------------------- */
131 135 : OGRShapeDataSource *poDS = NULL;
132 :
133 135 : poDS = new OGRShapeDataSource();
134 :
135 135 : if( !poDS->Open( pszName, TRUE, FALSE, bSingleNewFile ) )
136 : {
137 0 : delete poDS;
138 0 : return NULL;
139 : }
140 : else
141 135 : return poDS;
142 : }
143 :
144 : /************************************************************************/
145 : /* DeleteDataSource() */
146 : /************************************************************************/
147 :
148 135 : OGRErr OGRShapeDriver::DeleteDataSource( const char *pszDataSource )
149 :
150 : {
151 : int iExt;
152 : VSIStatBufL sStatBuf;
153 : static const char *apszExtensions[] =
154 : { "shp", "shx", "dbf", "sbn", "sbx", "prj", "idm", "ind",
155 : "qix", NULL };
156 :
157 135 : if( VSIStatL( pszDataSource, &sStatBuf ) != 0 )
158 : {
159 : CPLError( CE_Failure, CPLE_AppDefined,
160 : "%s does not appear to be a file or directory.",
161 3 : pszDataSource );
162 :
163 3 : return OGRERR_FAILURE;
164 : }
165 :
166 132 : if( VSI_ISREG(sStatBuf.st_mode)
167 : && (EQUAL(CPLGetExtension(pszDataSource),"shp")
168 : || EQUAL(CPLGetExtension(pszDataSource),"shx")
169 : || EQUAL(CPLGetExtension(pszDataSource),"dbf")) )
170 : {
171 1050 : for( iExt=0; apszExtensions[iExt] != NULL; iExt++ )
172 : {
173 : const char *pszFile = CPLResetExtension(pszDataSource,
174 945 : apszExtensions[iExt] );
175 945 : if( VSIStatL( pszFile, &sStatBuf ) == 0 )
176 391 : VSIUnlink( pszFile );
177 : }
178 : }
179 27 : else if( VSI_ISDIR(sStatBuf.st_mode) )
180 : {
181 27 : char **papszDirEntries = CPLReadDir( pszDataSource );
182 : int iFile;
183 :
184 3814 : for( iFile = 0;
185 1907 : papszDirEntries != NULL && papszDirEntries[iFile] != NULL;
186 : iFile++ )
187 : {
188 3760 : if( CSLFindString( (char **) apszExtensions,
189 1880 : CPLGetExtension(papszDirEntries[iFile])) != -1)
190 : {
191 : VSIUnlink( CPLFormFilename( pszDataSource,
192 1819 : papszDirEntries[iFile],
193 3638 : NULL ) );
194 : }
195 : }
196 :
197 27 : CSLDestroy( papszDirEntries );
198 :
199 27 : VSIRmdir( pszDataSource );
200 : }
201 :
202 132 : return OGRERR_NONE;
203 : }
204 :
205 :
206 : /************************************************************************/
207 : /* TestCapability() */
208 : /************************************************************************/
209 :
210 75 : int OGRShapeDriver::TestCapability( const char * pszCap )
211 :
212 : {
213 75 : if( EQUAL(pszCap,ODrCCreateDataSource) )
214 75 : return TRUE;
215 0 : else if( EQUAL(pszCap,ODrCDeleteDataSource) )
216 0 : return TRUE;
217 : else
218 0 : return FALSE;
219 : }
220 :
221 : /************************************************************************/
222 : /* RegisterOGRShape() */
223 : /************************************************************************/
224 :
225 226 : void RegisterOGRShape()
226 :
227 : {
228 226 : OGRSFDriverRegistrar::GetRegistrar()->RegisterDriver( new OGRShapeDriver );
229 226 : }
230 :
|