1 : /**********************************************************************
2 : * $Id: gmlfeature.cpp 22954 2011-08-19 21:47:19Z rouault $
3 : *
4 : * Project: GML Reader
5 : * Purpose: Implementation of GMLFeature.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : **********************************************************************
9 : * Copyright (c) 2002, Frank Warmerdam
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 OR
22 : * 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 "gmlreader.h"
31 : #include "cpl_conv.h"
32 : #include "cpl_string.h"
33 :
34 : /************************************************************************/
35 : /* GMLFeature() */
36 : /************************************************************************/
37 :
38 9694 : GMLFeature::GMLFeature( GMLFeatureClass *poClass )
39 :
40 : {
41 9694 : m_poClass = poClass;
42 9694 : m_pszFID = NULL;
43 :
44 9694 : m_nPropertyCount = 0;
45 9694 : m_pasProperties = NULL;
46 :
47 9694 : m_nGeometryCount = 0;
48 9694 : m_papsGeometry = m_apsGeometry;
49 9694 : m_apsGeometry[0] = NULL;
50 9694 : m_apsGeometry[1] = NULL;
51 :
52 9694 : m_papszOBProperties = NULL;
53 9694 : }
54 :
55 : /************************************************************************/
56 : /* ~GMLFeature() */
57 : /************************************************************************/
58 :
59 9694 : GMLFeature::~GMLFeature()
60 :
61 : {
62 9694 : CPLFree( m_pszFID );
63 :
64 : int i;
65 71136 : for( i = 0; i < m_nPropertyCount; i++ )
66 : {
67 61442 : int nSubProperties = m_pasProperties[i].nSubProperties;
68 61442 : if (nSubProperties == 1)
69 49202 : CPLFree( m_pasProperties[i].aszSubProperties[0] );
70 12240 : else if (nSubProperties > 1)
71 : {
72 704 : for( int j = 0; j < nSubProperties; j++)
73 504 : CPLFree( m_pasProperties[i].papszSubProperties[j] );
74 200 : CPLFree( m_pasProperties[i].papszSubProperties );
75 : }
76 : }
77 :
78 9694 : if (m_nGeometryCount == 1)
79 : {
80 7629 : CPLDestroyXMLNode(m_apsGeometry[0]);
81 : }
82 2065 : else if (m_nGeometryCount > 1)
83 : {
84 0 : for(i=0;i<m_nGeometryCount;i++)
85 0 : CPLDestroyXMLNode(m_papsGeometry[i]);
86 0 : CPLFree(m_papsGeometry);
87 : }
88 :
89 9694 : CPLFree( m_pasProperties );
90 9694 : CSLDestroy( m_papszOBProperties );
91 9694 : }
92 :
93 : /************************************************************************/
94 : /* SetFID() */
95 : /************************************************************************/
96 :
97 443 : void GMLFeature::SetFID( const char *pszFID )
98 :
99 : {
100 443 : CPLFree( m_pszFID );
101 443 : if( pszFID != NULL )
102 443 : m_pszFID = CPLStrdup( pszFID );
103 : else
104 0 : m_pszFID = NULL;
105 443 : }
106 :
107 : /************************************************************************/
108 : /* SetPropertyDirectly() */
109 : /************************************************************************/
110 :
111 49706 : void GMLFeature::SetPropertyDirectly( int iIndex, char *pszValue )
112 :
113 : {
114 49706 : if( iIndex >= m_nPropertyCount )
115 : {
116 9632 : int nClassPropertyCount = m_poClass->GetPropertyCount();
117 : m_pasProperties = (GMLProperty*)
118 : CPLRealloc( m_pasProperties,
119 9632 : sizeof(GMLProperty) * nClassPropertyCount );
120 : int i;
121 10076 : for( i = 0; i < m_nPropertyCount; i ++ )
122 : {
123 : /* Make sure papszSubProperties point to the right address in case */
124 : /* m_pasProperties has been relocated */
125 444 : if (m_pasProperties[i].nSubProperties <= 1)
126 408 : m_pasProperties[i].papszSubProperties = m_pasProperties[i].aszSubProperties;
127 : }
128 71074 : for( i = m_nPropertyCount; i < nClassPropertyCount; i++ )
129 : {
130 61442 : m_pasProperties[i].nSubProperties = 0;
131 61442 : m_pasProperties[i].papszSubProperties = m_pasProperties[i].aszSubProperties;
132 61442 : m_pasProperties[i].aszSubProperties[0] = NULL;
133 61442 : m_pasProperties[i].aszSubProperties[1] = NULL;
134 : }
135 9632 : m_nPropertyCount = nClassPropertyCount;
136 : }
137 :
138 49706 : GMLProperty* psProperty = &m_pasProperties[iIndex];
139 49706 : int nSubProperties = psProperty->nSubProperties;
140 49706 : if (nSubProperties == 0)
141 49402 : psProperty->aszSubProperties[0] = pszValue;
142 304 : else if (nSubProperties == 1)
143 : {
144 : psProperty->papszSubProperties = (char**) CPLMalloc(
145 200 : sizeof(char*) * (nSubProperties + 2) );
146 200 : psProperty->papszSubProperties[0] = psProperty->aszSubProperties[0];
147 200 : psProperty->aszSubProperties[0] = NULL;
148 200 : psProperty->papszSubProperties[nSubProperties] = pszValue;
149 200 : psProperty->papszSubProperties[nSubProperties + 1] = NULL;
150 : }
151 : else
152 : {
153 : psProperty->papszSubProperties = (char**) CPLRealloc(
154 : psProperty->papszSubProperties,
155 104 : sizeof(char*) * (nSubProperties + 2) );
156 104 : psProperty->papszSubProperties[nSubProperties] = pszValue;
157 104 : psProperty->papszSubProperties[nSubProperties + 1] = NULL;
158 : }
159 49706 : psProperty->nSubProperties ++;
160 49706 : }
161 :
162 : /************************************************************************/
163 : /* Dump() */
164 : /************************************************************************/
165 :
166 0 : void GMLFeature::Dump( FILE * fp )
167 :
168 : {
169 0 : printf( "GMLFeature(%s):\n", m_poClass->GetName() );
170 :
171 0 : if( m_pszFID != NULL )
172 0 : printf( " FID = %s\n", m_pszFID );
173 :
174 : int i;
175 0 : for( i = 0; i < m_nPropertyCount; i++ )
176 : {
177 0 : const GMLProperty * psGMLProperty = GetProperty( i );
178 0 : printf( " %s = ", m_poClass->GetProperty( i )->GetName());
179 0 : for ( int j = 0; j < psGMLProperty->nSubProperties; j ++)
180 : {
181 0 : if (j > 0) printf(", ");
182 0 : printf("%s", psGMLProperty->papszSubProperties[j]);
183 : }
184 0 : printf("\n");
185 : }
186 :
187 0 : for(i=0;i<m_nGeometryCount;i++)
188 : {
189 0 : char* pszXML = CPLSerializeXMLTree(m_papsGeometry[i]);
190 0 : printf( " %s\n", pszXML );
191 0 : CPLFree(pszXML);
192 : }
193 0 : }
194 :
195 : /************************************************************************/
196 : /* SetGeometryDirectly() */
197 : /************************************************************************/
198 :
199 7629 : void GMLFeature::SetGeometryDirectly( CPLXMLNode* psGeom )
200 :
201 : {
202 7629 : if (m_apsGeometry[0] != NULL)
203 0 : CPLDestroyXMLNode(m_apsGeometry[0]);
204 7629 : m_nGeometryCount = 1;
205 7629 : m_apsGeometry[0] = psGeom;
206 7629 : }
207 :
208 : /************************************************************************/
209 : /* AddGeometry() */
210 : /************************************************************************/
211 :
212 0 : void GMLFeature::AddGeometry( CPLXMLNode* psGeom )
213 :
214 : {
215 0 : if (m_nGeometryCount == 0)
216 : {
217 0 : m_apsGeometry[0] = psGeom;
218 : }
219 0 : else if (m_nGeometryCount == 1)
220 : {
221 : m_papsGeometry = (CPLXMLNode **) CPLMalloc(
222 0 : (m_nGeometryCount + 2) * sizeof(CPLXMLNode *));
223 0 : m_papsGeometry[0] = m_apsGeometry[0];
224 0 : m_apsGeometry[0] = NULL;
225 0 : m_papsGeometry[m_nGeometryCount] = psGeom;
226 0 : m_papsGeometry[m_nGeometryCount + 1] = NULL;
227 : }
228 : else
229 : {
230 : m_papsGeometry = (CPLXMLNode **) CPLRealloc(m_papsGeometry,
231 0 : (m_nGeometryCount + 2) * sizeof(CPLXMLNode *));
232 0 : m_papsGeometry[m_nGeometryCount] = psGeom;
233 0 : m_papsGeometry[m_nGeometryCount + 1] = NULL;
234 : }
235 0 : m_nGeometryCount ++;
236 0 : }
237 :
238 : /************************************************************************/
239 : /* AddOBProperty() */
240 : /************************************************************************/
241 :
242 2508 : void GMLFeature::AddOBProperty( const char *pszName, const char *pszValue )
243 :
244 : {
245 : m_papszOBProperties =
246 2508 : CSLAddNameValue( m_papszOBProperties, pszName, pszValue );
247 2508 : }
248 :
249 : /************************************************************************/
250 : /* GetOBProperty() */
251 : /************************************************************************/
252 :
253 0 : const char *GMLFeature::GetOBProperty( const char *pszName )
254 :
255 : {
256 0 : return CSLFetchNameValue( m_papszOBProperties, pszName );
257 : }
258 :
259 : /************************************************************************/
260 : /* GetOBProperties() */
261 : /************************************************************************/
262 :
263 8392 : char **GMLFeature::GetOBProperties()
264 :
265 : {
266 8392 : return m_papszOBProperties;
267 : }
|