001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/model/spatialschema/PointImpl.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53115 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042:
043: ---------------------------------------------------------------------------*/
044: package org.deegree.model.spatialschema;
045:
046: import java.io.Serializable;
047:
048: import org.deegree.model.crs.CoordinateSystem;
049:
050: /**
051: * default implementation of the Point interface.
052: *
053: * <p>
054: * ------------------------------------------------------------
055: * </p>
056: *
057: * @version 5.6.2001
058: * @author Andreas Poth
059: * <p>
060: */
061:
062: final class PointImpl extends PrimitiveImpl implements Point,
063: Serializable {
064: /** Use serialVersionUID for interoperability. */
065: private final static long serialVersionUID = 6106017748940535740L;
066:
067: private Position position = null;
068:
069: /**
070: * constructor. initializes a point to the coordinate 0/0
071: *
072: * @param crs
073: * spatial reference system of the point
074: */
075: public PointImpl(CoordinateSystem crs) {
076: super (crs);
077: position = new PositionImpl();
078: empty = true;
079: centroid = this ;
080: }
081:
082: /**
083: * constructor for initializing a point within a two-dimensional coordinate system
084: *
085: * @param x
086: * x-value of the point
087: * @param y
088: * y-value of the point
089: * @param crs
090: * spatial reference system of the point
091: */
092: public PointImpl(double x, double y, CoordinateSystem crs) {
093: super (crs);
094: position = new PositionImpl(x, y);
095: empty = false;
096: centroid = this ;
097: }
098:
099: /**
100: * constructor for initializing a point within a three-dimensional coordinate system
101: *
102: * @param x
103: * x-value of the point
104: * @param y
105: * y-value of the point
106: * @param z
107: * z-value of the point
108: * @param crs
109: * spatial reference system of the point
110: */
111: public PointImpl(double x, double y, double z, CoordinateSystem crs) {
112: super (crs);
113: position = new PositionImpl(x, y, z);
114: empty = false;
115: centroid = this ;
116: }
117:
118: /**
119: * constructor
120: *
121: * @param gmo
122: * existing GM_Point
123: */
124: public PointImpl(Point gmo) {
125: super (gmo.getCoordinateSystem());
126: position = new PositionImpl(gmo.getAsArray());
127: empty = false;
128: centroid = this ;
129: }
130:
131: /**
132: * constructor
133: *
134: * @param gmo
135: * existing GM_Point
136: * @param crs
137: * spatial reference system of the point
138: */
139: public PointImpl(Position gmo, CoordinateSystem crs) {
140: super (crs);
141: position = gmo;
142: empty = false;
143: centroid = this ;
144: }
145:
146: /**
147: * checks if this point is completly equal to the submitted geometry
148: */
149: @Override
150: public boolean equals(Object other) {
151: if (super .equals(other) && (other instanceof Point)) {
152: Point p = (Point) other;
153: boolean flagEq = Math.abs(getX() - p.getX()) < mute
154: && Math.abs(getY() - p.getY()) < mute;
155: if (getCoordinateDimension() == 3) {
156: flagEq = flagEq && Math.abs(getZ() - p.getZ()) < mute;
157: }
158: return flagEq;
159: }
160:
161: return false;
162: }
163:
164: /**
165: * The operation "dimension" shall return the inherent dimension of this Geometry, which shall
166: * be less than or equal to the coordinate dimension. The dimension of a collection of geometric
167: * objects shall be the largest dimension of any of its pieces. Points are 0-dimensional, curves
168: * are 1-dimensional, surfaces are 2-dimensional, and solids are 3-dimensional.
169: */
170: public int getDimension() {
171: return 0;
172: }
173:
174: /**
175: * The operation "coordinateDimension" shall return the dimension of the coordinates that define
176: * this Geometry, which must be the same as the coordinate dimension of the coordinate reference
177: * system for this Geometry.
178: */
179: public int getCoordinateDimension() {
180: return getPosition().getCoordinateDimension();
181: }
182:
183: /**
184: * returns a shallow copy of the geometry.
185: */
186: @Override
187: public Object clone() {
188: return new PointImpl(this );
189: }
190:
191: /**
192: * returns the x-value of this point
193: */
194: public double getX() {
195: return position.getX();
196: }
197:
198: /**
199: * returns the y-value of this point
200: */
201: public double getY() {
202: return position.getY();
203: }
204:
205: /**
206: * returns the y-value of this point
207: */
208: public double getZ() {
209: return position.getZ();
210: }
211:
212: /**
213: * returns the x- and y-value of the point as a two dimensional array the first field contains
214: * the x- the second field the y-value.
215: */
216: public double[] getAsArray() {
217: return position.getAsArray();
218: }
219:
220: /**
221: * translate the point by the submitted values. the <code>dz</code>- value will be ignored.
222: */
223: @Override
224: public void translate(double[] d) {
225: setValid(false);
226: position.translate(d);
227: }
228:
229: /**
230: * @return position
231: *
232: */
233: public Position getPosition() {
234: return position;
235: }
236:
237: /**
238: * The Boolean valued operation "intersects" shall return TRUE if this Geometry intersects
239: * another Geometry. Within a Complex, the Primitives do not intersect one another. In general,
240: * topologically structured data uses shared geometric objects to capture intersection
241: * information.
242: * <p>
243: * </p>
244: * dummy implementation
245: */
246: @Override
247: public boolean intersects(Geometry gmo) {
248: boolean inter = false;
249:
250: try {
251: if (gmo instanceof Point) {
252: inter = LinearIntersects.intersects((Point) gmo, this );
253: } else if (gmo instanceof Curve) {
254: inter = LinearIntersects.intersects(this , (Curve) gmo);
255: } else if (gmo instanceof Surface) {
256: inter = LinearIntersects
257: .intersects(this , (Surface) gmo);
258: } else if (gmo instanceof Aggregate) {
259: inter = intersectsAggregate((Aggregate) gmo);
260: }
261: } catch (Exception e) {
262: // ignore
263: }
264:
265: return inter;
266: }
267:
268: /**
269: * the operations returns true if the submitted multi primitive intersects with the curve
270: * segment
271: */
272: private boolean intersectsAggregate(Aggregate mprim)
273: throws Exception {
274: boolean inter = false;
275:
276: int cnt = mprim.getSize();
277:
278: for (int i = 0; i < cnt; i++) {
279: if (intersects(mprim.getObjectAt(i))) {
280: inter = true;
281: break;
282: }
283: }
284:
285: return inter;
286: }
287:
288: /**
289: * The Boolean valued operation "contains" shall return TRUE if this Geometry contains another
290: * Geometry.
291: * <p>
292: * </p>
293: */
294: @Override
295: public boolean contains(Geometry gmo) {
296: throw new NoSuchMethodError(
297: "the contains operation for points "
298: + "isn't supported at the moment.");
299: }
300:
301: /**
302: * recalculates internal parameters
303: */
304: @Override
305: protected void calculateParam() {
306: setValid(true);
307: }
308:
309: @Override
310: public String toString() {
311: StringBuffer ret = new StringBuffer(30);
312: ret.append(this .getClass().getName()).append(": ");
313:
314: for (int i = 0; i < getAsArray().length; i++) {
315: ret.append(getAsArray()[i]).append(' ');
316: }
317:
318: return ret.toString();
319: }
320:
321: }
|