File: GeometryTransformer.cpp

package info (click to toggle)
geos 3.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 10,060 kB
  • ctags: 8,674
  • sloc: cpp: 64,513; xml: 23,384; sh: 8,965; ruby: 1,295; makefile: 1,124; python: 824; ansic: 289
file content (394 lines) | stat: -rw-r--r-- 10,832 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/**********************************************************************
 * $Id: GeometryTransformer.cpp 1820 2006-09-06 16:54:23Z mloskot $
 *
 * GEOS - Geometry Engine Open Source
 * http://geos.refractions.net
 *
 * Copyright (C) 2006 Refractions Research Inc.
 *
 * This is free software; you can redistribute and/or modify it under
 * the terms of the GNU Lesser General Public Licence as published
 * by the Free Software Foundation. 
 * See the COPYING file for more information.
 *
 **********************************************************************
 *
 * Last port: geom/util/GeometryTransformer.java rev. 1.6 (JTS-1.7.1+)
 *
 **********************************************************************/

#include <geos/geom/util/GeometryTransformer.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/CoordinateSequenceFactory.h>
#include <geos/geom/Geometry.h>
#include <geos/geom/MultiPoint.h>
#include <geos/geom/MultiPolygon.h>
#include <geos/geom/MultiLineString.h>
#include <geos/geom/CoordinateSequence.h>
#include <geos/geom/Polygon.h>
#include <geos/geom/Point.h>
#include <geos/geom/LineString.h>
#include <geos/geom/LinearRing.h>
#include <geos/geom/GeometryCollection.h>
#include <geos/util/IllegalArgumentException.h>

#include <typeinfo>
#include <cassert>

#ifndef GEOS_DEBUG
#define GEOS_DEBUG 0
#endif

#ifdef GEOS_DEBUG
#include <iostream>
#endif

using namespace std;

