1 : /******************************************************************************
2 : * $Id: ogrcsvdatasource.cpp 17806 2009-10-13 17:27:54Z rouault $
3 : *
4 : * Project: PCIDSK Translator
5 : * Purpose: Implements OGRPCIDSKDataSource class
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2009, Frank Warmerdam <warmerdam@pobox.com>
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 "ogr_pcidsk.h"
31 : #include "cpl_conv.h"
32 : #include "cpl_string.h"
33 : #include "cpl_csv.h"
34 :
35 : CPL_CVSID("$Id: ogrcsvdatasource.cpp 17806 2009-10-13 17:27:54Z rouault $");
36 :
37 : const PCIDSK::PCIDSKInterfaces *PCIDSK2GetInterfaces(void);
38 :
39 : /************************************************************************/
40 : /* OGRPCIDSKDataSource() */
41 : /************************************************************************/
42 :
43 358 : OGRPCIDSKDataSource::OGRPCIDSKDataSource()
44 :
45 : {
46 358 : poFile = NULL;
47 358 : bUpdate = FALSE;
48 358 : }
49 :
50 : /************************************************************************/
51 : /* ~OGRPCIDSKDataSource() */
52 : /************************************************************************/
53 :
54 358 : OGRPCIDSKDataSource::~OGRPCIDSKDataSource()
55 :
56 : {
57 770 : while( apoLayers.size() > 0 )
58 : {
59 54 : delete apoLayers.back();
60 54 : apoLayers.pop_back();
61 : }
62 :
63 358 : if( poFile != NULL )
64 : {
65 18 : delete poFile;
66 18 : poFile = NULL;
67 : }
68 358 : }
69 :
70 : /************************************************************************/
71 : /* TestCapability() */
72 : /************************************************************************/
73 :
74 0 : int OGRPCIDSKDataSource::TestCapability( const char * pszCap )
75 :
76 : {
77 0 : if( EQUAL(pszCap,ODsCCreateLayer) )
78 0 : return bUpdate;
79 : else
80 0 : return FALSE;
81 : }
82 :
83 : /************************************************************************/
84 : /* GetLayer() */
85 : /************************************************************************/
86 :
87 194 : OGRLayer *OGRPCIDSKDataSource::GetLayer( int iLayer )
88 :
89 : {
90 194 : if( iLayer < 0 || iLayer >= (int) apoLayers.size() )
91 0 : return NULL;
92 : else
93 194 : return apoLayers[iLayer];
94 : }
95 :
96 : /************************************************************************/
97 : /* Open() */
98 : /************************************************************************/
99 :
100 358 : int OGRPCIDSKDataSource::Open( const char * pszFilename, int bUpdateIn )
101 :
102 : {
103 358 : if( !EQUAL(CPLGetExtension(pszFilename),"pix") )
104 340 : return FALSE;
105 :
106 18 : osName = pszFilename;
107 18 : if( bUpdateIn )
108 8 : bUpdate = true;
109 : else
110 10 : bUpdate = false;
111 :
112 : /* -------------------------------------------------------------------- */
113 : /* Open the file, and create layer for each vector segment. */
114 : /* -------------------------------------------------------------------- */
115 : try
116 : {
117 : PCIDSK::PCIDSKSegment *segobj;
118 18 : const char *pszAccess = "r";
119 :
120 18 : if( bUpdateIn )
121 8 : pszAccess = "r+";
122 :
123 : poFile = PCIDSK::Open( pszFilename, pszAccess,
124 18 : PCIDSK2GetInterfaces() );
125 :
126 116 : for( segobj = poFile->GetSegment( PCIDSK::SEG_VEC, "" );
127 : segobj != NULL;
128 : segobj = poFile->GetSegment( PCIDSK::SEG_VEC, "",
129 40 : segobj->GetSegmentNumber() ) )
130 : {
131 40 : apoLayers.push_back( new OGRPCIDSKLayer( segobj, bUpdate ) );
132 : }
133 :
134 : /* Check if this is a raster-only PCIDSK file */
135 18 : if ( !bUpdate && apoLayers.size() == 0 && poFile->GetChannels() != 0 )
136 2 : return FALSE;
137 : }
138 :
139 : /* -------------------------------------------------------------------- */
140 : /* Trap exceptions and report as CPL errors. */
141 : /* -------------------------------------------------------------------- */
142 0 : catch( PCIDSK::PCIDSKException ex )
143 : {
144 : CPLError( CE_Failure, CPLE_AppDefined,
145 0 : "%s", ex.what() );
146 0 : return FALSE;
147 : }
148 0 : catch(...)
149 : {
150 : CPLError( CE_Failure, CPLE_AppDefined,
151 0 : "Non-PCIDSK exception trapped." );
152 0 : return FALSE;
153 : }
154 :
155 16 : return TRUE;
156 : }
157 :
158 : /************************************************************************/
159 : /* CreateLayer() */
160 : /************************************************************************/
161 :
162 : OGRLayer *
163 14 : OGRPCIDSKDataSource::CreateLayer( const char * pszLayerName,
164 : OGRSpatialReference *poSRS,
165 : OGRwkbGeometryType eType,
166 : char ** papszOptions )
167 :
168 : {
169 : /* -------------------------------------------------------------------- */
170 : /* Verify we are in update mode. */
171 : /* -------------------------------------------------------------------- */
172 14 : if( !bUpdate )
173 : {
174 : CPLError( CE_Failure, CPLE_NoWriteAccess,
175 : "Data source %s opened read-only.\n"
176 : "New layer %s cannot be created.\n",
177 0 : osName.c_str(), pszLayerName );
178 :
179 0 : return NULL;
180 : }
181 :
182 : /* -------------------------------------------------------------------- */
183 : /* Figure out what type of layer we need. */
184 : /* -------------------------------------------------------------------- */
185 14 : std::string osLayerType;
186 :
187 14 : switch( wkbFlatten(eType) )
188 : {
189 : case wkbPoint:
190 4 : osLayerType = "POINTS";
191 4 : break;
192 :
193 : case wkbLineString:
194 4 : osLayerType = "ARCS";
195 4 : break;
196 :
197 : case wkbPolygon:
198 0 : osLayerType = "WHOLE_POLYGONS";
199 0 : break;
200 :
201 : case wkbNone:
202 4 : osLayerType = "TABLE";
203 : break;
204 :
205 : default:
206 : break;
207 : }
208 :
209 : /* -------------------------------------------------------------------- */
210 : /* Create the segment. */
211 : /* -------------------------------------------------------------------- */
212 : int nSegNum = poFile->CreateSegment( pszLayerName, "",
213 14 : PCIDSK::SEG_VEC, 0L );
214 28 : PCIDSK::PCIDSKSegment *poSeg = poFile->GetSegment( nSegNum );
215 : PCIDSK::PCIDSKVectorSegment *poVecSeg =
216 14 : dynamic_cast<PCIDSK::PCIDSKVectorSegment*>( poSeg );
217 :
218 14 : if( osLayerType != "" )
219 12 : poSeg->SetMetadataValue( "LAYER_TYPE", osLayerType );
220 :
221 : /* -------------------------------------------------------------------- */
222 : /* Do we need to apply a coordinate system? */
223 : /* -------------------------------------------------------------------- */
224 14 : char *pszGeosys = NULL;
225 14 : char *pszUnits = NULL;
226 14 : double *padfPrjParams = NULL;
227 :
228 14 : if( poSRS != NULL
229 : && poSRS->exportToPCI( &pszGeosys, &pszUnits,
230 : &padfPrjParams ) == OGRERR_NONE )
231 : {
232 : try
233 : {
234 4 : std::vector<double> adfPCIParameters;
235 : int i;
236 :
237 72 : for( i = 0; i < 17; i++ )
238 68 : adfPCIParameters.push_back( padfPrjParams[i] );
239 :
240 4 : if( EQUALN(pszUnits,"FOOT",4) )
241 : adfPCIParameters.push_back(
242 0 : (double)(int) PCIDSK::UNIT_US_FOOT );
243 4 : else if( EQUALN(pszUnits,"INTL FOOT",9) )
244 : adfPCIParameters.push_back(
245 0 : (double)(int) PCIDSK::UNIT_INTL_FOOT );
246 4 : else if( EQUALN(pszUnits,"DEGREE",6) )
247 : adfPCIParameters.push_back(
248 2 : (double)(int) PCIDSK::UNIT_DEGREE );
249 : else
250 : adfPCIParameters.push_back(
251 2 : (double)(int) PCIDSK::UNIT_METER );
252 :
253 4 : poVecSeg->SetProjection( pszGeosys, adfPCIParameters );
254 : }
255 0 : catch( PCIDSK::PCIDSKException ex )
256 : {
257 : CPLError( CE_Failure, CPLE_AppDefined,
258 0 : "%s", ex.what() );
259 : }
260 :
261 4 : CPLFree( pszGeosys );
262 4 : CPLFree( pszUnits );
263 4 : CPLFree( padfPrjParams );
264 : }
265 :
266 : /* -------------------------------------------------------------------- */
267 : /* Create the layer object. */
268 : /* -------------------------------------------------------------------- */
269 14 : apoLayers.push_back( new OGRPCIDSKLayer( poSeg, TRUE ) );
270 :
271 14 : return apoLayers[apoLayers.size()-1];
272 : }
273 :
|