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.gml2.bindings;
017:
018: import java.util.List;
019: import javax.xml.namespace.QName;
020: import com.vividsolutions.jts.geom.CoordinateSequence;
021: import com.vividsolutions.jts.geom.CoordinateSequenceFactory;
022: import com.vividsolutions.jts.geom.GeometryFactory;
023: import com.vividsolutions.jts.geom.LineString;
024: import com.vividsolutions.jts.geom.LinearRing;
025: import org.geotools.xml.AbstractComplexBinding;
026: import org.geotools.xml.ElementInstance;
027: import org.geotools.xml.Node;
028:
029: /**
030: * Binding object for the type http://www.opengis.net/gml:LinearRingType.
031: *
032: * <p>
033: * <pre>
034: * <code>
035: * <complexType name="LinearRingType">
036: * <annotation>
037: * <documentation> A LinearRing is defined by four or more
038: * coordinate tuples, with linear interpolation
039: * between them; the first and last coordinates must
040: * be coincident. </documentation>
041: * </annotation>
042: * <complexContent>
043: * <extension base="gml:AbstractGeometryType">
044: * <sequence>
045: * <choice>
046: * <element ref="gml:coord" minOccurs="4" maxOccurs="unbounded"/>
047: * <element ref="gml:coordinates"/>
048: * </choice>
049: * </sequence>
050: * </extension>
051: * </complexContent>
052: * </complexType>
053: *
054: * </code>
055: * </pre>
056: * </p>
057: *
058: * @generated
059: */
060: public class GMLLinearRingTypeBinding extends AbstractComplexBinding {
061: CoordinateSequenceFactory csFactory;
062: GeometryFactory gFactory;
063:
064: public GMLLinearRingTypeBinding(
065: CoordinateSequenceFactory csFactory,
066: GeometryFactory gFactory) {
067: this .csFactory = csFactory;
068: this .gFactory = gFactory;
069: }
070:
071: /**
072: * @generated
073: */
074: public QName getTarget() {
075: return GML.LinearRingType;
076: }
077:
078: public int getExecutionMode() {
079: return BEFORE;
080: }
081:
082: /**
083: * <!-- begin-user-doc -->
084: * <!-- end-user-doc -->
085: *
086: * @generated modifiable
087: */
088: public Class getType() {
089: return LinearRing.class;
090: }
091:
092: /**
093: * <!-- begin-user-doc -->
094: * <!-- end-user-doc -->
095: *
096: * @generated modifiable
097: */
098: public Object parse(ElementInstance instance, Node node,
099: Object value) throws Exception {
100: List coordinates = node.getChildren("coord");
101:
102: if (!coordinates.isEmpty() && (coordinates.size() < 4)) {
103: throw new RuntimeException(
104: "LinearRing must have at least 4 coordinates");
105: }
106:
107: if (!coordinates.isEmpty()) {
108: Node cnode = (Node) coordinates.get(0);
109: CoordinateSequence seq = (CoordinateSequence) cnode
110: .getValue();
111: int dimension = GMLUtil.getDimension(seq);
112:
113: CoordinateSequence lineSeq = csFactory.create(coordinates
114: .size(), dimension);
115:
116: for (int i = 0; i < coordinates.size(); i++) {
117: cnode = (Node) coordinates.get(i);
118: seq = (CoordinateSequence) cnode.getValue();
119:
120: for (int j = 0; j < dimension; j++) {
121: lineSeq.setOrdinate(i, j, seq.getOrdinate(0, j));
122: }
123: }
124:
125: return gFactory.createLinearRing(lineSeq);
126: }
127:
128: if (node.getChild("coordinates") != null) {
129: Node cnode = (Node) node.getChild("coordinates");
130: CoordinateSequence lineSeq = (CoordinateSequence) cnode
131: .getValue();
132:
133: return gFactory.createLinearRing(lineSeq);
134: }
135:
136: throw new RuntimeException(
137: "Could not find coordinates to build linestring");
138: }
139:
140: public Object getProperty(Object object, QName name)
141: throws Exception {
142: LinearRing linearRing = (LinearRing) object;
143:
144: if (GML.coordinates.equals(name)) {
145: return linearRing.getCoordinateSequence();
146: }
147:
148: return null;
149: }
150: }
|