1 : /******************************************************************************
2 : * $Id: ogridrisidatasource.cpp 23040 2011-09-04 14:42:12Z rouault $
3 : *
4 : * Project: Idrisi Translator
5 : * Purpose: Implements OGRIdrisiDataSource class
6 : * Author: Even Rouault, even dot rouault at mines dash paris dot org
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2011, Even Rouault <even dot rouault at mines dash paris dot org>
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_idrisi.h"
31 : #include "cpl_conv.h"
32 : #include "cpl_string.h"
33 : #include "idrisi.h"
34 :
35 : CPL_CVSID("$Id: ogridrisidatasource.cpp 23040 2011-09-04 14:42:12Z rouault $");
36 :
37 : /************************************************************************/
38 : /* OGRIdrisiDataSource() */
39 : /************************************************************************/
40 :
41 36 : OGRIdrisiDataSource::OGRIdrisiDataSource()
42 :
43 : {
44 36 : papoLayers = NULL;
45 36 : nLayers = 0;
46 :
47 36 : pszName = NULL;
48 36 : }
49 :
50 : /************************************************************************/
51 : /* ~OGRIdrisiDataSource() */
52 : /************************************************************************/
53 :
54 36 : OGRIdrisiDataSource::~OGRIdrisiDataSource()
55 :
56 : {
57 39 : for( int i = 0; i < nLayers; i++ )
58 3 : delete papoLayers[i];
59 36 : CPLFree( papoLayers );
60 :
61 36 : CPLFree( pszName );
62 36 : }
63 :
64 : /************************************************************************/
65 : /* TestCapability() */
66 : /************************************************************************/
67 :
68 0 : int OGRIdrisiDataSource::TestCapability( const char * pszCap )
69 :
70 : {
71 0 : return FALSE;
72 : }
73 :
74 : /************************************************************************/
75 : /* GetLayer() */
76 : /************************************************************************/
77 :
78 3 : OGRLayer *OGRIdrisiDataSource::GetLayer( int iLayer )
79 :
80 : {
81 3 : if( iLayer < 0 || iLayer >= nLayers )
82 0 : return NULL;
83 : else
84 3 : return papoLayers[iLayer];
85 : }
86 :
87 : /************************************************************************/
88 : /* Open() */
89 : /************************************************************************/
90 :
91 36 : int OGRIdrisiDataSource::Open( const char * pszFilename, int bUpdateIn)
92 :
93 : {
94 36 : if (bUpdateIn)
95 : {
96 5 : return FALSE;
97 : }
98 :
99 : // --------------------------------------------------------------------
100 : // Does this appear to be a .vct file?
101 : // --------------------------------------------------------------------
102 31 : if ( !EQUAL(CPLGetExtension(pszFilename), "vct") )
103 28 : return FALSE;
104 :
105 3 : pszName = CPLStrdup( pszFilename );
106 :
107 3 : VSILFILE* fpVCT = VSIFOpenL(pszFilename, "rb");
108 3 : if (fpVCT == NULL)
109 0 : return FALSE;
110 :
111 3 : char* pszWTKString = NULL;
112 :
113 : // --------------------------------------------------------------------
114 : // Look for .vdc file
115 : // --------------------------------------------------------------------
116 3 : const char* pszVDCFilename = CPLResetExtension(pszFilename, "vdc");
117 3 : VSILFILE* fpVDC = VSIFOpenL(pszVDCFilename, "rb");
118 3 : if (fpVDC == NULL)
119 : {
120 0 : pszVDCFilename = CPLResetExtension(pszFilename, "VDC");
121 0 : fpVDC = VSIFOpenL(pszVDCFilename, "rb");
122 : }
123 :
124 3 : char** papszVDC = NULL;
125 3 : if (fpVDC != NULL)
126 : {
127 3 : VSIFCloseL(fpVDC);
128 3 : fpVDC = NULL;
129 :
130 3 : CPLPushErrorHandler(CPLQuietErrorHandler);
131 3 : papszVDC = CSLLoad2(pszVDCFilename, 1024, 256, NULL);
132 3 : CPLPopErrorHandler();
133 3 : CPLErrorReset();
134 : }
135 :
136 3 : OGRwkbGeometryType eType = wkbUnknown;
137 :
138 3 : if (papszVDC != NULL)
139 : {
140 3 : CSLSetNameValueSeparator( papszVDC, ":" );
141 :
142 3 : const char *pszVersion = CSLFetchNameValue( papszVDC, "file format " );
143 :
144 3 : if( pszVersion == NULL || !EQUAL( pszVersion, "IDRISI Vector A.1" ) )
145 : {
146 0 : CSLDestroy( papszVDC );
147 0 : VSIFCloseL(fpVCT);
148 0 : return FALSE;
149 : }
150 :
151 3 : const char *pszRefSystem = CSLFetchNameValue( papszVDC, "ref. system " );
152 3 : const char *pszRefUnits = CSLFetchNameValue( papszVDC, "ref. units " );
153 :
154 3 : if (pszRefSystem != NULL && pszRefUnits != NULL)
155 3 : IdrisiGeoReference2Wkt( pszFilename, pszRefSystem, pszRefUnits, &pszWTKString);
156 : }
157 :
158 : GByte chType;
159 3 : if (VSIFReadL(&chType, 1, 1, fpVCT) != 1)
160 : {
161 0 : VSIFCloseL(fpVCT);
162 0 : CSLDestroy( papszVDC );
163 0 : return FALSE;
164 : }
165 :
166 3 : if (chType == 1)
167 1 : eType = wkbPoint;
168 2 : else if (chType == 2)
169 1 : eType = wkbLineString;
170 1 : else if (chType == 3)
171 1 : eType = wkbPolygon;
172 : else
173 : {
174 : CPLError(CE_Failure, CPLE_AppDefined, "Unsupport geometry type : %d",
175 0 : (int)chType);
176 0 : VSIFCloseL(fpVCT);
177 0 : CSLDestroy( papszVDC );
178 0 : return FALSE;
179 : }
180 :
181 3 : const char *pszMinX = CSLFetchNameValue( papszVDC, "min. X " );
182 3 : const char *pszMaxX = CSLFetchNameValue( papszVDC, "max. X " );
183 3 : const char *pszMinY = CSLFetchNameValue( papszVDC, "min. Y " );
184 3 : const char *pszMaxY = CSLFetchNameValue( papszVDC, "max. Y " );
185 :
186 3 : OGRIdrisiLayer* poLayer = new OGRIdrisiLayer(CPLGetBasename(pszFilename), fpVCT, eType, pszWTKString);
187 3 : papoLayers = (OGRLayer**) CPLMalloc(sizeof(OGRLayer*));
188 3 : papoLayers[nLayers ++] = poLayer;
189 :
190 6 : if (pszMinX != NULL && pszMaxX != NULL && pszMinY != NULL && pszMaxY != NULL)
191 : {
192 3 : poLayer->SetExtent(CPLAtof(pszMinX), CPLAtof(pszMinY), CPLAtof(pszMaxX), CPLAtof(pszMaxY));
193 : }
194 :
195 3 : CPLFree(pszWTKString);
196 :
197 3 : CSLDestroy( papszVDC );
198 :
199 3 : return TRUE;
200 : }
|