1 : /******************************************************************************
2 : * $Id: ogrmultilinestring.cpp 18913 2010-02-24 23:41:17Z rouault $
3 : *
4 : * Project: OpenGIS Simple Features Reference Implementation
5 : * Purpose: The OGRMultiLineString class.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 1999, 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
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 "ogr_geometry.h"
31 : #include "ogr_p.h"
32 :
33 : CPL_CVSID("$Id: ogrmultilinestring.cpp 18913 2010-02-24 23:41:17Z rouault $");
34 :
35 : /************************************************************************/
36 : /* OGRMultiLineString() */
37 : /************************************************************************/
38 :
39 2080 : OGRMultiLineString::OGRMultiLineString()
40 : {
41 2080 : }
42 :
43 : /************************************************************************/
44 : /* ~OGRMultiLineString() */
45 : /************************************************************************/
46 :
47 2080 : OGRMultiLineString::~OGRMultiLineString()
48 : {
49 2080 : }
50 :
51 : /************************************************************************/
52 : /* getGeometryType() */
53 : /************************************************************************/
54 :
55 2088 : OGRwkbGeometryType OGRMultiLineString::getGeometryType() const
56 :
57 : {
58 2088 : if( getCoordinateDimension() == 3 )
59 230 : return wkbMultiLineString25D;
60 : else
61 1858 : return wkbMultiLineString;
62 : }
63 :
64 : /************************************************************************/
65 : /* getGeometryName() */
66 : /************************************************************************/
67 :
68 404 : const char * OGRMultiLineString::getGeometryName() const
69 :
70 : {
71 404 : return "MULTILINESTRING";
72 : }
73 :
74 : /************************************************************************/
75 : /* addGeometryDirectly() */
76 : /************************************************************************/
77 :
78 3536 : OGRErr OGRMultiLineString::addGeometryDirectly( OGRGeometry * poNewGeom )
79 :
80 : {
81 3890 : if( poNewGeom->getGeometryType() != wkbLineString
82 354 : && poNewGeom->getGeometryType() != wkbLineString25D )
83 0 : return OGRERR_UNSUPPORTED_GEOMETRY_TYPE;
84 :
85 3536 : return OGRGeometryCollection::addGeometryDirectly( poNewGeom );
86 : }
87 :
88 : /************************************************************************/
89 : /* clone() */
90 : /************************************************************************/
91 :
92 328 : OGRGeometry *OGRMultiLineString::clone() const
93 :
94 : {
95 : OGRMultiLineString *poNewGC;
96 :
97 328 : poNewGC = new OGRMultiLineString;
98 328 : poNewGC->assignSpatialReference( getSpatialReference() );
99 :
100 1100 : for( int i = 0; i < getNumGeometries(); i++ )
101 : {
102 772 : poNewGC->addGeometry( getGeometryRef(i) );
103 : }
104 :
105 328 : return poNewGC;
106 : }
107 :
108 : /************************************************************************/
109 : /* importFromWkt() */
110 : /* */
111 : /* Instantiate from well known text format. Currently this is */
112 : /* `MULTILINESTRING ((x y, x y, ...),(x y, ...),...)'. */
113 : /************************************************************************/
114 :
115 254 : OGRErr OGRMultiLineString::importFromWkt( char ** ppszInput )
116 :
117 : {
118 : char szToken[OGR_WKT_TOKEN_MAX];
119 254 : const char *pszInput = *ppszInput;
120 : OGRErr eErr;
121 :
122 : /* -------------------------------------------------------------------- */
123 : /* Clear existing rings. */
124 : /* -------------------------------------------------------------------- */
125 254 : empty();
126 :
127 : /* -------------------------------------------------------------------- */
128 : /* Read and verify the ``MULTILINESTRING'' keyword token. */
129 : /* -------------------------------------------------------------------- */
130 254 : pszInput = OGRWktReadToken( pszInput, szToken );
131 :
132 254 : if( !EQUAL(szToken,getGeometryName()) )
133 0 : return OGRERR_CORRUPT_DATA;
134 :
135 : /* -------------------------------------------------------------------- */
136 : /* Check for EMPTY ... */
137 : /* -------------------------------------------------------------------- */
138 : const char *pszPreScan;
139 254 : int bHasZ = FALSE, bHasM = FALSE;
140 :
141 254 : pszPreScan = OGRWktReadToken( pszInput, szToken );
142 254 : if( EQUAL(szToken,"EMPTY") )
143 : {
144 30 : *ppszInput = (char *) pszPreScan;
145 30 : empty();
146 30 : return OGRERR_NONE;
147 : }
148 :
149 : /* -------------------------------------------------------------------- */
150 : /* Check for Z, M or ZM. Will ignore the Measure */
151 : /* -------------------------------------------------------------------- */
152 224 : else if( EQUAL(szToken,"Z") )
153 : {
154 26 : bHasZ = TRUE;
155 : }
156 198 : else if( EQUAL(szToken,"M") )
157 : {
158 4 : bHasM = TRUE;
159 : }
160 194 : else if( EQUAL(szToken,"ZM") )
161 : {
162 4 : bHasZ = TRUE;
163 4 : bHasM = TRUE;
164 : }
165 :
166 224 : if (bHasZ || bHasM)
167 : {
168 34 : pszInput = pszPreScan;
169 34 : pszPreScan = OGRWktReadToken( pszInput, szToken );
170 34 : if( EQUAL(szToken,"EMPTY") )
171 : {
172 8 : *ppszInput = (char *) pszPreScan;
173 8 : empty();
174 : /* FIXME?: In theory we should store the dimension and M presence */
175 : /* if we want to allow round-trip with ExportToWKT v1.2 */
176 8 : return OGRERR_NONE;
177 : }
178 : }
179 :
180 216 : if( !EQUAL(szToken,"(") )
181 8 : return OGRERR_CORRUPT_DATA;
182 :
183 208 : if ( !bHasZ && !bHasM )
184 : {
185 : /* Test for old-style MULTILINESTRING(EMPTY) */
186 186 : pszPreScan = OGRWktReadToken( pszPreScan, szToken );
187 186 : if( EQUAL(szToken,"EMPTY") )
188 : {
189 16 : pszPreScan = OGRWktReadToken( pszPreScan, szToken );
190 :
191 16 : if( EQUAL(szToken,",") )
192 : {
193 : /* This is OK according to SFSQL SPEC. */
194 : }
195 8 : else if( !EQUAL(szToken,")") )
196 2 : return OGRERR_CORRUPT_DATA;
197 : else
198 : {
199 6 : *ppszInput = (char *) pszPreScan;
200 6 : empty();
201 6 : return OGRERR_NONE;
202 : }
203 : }
204 : }
205 :
206 : /* Skip first '(' */
207 200 : pszInput = OGRWktReadToken( pszInput, szToken );
208 :
209 : /* ==================================================================== */
210 : /* Read each line in turn. Note that we try to reuse the same */
211 : /* point list buffer from ring to ring to cut down on */
212 : /* allocate/deallocate overhead. */
213 : /* ==================================================================== */
214 200 : OGRRawPoint *paoPoints = NULL;
215 200 : int nMaxPoints = 0;
216 200 : double *padfZ = NULL;
217 :
218 378 : do
219 : {
220 420 : int nPoints = 0;
221 :
222 420 : const char* pszNext = OGRWktReadToken( pszInput, szToken );
223 420 : if (EQUAL(szToken,"EMPTY"))
224 : {
225 20 : eErr = addGeometryDirectly( new OGRLineString() );
226 20 : if( eErr != OGRERR_NONE )
227 0 : return eErr;
228 :
229 20 : pszInput = OGRWktReadToken( pszNext, szToken );
230 20 : if ( !EQUAL(szToken, ",") )
231 10 : break;
232 :
233 10 : continue;
234 : }
235 : /* -------------------------------------------------------------------- */
236 : /* Read points for one line from input. */
237 : /* -------------------------------------------------------------------- */
238 : pszInput = OGRWktReadPoints( pszInput, &paoPoints, &padfZ, &nMaxPoints,
239 400 : &nPoints );
240 :
241 400 : if( pszInput == NULL || nPoints == 0 )
242 : {
243 32 : eErr = OGRERR_CORRUPT_DATA;
244 32 : break;
245 : }
246 :
247 : /* -------------------------------------------------------------------- */
248 : /* Create the new line, and add to collection. */
249 : /* -------------------------------------------------------------------- */
250 : OGRLineString *poLine;
251 :
252 368 : poLine = new OGRLineString();
253 : /* Ignore Z array when we have a MULTILINESTRING M */
254 374 : if (bHasM && !bHasZ)
255 2 : poLine->setPoints( nPoints, paoPoints, NULL );
256 : else
257 366 : poLine->setPoints( nPoints, paoPoints, padfZ );
258 :
259 368 : eErr = addGeometryDirectly( poLine );
260 :
261 : /* -------------------------------------------------------------------- */
262 : /* Read the delimeter following the ring. */
263 : /* -------------------------------------------------------------------- */
264 :
265 368 : pszInput = OGRWktReadToken( pszInput, szToken );
266 378 : } while( szToken[0] == ',' && eErr == OGRERR_NONE );
267 :
268 : /* -------------------------------------------------------------------- */
269 : /* freak if we don't get a closing bracket. */
270 : /* -------------------------------------------------------------------- */
271 200 : CPLFree( paoPoints );
272 200 : CPLFree( padfZ );
273 :
274 200 : if( eErr != OGRERR_NONE )
275 32 : return eErr;
276 :
277 168 : if( szToken[0] != ')' )
278 12 : return OGRERR_CORRUPT_DATA;
279 :
280 156 : *ppszInput = (char *) pszInput;
281 156 : return OGRERR_NONE;
282 : }
283 :
284 : /************************************************************************/
285 : /* exportToWkt() */
286 : /* */
287 : /* Translate this structure into it's well known text format */
288 : /* equivelent. This could be made alot more CPU efficient! */
289 : /************************************************************************/
290 :
291 124 : OGRErr OGRMultiLineString::exportToWkt( char ** ppszDstText ) const
292 :
293 : {
294 : char **papszLines;
295 124 : int iLine, nCumulativeLength = 0, nValidLineStrings=0;
296 : OGRErr eErr;
297 :
298 : /* -------------------------------------------------------------------- */
299 : /* Build a list of strings containing the stuff for each ring. */
300 : /* -------------------------------------------------------------------- */
301 124 : papszLines = (char **) CPLCalloc(sizeof(char *),getNumGeometries());
302 :
303 292 : for( iLine = 0; iLine < getNumGeometries(); iLine++ )
304 : {
305 168 : eErr = getGeometryRef(iLine)->exportToWkt( &(papszLines[iLine]) );
306 168 : if( eErr != OGRERR_NONE )
307 0 : return eErr;
308 :
309 168 : if( !EQUALN(papszLines[iLine],"LINESTRING (", 12) )
310 : {
311 : CPLDebug( "OGR", "OGRMultiLineString::exportToWkt() - skipping %s.",
312 20 : papszLines[iLine] );
313 20 : CPLFree( papszLines[iLine] );
314 20 : papszLines[iLine] = NULL;
315 20 : continue;
316 : }
317 :
318 148 : nCumulativeLength += strlen(papszLines[iLine] + 11);
319 148 : nValidLineStrings++;
320 : }
321 :
322 : /* -------------------------------------------------------------------- */
323 : /* Return MULTILINESTRING EMPTY if we get no valid line string. */
324 : /* -------------------------------------------------------------------- */
325 124 : if( nValidLineStrings == 0 )
326 : {
327 46 : CPLFree( papszLines );
328 46 : *ppszDstText = CPLStrdup("MULTILINESTRING EMPTY");
329 46 : return OGRERR_NONE;
330 : }
331 :
332 : /* -------------------------------------------------------------------- */
333 : /* Allocate exactly the right amount of space for the */
334 : /* aggregated string. */
335 : /* -------------------------------------------------------------------- */
336 78 : *ppszDstText = (char *) VSIMalloc(nCumulativeLength+getNumGeometries()+20);
337 :
338 78 : if( *ppszDstText == NULL )
339 0 : return OGRERR_NOT_ENOUGH_MEMORY;
340 :
341 : /* -------------------------------------------------------------------- */
342 : /* Build up the string, freeing temporary strings as we go. */
343 : /* -------------------------------------------------------------------- */
344 78 : char *pszAppendPoint = *ppszDstText;
345 :
346 78 : strcpy( pszAppendPoint, "MULTILINESTRING (" );
347 :
348 78 : int bMustWriteComma = FALSE;
349 240 : for( iLine = 0; iLine < getNumGeometries(); iLine++ )
350 : {
351 162 : if( papszLines[iLine] == NULL )
352 14 : continue;
353 :
354 148 : if( bMustWriteComma )
355 70 : strcat( pszAppendPoint, "," );
356 148 : bMustWriteComma = TRUE;
357 :
358 148 : strcat( pszAppendPoint, papszLines[iLine] + 11 );
359 148 : pszAppendPoint += strlen(pszAppendPoint);
360 :
361 148 : VSIFree( papszLines[iLine] );
362 : }
363 :
364 78 : strcat( pszAppendPoint, ")" );
365 :
366 78 : CPLFree( papszLines );
367 :
368 78 : return OGRERR_NONE;
369 : }
370 :
|