1 : /******************************************************************************
2 : * $Id: ogrvfklayer.cpp 25340 2012-12-21 20:30:21Z rouault $
3 : *
4 : * Project: OpenGIS Simple Features Reference Implementation
5 : * Purpose: Implements OGRVFKLayer 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: ogrvfklayer.cpp 25340 2012-12-21 20:30:21Z rouault $");
37 :
38 : /*!
39 : \brief OGRVFKLayer constructor
40 :
41 : \param pszName layer name
42 : \param poSRSIn spatial reference
43 : \param eReqType WKB geometry type
44 : \param poDSIn data source where to registrate OGR layer
45 : */
46 122 : OGRVFKLayer::OGRVFKLayer(const char *pszName,
47 : OGRSpatialReference *poSRSIn,
48 : OGRwkbGeometryType eReqType,
49 122 : OGRVFKDataSource *poDSIn)
50 : {
51 : /* set spatial reference */
52 122 : if( poSRSIn == NULL ) {
53 : /* default is S-JTSK
54 : const char *wktString = "PROJCS[\"Krovak\","
55 : "GEOGCS[\"GCS_Bessel 1841\","
56 : "DATUM[\"D_unknown\","
57 : "SPHEROID[\"bessel\","
58 : "6377397.155,299.1528128]],"
59 : "PRIMEM[\"Greenwich\",0],"
60 : "UNIT[\"Degree\",0.017453292519943295]],"
61 : "PROJECTION[\"Krovak\"],"
62 : "PARAMETER[\"latitude_of_center\",49.5],"
63 : "PARAMETER[\"longitude_of_center\",24.83333333333333],"
64 : "PARAMETER[\"azimuth\",0],"
65 : "PARAMETER[\"pseudo_standard_parallel_1\",0],"
66 : "PARAMETER[\"scale_factor\",0.9999],"
67 : "PARAMETER[\"false_easting\",0],"
68 : "PARAMETER[\"false_northing\",0],"
69 : "UNIT[\"Meter\",1]]";
70 : */
71 :
72 122 : poSRS = new OGRSpatialReference();
73 : /*
74 : if (poSRS->importFromWkt((char **)&wktString) != OGRERR_NONE) {
75 : */
76 244 : if (poSRS->importFromEPSG(2065) != OGRERR_NONE) {
77 0 : delete poSRS;
78 0 : poSRS = NULL;
79 : }
80 : }
81 : else {
82 0 : poSRS = poSRSIn->Clone();
83 : }
84 :
85 : /* layer datasource */
86 122 : poDS = poDSIn;
87 :
88 : /* feature definition */
89 122 : poFeatureDefn = new OGRFeatureDefn(pszName);
90 :
91 122 : poFeatureDefn->Reference();
92 122 : poFeatureDefn->SetGeomType(eReqType);
93 :
94 : /* data block reference */
95 122 : poDataBlock = poDS->GetReader()->GetDataBlock(pszName);
96 122 : }
97 :
98 : /*!
99 : \brief OGRVFKLayer() destructor
100 : */
101 122 : OGRVFKLayer::~OGRVFKLayer()
102 : {
103 122 : if(poFeatureDefn)
104 122 : poFeatureDefn->Release();
105 :
106 122 : if(poSRS)
107 122 : poSRS->Release();
108 122 : }
109 :
110 : /*!
111 : \brief Test capability (random access, etc.)
112 :
113 : \param pszCap capability name
114 : */
115 0 : int OGRVFKLayer::TestCapability(const char * pszCap)
116 : {
117 0 : if (EQUAL(pszCap, OLCRandomRead)) {
118 0 : return TRUE; /* ? */
119 : }
120 :
121 0 : return FALSE;
122 : }
123 :
124 : /*!
125 : \brief Reset reading
126 :
127 : \todo To be implemented
128 : */
129 3 : void OGRVFKLayer::ResetReading()
130 : {
131 3 : m_iNextFeature = 0;
132 3 : poDataBlock->ResetReading();
133 3 : }
134 :
135 : /*!
136 : \brief Create geometry from VFKFeature
137 :
138 : \param poVfkFeature pointer to VFKFeature
139 :
140 : \return pointer to OGRGeometry or NULL on error
141 : */
142 15 : OGRGeometry *OGRVFKLayer::CreateGeometry(IVFKFeature * poVfkFeature)
143 : {
144 15 : return poVfkFeature->GetGeometry();
145 : }
146 :
147 : /*!
148 : \brief Get spatial reference information
149 : */
150 0 : OGRSpatialReference *OGRVFKLayer::GetSpatialRef()
151 : {
152 0 : return poSRS;
153 : }
154 :
155 : /*!
156 : \brief Get feature count
157 :
158 : This method overwrites OGRLayer::GetFeatureCount(),
159 :
160 : \param bForce skip (return -1)
161 :
162 : \return number of features
163 : */
164 2 : int OGRVFKLayer::GetFeatureCount(int bForce)
165 : {
166 : int nfeatures;
167 :
168 2 : if(!bForce)
169 0 : return -1;
170 :
171 2 : if (m_poFilterGeom || m_poAttrQuery)
172 0 : nfeatures = OGRLayer::GetFeatureCount(bForce);
173 : else
174 2 : nfeatures = poDataBlock->GetMaxFID();
175 :
176 2 : CPLDebug("OGR_VFK", "OGRVFKLayer::GetFeatureCount(): n=%d", nfeatures);
177 :
178 2 : return nfeatures;
179 : }
180 :
181 : /*!
182 : \brief Get next feature
183 :
184 : \return pointer to OGRFeature instance
185 : */
186 3 : OGRFeature *OGRVFKLayer::GetNextFeature()
187 : {
188 : VFKFeature *poVFKFeature;
189 :
190 : OGRFeature *poOGRFeature;
191 : OGRGeometry *poOGRGeom;
192 :
193 3 : poOGRFeature = NULL;
194 3 : poOGRGeom = NULL;
195 :
196 : /* loop till we find and translate a feature meeting all our
197 : requirements
198 : */
199 12 : while (TRUE) {
200 : /* cleanup last feature, and get a new raw vfk feature */
201 15 : if (poOGRGeom != NULL) {
202 0 : delete poOGRGeom;
203 0 : poOGRGeom = NULL;
204 : }
205 :
206 15 : poVFKFeature = (VFKFeature *) poDataBlock->GetNextFeature();
207 15 : if (!poVFKFeature)
208 1 : return NULL;
209 :
210 : /* skip feature with unknown geometry type */
211 14 : if (poVFKFeature->GetGeometryType() == wkbUnknown)
212 0 : continue;
213 :
214 14 : poOGRFeature = GetFeature(poVFKFeature);
215 14 : if (poOGRFeature)
216 2 : return poOGRFeature;
217 : }
218 : }
219 :
220 : /*!
221 : \brief Get feature by fid
222 :
223 : \param nFID feature id (-1 for next)
224 :
225 : \return pointer to OGRFeature or NULL not found
226 : */
227 1 : OGRFeature *OGRVFKLayer::GetFeature(long nFID)
228 : {
229 : IVFKFeature *poVFKFeature;
230 :
231 1 : poVFKFeature = poDataBlock->GetFeature(nFID);
232 1 : if (!poVFKFeature)
233 0 : return NULL;
234 :
235 1 : CPLDebug("OGR_VFK", "OGRVFKLayer::GetFeature(): fid=%ld", nFID);
236 :
237 1 : return GetFeature(poVFKFeature);
238 : }
239 :
240 : /*!
241 : \brief Get feature (private)
242 :
243 : \return pointer to OGRFeature or NULL not found
244 : */
245 15 : OGRFeature *OGRVFKLayer::GetFeature(IVFKFeature *poVFKFeature)
246 : {
247 : OGRGeometry *poGeom;
248 :
249 : /* skip feature with unknown geometry type */
250 15 : if (poVFKFeature->GetGeometryType() == wkbUnknown)
251 0 : return NULL;
252 :
253 : /* get features geometry */
254 15 : poGeom = CreateGeometry(poVFKFeature);
255 15 : if (poGeom != NULL)
256 15 : poGeom->assignSpatialReference(poSRS);
257 :
258 : /* does it satisfy the spatial query, if there is one? */
259 15 : if (m_poFilterGeom != NULL && poGeom && !FilterGeometry(poGeom)) {
260 0 : return NULL;
261 : }
262 :
263 : /* convert the whole feature into an OGRFeature */
264 15 : OGRFeature *poOGRFeature = new OGRFeature(GetLayerDefn());
265 15 : poOGRFeature->SetFID(poVFKFeature->GetFID());
266 : // poOGRFeature->SetFID(++m_iNextFeature);
267 :
268 15 : poVFKFeature->LoadProperties(poOGRFeature);
269 :
270 : /* test against the attribute query */
271 28 : if (m_poAttrQuery != NULL &&
272 : !m_poAttrQuery->Evaluate(poOGRFeature)) {
273 12 : delete poOGRFeature;
274 12 : return NULL;
275 : }
276 :
277 3 : if (poGeom)
278 3 : poOGRFeature->SetGeometryDirectly(poGeom->clone());
279 :
280 3 : return poOGRFeature;
281 : }
|