Complete Note#1 in the http://wiki.osgeo.org/wiki/GEOS_Provenance_Review to get out...
[geos.git] / src / geom / Point.cpp
blob9cda4d3790f2e1322063ed5a76d4e5800e94d562
1 /**********************************************************************
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
6 * Copyright (C) 2011 Sandro Santilli <strk@keybit.net>
7 * Copyright (C) 2001-2002 Vivid Solutions Inc.
8 * Copyright (C) 2005 2006 Refractions Research Inc.
10 * This is free software; you can redistribute and/or modify it under
11 * the terms of the GNU Lesser General Public Licence as published
12 * by the Free Software Foundation.
13 * See the COPYING file for more information.
15 **********************************************************************
17 * Last port: geom/Point.java r320 (JTS-1.12)
19 **********************************************************************/
21 #include <geos/util/UnsupportedOperationException.h>
22 #include <geos/util/IllegalArgumentException.h>
23 #include <geos/geom/Coordinate.h>
24 #include <geos/geom/Point.h>
25 #include <geos/geom/CoordinateSequence.h>
26 #include <geos/geom/CoordinateSequenceFilter.h>
27 #include <geos/geom/CoordinateFilter.h>
28 #include <geos/geom/GeometryFilter.h>
29 #include <geos/geom/GeometryComponentFilter.h>
30 #include <geos/geom/CoordinateSequenceFactory.h>
31 #include <geos/geom/Dimension.h>
32 #include <geos/geom/Envelope.h>
33 #include <geos/geom/GeometryCollection.h>
34 #include <geos/geom/GeometryFactory.h>
36 #include <string>
37 #include <memory>
39 using namespace std;
41 namespace geos {
42 namespace geom { // geos::geom
45 /*protected*/
46 Point::Point(CoordinateSequence *newCoords, const GeometryFactory *factory)
48 Geometry(factory),
49 coordinates(newCoords)
51 if (coordinates.get()==NULL) {
52 coordinates.reset(factory->getCoordinateSequenceFactory()->create(NULL));
53 return;
55 if (coordinates->getSize() != 1)
57 throw util::IllegalArgumentException("Point coordinate list must contain a single element");
61 /*protected*/
62 Point::Point(const Point &p)
64 Geometry(p),
65 coordinates(p.coordinates->clone())
69 CoordinateSequence *
70 Point::getCoordinates() const
72 return coordinates->clone();
75 size_t
76 Point::getNumPoints() const
78 return isEmpty() ? 0 : 1;
81 bool
82 Point::isEmpty() const
84 return coordinates->isEmpty();
87 bool
88 Point::isSimple() const
90 return true;
93 Dimension::DimensionType
94 Point::getDimension() const
96 return Dimension::P; // point
99 int
100 Point::getCoordinateDimension() const
102 return (int) coordinates->getDimension();
106 Point::getBoundaryDimension() const
108 return Dimension::False;
111 double
112 Point::getX() const
114 if (isEmpty()) {
115 throw util::UnsupportedOperationException("getX called on empty Point\n");
117 return getCoordinate()->x;
120 double
121 Point::getY() const
123 if (isEmpty()) {
124 throw util::UnsupportedOperationException("getY called on empty Point\n");
126 return getCoordinate()->y;
129 const Coordinate *
130 Point::getCoordinate() const
132 return coordinates->getSize()!=0 ? &(coordinates->getAt(0)) : NULL;
135 string
136 Point::getGeometryType() const
138 return "Point";
141 Geometry *
142 Point::getBoundary() const
144 return getFactory()->createGeometryCollection(NULL);
147 Envelope::AutoPtr
148 Point::computeEnvelopeInternal() const
150 if (isEmpty()) {
151 return Envelope::AutoPtr(new Envelope());
154 return Envelope::AutoPtr(new Envelope(getCoordinate()->x,
155 getCoordinate()->x, getCoordinate()->y,
156 getCoordinate()->y));
159 void
160 Point::apply_ro(CoordinateFilter *filter) const
162 if (isEmpty()) {return;}
163 filter->filter_ro(getCoordinate());
166 void
167 Point::apply_rw(const CoordinateFilter *filter)
169 if (isEmpty()) {return;}
170 Coordinate newcoord = coordinates->getAt(0);
171 filter->filter_rw(&newcoord);
172 coordinates->setAt(newcoord, 0);
175 void
176 Point::apply_rw(GeometryFilter *filter)
178 filter->filter_rw(this);
181 void
182 Point::apply_ro(GeometryFilter *filter) const
184 filter->filter_ro(this);
187 void
188 Point::apply_rw(GeometryComponentFilter *filter)
190 filter->filter_rw(this);
193 void
194 Point::apply_ro(GeometryComponentFilter *filter) const
196 filter->filter_ro(this);
199 void
200 Point::apply_rw(CoordinateSequenceFilter& filter)
202 if (isEmpty()) return;
203 filter.filter_rw(*coordinates, 0);
204 if (filter.isGeometryChanged()) geometryChanged();
207 void
208 Point::apply_ro(CoordinateSequenceFilter& filter) const
210 if (isEmpty()) return;
211 filter.filter_ro(*coordinates, 0);
212 //if (filter.isGeometryChanged()) geometryChanged();
215 bool
216 Point::equalsExact(const Geometry *other, double tolerance) const
218 if (!isEquivalentClass(other)) {
219 return false;
222 // assume the isEquivalentClass would return false
223 // if other is not a point
224 assert(dynamic_cast<const Point*>(other));
226 if ( isEmpty() ) return other->isEmpty();
227 else if ( other->isEmpty() ) return false;
229 const Coordinate* this_coord = getCoordinate();
230 const Coordinate* other_coord = other->getCoordinate();
232 // assume the isEmpty checks above worked :)
233 assert(this_coord && other_coord);
235 return equal(*this_coord, *other_coord, tolerance);
239 Point::compareToSameClass(const Geometry *g) const
241 const Point* p = dynamic_cast<const Point*>(g);
242 return getCoordinate()->compareTo(*(p->getCoordinate()));
245 Point::~Point()
247 //delete coordinates;
250 GeometryTypeId
251 Point::getGeometryTypeId() const
253 return GEOS_POINT;
256 /*public*/
257 const CoordinateSequence*
258 Point::getCoordinatesRO() const
260 return coordinates.get();
263 } // namespace geos::geom
264 } // namesapce geos