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>
42 namespace geom
{ // geos::geom
46 Point::Point(CoordinateSequence
*newCoords
, const GeometryFactory
*factory
)
49 coordinates(newCoords
)
51 if (coordinates
.get()==NULL
) {
52 coordinates
.reset(factory
->getCoordinateSequenceFactory()->create(NULL
));
55 if (coordinates
->getSize() != 1)
57 throw util::IllegalArgumentException("Point coordinate list must contain a single element");
62 Point::Point(const Point
&p
)
65 coordinates(p
.coordinates
->clone())
70 Point::getCoordinates() const
72 return coordinates
->clone();
76 Point::getNumPoints() const
78 return isEmpty() ? 0 : 1;
82 Point::isEmpty() const
84 return coordinates
->isEmpty();
88 Point::isSimple() const
93 Dimension::DimensionType
94 Point::getDimension() const
96 return Dimension::P
; // point
100 Point::getCoordinateDimension() const
102 return (int) coordinates
->getDimension();
106 Point::getBoundaryDimension() const
108 return Dimension::False
;
115 throw util::UnsupportedOperationException("getX called on empty Point\n");
117 return getCoordinate()->x
;
124 throw util::UnsupportedOperationException("getY called on empty Point\n");
126 return getCoordinate()->y
;
130 Point::getCoordinate() const
132 return coordinates
->getSize()!=0 ? &(coordinates
->getAt(0)) : NULL
;
136 Point::getGeometryType() const
142 Point::getBoundary() const
144 return getFactory()->createGeometryCollection(NULL
);
148 Point::computeEnvelopeInternal() const
151 return Envelope::AutoPtr(new Envelope());
154 return Envelope::AutoPtr(new Envelope(getCoordinate()->x
,
155 getCoordinate()->x
, getCoordinate()->y
,
156 getCoordinate()->y
));
160 Point::apply_ro(CoordinateFilter
*filter
) const
162 if (isEmpty()) {return;}
163 filter
->filter_ro(getCoordinate());
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);
176 Point::apply_rw(GeometryFilter
*filter
)
178 filter
->filter_rw(this);
182 Point::apply_ro(GeometryFilter
*filter
) const
184 filter
->filter_ro(this);
188 Point::apply_rw(GeometryComponentFilter
*filter
)
190 filter
->filter_rw(this);
194 Point::apply_ro(GeometryComponentFilter
*filter
) const
196 filter
->filter_ro(this);
200 Point::apply_rw(CoordinateSequenceFilter
& filter
)
202 if (isEmpty()) return;
203 filter
.filter_rw(*coordinates
, 0);
204 if (filter
.isGeometryChanged()) geometryChanged();
208 Point::apply_ro(CoordinateSequenceFilter
& filter
) const
210 if (isEmpty()) return;
211 filter
.filter_ro(*coordinates
, 0);
212 //if (filter.isGeometryChanged()) geometryChanged();
216 Point::equalsExact(const Geometry
*other
, double tolerance
) const
218 if (!isEquivalentClass(other
)) {
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()));
247 //delete coordinates;
251 Point::getGeometryTypeId() const
257 const CoordinateSequence
*
258 Point::getCoordinatesRO() const
260 return coordinates
.get();
263 } // namespace geos::geom