1 : /******************************************************************************
2 : * $Id: ogrvfkdatasource.cpp 25703 2013-03-07 18:23:48Z 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, 2013 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 25703 2013-03-07 18:23:48Z martinl $");
37 :
38 : /*!
39 : \brief OGRVFKDataSource constructor
40 : */
41 148 : OGRVFKDataSource::OGRVFKDataSource()
42 : {
43 148 : pszName = NULL;
44 :
45 148 : poReader = NULL;
46 :
47 148 : papoLayers = NULL;
48 148 : nLayers = 0;
49 148 : }
50 :
51 : /*!
52 : \brief OGRVFKDataSource destructor
53 : */
54 148 : OGRVFKDataSource::~OGRVFKDataSource()
55 : {
56 148 : CPLFree(pszName);
57 :
58 148 : if (poReader)
59 2 : delete poReader;
60 :
61 270 : for(int i = 0; i < nLayers; i++)
62 122 : delete papoLayers[i];
63 :
64 148 : CPLFree(papoLayers);
65 148 : }
66 :
67 : /*!
68 : \brief Open VFK datasource
69 :
70 : \param pszNewName datasource name
71 : \param bTestOpen True to test if datasource is possible to open
72 :
73 : \return TRUE on success or FALSE on failure
74 : */
75 148 : int OGRVFKDataSource::Open(const char *pszNewName, int bTestOpen)
76 : {
77 : FILE * fp;
78 : char szHeader[1000];
79 :
80 : /* open the source file */
81 148 : fp = VSIFOpen(pszNewName, "r");
82 148 : if (fp == NULL) {
83 61 : if (!bTestOpen)
84 : CPLError(CE_Failure, CPLE_OpenFailed,
85 : "Failed to open VFK file `%s'",
86 0 : pszNewName);
87 :
88 61 : return FALSE;
89 : }
90 :
91 : /* If we aren't sure it is VFK, load a header chunk and check
92 : for signs it is VFK */
93 87 : if (bTestOpen) {
94 87 : size_t nRead = VSIFRead(szHeader, 1, sizeof(szHeader), fp);
95 87 : if (nRead <= 0) {
96 1 : VSIFClose(fp);
97 1 : return FALSE;
98 : }
99 86 : szHeader[MIN(nRead, sizeof(szHeader))-1] = '\0';
100 :
101 : // TODO: improve check
102 86 : if (strncmp(szHeader, "&HVERZE;", 8) != 0) {
103 84 : VSIFClose(fp);
104 84 : return FALSE;
105 : }
106 : }
107 :
108 : /* We assume now that it is VFK. Close and instantiate a
109 : VFKReader on it. */
110 2 : VSIFClose(fp);
111 :
112 2 : pszName = CPLStrdup(pszNewName);
113 :
114 2 : poReader = CreateVFKReader(pszNewName);
115 2 : if (poReader == NULL) {
116 : CPLError(CE_Failure, CPLE_AppDefined,
117 : "File %s appears to be VFK but the VFK reader can't"
118 : "be instantiated",
119 0 : pszNewName);
120 0 : return FALSE;
121 : }
122 :
123 : /* read data blocks, i.e. &B */
124 2 : poReader->ReadDataBlocks();
125 :
126 : /* get list of layers */
127 2 : papoLayers = (OGRVFKLayer **) CPLCalloc(sizeof(OGRVFKLayer *), poReader->GetDataBlockCount());
128 :
129 124 : for (int iLayer = 0; iLayer < poReader->GetDataBlockCount(); iLayer++) {
130 122 : papoLayers[iLayer] = CreateLayerFromBlock(poReader->GetDataBlock(iLayer));
131 122 : nLayers++;
132 : }
133 :
134 2 : return TRUE;
135 : }
136 :
137 : /*!
138 : \brief Get VFK layer
139 :
140 : \param iLayer layer number
141 :
142 : \return pointer to OGRLayer instance or NULL on error
143 : */
144 97 : OGRLayer *OGRVFKDataSource::GetLayer(int iLayer)
145 : {
146 97 : if( iLayer < 0 || iLayer >= nLayers )
147 0 : return NULL;
148 :
149 97 : return papoLayers[iLayer];
150 : }
151 :
152 : /*!
153 : \brief Test datasource capabilies
154 :
155 : \param pszCap capability
156 :
157 : \return TRUE if supported or FALSE if not supported
158 : */
159 0 : int OGRVFKDataSource::TestCapability(const char * pszCap)
160 : {
161 0 : return FALSE;
162 : }
163 :
164 : /*!
165 : \brief Create OGR layer from VFKDataBlock
166 :
167 : \param poDataBlock pointer to VFKDataBlock instance
168 :
169 : \return pointer to OGRVFKLayer instance or NULL on error
170 : */
171 122 : OGRVFKLayer *OGRVFKDataSource::CreateLayerFromBlock(const IVFKDataBlock *poDataBlock)
172 : {
173 : OGRVFKLayer *poLayer;
174 :
175 122 : poLayer = NULL;
176 :
177 : /* create an empty layer */
178 : poLayer = new OGRVFKLayer(poDataBlock->GetName(), NULL,
179 122 : poDataBlock->GetGeometryType(), this);
180 :
181 : /* define attributes (properties) */
182 1272 : for (int iField = 0; iField < poDataBlock->GetPropertyCount(); iField++) {
183 1150 : VFKPropertyDefn *poProperty = poDataBlock->GetProperty(iField);
184 1150 : OGRFieldDefn oField(poProperty->GetName(), poProperty->GetType());
185 :
186 1150 : if(poProperty->GetWidth() > 0)
187 1150 : oField.SetWidth(poProperty->GetWidth());
188 1150 : if(poProperty->GetPrecision() > 0)
189 60 : oField.SetPrecision(poProperty->GetPrecision());
190 :
191 1150 : poLayer->GetLayerDefn()->AddFieldDefn(&oField);
192 : }
193 :
194 122 : return poLayer;
195 : }
|