001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.geom;
034:
035: import com.vividsolutions.jts.geom.*;
036: import com.vividsolutions.jump.util.MathUtil;
037:
038: /**
039: * Utility functions for {@link Envelope}s.
040: */
041: public class EnvelopeUtil {
042: private static GeometryFactory factory = new GeometryFactory();
043:
044: /**
045: * Expands an Envelope by a given distance.
046: * Both positive and negative distances are handled.
047: */
048: public static Envelope expand(Envelope env, double distance) {
049: /**
050: * If creating a negative buffer, check if Envelope becomes null (0-size)
051: */
052: if (distance < 0) {
053: double minSize = 2.0 * -distance;
054:
055: if (env.getWidth() < minSize) {
056: return new Envelope();
057: }
058:
059: if (env.getHeight() < minSize) {
060: return new Envelope();
061: }
062: }
063:
064: return new Envelope(env.getMinX() - distance, env.getMaxX()
065: + distance, env.getMinY() - distance, env.getMaxY()
066: + distance);
067: }
068:
069: public static void translate(Envelope e, Coordinate displacement) {
070: if (e.isNull()) {
071: return;
072: }
073:
074: e.init(e.getMinX() + displacement.x, e.getMaxX()
075: + displacement.x, e.getMinY() + displacement.y, e
076: .getMaxY()
077: + displacement.y);
078: }
079:
080: /**
081: * @param originalEnvelope the original envelope
082: * @param extentFraction the buffer distance expressed as a fraction of the
083: * average envelope extent
084: */
085: public static Envelope bufferByFraction(Envelope originalEnvelope,
086: double extentFraction) {
087: Envelope bufferedEnvelope = new Envelope(originalEnvelope);
088: double averageExtent = (bufferedEnvelope.getWidth() + bufferedEnvelope
089: .getHeight()) / 2d;
090: double buffer = averageExtent * extentFraction;
091:
092: if (averageExtent == 0) {
093: //Point feature. Set the buffer to something reasonable. [Jon Aquino]
094: buffer = 10;
095: }
096:
097: bufferedEnvelope.expandToInclude(bufferedEnvelope.getMaxX()
098: + buffer, bufferedEnvelope.getMaxY() + buffer);
099: bufferedEnvelope.expandToInclude(bufferedEnvelope.getMinX()
100: - buffer, bufferedEnvelope.getMinY() - buffer);
101:
102: return bufferedEnvelope;
103: }
104:
105: public static Coordinate centre(Envelope e) {
106: return new Coordinate(MathUtil.avg(e.getMinX(), e.getMaxX()),
107: MathUtil.avg(e.getMinY(), e.getMaxY()));
108: }
109:
110: public static Geometry toGeometry(Envelope envelope) {
111: if ((envelope.getWidth() == 0) && (envelope.getHeight() == 0)) {
112: return factory.createPoint(new Coordinate(envelope
113: .getMinX(), envelope.getMinY()));
114: }
115:
116: if ((envelope.getWidth() == 0) || (envelope.getHeight() == 0)) {
117: return factory.createLineString(new Coordinate[] {
118: new Coordinate(envelope.getMinX(), envelope
119: .getMinY()),
120: new Coordinate(envelope.getMaxX(), envelope
121: .getMaxY()) });
122: }
123:
124: return factory.createPolygon(factory
125: .createLinearRing(new Coordinate[] {
126: new Coordinate(envelope.getMinX(), envelope
127: .getMinY()),
128: new Coordinate(envelope.getMinX(), envelope
129: .getMaxY()),
130: new Coordinate(envelope.getMaxX(), envelope
131: .getMaxY()),
132: new Coordinate(envelope.getMaxX(), envelope
133: .getMinY()),
134: new Coordinate(envelope.getMinX(), envelope
135: .getMinY()) }), null);
136: }
137: }
|