001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.gml;
017:
018: import java.util.logging.Logger;
019:
020: import com.vividsolutions.jts.geom.Coordinate;
021: import com.vividsolutions.jts.geom.Polygon;
022:
023: /**
024: * Creates a simple OGC box.
025: *
026: * @author Ian Turton, CCG
027: * @author Rob Hranac, Vision for New York
028: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/gml/SubHandlerBox.java $
029: * @version $Id: SubHandlerBox.java 27862 2007-11-12 19:51:19Z desruisseaux $
030: */
031: public class SubHandlerBox extends SubHandler {
032: /** The logger for the GML module */
033: private static final Logger LOGGER = org.geotools.util.logging.Logging
034: .getLogger("org.geotools.gml");
035:
036: /** */
037: com.vividsolutions.jts.geom.Envelope e = new com.vividsolutions.jts.geom.Envelope();
038:
039: /**
040: * Creates a new instance of GMLBoxHandler.
041: */
042: public SubHandlerBox() {
043: LOGGER.entering("SubHandlerBox", "new");
044: LOGGER.exiting("SubHandlerBox", "new");
045: }
046:
047: /**
048: * Sets a corner.
049: *
050: * @param c the coordinate of the corner.
051: */
052: public void addCoordinate(Coordinate c) {
053: LOGGER.entering("SubHandlerBox", "addCoordinate", c);
054: e.expandToInclude(c);
055: LOGGER.exiting("SubHandlerBox", "addCoordinate");
056: }
057:
058: /**
059: * Sets a corner.
060: *
061: * @param message The geometry to inspect.
062: *
063: * @return Flag for a complete geometry.
064: */
065: public boolean isComplete(String message) {
066: LOGGER.entering("SubHandlerBox", "isComplete", message);
067: LOGGER.exiting("SubHandlerBox", "isComplete", Boolean.TRUE);
068:
069: return true;
070: }
071:
072: /**
073: * Builds and returns the polygon.
074: *
075: * @param geometryFactory the geometryFactory to be used to build the
076: * polygon.
077: *
078: * @return the polygon.
079: */
080: public com.vividsolutions.jts.geom.Geometry create(
081: com.vividsolutions.jts.geom.GeometryFactory geometryFactory) {
082: LOGGER.entering("SubHandlerBox", "create", geometryFactory);
083:
084: Coordinate[] c = new Coordinate[5];
085: c[0] = new Coordinate(e.getMinX(), e.getMinY());
086: c[1] = new Coordinate(e.getMinX(), e.getMaxY());
087: c[2] = new Coordinate(e.getMaxX(), e.getMaxY());
088: c[3] = new Coordinate(e.getMaxX(), e.getMinY());
089: c[4] = new Coordinate(e.getMinX(), e.getMinY());
090:
091: com.vividsolutions.jts.geom.LinearRing r = null;
092:
093: try {
094: r = geometryFactory.createLinearRing(c);
095: } catch (com.vividsolutions.jts.geom.TopologyException e) {
096: System.err.println("Topology Exception in GMLBoxHandler");
097:
098: return null; // could this be handled better?
099: }
100:
101: Polygon polygon = geometryFactory.createPolygon(r, null);
102: LOGGER.exiting("SubHandlerBox", "create", polygon);
103: polygon.setUserData(getSRS());
104: polygon.setSRID(getSRID());
105:
106: return polygon;
107: }
108: }
|