1 : /******************************************************************************
2 : * $Id: vfkfeature.cpp 21684 2011-02-11 22:14:01Z warmerdam $
3 : *
4 : * Project: VFK Reader - Feature definition
5 : * Purpose: Implements VFKFeature 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 "vfkreader.h"
33 : #include "vfkreaderp.h"
34 :
35 : #include "cpl_conv.h"
36 : #include "cpl_error.h"
37 :
38 : /*!
39 : \brief VFKFeature constructor
40 :
41 : \param poDataBlock pointer to VFKDataBlock instance
42 : */
43 53 : VFKFeature::VFKFeature(VFKDataBlock *poDataBlock)
44 : {
45 53 : CPLAssert(NULL != poDataBlock);
46 53 : m_poDataBlock = poDataBlock;
47 :
48 53 : m_nFID = -1;
49 53 : m_nGeometryType = poDataBlock->GetGeometryType();
50 53 : m_bGeometry = FALSE;
51 53 : m_paGeom = NULL;
52 :
53 53 : m_propertyList.assign(poDataBlock->GetPropertyCount(), VFKProperty());
54 53 : CPLAssert(size_t (poDataBlock->GetPropertyCount()) == m_propertyList.size());
55 53 : }
56 :
57 : /*!
58 : \brief VFKFeature destructor
59 : */
60 53 : VFKFeature::~VFKFeature()
61 : {
62 53 : if (m_paGeom)
63 40 : delete m_paGeom;
64 :
65 53 : m_poDataBlock = NULL;
66 53 : }
67 :
68 : /*!
69 : \brief Set feature property
70 :
71 : \param iIndex property index
72 : \param pszValue property value
73 : */
74 652 : void VFKFeature::SetProperty(int iIndex, const char *pszValue)
75 : {
76 652 : if (iIndex < 0 || iIndex >= m_poDataBlock->GetPropertyCount() || size_t(iIndex) >= m_propertyList.size()) {
77 0 : CPLAssert(FALSE);
78 0 : return;
79 : }
80 :
81 652 : if (strlen(pszValue) < 1)
82 180 : m_propertyList[iIndex] = VFKProperty();
83 : else {
84 : OGRFieldType fType;
85 472 : fType = m_poDataBlock->GetProperty(iIndex)->GetType();
86 472 : switch (fType) {
87 : case OFTInteger:
88 378 : m_propertyList[iIndex] = VFKProperty(atoi(pszValue));
89 378 : break;
90 : case OFTReal:
91 26 : m_propertyList[iIndex] = VFKProperty(CPLAtof(pszValue));
92 26 : break;
93 : default:
94 68 : m_propertyList[iIndex] = VFKProperty(pszValue);
95 : break;
96 : }
97 : }
98 : }
99 :
100 : /*!
101 : \brief Get property value by index
102 :
103 : \param iIndex property index
104 :
105 : \return property value
106 : \return NULL on error
107 : */
108 657 : const VFKProperty *VFKFeature::GetProperty(int iIndex) const
109 : {
110 657 : if (iIndex < 0 || iIndex >= m_poDataBlock->GetPropertyCount() || size_t(iIndex) >= m_propertyList.size())
111 0 : return NULL;
112 :
113 657 : const VFKProperty* poProperty = &m_propertyList[iIndex];
114 657 : return poProperty;
115 : }
116 :
117 : /*!
118 : \brief Get property value by name
119 :
120 : \param pszName property name
121 :
122 : \return property value
123 : \return NULL on error
124 : */
125 26 : const VFKProperty *VFKFeature::GetProperty(const char *pszName) const
126 : {
127 26 : return GetProperty(m_poDataBlock->GetPropertyIndex(pszName));
128 : }
129 :
130 : /*!
131 : \brief Set feature id
132 :
133 : FID: 0 for next, -1 for same
134 :
135 : \param FID feature id
136 : */
137 53 : void VFKFeature::SetFID(long nFID)
138 : {
139 53 : if (m_nFID > 0)
140 : {
141 0 : m_nFID = nFID;
142 : }
143 :
144 53 : if (m_nFID < 1)
145 : {
146 53 : long nMaxFID = m_poDataBlock->GetMaxFID();
147 53 : if (nFID == 0) /* next */
148 : {
149 40 : m_nFID = nMaxFID + 1;
150 : }
151 : else /* same */
152 : {
153 13 : m_nFID = nMaxFID;
154 : }
155 : }
156 53 : }
157 :
158 : /*!
159 : \brief Set feature geometry
160 :
161 : \param poGeom pointer to OGRGeometry
162 : */
163 66 : void VFKFeature::SetGeometry(OGRGeometry *poGeom)
164 : {
165 66 : m_bGeometry = TRUE;
166 66 : if (!poGeom)
167 26 : return;
168 :
169 40 : m_paGeom = (OGRGeometry *) poGeom->clone(); /* make copy */
170 :
171 40 : if (m_nGeometryType == wkbNone && m_paGeom->IsEmpty())
172 : {
173 : CPLError(CE_Warning, CPLE_AppDefined,
174 0 : "Empty geometry FID %ld.\n", m_nFID);
175 : }
176 :
177 40 : if (m_nGeometryType == wkbLineString && ((OGRLineString *) m_paGeom)->getNumPoints() < 2)
178 : {
179 : CPLError(CE_Warning, CPLE_AppDefined,
180 : "Invalid LineString FID %ld (%d points).\n",
181 : m_nFID,
182 0 : ((OGRLineString *) m_paGeom)->getNumPoints());
183 : }
184 : }
185 :
186 : /*!
187 : \brief Load geometry
188 :
189 : \return TRUE on success
190 : \return FALSE on failure
191 : */
192 0 : bool VFKFeature::LoadGeometry()
193 : {
194 : const char *pszName;
195 :
196 0 : if (m_bGeometry) /* geometry already loaded */
197 0 : return TRUE;
198 :
199 0 : pszName = m_poDataBlock->GetName();
200 :
201 0 : if (EQUAL (pszName, "SOBR") || EQUAL (pszName, "OBBP")
202 : || EQUAL (pszName, "SPOL") || EQUAL (pszName, "OB")
203 : || EQUAL (pszName, "OP") || EQUAL (pszName, "OBPEJ"))
204 : {
205 : /* -> wkbPoint */
206 : double x, y;
207 : int i_idxX, i_idxY;
208 :
209 0 : i_idxY = m_poDataBlock->GetPropertyIndex("SOURADNICE_Y");
210 0 : i_idxX = m_poDataBlock->GetPropertyIndex("SOURADNICE_X");
211 0 : if (i_idxY < 0 || i_idxX < 0)
212 0 : return FALSE;
213 :
214 0 : x = -1.0 * GetProperty(i_idxY)->GetValueD();
215 0 : y = -1.0 * GetProperty(i_idxX)->GetValueD();
216 0 : OGRPoint pt(x, y);
217 0 : SetGeometry(&pt);
218 :
219 0 : return TRUE;
220 : }
221 :
222 0 : if (EQUAL (pszName, "SBP"))
223 : {
224 : /* -> wkbLineString */
225 : int id, idxId, idxBp_Id, idxPCB, ipcb;
226 :
227 : VFKDataBlock *poDataBlockPoints;
228 : VFKFeature *poPoint, *poLine;
229 :
230 0 : OGRLineString OGRLine;
231 :
232 0 : poDataBlockPoints = m_poDataBlock->GetReader()->GetDataBlock("SOBR");
233 0 : if (!poDataBlockPoints)
234 0 : return FALSE;
235 :
236 0 : idxId = poDataBlockPoints->GetPropertyIndex("ID");
237 0 : idxBp_Id = m_poDataBlock->GetPropertyIndex("BP_ID");
238 0 : idxPCB = m_poDataBlock->GetPropertyIndex("PORADOVE_CISLO_BODU");
239 0 : if (idxId < 0 || idxBp_Id < 0 || idxPCB < 0)
240 0 : return false;
241 :
242 0 : poLine = this;
243 0 : while (TRUE)
244 : {
245 0 : id = poLine->GetProperty(idxBp_Id)->GetValueI();
246 0 : ipcb = poLine->GetProperty(idxPCB)->GetValueI();
247 0 : if (OGRLine.getNumPoints() > 0 && ipcb == 1)
248 : {
249 0 : m_poDataBlock->GetPreviousFeature(); /* push back */
250 0 : break;
251 : }
252 :
253 0 : poPoint = poDataBlockPoints->GetFeature(idxId, id);
254 0 : if (!poPoint)
255 : {
256 0 : continue;
257 : }
258 0 : OGRPoint *pt = (OGRPoint *) poPoint->GetGeometry();
259 0 : OGRLine.addPoint(pt);
260 :
261 0 : poLine = m_poDataBlock->GetNextFeature();
262 0 : if (!poLine)
263 0 : break;
264 : };
265 :
266 0 : OGRLine.setCoordinateDimension(2); /* force 2D */
267 0 : SetGeometry(&OGRLine);
268 :
269 : /* reset reading */
270 0 : poDataBlockPoints->ResetReading();
271 :
272 0 : return TRUE;
273 : }
274 :
275 0 : if (EQUAL (pszName, "HP"))
276 : {
277 : /* -> wkbLineString */
278 : int id, idxId, idxHp_Id;
279 : VFKDataBlock *poDataBlockLines;
280 : VFKFeature *poLine;
281 :
282 0 : poDataBlockLines = m_poDataBlock->GetReader()->GetDataBlock("SBP");
283 0 : if (!poDataBlockLines)
284 0 : return FALSE;
285 :
286 0 : idxId = m_poDataBlock->GetPropertyIndex("ID");
287 0 : idxHp_Id = poDataBlockLines->GetPropertyIndex("HP_ID");
288 0 : if (idxId < 0 || idxHp_Id < 0)
289 0 : return FALSE;
290 :
291 0 : id = GetProperty(idxId)->GetValueI();
292 0 : poLine = poDataBlockLines->GetFeature(idxHp_Id, id);
293 0 : if (!poLine || !poLine->GetGeometry())
294 0 : return FALSE;
295 :
296 0 : SetGeometry(poLine->GetGeometry());
297 0 : poDataBlockLines->ResetReading();
298 :
299 0 : return TRUE;
300 : }
301 :
302 0 : return FALSE;
303 : }
304 :
305 : /*!
306 : \brief Get feature geometry
307 :
308 : \return pointer to OGRGeometry
309 : \return NULL on error
310 : */
311 107 : OGRGeometry *VFKFeature::GetGeometry()
312 : {
313 107 : if (m_nGeometryType != wkbNone && !m_bGeometry)
314 0 : LoadGeometry();
315 :
316 107 : return m_paGeom;
317 : }
|