1 : /******************************************************************************
2 : * $Id: ogrvfkdatasource.cpp 24217 2012-04-11 14:25:09Z martinl $
3 : *
4 : * Project: OpenGIS Simple Features Reference Implementation
5 : * Purpose: Implements OGRVFKDatasource class.
6 : * Author: Martin Landa, landa.martin gmail.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2009-2010, Martin Landa <landa.martin gmail.com>
10 : *
11 : * Permission is hereby granted, free of charge, to any person
12 : * obtaining a copy of this software and associated documentation
13 : * files (the "Software"), to deal in the Software without
14 : * restriction, including without limitation the rights to use, copy,
15 : * modify, merge, publish, distribute, sublicense, and/or sell copies
16 : * of the Software, and to permit persons to whom the Software is
17 : * furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be
20 : * included in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 : * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26 : * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 : * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28 : * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 : * SOFTWARE.
30 : ****************************************************************************/
31 :
32 : #include "ogr_vfk.h"
33 : #include "cpl_conv.h"
34 : #include "cpl_string.h"
35 :
36 : CPL_CVSID("$Id: ogrvfkdatasource.cpp 24217 2012-04-11 14:25:09Z martinl $");
37 :
38 : /*!
39 : \brief OGRVFKDataSource constructor
40 : */
41 212 : OGRVFKDataSource::OGRVFKDataSource()
42 : {
43 212 : pszName = NULL;
44 :
45 212 : poReader = NULL;
46 :
47 212 : papoLayers = NULL;
48 212 : nLayers = 0;
49 212 : }
50 :
51 : /*!
52 : \brief OGRVFKDataSource destructor
53 : */
54 212 : OGRVFKDataSource::~OGRVFKDataSource()
55 : {
56 212 : CPLFree(pszName);
57 :
58 212 : if (poReader)
59 2 : delete poReader;
60 :
61 334 : for(int i = 0; i < nLayers; i++)
62 122 : delete papoLayers[i];
63 :
64 212 : CPLFree(papoLayers);
65 212 : }
66 :
67 : /*!
68 : \brief Open VFK datasource
69 :
70 : \param pszNewName datasource name
71 : \param bTextOpen True to test if datasource is possible to open
72 :
73 : \return TRUE on success
74 : \return FALSE on failure
75 : */
76 212 : int OGRVFKDataSource::Open(const char *pszNewName, int bTestOpen)
77 : {
78 : FILE * fp;
79 : char szHeader[1000];
80 :
81 : /* open the source file */
82 212 : fp = VSIFOpen(pszNewName, "r");
83 212 : if (fp == NULL) {
84 80 : if (!bTestOpen)
85 : CPLError(CE_Failure, CPLE_OpenFailed,
86 : "Failed to open VFK file `%s'.",
87 0 : pszNewName);
88 :
89 80 : return FALSE;
90 : }
91 :
92 : /* If we aren't sure it is VFK, load a header chunk and check
93 : for signs it is VFK */
94 132 : if (bTestOpen) {
95 132 : size_t nRead = VSIFRead(szHeader, 1, sizeof(szHeader), fp);
96 132 : if (nRead <= 0) {
97 0 : VSIFClose(fp);
98 0 : return FALSE;
99 : }
100 132 : szHeader[MIN(nRead, sizeof(szHeader))-1] = '\0';
101 :
102 : // TODO: improve check
103 132 : if (strncmp(szHeader, "&HVERZE;", 8) != 0) {
104 130 : VSIFClose(fp);
105 130 : return FALSE;
106 : }
107 : }
108 :
109 : /* We assume now that it is VFK. Close and instantiate a
110 : VFKReader on it. */
111 2 : VSIFClose(fp);
112 :
113 2 : pszName = CPLStrdup(pszNewName);
114 :
115 2 : poReader = CreateVFKReader(pszNewName);
116 2 : if (poReader == NULL) {
117 : CPLError(CE_Failure, CPLE_AppDefined,
118 : "File %s appears to be VFK but the VFK reader can't"
119 : "be instantiated.",
120 0 : pszNewName);
121 0 : return FALSE;
122 : }
123 :
124 : #ifndef HAVE_SQLITE
125 : CPLError(CE_Warning, CPLE_AppDefined,
126 : "OGR is not compiled with SQLite support. "
127 : "VFK driver will not work properly.");
128 : #endif
129 :
130 : /* read data blocks, i.e. &B */
131 2 : poReader->ReadDataBlocks();
132 :
133 : /* get list of layers */
134 2 : papoLayers = (OGRVFKLayer **) CPLCalloc(sizeof(OGRVFKLayer *), poReader->GetDataBlockCount());
135 :
136 124 : for (int iLayer = 0; iLayer < poReader->GetDataBlockCount(); iLayer++) {
137 122 : papoLayers[iLayer] = CreateLayerFromBlock(poReader->GetDataBlock(iLayer));
138 122 : nLayers++;
139 : }
140 :
141 2 : return TRUE;
142 : }
143 :
144 : /*!
145 : \brief Get VFK layer
146 :
147 : \param iLayer layer number
148 :
149 : \return pointer to OGRLayer instance
150 : \return NULL on error
151 : */
152 192 : OGRLayer *OGRVFKDataSource::GetLayer(int iLayer)
153 : {
154 192 : if( iLayer < 0 || iLayer >= nLayers )
155 0 : return NULL;
156 :
157 192 : return papoLayers[iLayer];
158 : }
159 :
160 : /*!
161 : \brief Test datasource capabilies
162 :
163 : \param pszCap capability
164 :
165 : \return True if supported
166 : \return False if not supported
167 : */
168 0 : int OGRVFKDataSource::TestCapability(const char * pszCap)
169 : {
170 0 : return FALSE;
171 : }
172 :
173 : /*!
174 : \brief Create OGR layer from VFKDataBlock
175 :
176 : \param poDataBlock pointer to VFKDataBlock instance
177 :
178 : \return poiter to OGRVFKLayer instance
179 : \return NULL on error
180 : */
181 122 : OGRVFKLayer *OGRVFKDataSource::CreateLayerFromBlock(const IVFKDataBlock *poDataBlock)
182 : {
183 : OGRVFKLayer *poLayer;
184 :
185 122 : poLayer = NULL;
186 :
187 : /* create an empty layer */
188 : poLayer = new OGRVFKLayer(poDataBlock->GetName(), NULL,
189 122 : poDataBlock->GetGeometryType(), this);
190 :
191 : /* define attributes (properties) */
192 1272 : for (int iField = 0; iField < poDataBlock->GetPropertyCount(); iField++) {
193 1150 : VFKPropertyDefn *poProperty = poDataBlock->GetProperty(iField);
194 1150 : OGRFieldDefn oField(poProperty->GetName(), poProperty->GetType());
195 :
196 1150 : if(poProperty->GetWidth() > 0)
197 1150 : oField.SetWidth(poProperty->GetWidth());
198 1150 : if(poProperty->GetPrecision() > 0)
199 60 : oField.SetPrecision(poProperty->GetPrecision());
200 :
201 1150 : poLayer->GetLayerDefn()->AddFieldDefn(&oField);
202 : }
203 :
204 122 : return poLayer;
205 : }
|