1 : /******************************************************************************
2 : * $Id: ogrlayer.cpp 19999 2010-07-10 09:59:58Z rouault $
3 : *
4 : * Project: OpenGIS Simple Features Reference Implementation
5 : * Purpose: The generic portions of the OGRSFLayer class.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 1999, Les Technologies SoftMap Inc.
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 "ogrsf_frmts.h"
31 : #include "ogr_api.h"
32 : #include "ogr_p.h"
33 : #include "ogr_attrind.h"
34 :
35 : CPL_CVSID("$Id: ogrlayer.cpp 19999 2010-07-10 09:59:58Z rouault $");
36 :
37 : /************************************************************************/
38 : /* OGRLayer() */
39 : /************************************************************************/
40 :
41 4003 : OGRLayer::OGRLayer()
42 :
43 : {
44 4003 : m_poStyleTable = NULL;
45 4003 : m_poAttrQuery = NULL;
46 4003 : m_poAttrIndex = NULL;
47 4003 : m_nRefCount = 0;
48 :
49 4003 : m_nFeaturesRead = 0;
50 :
51 4003 : m_poFilterGeom = NULL;
52 4003 : m_bFilterIsEnvelope = FALSE;
53 4003 : }
54 :
55 : /************************************************************************/
56 : /* ~OGRLayer() */
57 : /************************************************************************/
58 :
59 4003 : OGRLayer::~OGRLayer()
60 :
61 : {
62 4003 : if ( m_poStyleTable )
63 : {
64 1 : delete m_poStyleTable;
65 1 : m_poStyleTable = NULL;
66 : }
67 :
68 4003 : if( m_poAttrIndex != NULL )
69 : {
70 1023 : delete m_poAttrIndex;
71 1023 : m_poAttrIndex = NULL;
72 : }
73 :
74 4003 : if( m_poAttrQuery != NULL )
75 : {
76 16 : delete m_poAttrQuery;
77 16 : m_poAttrQuery = NULL;
78 : }
79 :
80 4003 : if( m_poFilterGeom )
81 : {
82 24 : delete m_poFilterGeom;
83 24 : m_poFilterGeom = NULL;
84 : }
85 4003 : }
86 :
87 : /************************************************************************/
88 : /* Reference() */
89 : /************************************************************************/
90 :
91 0 : int OGRLayer::Reference()
92 :
93 : {
94 0 : return ++m_nRefCount;
95 : }
96 :
97 : /************************************************************************/
98 : /* OGR_L_Reference() */
99 : /************************************************************************/
100 :
101 0 : int OGR_L_Reference( OGRLayerH hLayer )
102 :
103 : {
104 0 : VALIDATE_POINTER1( hLayer, "OGR_L_Reference", 0 );
105 :
106 0 : return ((OGRLayer *) hLayer)->Reference();
107 : }
108 :
109 : /************************************************************************/
110 : /* Dereference() */
111 : /************************************************************************/
112 :
113 0 : int OGRLayer::Dereference()
114 :
115 : {
116 0 : return --m_nRefCount;
117 : }
118 :
119 : /************************************************************************/
120 : /* OGR_L_Dereference() */
121 : /************************************************************************/
122 :
123 0 : int OGR_L_Dereference( OGRLayerH hLayer )
124 :
125 : {
126 0 : VALIDATE_POINTER1( hLayer, "OGR_L_Dereference", 0 );
127 :
128 0 : return ((OGRLayer *) hLayer)->Dereference();
129 : }
130 :
131 : /************************************************************************/
132 : /* GetRefCount() */
133 : /************************************************************************/
134 :
135 10 : int OGRLayer::GetRefCount() const
136 :
137 : {
138 10 : return m_nRefCount;
139 : }
140 :
141 : /************************************************************************/
142 : /* OGR_L_GetRefCount() */
143 : /************************************************************************/
144 :
145 0 : int OGR_L_GetRefCount( OGRLayerH hLayer )
146 :
147 : {
148 0 : VALIDATE_POINTER1( hLayer, "OGR_L_GetRefCount", 0 );
149 :
150 0 : return ((OGRLayer *) hLayer)->GetRefCount();
151 : }
152 :
153 : /************************************************************************/
154 : /* GetFeatureCount() */
155 : /************************************************************************/
156 :
157 131 : int OGRLayer::GetFeatureCount( int bForce )
158 :
159 : {
160 : OGRFeature *poFeature;
161 131 : int nFeatureCount = 0;
162 :
163 131 : if( !bForce )
164 1 : return -1;
165 :
166 130 : ResetReading();
167 32224 : while( (poFeature = GetNextFeature()) != NULL )
168 : {
169 31964 : nFeatureCount++;
170 31964 : delete poFeature;
171 : }
172 130 : ResetReading();
173 :
174 130 : return nFeatureCount;
175 : }
176 :
177 : /************************************************************************/
178 : /* OGR_L_GetFeatureCount() */
179 : /************************************************************************/
180 :
181 362 : int OGR_L_GetFeatureCount( OGRLayerH hLayer, int bForce )
182 :
183 : {
184 362 : VALIDATE_POINTER1( hLayer, "OGR_L_GetFeature", 0 );
185 :
186 362 : return ((OGRLayer *) hLayer)->GetFeatureCount(bForce);
187 : }
188 :
189 : /************************************************************************/
190 : /* GetExtent() */
191 : /************************************************************************/
192 :
193 40 : OGRErr OGRLayer::GetExtent(OGREnvelope *psExtent, int bForce )
194 :
195 : {
196 : OGRFeature *poFeature;
197 40 : OGREnvelope oEnv;
198 40 : GBool bExtentSet = FALSE;
199 :
200 40 : psExtent->MinX = 0.0;
201 40 : psExtent->MaxX = 0.0;
202 40 : psExtent->MinY = 0.0;
203 40 : psExtent->MaxY = 0.0;
204 :
205 : /* -------------------------------------------------------------------- */
206 : /* If this layer has a none geometry type, then we can */
207 : /* reasonably assume there are not extents available. */
208 : /* -------------------------------------------------------------------- */
209 40 : if( GetLayerDefn()->GetGeomType() == wkbNone )
210 0 : return OGRERR_FAILURE;
211 :
212 : /* -------------------------------------------------------------------- */
213 : /* If not forced, we should avoid having to scan all the */
214 : /* features and just return a failure. */
215 : /* -------------------------------------------------------------------- */
216 40 : if( !bForce )
217 0 : return OGRERR_FAILURE;
218 :
219 : /* -------------------------------------------------------------------- */
220 : /* OK, we hate to do this, but go ahead and read through all */
221 : /* the features to collect geometries and build extents. */
222 : /* -------------------------------------------------------------------- */
223 40 : ResetReading();
224 146 : while( (poFeature = GetNextFeature()) != NULL )
225 : {
226 66 : OGRGeometry *poGeom = poFeature->GetGeometryRef();
227 66 : if (poGeom == NULL || poGeom->IsEmpty())
228 : {
229 : /* Do nothing */
230 : }
231 63 : else if (!bExtentSet)
232 : {
233 40 : poGeom->getEnvelope(psExtent);
234 40 : bExtentSet = TRUE;
235 : }
236 : else
237 : {
238 23 : poGeom->getEnvelope(&oEnv);
239 23 : if (oEnv.MinX < psExtent->MinX)
240 4 : psExtent->MinX = oEnv.MinX;
241 23 : if (oEnv.MinY < psExtent->MinY)
242 6 : psExtent->MinY = oEnv.MinY;
243 23 : if (oEnv.MaxX > psExtent->MaxX)
244 4 : psExtent->MaxX = oEnv.MaxX;
245 23 : if (oEnv.MaxY > psExtent->MaxY)
246 0 : psExtent->MaxY = oEnv.MaxY;
247 : }
248 66 : delete poFeature;
249 : }
250 40 : ResetReading();
251 :
252 40 : return (bExtentSet ? OGRERR_NONE : OGRERR_FAILURE);
253 : }
254 :
255 : /************************************************************************/
256 : /* OGR_L_GetExtent() */
257 : /************************************************************************/
258 :
259 64 : OGRErr OGR_L_GetExtent( OGRLayerH hLayer, OGREnvelope *psExtent, int bForce )
260 :
261 : {
262 64 : VALIDATE_POINTER1( hLayer, "OGR_L_GetExtent", OGRERR_INVALID_HANDLE );
263 :
264 64 : return ((OGRLayer *) hLayer)->GetExtent( psExtent, bForce );
265 : }
266 :
267 : /************************************************************************/
268 : /* SetAttributeFilter() */
269 : /************************************************************************/
270 :
271 321 : OGRErr OGRLayer::SetAttributeFilter( const char *pszQuery )
272 :
273 : {
274 : /* -------------------------------------------------------------------- */
275 : /* Are we just clearing any existing query? */
276 : /* -------------------------------------------------------------------- */
277 321 : if( pszQuery == NULL || strlen(pszQuery) == 0 )
278 : {
279 181 : if( m_poAttrQuery )
280 : {
281 51 : delete m_poAttrQuery;
282 51 : m_poAttrQuery = NULL;
283 51 : ResetReading();
284 : }
285 181 : return OGRERR_NONE;
286 : }
287 :
288 : /* -------------------------------------------------------------------- */
289 : /* Or are we installing a new query? */
290 : /* -------------------------------------------------------------------- */
291 : OGRErr eErr;
292 :
293 140 : if( !m_poAttrQuery )
294 67 : m_poAttrQuery = new OGRFeatureQuery();
295 :
296 140 : eErr = m_poAttrQuery->Compile( GetLayerDefn(), pszQuery );
297 140 : if( eErr != OGRERR_NONE )
298 : {
299 0 : delete m_poAttrQuery;
300 0 : m_poAttrQuery = NULL;
301 : }
302 :
303 140 : ResetReading();
304 :
305 140 : return eErr;
306 : }
307 :
308 : /************************************************************************/
309 : /* OGR_L_SetAttributeFilter() */
310 : /************************************************************************/
311 :
312 149 : OGRErr OGR_L_SetAttributeFilter( OGRLayerH hLayer, const char *pszQuery )
313 :
314 : {
315 149 : VALIDATE_POINTER1( hLayer, "OGR_L_SetAttributeFilter", OGRERR_INVALID_HANDLE );
316 :
317 149 : return ((OGRLayer *) hLayer)->SetAttributeFilter( pszQuery );
318 : }
319 :
320 : /************************************************************************/
321 : /* GetFeature() */
322 : /************************************************************************/
323 :
324 7 : OGRFeature *OGRLayer::GetFeature( long nFID )
325 :
326 : {
327 : OGRFeature *poFeature;
328 :
329 7 : ResetReading();
330 17 : while( (poFeature = GetNextFeature()) != NULL )
331 : {
332 10 : if( poFeature->GetFID() == nFID )
333 7 : return poFeature;
334 : else
335 3 : delete poFeature;
336 : }
337 :
338 0 : return NULL;
339 : }
340 :
341 : /************************************************************************/
342 : /* OGR_L_GetFeature() */
343 : /************************************************************************/
344 :
345 53 : OGRFeatureH OGR_L_GetFeature( OGRLayerH hLayer, long nFeatureId )
346 :
347 : {
348 53 : VALIDATE_POINTER1( hLayer, "OGR_L_GetFeature", NULL );
349 :
350 53 : return (OGRFeatureH) ((OGRLayer *)hLayer)->GetFeature( nFeatureId );
351 : }
352 :
353 : /************************************************************************/
354 : /* SetNextByIndex() */
355 : /************************************************************************/
356 :
357 0 : OGRErr OGRLayer::SetNextByIndex( long nIndex )
358 :
359 : {
360 : OGRFeature *poFeature;
361 :
362 0 : ResetReading();
363 0 : while( nIndex-- > 0 )
364 : {
365 0 : poFeature = GetNextFeature();
366 0 : if( poFeature == NULL )
367 0 : return OGRERR_FAILURE;
368 :
369 0 : delete poFeature;
370 : }
371 :
372 0 : return OGRERR_NONE;
373 : }
374 :
375 : /************************************************************************/
376 : /* OGR_L_SetNextByIndex() */
377 : /************************************************************************/
378 :
379 4 : OGRErr OGR_L_SetNextByIndex( OGRLayerH hLayer, long nIndex )
380 :
381 : {
382 4 : VALIDATE_POINTER1( hLayer, "OGR_L_SetNextByIndex", OGRERR_INVALID_HANDLE );
383 :
384 4 : return ((OGRLayer *)hLayer)->SetNextByIndex( nIndex );
385 : }
386 :
387 : /************************************************************************/
388 : /* OGR_L_GetNextFeature() */
389 : /************************************************************************/
390 :
391 20286 : OGRFeatureH OGR_L_GetNextFeature( OGRLayerH hLayer )
392 :
393 : {
394 20286 : VALIDATE_POINTER1( hLayer, "OGR_L_GetNextFeature", NULL );
395 :
396 20286 : return (OGRFeatureH) ((OGRLayer *)hLayer)->GetNextFeature();
397 : }
398 :
399 : /************************************************************************/
400 : /* SetFeature() */
401 : /************************************************************************/
402 :
403 0 : OGRErr OGRLayer::SetFeature( OGRFeature * )
404 :
405 : {
406 0 : return OGRERR_UNSUPPORTED_OPERATION;
407 : }
408 :
409 : /************************************************************************/
410 : /* OGR_L_SetFeature() */
411 : /************************************************************************/
412 :
413 2024 : OGRErr OGR_L_SetFeature( OGRLayerH hLayer, OGRFeatureH hFeat )
414 :
415 : {
416 2024 : VALIDATE_POINTER1( hLayer, "OGR_L_SetFeature", OGRERR_INVALID_HANDLE );
417 2024 : VALIDATE_POINTER1( hFeat, "OGR_L_SetFeature", OGRERR_INVALID_HANDLE );
418 :
419 2024 : return ((OGRLayer *)hLayer)->SetFeature( (OGRFeature *) hFeat );
420 : }
421 :
422 : /************************************************************************/
423 : /* CreateFeature() */
424 : /************************************************************************/
425 :
426 0 : OGRErr OGRLayer::CreateFeature( OGRFeature * )
427 :
428 : {
429 0 : return OGRERR_UNSUPPORTED_OPERATION;
430 : }
431 :
432 : /************************************************************************/
433 : /* OGR_L_CreateFeature() */
434 : /************************************************************************/
435 :
436 18430 : OGRErr OGR_L_CreateFeature( OGRLayerH hLayer, OGRFeatureH hFeat )
437 :
438 : {
439 18430 : VALIDATE_POINTER1( hLayer, "OGR_L_CreateFeature", OGRERR_INVALID_HANDLE );
440 18430 : VALIDATE_POINTER1( hFeat, "OGR_L_SetFeature", OGRERR_INVALID_HANDLE );
441 :
442 18430 : return ((OGRLayer *) hLayer)->CreateFeature( (OGRFeature *) hFeat );
443 : }
444 :
445 : /************************************************************************/
446 : /* GetInfo() */
447 : /************************************************************************/
448 :
449 0 : const char *OGRLayer::GetInfo( const char * pszTag )
450 :
451 : {
452 : (void) pszTag;
453 0 : return NULL;
454 : }
455 :
456 : /************************************************************************/
457 : /* CreateField() */
458 : /************************************************************************/
459 :
460 0 : OGRErr OGRLayer::CreateField( OGRFieldDefn * poField, int bApproxOK )
461 :
462 : {
463 : (void) poField;
464 : (void) bApproxOK;
465 :
466 : CPLError( CE_Failure, CPLE_NotSupported,
467 0 : "CreateField() not supported by this layer.\n" );
468 :
469 0 : return OGRERR_UNSUPPORTED_OPERATION;
470 : }
471 :
472 : /************************************************************************/
473 : /* OGR_L_CreateField() */
474 : /************************************************************************/
475 :
476 : OGRErr OGR_L_CreateField( OGRLayerH hLayer, OGRFieldDefnH hField,
477 449 : int bApproxOK )
478 :
479 : {
480 449 : VALIDATE_POINTER1( hLayer, "OGR_L_CreateField", OGRERR_INVALID_HANDLE );
481 449 : VALIDATE_POINTER1( hField, "OGR_L_CreateField", OGRERR_INVALID_HANDLE );
482 :
483 : return ((OGRLayer *) hLayer)->CreateField( (OGRFieldDefn *) hField,
484 449 : bApproxOK );
485 : }
486 :
487 : /************************************************************************/
488 : /* StartTransaction() */
489 : /************************************************************************/
490 :
491 54 : OGRErr OGRLayer::StartTransaction()
492 :
493 : {
494 54 : return OGRERR_NONE;
495 : }
496 :
497 : /************************************************************************/
498 : /* OGR_L_StartTransaction() */
499 : /************************************************************************/
500 :
501 30 : OGRErr OGR_L_StartTransaction( OGRLayerH hLayer )
502 :
503 : {
504 30 : VALIDATE_POINTER1( hLayer, "OGR_L_StartTransaction", OGRERR_INVALID_HANDLE );
505 :
506 30 : return ((OGRLayer *)hLayer)->StartTransaction();
507 : }
508 :
509 : /************************************************************************/
510 : /* CommitTransaction() */
511 : /************************************************************************/
512 :
513 54 : OGRErr OGRLayer::CommitTransaction()
514 :
515 : {
516 54 : return OGRERR_NONE;
517 : }
518 :
519 : /************************************************************************/
520 : /* OGR_L_CommitTransaction() */
521 : /************************************************************************/
522 :
523 29 : OGRErr OGR_L_CommitTransaction( OGRLayerH hLayer )
524 :
525 : {
526 29 : VALIDATE_POINTER1( hLayer, "OGR_L_CommitTransaction", OGRERR_INVALID_HANDLE );
527 :
528 29 : return ((OGRLayer *)hLayer)->CommitTransaction();
529 : }
530 :
531 : /************************************************************************/
532 : /* RollbackTransaction() */
533 : /************************************************************************/
534 :
535 0 : OGRErr OGRLayer::RollbackTransaction()
536 :
537 : {
538 0 : return OGRERR_UNSUPPORTED_OPERATION;
539 : }
540 :
541 : /************************************************************************/
542 : /* OGR_L_RollbackTransaction() */
543 : /************************************************************************/
544 :
545 1 : OGRErr OGR_L_RollbackTransaction( OGRLayerH hLayer )
546 :
547 : {
548 1 : VALIDATE_POINTER1( hLayer, "OGR_L_RollbackTransaction", OGRERR_INVALID_HANDLE );
549 :
550 1 : return ((OGRLayer *)hLayer)->RollbackTransaction();
551 : }
552 :
553 : /************************************************************************/
554 : /* OGR_L_GetLayerDefn() */
555 : /************************************************************************/
556 :
557 17917 : OGRFeatureDefnH OGR_L_GetLayerDefn( OGRLayerH hLayer )
558 :
559 : {
560 17917 : VALIDATE_POINTER1( hLayer, "OGR_L_GetLayerDefn", NULL );
561 :
562 17917 : return (OGRFeatureDefnH) ((OGRLayer *)hLayer)->GetLayerDefn();
563 : }
564 :
565 : /************************************************************************/
566 : /* OGR_L_GetSpatialRef() */
567 : /************************************************************************/
568 :
569 128 : OGRSpatialReferenceH OGR_L_GetSpatialRef( OGRLayerH hLayer )
570 :
571 : {
572 128 : VALIDATE_POINTER1( hLayer, "OGR_L_GetSpatialRef", NULL );
573 :
574 128 : return (OGRSpatialReferenceH) ((OGRLayer *) hLayer)->GetSpatialRef();
575 : }
576 :
577 : /************************************************************************/
578 : /* OGR_L_TestCapability() */
579 : /************************************************************************/
580 :
581 48 : int OGR_L_TestCapability( OGRLayerH hLayer, const char *pszCap )
582 :
583 : {
584 48 : VALIDATE_POINTER1( hLayer, "OGR_L_TestCapability", 0 );
585 48 : VALIDATE_POINTER1( pszCap, "OGR_L_TestCapability", 0 );
586 :
587 48 : return ((OGRLayer *) hLayer)->TestCapability( pszCap );
588 : }
589 :
590 : /************************************************************************/
591 : /* GetSpatialFilter() */
592 : /************************************************************************/
593 :
594 0 : OGRGeometry *OGRLayer::GetSpatialFilter()
595 :
596 : {
597 0 : return m_poFilterGeom;
598 : }
599 :
600 : /************************************************************************/
601 : /* OGR_L_GetSpatialFilter() */
602 : /************************************************************************/
603 :
604 0 : OGRGeometryH OGR_L_GetSpatialFilter( OGRLayerH hLayer )
605 :
606 : {
607 0 : VALIDATE_POINTER1( hLayer, "OGR_L_GetSpatialFilter", NULL );
608 :
609 0 : return (OGRGeometryH) ((OGRLayer *) hLayer)->GetSpatialFilter();
610 : }
611 :
612 : /************************************************************************/
613 : /* SetSpatialFilter() */
614 : /************************************************************************/
615 :
616 240 : void OGRLayer::SetSpatialFilter( OGRGeometry * poGeomIn )
617 :
618 : {
619 240 : if( InstallFilter( poGeomIn ) )
620 50 : ResetReading();
621 240 : }
622 :
623 : /************************************************************************/
624 : /* OGR_L_SetSpatialFilter() */
625 : /************************************************************************/
626 :
627 39 : void OGR_L_SetSpatialFilter( OGRLayerH hLayer, OGRGeometryH hGeom )
628 :
629 : {
630 39 : VALIDATE_POINTER0( hLayer, "OGR_L_SetSpatialFilter" );
631 :
632 39 : ((OGRLayer *) hLayer)->SetSpatialFilter( (OGRGeometry *) hGeom );
633 : }
634 :
635 : /************************************************************************/
636 : /* SetSpatialFilterRect() */
637 : /************************************************************************/
638 :
639 : void OGRLayer::SetSpatialFilterRect( double dfMinX, double dfMinY,
640 23 : double dfMaxX, double dfMaxY )
641 :
642 : {
643 23 : OGRLinearRing oRing;
644 23 : OGRPolygon oPoly;
645 :
646 23 : oRing.addPoint( dfMinX, dfMinY );
647 23 : oRing.addPoint( dfMinX, dfMaxY );
648 23 : oRing.addPoint( dfMaxX, dfMaxY );
649 23 : oRing.addPoint( dfMaxX, dfMinY );
650 23 : oRing.addPoint( dfMinX, dfMinY );
651 :
652 23 : oPoly.addRing( &oRing );
653 :
654 23 : SetSpatialFilter( &oPoly );
655 23 : }
656 :
657 : /************************************************************************/
658 : /* OGR_L_SetSpatialFilterRect() */
659 : /************************************************************************/
660 :
661 : void OGR_L_SetSpatialFilterRect( OGRLayerH hLayer,
662 : double dfMinX, double dfMinY,
663 23 : double dfMaxX, double dfMaxY )
664 :
665 : {
666 23 : VALIDATE_POINTER0( hLayer, "OGR_L_SetSpatialFilterRect" );
667 :
668 : ((OGRLayer *) hLayer)->SetSpatialFilterRect( dfMinX, dfMinY,
669 23 : dfMaxX, dfMaxY );
670 : }
671 :
672 : /************************************************************************/
673 : /* InstallFilter() */
674 : /* */
675 : /* This method is only intended to be used from within */
676 : /* drivers, normally from the SetSpatialFilter() method. */
677 : /* It installs a filter, and also tests it to see if it is */
678 : /* rectangular. If so, it this is kept track of alongside the */
679 : /* filter geometry itself so we can do cheaper comparisons in */
680 : /* the FilterGeometry() call. */
681 : /* */
682 : /* Returns TRUE if the newly installed filter differs in some */
683 : /* way from the current one. */
684 : /************************************************************************/
685 :
686 261 : int OGRLayer::InstallFilter( OGRGeometry * poFilter )
687 :
688 : {
689 261 : if( m_poFilterGeom == NULL && poFilter == NULL )
690 192 : return FALSE;
691 :
692 : /* -------------------------------------------------------------------- */
693 : /* Replace the existing filter. */
694 : /* -------------------------------------------------------------------- */
695 69 : if( m_poFilterGeom != NULL )
696 : {
697 27 : delete m_poFilterGeom;
698 27 : m_poFilterGeom = NULL;
699 : }
700 :
701 69 : if( poFilter != NULL )
702 51 : m_poFilterGeom = poFilter->clone();
703 :
704 69 : m_bFilterIsEnvelope = FALSE;
705 :
706 69 : if( m_poFilterGeom == NULL )
707 18 : return TRUE;
708 :
709 51 : if( m_poFilterGeom != NULL )
710 51 : m_poFilterGeom->getEnvelope( &m_sFilterEnvelope );
711 :
712 : /* -------------------------------------------------------------------- */
713 : /* Now try to determine if the filter is really a rectangle. */
714 : /* -------------------------------------------------------------------- */
715 51 : if( wkbFlatten(m_poFilterGeom->getGeometryType()) != wkbPolygon )
716 11 : return TRUE;
717 :
718 40 : OGRPolygon *poPoly = (OGRPolygon *) m_poFilterGeom;
719 :
720 40 : if( poPoly->getNumInteriorRings() != 0 )
721 0 : return TRUE;
722 :
723 40 : OGRLinearRing *poRing = poPoly->getExteriorRing();
724 40 : if (poRing == NULL)
725 0 : return TRUE;
726 :
727 40 : if( poRing->getNumPoints() > 5 || poRing->getNumPoints() < 4 )
728 0 : return TRUE;
729 :
730 : // If the ring has 5 points, the last should be the first.
731 40 : if( poRing->getNumPoints() == 5
732 : && ( poRing->getX(0) != poRing->getX(4)
733 : || poRing->getY(0) != poRing->getY(4) ) )
734 0 : return TRUE;
735 :
736 : // Polygon with first segment in "y" direction.
737 40 : if( poRing->getX(0) == poRing->getX(1)
738 : && poRing->getY(1) == poRing->getY(2)
739 : && poRing->getX(2) == poRing->getX(3)
740 : && poRing->getY(3) == poRing->getY(0) )
741 39 : m_bFilterIsEnvelope = TRUE;
742 :
743 : // Polygon with first segment in "x" direction.
744 40 : if( poRing->getY(0) == poRing->getY(1)
745 : && poRing->getX(1) == poRing->getX(2)
746 : && poRing->getY(2) == poRing->getY(3)
747 : && poRing->getX(3) == poRing->getX(0) )
748 1 : m_bFilterIsEnvelope = TRUE;
749 :
750 40 : return TRUE;
751 : }
752 :
753 : /************************************************************************/
754 : /* FilterGeometry() */
755 : /* */
756 : /* Compare the passed in geometry to the currently installed */
757 : /* filter. Optimize for case where filter is just an */
758 : /* envelope. */
759 : /************************************************************************/
760 :
761 333 : int OGRLayer::FilterGeometry( OGRGeometry *poGeometry )
762 :
763 : {
764 : /* -------------------------------------------------------------------- */
765 : /* In trivial cases of new filter or target geometry, we accept */
766 : /* an intersection. No geometry is taken to mean "the whole */
767 : /* world". */
768 : /* -------------------------------------------------------------------- */
769 333 : if( m_poFilterGeom == NULL )
770 28 : return TRUE;
771 :
772 305 : if( poGeometry == NULL )
773 7 : return TRUE;
774 :
775 : /* -------------------------------------------------------------------- */
776 : /* Compute the target geometry envelope, and if there is no */
777 : /* intersection between the envelopes we are sure not to have */
778 : /* any intersection. */
779 : /* -------------------------------------------------------------------- */
780 298 : OGREnvelope sGeomEnv;
781 :
782 298 : poGeometry->getEnvelope( &sGeomEnv );
783 :
784 298 : if( sGeomEnv.MaxX < m_sFilterEnvelope.MinX
785 : || sGeomEnv.MaxY < m_sFilterEnvelope.MinY
786 : || m_sFilterEnvelope.MaxX < sGeomEnv.MinX
787 : || m_sFilterEnvelope.MaxY < sGeomEnv.MinY )
788 170 : return FALSE;
789 :
790 :
791 : /* -------------------------------------------------------------------- */
792 : /* If the filter geometry is its own envelope and if the */
793 : /* envelope of the geometry is inside the filter geometry, */
794 : /* the geometry itself is inside the filter geometry */
795 : /* -------------------------------------------------------------------- */
796 128 : if( m_bFilterIsEnvelope &&
797 : sGeomEnv.MinX >= m_sFilterEnvelope.MinX &&
798 : sGeomEnv.MinY >= m_sFilterEnvelope.MinY &&
799 : sGeomEnv.MaxX <= m_sFilterEnvelope.MaxX &&
800 : sGeomEnv.MaxY <= m_sFilterEnvelope.MaxY)
801 : {
802 78 : return TRUE;
803 : }
804 : else
805 : {
806 : /* -------------------------------------------------------------------- */
807 : /* Fallback to full intersect test (using GEOS) if we still */
808 : /* don't know for sure. */
809 : /* -------------------------------------------------------------------- */
810 50 : if( OGRGeometryFactory::haveGEOS() )
811 50 : return m_poFilterGeom->Intersects( poGeometry );
812 : else
813 0 : return TRUE;
814 : }
815 : }
816 :
817 : /************************************************************************/
818 : /* OGR_L_ResetReading() */
819 : /************************************************************************/
820 :
821 226 : void OGR_L_ResetReading( OGRLayerH hLayer )
822 :
823 : {
824 226 : VALIDATE_POINTER0( hLayer, "OGR_L_ResetReading" );
825 :
826 226 : ((OGRLayer *) hLayer)->ResetReading();
827 : }
828 :
829 : /************************************************************************/
830 : /* InitializeIndexSupport() */
831 : /* */
832 : /* This is only intended to be called by driver layer */
833 : /* implementations but we don't make it protected so that the */
834 : /* datasources can do it too if that is more appropriate. */
835 : /************************************************************************/
836 :
837 1023 : OGRErr OGRLayer::InitializeIndexSupport( const char *pszFilename )
838 :
839 : {
840 : OGRErr eErr;
841 :
842 1023 : m_poAttrIndex = OGRCreateDefaultLayerIndex();
843 :
844 1023 : eErr = m_poAttrIndex->Initialize( pszFilename, this );
845 1023 : if( eErr != OGRERR_NONE )
846 : {
847 0 : delete m_poAttrIndex;
848 0 : m_poAttrIndex = NULL;
849 : }
850 :
851 1023 : return eErr;
852 : }
853 :
854 : /************************************************************************/
855 : /* SyncToDisk() */
856 : /************************************************************************/
857 :
858 0 : OGRErr OGRLayer::SyncToDisk()
859 :
860 : {
861 0 : return OGRERR_NONE;
862 : }
863 :
864 : /************************************************************************/
865 : /* OGR_L_SyncToDisk() */
866 : /************************************************************************/
867 :
868 0 : OGRErr OGR_L_SyncToDisk( OGRLayerH hDS )
869 :
870 : {
871 0 : VALIDATE_POINTER1( hDS, "OGR_L_SyncToDisk", OGRERR_INVALID_HANDLE );
872 :
873 0 : return ((OGRLayer *) hDS)->SyncToDisk();
874 : }
875 :
876 : /************************************************************************/
877 : /* DeleteFeature() */
878 : /************************************************************************/
879 :
880 0 : OGRErr OGRLayer::DeleteFeature( long nFID )
881 :
882 : {
883 0 : return OGRERR_UNSUPPORTED_OPERATION;
884 : }
885 :
886 : /************************************************************************/
887 : /* OGR_L_DeleteFeature() */
888 : /************************************************************************/
889 :
890 7 : OGRErr OGR_L_DeleteFeature( OGRLayerH hDS, long nFID )
891 :
892 : {
893 7 : VALIDATE_POINTER1( hDS, "OGR_L_DeleteFeature", OGRERR_INVALID_HANDLE );
894 :
895 7 : return ((OGRLayer *) hDS)->DeleteFeature( nFID );
896 : }
897 :
898 : /************************************************************************/
899 : /* GetFeaturesRead() */
900 : /************************************************************************/
901 :
902 0 : GIntBig OGRLayer::GetFeaturesRead()
903 :
904 : {
905 0 : return m_nFeaturesRead;
906 : }
907 :
908 : /************************************************************************/
909 : /* OGR_L_GetFeaturesRead() */
910 : /************************************************************************/
911 :
912 0 : GIntBig OGR_L_GetFeaturesRead( OGRLayerH hLayer )
913 :
914 : {
915 0 : VALIDATE_POINTER1( hLayer, "OGR_L_GetFeaturesRead", 0 );
916 :
917 0 : return ((OGRLayer *) hLayer)->GetFeaturesRead();
918 : }
919 :
920 : /************************************************************************/
921 : /* GetFIDColumn */
922 : /************************************************************************/
923 :
924 19 : const char *OGRLayer::GetFIDColumn()
925 :
926 : {
927 19 : return "";
928 : }
929 :
930 : /************************************************************************/
931 : /* OGR_L_GetFIDColumn() */
932 : /************************************************************************/
933 :
934 14 : const char *OGR_L_GetFIDColumn( OGRLayerH hLayer )
935 :
936 : {
937 14 : VALIDATE_POINTER1( hLayer, "OGR_L_GetFIDColumn", NULL );
938 :
939 14 : return ((OGRLayer *) hLayer)->GetFIDColumn();
940 : }
941 :
942 : /************************************************************************/
943 : /* GetGeometryColumn() */
944 : /************************************************************************/
945 :
946 20 : const char *OGRLayer::GetGeometryColumn()
947 :
948 : {
949 20 : return "";
950 : }
951 :
952 : /************************************************************************/
953 : /* OGR_L_GetGeometryColumn() */
954 : /************************************************************************/
955 :
956 18 : const char *OGR_L_GetGeometryColumn( OGRLayerH hLayer )
957 :
958 : {
959 18 : VALIDATE_POINTER1( hLayer, "OGR_L_GetGeometryColumn", NULL );
960 :
961 18 : return ((OGRLayer *) hLayer)->GetGeometryColumn();
962 : }
963 :
964 : /************************************************************************/
965 : /* GetStyleTable() */
966 : /************************************************************************/
967 :
968 34 : OGRStyleTable *OGRLayer::GetStyleTable()
969 : {
970 34 : return m_poStyleTable;
971 : }
972 :
973 : /************************************************************************/
974 : /* SetStyleTableDirectly() */
975 : /************************************************************************/
976 :
977 0 : void OGRLayer::SetStyleTableDirectly( OGRStyleTable *poStyleTable )
978 : {
979 0 : if ( m_poStyleTable )
980 0 : delete m_poStyleTable;
981 0 : m_poStyleTable = poStyleTable;
982 0 : }
983 :
984 : /************************************************************************/
985 : /* SetStyleTable() */
986 : /************************************************************************/
987 :
988 34 : void OGRLayer::SetStyleTable(OGRStyleTable *poStyleTable)
989 : {
990 34 : if ( m_poStyleTable )
991 0 : delete m_poStyleTable;
992 34 : if ( poStyleTable )
993 0 : m_poStyleTable = poStyleTable->Clone();
994 34 : }
995 :
996 : /************************************************************************/
997 : /* OGR_L_GetStyleTable() */
998 : /************************************************************************/
999 :
1000 0 : OGRStyleTableH OGR_L_GetStyleTable( OGRLayerH hLayer )
1001 :
1002 : {
1003 0 : VALIDATE_POINTER1( hLayer, "OGR_L_GetStyleTable", NULL );
1004 :
1005 0 : return (OGRStyleTableH) ((OGRLayer *) hLayer)->GetStyleTable( );
1006 : }
1007 :
1008 : /************************************************************************/
1009 : /* OGR_L_SetStyleTableDirectly() */
1010 : /************************************************************************/
1011 :
1012 : void OGR_L_SetStyleTableDirectly( OGRLayerH hLayer,
1013 0 : OGRStyleTableH hStyleTable )
1014 :
1015 : {
1016 0 : VALIDATE_POINTER0( hLayer, "OGR_L_SetStyleTableDirectly" );
1017 :
1018 0 : ((OGRLayer *) hLayer)->SetStyleTableDirectly( (OGRStyleTable *) hStyleTable);
1019 : }
1020 :
1021 : /************************************************************************/
1022 : /* OGR_L_SetStyleTable() */
1023 : /************************************************************************/
1024 :
1025 : void OGR_L_SetStyleTable( OGRLayerH hLayer,
1026 0 : OGRStyleTableH hStyleTable )
1027 :
1028 : {
1029 0 : VALIDATE_POINTER0( hLayer, "OGR_L_SetStyleTable" );
1030 0 : VALIDATE_POINTER0( hStyleTable, "OGR_L_SetStyleTable" );
1031 :
1032 0 : ((OGRLayer *) hLayer)->SetStyleTable( (OGRStyleTable *) hStyleTable);
1033 : }
|