1 : /******************************************************************************
2 : * $Id: ogrnaslayer.cpp 10645 2007-01-18 02:22:39Z warmerdam $
3 : *
4 : * Project: OGR
5 : * Purpose: Implements OGRNASRelationLayer class, a special layer holding all
6 : * the relations from the NAS file.
7 : * Author: Frank Warmerdam, warmerdam@pobox.com
8 : *
9 : ******************************************************************************
10 : * Copyright (c) 2009, Frank Warmerdam <warmerdam@pobox.com>
11 : *
12 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included
20 : * in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE.
29 : ****************************************************************************/
30 :
31 : #include "ogr_nas.h"
32 : #include "cpl_conv.h"
33 : #include "cpl_port.h"
34 : #include "cpl_string.h"
35 :
36 : CPL_CVSID("$Id: ogrnaslayer.cpp 10645 2007-01-18 02:22:39Z warmerdam $");
37 :
38 : /************************************************************************/
39 : /* OGRNASRelationLayer() */
40 : /************************************************************************/
41 :
42 5 : OGRNASRelationLayer::OGRNASRelationLayer( OGRNASDataSource *poDSIn )
43 :
44 : {
45 5 : poDS = poDSIn;
46 :
47 5 : iNextFeature = 0;
48 5 : bPopulated = FALSE;
49 :
50 : /* -------------------------------------------------------------------- */
51 : /* Establish the layer fields. */
52 : /* -------------------------------------------------------------------- */
53 5 : poFeatureDefn = new OGRFeatureDefn( "ALKIS_beziehungen" );
54 5 : poFeatureDefn->Reference();
55 5 : poFeatureDefn->SetGeomType( wkbNone );
56 :
57 5 : OGRFieldDefn oFD( "", OFTString );
58 :
59 5 : oFD.SetName( "beziehung_von" );
60 5 : poFeatureDefn->AddFieldDefn( &oFD );
61 :
62 5 : oFD.SetName( "beziehungsart" );
63 5 : poFeatureDefn->AddFieldDefn( &oFD );
64 :
65 5 : oFD.SetName( "beziehung_zu" );
66 5 : poFeatureDefn->AddFieldDefn( &oFD );
67 5 : }
68 :
69 : /************************************************************************/
70 : /* ~OGRNASRelationLayer() */
71 : /************************************************************************/
72 :
73 5 : OGRNASRelationLayer::~OGRNASRelationLayer()
74 :
75 : {
76 5 : if( poFeatureDefn )
77 5 : poFeatureDefn->Release();
78 5 : }
79 :
80 : /************************************************************************/
81 : /* ResetReading() */
82 : /************************************************************************/
83 :
84 0 : void OGRNASRelationLayer::ResetReading()
85 :
86 : {
87 0 : iNextFeature = 0;
88 0 : }
89 :
90 : /************************************************************************/
91 : /* GetNextFeature() */
92 : /************************************************************************/
93 :
94 1 : OGRFeature *OGRNASRelationLayer::GetNextFeature()
95 :
96 : {
97 1 : if( !bPopulated )
98 1 : poDS->PopulateRelations();
99 :
100 : /* ==================================================================== */
101 : /* Loop till we find and translate a feature meeting all our */
102 : /* requirements. */
103 : /* ==================================================================== */
104 0 : while( TRUE )
105 : {
106 : // out of features?
107 1 : if( iNextFeature >= (int) aoRelationCollection.size() )
108 0 : return NULL;
109 :
110 : /* -------------------------------------------------------------------- */
111 : /* The from/type/to values are stored in a packed string with */
112 : /* \0 separators for compactness. Split out components. */
113 : /* -------------------------------------------------------------------- */
114 : const char *pszFromID, *pszType, *pszToID;
115 :
116 1 : pszFromID = aoRelationCollection[iNextFeature].c_str();
117 1 : pszType = pszFromID + strlen(pszFromID) + 1;
118 1 : pszToID = pszType + strlen(pszType) + 1;
119 :
120 1 : m_nFeaturesRead++;
121 :
122 : /* -------------------------------------------------------------------- */
123 : /* Translate values into an OGRFeature. */
124 : /* -------------------------------------------------------------------- */
125 1 : OGRFeature *poFeature = new OGRFeature( poFeatureDefn );
126 :
127 1 : poFeature->SetField( 0, pszFromID );
128 1 : poFeature->SetField( 1, pszType );
129 1 : poFeature->SetField( 2, pszToID );
130 :
131 1 : poFeature->SetFID( iNextFeature++ );
132 :
133 : /* -------------------------------------------------------------------- */
134 : /* Test against the attribute query. */
135 : /* -------------------------------------------------------------------- */
136 1 : if( m_poAttrQuery != NULL
137 : && !m_poAttrQuery->Evaluate( poFeature ) )
138 0 : delete poFeature;
139 : else
140 1 : return poFeature;
141 : }
142 :
143 : return NULL;
144 : }
145 :
146 : /************************************************************************/
147 : /* GetFeatureCount() */
148 : /************************************************************************/
149 :
150 0 : int OGRNASRelationLayer::GetFeatureCount( int bForce )
151 :
152 : {
153 0 : if( !bPopulated )
154 0 : poDS->PopulateRelations();
155 :
156 0 : if( m_poAttrQuery == NULL )
157 0 : return aoRelationCollection.size();
158 : else
159 0 : return OGRLayer::GetFeatureCount( bForce );
160 : }
161 :
162 : /************************************************************************/
163 : /* TestCapability() */
164 : /************************************************************************/
165 :
166 0 : int OGRNASRelationLayer::TestCapability( const char * pszCap )
167 :
168 : {
169 0 : if( EQUAL(pszCap,OLCFastGetExtent) )
170 0 : return TRUE;
171 :
172 0 : else if( EQUAL(pszCap,OLCFastFeatureCount) )
173 0 : return bPopulated && m_poAttrQuery == NULL;
174 :
175 0 : else if( EQUAL(pszCap,OLCStringsAsUTF8) )
176 0 : return TRUE;
177 :
178 : else
179 0 : return FALSE;
180 : }
181 :
182 : /************************************************************************/
183 : /* AddRelation() */
184 : /************************************************************************/
185 :
186 1836 : void OGRNASRelationLayer::AddRelation( const char *pszFromID,
187 : const char *pszType,
188 : const char *pszToID )
189 :
190 : {
191 1836 : int nMergedLen = strlen(pszFromID) + strlen(pszType) + strlen(pszToID) + 3;
192 1836 : char *pszMerged = (char *) CPLMalloc(nMergedLen);
193 :
194 1836 : strcpy( pszMerged, pszFromID );
195 1836 : strcpy( pszMerged + strlen(pszFromID) + 1, pszType );
196 1836 : strcpy( pszMerged + strlen(pszFromID) + strlen(pszType) + 2, pszToID );
197 :
198 1836 : CPLString osRelation;
199 1836 : osRelation.assign( pszMerged, nMergedLen );
200 :
201 1836 : CPLFree( pszMerged );
202 :
203 1836 : aoRelationCollection.push_back( osRelation );
204 1836 : }
205 :
|