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 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
/**********************************************************************
* $Id: Polygon.cpp,v 1.45 2004/12/08 13:54:43 strk Exp $
*
* GEOS - Geometry Engine Open Source
* http://geos.refractions.net
*
* Copyright (C) 2001-2002 Vivid Solutions 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.
*
**********************************************************************
* $Log: Polygon.cpp,v $
* Revision 1.45 2004/12/08 13:54:43 strk
* gcc warnings checked and fixed, general cleanups.
*
* Revision 1.44 2004/11/12 18:12:05 strk
* Changed ::getBoundary() to return LineString if polygon has no holes.
* (has required to pass OGC conformance test T20)
*
* Revision 1.43 2004/07/27 16:35:46 strk
* Geometry::getEnvelopeInternal() changed to return a const Envelope *.
* This should reduce object copies as once computed the envelope of a
* geometry remains the same.
*
* Revision 1.42 2004/07/22 07:04:49 strk
* Documented missing geometry functions.
*
* Revision 1.41 2004/07/13 08:33:52 strk
* Added missing virtual destructor to virtual classes.
* Fixed implicit unsigned int -> int casts
*
* Revision 1.40 2004/07/08 19:34:49 strk
* Mirrored JTS interface of CoordinateSequence, factory and
* default implementations.
* Added DefaultCoordinateSequenceFactory::instance() function.
*
* Revision 1.39 2004/07/06 17:58:22 strk
* Removed deprecated Geometry constructors based on PrecisionModel and
* SRID specification. Removed SimpleGeometryPrecisionReducer capability
* of changing Geometry's factory. Reverted Geometry::factory member
* to be a reference to external factory.
*
* Revision 1.38 2004/07/05 10:50:20 strk
* deep-dopy construction taken out of Geometry and implemented only
* in GeometryFactory.
* Deep-copy geometry construction takes care of cleaning up copies
* on exception.
* Implemented clone() method for CoordinateSequence
* Changed createMultiPoint(CoordinateSequence) signature to reflect
* copy semantic (by-ref instead of by-pointer).
* Cleaned up documentation.
*
* Revision 1.37 2004/07/02 13:28:26 strk
* Fixed all #include lines to reflect headers layout change.
* Added client application build tips in README.
*
* Revision 1.36 2004/07/01 14:12:44 strk
*
* Geometry constructors come now in two flavors:
* - deep-copy args (pass-by-reference)
* - take-ownership of args (pass-by-pointer)
* Same functionality is available through GeometryFactory,
* including buildGeometry().
*
* Revision 1.35 2004/06/28 21:58:24 strk
* Constructors speedup.
*
* Revision 1.34 2004/06/28 21:11:43 strk
* Moved getGeometryTypeId() definitions from geom.h to each geometry module.
* Added holes argument check in Polygon.cpp.
*
* Revision 1.33 2004/06/15 20:30:47 strk
* updated to respect deep-copy GeometryCollection interface
*
* Revision 1.32 2004/04/20 13:24:15 strk
* More leaks removed.
*
* Revision 1.31 2004/04/20 08:52:01 strk
* GeometryFactory and Geometry const correctness.
* Memory leaks removed from SimpleGeometryPrecisionReducer
* and GeometryFactory.
*
* Revision 1.30 2004/04/01 10:44:33 ybychkov
* All "geom" classes from JTS 1.3 upgraded to JTS 1.4
*
* Revision 1.29 2004/03/31 07:50:37 ybychkov
* "geom" partially upgraded to JTS 1.4
*
* Revision 1.28 2004/02/27 17:43:45 strk
* memory leak fix in Polygon::getArea() - reported by 'Manuel Prieto Villegas' <mprieto@dap.es>
*
* Revision 1.27 2003/11/07 01:23:42 pramsey
* Add standard CVS headers licence notices and copyrights to all cpp and h
* files.
*
* Revision 1.26 2003/10/31 16:36:04 strk
* Re-introduced clone() method. Copy constructor could not really
* replace it.
*
* Revision 1.25 2003/10/17 05:51:21 ybychkov
* Fixed a small memory leak.
*
* Revision 1.24 2003/10/16 08:50:00 strk
* Memory leak fixes. Improved performance by mean of more calls to
* new getCoordinatesRO() when applicable.
*
**********************************************************************/
#include <geos/geom.h>
#include <typeinfo>
#include <geos/geosAlgorithm.h>
namespace geos {
Polygon::Polygon(const Polygon &p): Geometry(p.getFactory()){
shell=new LinearRing(*p.shell);
holes=new vector<Geometry *>();
for(int i=0;i<(int)p.holes->size();i++) {
LinearRing *h=new LinearRing(* (LinearRing*)(*p.holes)[i]);
holes->push_back(h);
}
}
/*
* Constructs a <code>Polygon</code> with the given exterior
* and interior boundaries.
*
* @param shell the outer boundary of the new <code>Polygon</code>,
* or <code>null</code> or an empty
* <code>LinearRing</code> if the empty geometry
* is to be created.
*
* @param holes the <code>LinearRings</code> defining the inner
* boundaries of the new <code>Polygon</code>, or
* <code>null</code> or empty <code>LinearRing</code>s
* if the empty geometry is to be created.
*
* Polygon will take ownership of Shell and Holes LinearRings
*
*/
Polygon::Polygon(LinearRing *newShell, vector<Geometry *> *newHoles, const GeometryFactory *newFactory): Geometry(newFactory)
{
if (newShell==NULL) {
shell=getFactory()->createLinearRing(NULL);
}
else
{
if (newShell->isEmpty() && hasNonEmptyElements(newHoles)) {
delete newShell;
delete newHoles;
throw new IllegalArgumentException("shell is empty but holes are not");
}
shell=newShell;
}
if (newHoles==NULL)
{
holes=new vector<Geometry *>();
}
else
{
if (hasNullElements(newHoles)) {
delete newShell;
delete newHoles;
throw new IllegalArgumentException("holes must not contain null elements");
}
for (unsigned int i=0; i<newHoles->size(); i++)
if ( (*newHoles)[i]->getGeometryTypeId() != GEOS_LINEARRING)
throw new IllegalArgumentException("holes must be LinearRings");
holes=newHoles;
}
}
Geometry *Polygon::clone() const {
return new Polygon(*this);
}
CoordinateSequence* Polygon::getCoordinates() const {
if (isEmpty()) {
return getFactory()->getCoordinateSequenceFactory()->create(NULL);
}
vector<Coordinate> *cl = new vector<Coordinate>;
int k = -1;
const CoordinateSequence* shellCoords=shell->getCoordinatesRO();
for (int x = 0; x < shellCoords->getSize(); x++) {
k++;
cl->push_back(shellCoords->getAt(x));
}
for (unsigned int i = 0; i < holes->size(); i++) {
const CoordinateSequence* childCoords=((LinearRing *)(*holes)[i])->getCoordinatesRO();
for (int j = 0; j < childCoords->getSize(); j++) {
k++;
cl->push_back(childCoords->getAt(j));
}
}
return getFactory()->getCoordinateSequenceFactory()->create(cl);
}
int Polygon::getNumPoints() const {
int numPoints = shell->getNumPoints();
for (unsigned int i = 0; i < holes->size(); i++) {
numPoints += ((LinearRing *)(*holes)[i])->getNumPoints();
}
return numPoints;
}
int Polygon::getDimension() const {
return 2;
}
int Polygon::getBoundaryDimension() const {
return 1;
}
bool Polygon::isEmpty() const {
return shell->isEmpty();
}
bool Polygon::isSimple() const {
return true;
}
const LineString* Polygon::getExteriorRing() const {
return shell;
}
int Polygon::getNumInteriorRing() const {
return (int)holes->size();
}
const LineString* Polygon::getInteriorRingN(int n) const {
return (LineString *) (*holes)[n];
}
string Polygon::getGeometryType() const {
return "Polygon";
}
// Returns a newly allocated Geometry object
Geometry* Polygon::getBoundary() const {
if (isEmpty()) {
return getFactory()->createGeometryCollection(NULL);
}
if ( ! holes->size() )
{
return shell->clone();
}
vector<Geometry *> rings(holes->size()+1);
rings[0]=shell;
for (unsigned int i=0; i<holes->size(); i++) {
rings[i + 1] = (*holes)[i];
}
MultiLineString *ret =getFactory()->createMultiLineString(rings);
return ret;
}
Envelope* Polygon::computeEnvelopeInternal() const {
return new Envelope(*(shell->getEnvelopeInternal()));
}
bool Polygon::equalsExact(const Geometry *other, double tolerance) const {
if (!isEquivalentClass(other)) {
return false;
}
const Polygon* otherPolygon=dynamic_cast<const Polygon*>(other);
if (typeid(*(otherPolygon->shell))!=typeid(Geometry)) {
return false;
}
Geometry* otherPolygonShell=dynamic_cast<Geometry *>(otherPolygon->shell);
if (!shell->equalsExact(otherPolygonShell, tolerance)) {
return false;
}
if (holes->size()!=otherPolygon->holes->size()) {
return false;
}
for (unsigned int i = 0; i < holes->size(); i++) {
if (!((LinearRing *)(*holes)[i])->equalsExact((*(otherPolygon->holes))[i],tolerance)) {
return false;
}
}
return true;
}
void Polygon::apply_ro(CoordinateFilter *filter) const {
shell->apply_ro(filter);
for (unsigned int i = 0; i < holes->size(); i++) {
((LinearRing *)(*holes)[i])->apply_ro(filter);
}
}
void Polygon::apply_rw(CoordinateFilter *filter) {
shell->apply_rw(filter);
for (unsigned int i = 0; i < holes->size(); i++) {
((LinearRing *)(*holes)[i])->apply_rw(filter);
}
}
void Polygon::apply_rw(GeometryFilter *filter) {
filter->filter_rw(this);
}
void Polygon::apply_ro(GeometryFilter *filter) const {
filter->filter_ro(this);
}
Geometry* Polygon::convexHull() const {
return getExteriorRing()->convexHull();
}
void Polygon::normalize() {
normalize(shell, true);
for (unsigned int i = 0; i < holes->size(); i++) {
normalize((LinearRing *)(*holes)[i], false);
}
sort(holes->begin(),holes->end(),greaterThen);
}
int Polygon::compareToSameClass(const Geometry *p) const {
return shell->compareToSameClass(((Polygon*)p)->shell);
}
void Polygon::normalize(LinearRing *ring, bool clockwise) {
if (ring->isEmpty()) {
return;
}
CoordinateSequence* uniqueCoordinates=ring->getCoordinates();
uniqueCoordinates->deleteAt(uniqueCoordinates->getSize()-1);
const Coordinate* minCoordinate=CoordinateSequence::minCoordinate(uniqueCoordinates);
CoordinateSequence::scroll(uniqueCoordinates, minCoordinate);
uniqueCoordinates->add(uniqueCoordinates->getAt(0));
if (CGAlgorithms::isCCW(uniqueCoordinates)==clockwise) {
CoordinateSequence::reverse(uniqueCoordinates);
}
ring->setPoints(uniqueCoordinates);
delete(uniqueCoordinates);
}
const Coordinate* Polygon::getCoordinate() const {
return shell->getCoordinate();
}
/*
* Returns the area of this <code>Polygon</code>
*
*@return the area of the polygon
*/
double Polygon::getArea() const {
double area=0.0;
area+=fabs(CGAlgorithms::signedArea(shell->getCoordinatesRO()));
for(unsigned int i=0;i<holes->size();i++) {
CoordinateSequence *h=(*holes)[i]->getCoordinates();
area-=fabs(CGAlgorithms::signedArea(h));
delete h;
}
return area;
}
/**
* Returns the perimeter of this <code>Polygon</code>
*
* @return the perimeter of the polygon
*/
double Polygon::getLength() const {
double len=0.0;
len+=shell->getLength();
for(unsigned int i=0;i<holes->size();i++) {
len+=(*holes)[i]->getLength();
}
return len;
}
void Polygon::apply_ro(GeometryComponentFilter *filter) const {
filter->filter_ro(this);
shell->apply_ro(filter);
for(unsigned int i=0;i<holes->size();i++) {
(*holes)[i]->apply_ro(filter);
}
}
void Polygon::apply_rw(GeometryComponentFilter *filter) {
filter->filter_rw(this);
shell->apply_rw(filter);
for(unsigned int i=0;i<holes->size();i++) {
(*holes)[i]->apply_rw(filter);
}
}
Polygon::~Polygon(){
delete shell;
for(int i=0;i<(int)holes->size();i++) {
delete (*holes)[i];
}
delete holes;
}
GeometryTypeId
Polygon::getGeometryTypeId() const {
return GEOS_POLYGON;
}
}
|