namespace geos {
namespace geom { // geos.geom
namespace util { // geos.geom.util


/*public*/
GeometryTransformer::GeometryTransformer()
	:
	factory(NULL),
	inputGeom(NULL),
	pruneEmptyGeometry(true),
	preserveGeometryCollectionType(true),
	preserveCollections(false),
	preserveType(false)
{}

GeometryTransformer::~GeometryTransformer()
{
}

/*public*/
auto_ptr<Geometry>
GeometryTransformer::transform(const Geometry* nInputGeom)
{
	using geos::util::IllegalArgumentException;

#if GEOS_DEBUG
	std::cerr << "GeometryTransformer::transform(Geometry " << nInputGeom << ");" << std::endl;
#endif

	inputGeom = nInputGeom;
	factory = inputGeom->getFactory();

	if ( const Point* p=dynamic_cast<const Point*>(inputGeom) )
		return transformPoint(p, NULL);
	if ( const MultiPoint* mp=dynamic_cast<const MultiPoint*>(inputGeom) )
		return transformMultiPoint(mp, NULL);
	if ( const LinearRing* lr=dynamic_cast<const LinearRing*>(inputGeom) )
		return transformLinearRing(lr, NULL);
	if ( const LineString* ls=dynamic_cast<const LineString*>(inputGeom) )
		return transformLineString(ls, NULL);
	if ( const MultiLineString* mls=dynamic_cast<const MultiLineString*>(inputGeom) )
		return transformMultiLineString(mls, NULL);
	if ( const Polygon* p=dynamic_cast<const Polygon*>(inputGeom) )
		return transformPolygon(p, NULL);
	if ( const MultiPolygon* mp=dynamic_cast<const MultiPolygon*>(inputGeom) )
		return transformMultiPolygon(mp, NULL);
	if ( const GeometryCollection* gc=dynamic_cast<const GeometryCollection*>(inputGeom) )
		return transformGeometryCollection(gc, NULL);

	throw IllegalArgumentException("Unknown Geometry subtype.");
}
 
std::auto_ptr<CoordinateSequence>
GeometryTransformer::createCoordinateSequence(
		std::auto_ptr< std::vector<Coordinate> > coords)
{
	return std::auto_ptr<CoordinateSequence>(
		factory->getCoordinateSequenceFactory()->create(
				coords.release())
	);
}

std::auto_ptr<CoordinateSequence>
GeometryTransformer::transformCoordinates(
		const CoordinateSequence* coords,
		const Geometry* parent)
{

#if GEOS_DEBUG
	std::cerr << "GeometryTransformer::transformCoordinates(CoordinateSequence " << coords <<", Geometry " << parent << ");" << std::endl;
#endif

	return std::auto_ptr<CoordinateSequence>(coords->clone());
}

Geometry::AutoPtr
GeometryTransformer::transformPoint(
		const Point* geom,
		const Geometry* parent)
{

#if GEOS_DEBUG
	std::cerr << "GeometryTransformer::transformPoint(Point " << geom <<", Geometry " << parent << ");" << std::endl;
#endif

	CoordinateSequence::AutoPtr cs(transformCoordinates(
		geom->getCoordinatesRO(), geom));

	return Geometry::AutoPtr(factory->createPoint(cs.release()));
}

Geometry::AutoPtr
GeometryTransformer::transformMultiPoint(
		const MultiPoint* geom,
		const Geometry* parent)
{

#if GEOS_DEBUG
	std::cerr << "GeometryTransformer::transformMultiPoint(MultiPoint " << geom <<", Geometry " << parent << ");" << std::endl;
#endif

	vector<Geometry*>* transGeomList = new vector<Geometry*>();

	for (unsigned int i=0, n=geom->getNumGeometries(); i<n; i++)
	{
		assert(dynamic_cast<const Point*>(geom->getGeometryN(i)));
		const Point* p = static_cast<const Point*>(
				geom->getGeometryN(i));

		Geometry::AutoPtr transformGeom = transformPoint(p, geom);
		if ( transformGeom.get() == NULL ) continue;
		if ( transformGeom->isEmpty() ) continue;

		// If an exception is thrown we'll leak
		transGeomList->push_back(transformGeom.release());
	}

	return Geometry::AutoPtr(factory->buildGeometry(transGeomList));

}

Geometry::AutoPtr
GeometryTransformer::transformLinearRing(
		const LinearRing* geom,
		const Geometry* parent)
{

#if GEOS_DEBUG
	std::cerr << "GeometryTransformer::transformLinearRing(LinearRing " << geom <<", Geometry " << parent << ");" << std::endl;
#endif

	CoordinateSequence::AutoPtr seq(transformCoordinates(
		geom->getCoordinatesRO(), geom));

	unsigned int seqSize = seq->size();

	// ensure a valid LinearRing
	if ( seqSize > 0 && seqSize < 4 && ! preserveType )
	{
		return factory->createLineString(seq);
	}

	return factory->createLinearRing(seq);

}

Geometry::AutoPtr
GeometryTransformer::transformLineString(
		const LineString* geom,
		const Geometry* parent)
{

#if GEOS_DEBUG
	std::cerr << "GeometryTransformer::transformLineString(LineString " << geom <<", Geometry " << parent << ");" << std::endl;
#endif

	// should check for 1-point sequences and downgrade them to points
	return factory->createLineString(
		transformCoordinates(geom->getCoordinatesRO(), geom));
}

Geometry::AutoPtr
GeometryTransformer::transformMultiLineString(
		const MultiLineString* geom,
		const Geometry* parent)
{

#if GEOS_DEBUG
	std::cerr << "GeometryTransformer::transformMultiLineString(MultiLineString " << geom <<", Geometry " << parent << ");" << std::endl;
#endif

	vector<Geometry*>* transGeomList = new vector<Geometry*>();

	for (unsigned int i=0, n=geom->getNumGeometries(); i<n; i++)
	{
		assert(dynamic_cast<const LineString*>(geom->getGeometryN(i)));
		const LineString* l = static_cast<const LineString*>(
				geom->getGeometryN(i));

		Geometry::AutoPtr transformGeom = transformLineString(l, geom);
		if ( transformGeom.get() == NULL ) continue;
		if ( transformGeom->isEmpty() ) continue;

		// If an exception is thrown we'll leak
		transGeomList->push_back(transformGeom.release());
	}

	return Geometry::AutoPtr(factory->buildGeometry(transGeomList));

}

Geometry::AutoPtr
GeometryTransformer::transformPolygon(
		const Polygon* geom,
		const Geometry* parent)
{

#if GEOS_DEBUG
	std::cerr << "GeometryTransformer::transformPolygon(Polygon " << geom <<", Geometry " << parent << ");" << std::endl;
#endif

	bool isAllValidLinearRings = true;

	assert(dynamic_cast<const LinearRing*>(geom->getExteriorRing()));
	const LinearRing* lr = static_cast<const LinearRing*>(
			geom->getExteriorRing());

	Geometry::AutoPtr shell = transformLinearRing(lr, geom);
	if ( shell.get() == NULL
		|| ! dynamic_cast<LinearRing*>(shell.get()) 
		|| shell->isEmpty() )
	{
		isAllValidLinearRings = false;
	}

	vector<Geometry*>* holes = new vector<Geometry*>();
	for (unsigned int i=0, n=geom->getNumInteriorRing(); i<n; i++)
	{
		assert(dynamic_cast<const LinearRing*>(
			geom->getInteriorRingN(i)));
		const LinearRing* lr = static_cast<const LinearRing*>(
			geom->getInteriorRingN(i));

		Geometry::AutoPtr hole(transformLinearRing(lr, geom));

		if ( hole.get() == NULL || hole->isEmpty() ) {
			continue;
		}

		if ( ! dynamic_cast<LinearRing*>(hole.get()) )
		{
			isAllValidLinearRings = false;
		}

		holes->push_back(hole.release());
	}

	if ( isAllValidLinearRings)
	{
		Geometry* sh = shell.release();
		assert(dynamic_cast<LinearRing*>(sh));
		return Geometry::AutoPtr(factory->createPolygon(
			static_cast<LinearRing*>(sh),
			holes));
	}
	else
	{
		// would like to use a manager constructor here
		vector<Geometry*>* components = new vector<Geometry*>();
		if ( shell.get() != NULL ) {
			components->push_back(shell.release());
		}

		components->insert(components->end(),
			holes->begin(), holes->end());

		delete holes; // :(

		return Geometry::AutoPtr(factory->buildGeometry(components));
	}

}

Geometry::AutoPtr
GeometryTransformer::transformMultiPolygon(
		const MultiPolygon* geom,
		const Geometry* parent)
{

#if GEOS_DEBUG
	std::cerr << "GeometryTransformer::transformMultiPolygon(MultiPolygon " << geom <<", Geometry " << parent << ");" << std::endl;
#endif

	vector<Geometry*>* transGeomList = new vector<Geometry*>();

	for (unsigned int i=0, n=geom->getNumGeometries(); i<n; i++)
	{
		assert(dynamic_cast<const Polygon*>(geom->getGeometryN(i)));
		const Polygon* p = static_cast<const Polygon*>(
				geom->getGeometryN(i));

		Geometry::AutoPtr transformGeom = transformPolygon(p, geom);
		if ( transformGeom.get() == NULL ) continue;
		if ( transformGeom->isEmpty() ) continue;

		// If an exception is thrown we'll leak
		transGeomList->push_back(transformGeom.release());
	}

	return Geometry::AutoPtr(factory->buildGeometry(transGeomList));

}

Geometry::AutoPtr
GeometryTransformer::transformGeometryCollection(
		const GeometryCollection* geom,
		const Geometry* parent)
{

#if GEOS_DEBUG
	std::cerr << "GeometryTransformer::transformGeometryCollection(GeometryCollection " << geom <<", Geometry " << parent << ");" << std::endl;
#endif

	vector<Geometry*>* transGeomList = new vector<Geometry*>();

	for (unsigned int i=0, n=geom->getNumGeometries(); i<n; i++)
	{
		Geometry::AutoPtr transformGeom = transform(
			geom->getGeometryN(i)); // no parent ?
		if ( transformGeom.get() == NULL ) continue;
		if ( pruneEmptyGeometry && transformGeom->isEmpty() ) continue;

		// If an exception is thrown we'll leak
		transGeomList->push_back(transformGeom.release());
	}

	if ( preserveGeometryCollectionType )
	{
		return Geometry::AutoPtr(factory->createGeometryCollection(
			transGeomList));
	}
	else
	{
		return Geometry::AutoPtr(factory->buildGeometry(transGeomList));
	}

}


} // namespace geos.geom.util
} // namespace geos.geom
} // namespace geos

/**********************************************************************
 * $Log$
 * Revision 1.3  2006/06/19 21:20:22  strk
 * updated port info
 *
 * Revision 1.2  2006/04/11 16:04:34  strk
 * geos::simplify::DouglasPeukerSimplifier class + unit test
 *
 * Revision 1.1  2006/04/11 12:21:48  strk
 * GeometryTransformer class ported
 *
 **********************************************************************/