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.ArrayList;
019:
020: import com.vividsolutions.jts.geom.Coordinate;
021: import com.vividsolutions.jts.geom.Geometry;
022: import com.vividsolutions.jts.geom.GeometryFactory;
023: import com.vividsolutions.jts.geom.LinearRing;
024: import com.vividsolutions.jts.geom.TopologyException;
025:
026: /**
027: * Creates a simple OGC LinearRing (a closed LineString).
028: *
029: * @author Ian Turton, CCG
030: * @author Rob Hranac, Vision for New York
031: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/gml/SubHandlerLinearRing.java $
032: * @version $Id: SubHandlerLinearRing.java 26168 2007-07-06 19:58:03Z jgarnett $
033: */
034: public class SubHandlerLinearRing extends SubHandler {
035: /** Internal coordinate list. */
036: private ArrayList coordinateList = new ArrayList();
037:
038: /**
039: * Creates a new instance of GMLLinearRingHandler.
040: */
041: public SubHandlerLinearRing() {
042: }
043:
044: /**
045: * Adds a coordinate to the LinearRing.
046: *
047: * @param coordinate The coordinate to add to the LinearRing.
048: */
049: public void addCoordinate(Coordinate coordinate) {
050: coordinateList.add(coordinate);
051: }
052:
053: /**
054: * Determine whether or not this LinearRing is ready to be created.
055: *
056: * @param message The current geometry type in the GML stream.
057: *
058: * @return Ready for creation flag.
059: */
060: public boolean isComplete(String message) {
061: // makes sure that this LinearRing has more than one coordinate and its first and last are identical
062: if (coordinateList.size() > 1) {
063: Coordinate firstCoordinate = (Coordinate) coordinateList
064: .get(0);
065: Coordinate lastCoordinate = (Coordinate) coordinateList
066: .get(coordinateList.size() - 1);
067:
068: if (lastCoordinate.equals2D(firstCoordinate)) {
069: return true;
070: } else {
071: return false;
072: }
073: } else {
074: return false;
075: }
076: }
077:
078: /**
079: * Create the LinearRing.
080: *
081: * @param geometryFactory The geometry factory used for the build.
082: *
083: * @return LinearRing geometry created.
084: */
085: public Geometry create(GeometryFactory geometryFactory) {
086: try {
087: Coordinate[] coords = (Coordinate[]) coordinateList
088: .toArray(new Coordinate[coordinateList.size()]);
089: LinearRing ring = geometryFactory.createLinearRing(coords);
090: ring.setUserData(getSRS());
091: ring.setSRID(getSRID());
092: return ring;
093: } catch (TopologyException e) {
094: System.err
095: .println("Caught Topology exception in GMLLinearRingHandler");
096:
097: return null;
098: }
099: }
100: }
|