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.gml3.bindings;
017:
018: import org.w3c.dom.Document;
019: import org.w3c.dom.Element;
020: import java.util.List;
021: import javax.xml.namespace.QName;
022: import com.vividsolutions.jts.geom.Coordinate;
023: import com.vividsolutions.jts.geom.CoordinateSequence;
024: import com.vividsolutions.jts.geom.Envelope;
025: import org.opengis.geometry.DirectPosition;
026: import org.opengis.referencing.crs.CoordinateReferenceSystem;
027: import org.geotools.geometry.DirectPosition2D;
028: import org.geotools.geometry.jts.ReferencedEnvelope;
029: import org.geotools.xml.AbstractComplexBinding;
030: import org.geotools.xml.ElementInstance;
031: import org.geotools.xml.Node;
032:
033: /**
034: * Binding object for the type http://www.opengis.net/gml:EnvelopeType.
035: *
036: * <p>
037: * <pre>
038: * <code>
039: * <complexType name="EnvelopeType">
040: * <annotation>
041: * <documentation>Envelope defines an extent using a pair of positions defining opposite corners in arbitrary dimensions. The first direct
042: * position is the "lower corner" (a coordinate position consisting of all the minimal ordinates for each dimension for all points within the envelope),
043: * the second one the "upper corner" (a coordinate position consisting of all the maximal ordinates for each dimension for all points within the
044: * envelope).</documentation>
045: * </annotation>
046: * <choice>
047: * <sequence>
048: * <element name="lowerCorner" type="gml:DirectPositionType"/>
049: * <element name="upperCorner" type="gml:DirectPositionType"/>
050: * </sequence>
051: * <element maxOccurs="2" minOccurs="2" ref="gml:coord">
052: * <annotation>
053: * <appinfo>deprecated</appinfo>
054: * <documentation>deprecated with GML version 3.0</documentation>
055: * </annotation>
056: * </element>
057: * <element maxOccurs="2" minOccurs="2" ref="gml:pos">
058: * <annotation>
059: * <appinfo>deprecated</appinfo>
060: * <documentation>Deprecated with GML version 3.1. Use the explicit properties "lowerCorner" and "upperCorner" instead.</documentation>
061: * </annotation>
062: * </element>
063: * <element ref="gml:coordinates">
064: * <annotation>
065: * <documentation>Deprecated with GML version 3.1.0. Use the explicit properties "lowerCorner" and "upperCorner" instead.</documentation>
066: * </annotation>
067: * </element>
068: * </choice>
069: * <attributeGroup ref="gml:SRSReferenceGroup"/>
070: * </complexType>
071: *
072: * </code>
073: * </pre>
074: * </p>
075: *
076: * @generated
077: */
078: public class EnvelopeTypeBinding extends AbstractComplexBinding {
079: /**
080: * @generated
081: */
082: public QName getTarget() {
083: return GML.EnvelopeType;
084: }
085:
086: /**
087: * <!-- begin-user-doc -->
088: * <!-- end-user-doc -->
089: *
090: * @generated modifiable
091: */
092: public Class getType() {
093: return Envelope.class;
094: }
095:
096: /**
097: * <!-- begin-user-doc -->
098: * <!-- end-user-doc -->
099: *
100: * @generated modifiable
101: */
102: public Object parse(ElementInstance instance, Node node,
103: Object value) throws Exception {
104: CoordinateReferenceSystem crs = GML3ParsingUtils.crs(node);
105:
106: if (node.getChild("lowerCorner") != null) {
107: DirectPosition l = (DirectPosition) node
108: .getChildValue("lowerCorner");
109: DirectPosition u = (DirectPosition) node
110: .getChildValue("upperCorner");
111:
112: return new ReferencedEnvelope(l.getOrdinate(0), u
113: .getOrdinate(0), l.getOrdinate(1),
114: u.getOrdinate(1), crs);
115: }
116:
117: if (node.hasChild(Coordinate.class)) {
118: List c = node.getChildValues(Coordinate.class);
119: Coordinate c1 = (Coordinate) c.get(0);
120: Coordinate c2 = (Coordinate) c.get(1);
121:
122: return new ReferencedEnvelope(c1.x, c2.x, c1.y, c2.y, crs);
123: }
124:
125: if (node.hasChild(DirectPosition.class)) {
126: List dp = node.getChildValues(DirectPosition.class);
127: DirectPosition dp1 = (DirectPosition) dp.get(0);
128: DirectPosition dp2 = (DirectPosition) dp.get(1);
129:
130: return new ReferencedEnvelope(dp1.getOrdinate(0), dp2
131: .getOrdinate(0), dp1.getOrdinate(1), dp2
132: .getOrdinate(1), crs);
133: }
134:
135: if (node.hasChild(CoordinateSequence.class)) {
136: CoordinateSequence seq = (CoordinateSequence) node
137: .getChildValue(CoordinateSequence.class);
138:
139: return new ReferencedEnvelope(seq.getX(0), seq.getX(1), seq
140: .getY(0), seq.getY(1), crs);
141: }
142:
143: return null;
144: }
145:
146: public Element encode(Object object, Document document,
147: Element value) throws Exception {
148: Envelope envelope = (Envelope) object;
149:
150: if (envelope.isNull()) {
151: value.appendChild(document.createElementNS(GML.NAMESPACE,
152: GML.Null.getLocalPart()));
153: }
154:
155: return null;
156: }
157:
158: public Object getProperty(Object object, QName name) {
159: Envelope envelope = (Envelope) object;
160:
161: if (envelope.isNull()) {
162: return null;
163: }
164:
165: if (name.getLocalPart().equals("lowerCorner")) {
166: return new DirectPosition2D(envelope.getMinX(), envelope
167: .getMinY());
168: }
169:
170: if (name.getLocalPart().equals("upperCorner")) {
171: return new DirectPosition2D(envelope.getMaxX(), envelope
172: .getMaxY());
173: }
174:
175: if (name.getLocalPart().equals("srsName")
176: && envelope instanceof ReferencedEnvelope) {
177: return GML3EncodingUtils
178: .crs(((ReferencedEnvelope) envelope)
179: .getCoordinateReferenceSystem());
180: }
181:
182: return null;
183: }
184: }
|