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.Coordinate;
021: import com.vividsolutions.jts.geom.CoordinateSequence;
022: import com.vividsolutions.jts.geom.Envelope;
023: import org.geotools.xml.AbstractComplexBinding;
024: import org.geotools.xml.ElementInstance;
025: import org.geotools.xml.Node;
026:
027: /**
028: * Binding object for the type http://www.opengis.net/gml:BoxType.
029: *
030: * <p>
031: * <pre>
032: * <code>
033: * <complexType name="BoxType">
034: * <annotation>
035: * <documentation> The Box structure defines an extent
036: * using a pair of coordinate tuples. </documentation>
037: * </annotation>
038: * <complexContent>
039: * <extension base="gml:AbstractGeometryType">
040: * <sequence>
041: * <choice>
042: * <element ref="gml:coord" minOccurs="2" maxOccurs="2"/>
043: * <element ref="gml:coordinates"/>
044: * </choice>
045: * </sequence>
046: * </extension>
047: * </complexContent>
048: * </complexType>
049: *
050: * </code>
051: * </pre>
052: * </p>
053: *
054: * @generated
055: */
056: public class GMLBoxTypeBinding extends AbstractComplexBinding {
057: /**
058: * @generated
059: */
060: public QName getTarget() {
061: return GML.BoxType;
062: }
063:
064: /**
065: * <!-- begin-user-doc -->
066: * <!-- end-user-doc -->
067: *
068: * @generated modifiable
069: */
070: public Class getType() {
071: return Envelope.class;
072: }
073:
074: /**
075: * <!-- begin-user-doc -->
076: * This method returns an object of type
077: * @link com.vividsolutions.jts.geom.Envelope.
078: * <!-- end-user-doc -->
079: *
080: * @generated modifiable
081: */
082: public Object parse(ElementInstance instance, Node node,
083: Object value) throws Exception {
084: List coordinates = node.getChildren("coord");
085:
086: if (!coordinates.isEmpty() && (coordinates.size() == 2)) {
087: Node n1 = (Node) coordinates.get(0);
088: Node n2 = (Node) coordinates.get(1);
089: Coordinate c1 = (Coordinate) n1.getValue();
090: Coordinate c2 = (Coordinate) n2.getValue();
091:
092: return new Envelope(c1.x, c2.x, c1.y, c2.y);
093: }
094:
095: if (!coordinates.isEmpty()) {
096: throw new RuntimeException(
097: "Envelope can have only two coordinates");
098: }
099:
100: if (node.getChild("coordinates") != null) {
101: CoordinateSequence cs = (CoordinateSequence) node.getChild(
102: "coordinates").getValue();
103:
104: if (cs.size() != 2) {
105: throw new RuntimeException(
106: "Envelope can have only two coordinates");
107: }
108:
109: return new Envelope(cs.getX(0), cs.getX(1), cs.getY(0), cs
110: .getY(1));
111: }
112:
113: throw new RuntimeException(
114: "Could not find coordinates for envelope");
115: }
116:
117: public Object getProperty(Object object, QName name)
118: throws Exception {
119: Envelope e = (Envelope) object;
120:
121: if (GML.coord.equals(name)) {
122: return new Coordinate[] {
123: new Coordinate(e.getMinX(), e.getMinY()),
124: new Coordinate(e.getMaxX(), e.getMaxY()) };
125: }
126:
127: return null;
128: }
129: }
|