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.net.URI;
021: import javax.xml.namespace.QName;
022: import com.vividsolutions.jts.geom.GeometryFactory;
023: import org.opengis.geometry.DirectPosition;
024: import org.opengis.referencing.crs.CoordinateReferenceSystem;
025: import org.geotools.geometry.DirectPosition1D;
026: import org.geotools.geometry.DirectPosition2D;
027: import org.geotools.referencing.CRS;
028: import org.geotools.xml.*;
029:
030: /**
031: * Binding object for the type http://www.opengis.net/gml:DirectPositionType.
032: *
033: * <p>
034: * <pre>
035: * <code>
036: * <complexType name="DirectPositionType">
037: * <annotation>
038: * <documentation>DirectPosition instances hold the coordinates for a position within some coordinate reference system (CRS). Since
039: * DirectPositions, as data types, will often be included in larger objects (such as geometry elements) that have references to CRS, the
040: * "srsName" attribute will in general be missing, if this particular DirectPosition is included in a larger element with such a reference to a
041: * CRS. In this case, the CRS is implicitly assumed to take on the value of the containing object's CRS.</documentation>
042: * </annotation>
043: * <simpleContent>
044: * <extension base="gml:doubleList">
045: * <attributeGroup ref="gml:SRSReferenceGroup"/>
046: * </extension>
047: * </simpleContent>
048: * </complexType>
049: *
050: * </code>
051: * </pre>
052: * </p>
053: *
054: * @generated
055: */
056: public class DirectPositionTypeBinding extends AbstractComplexBinding {
057: GeometryFactory factory;
058:
059: public DirectPositionTypeBinding(GeometryFactory factory) {
060: this .factory = factory;
061: }
062:
063: /**
064: * @generated
065: */
066: public QName getTarget() {
067: return GML.DirectPositionType;
068: }
069:
070: public int getExecutionMode() {
071: return AFTER;
072: }
073:
074: /**
075: * <!-- begin-user-doc -->
076: * <!-- end-user-doc -->
077: *
078: * @generated modifiable
079: */
080: public Class getType() {
081: return DirectPosition2D.class;
082: }
083:
084: /**
085: * <!-- begin-user-doc -->
086: * <!-- end-user-doc -->
087: *
088: * @generated modifiable
089: */
090: public Object parse(ElementInstance instance, Node node,
091: Object value) throws Exception {
092: CoordinateReferenceSystem crs = GML3ParsingUtils.crs(node);
093:
094: //double[] position = (double[]) value;
095: Double[] position = (Double[]) value;
096: DirectPosition dp = null;
097:
098: if (position.length < 2) {
099: dp = (crs != null) ? new DirectPosition1D(crs)
100: : new DirectPosition1D();
101: dp.setOrdinate(0, position[0].doubleValue());
102: } else {
103: dp = (crs != null) ? new DirectPosition2D(crs)
104: : new DirectPosition2D();
105: dp.setOrdinate(0, position[0].doubleValue());
106: dp.setOrdinate(1, position[1].doubleValue());
107: }
108:
109: return dp;
110: }
111:
112: public Element encode(Object object, Document document,
113: Element value) throws Exception {
114: DirectPosition dp = (DirectPosition) object;
115:
116: double[] coordinates = dp.getCoordinates();
117:
118: StringBuffer sb = new StringBuffer();
119:
120: for (int i = 0; i < coordinates.length; i++) {
121: sb.append(String.valueOf(coordinates[i]));
122:
123: if (i != (coordinates.length - 1)) {
124: sb.append(" ");
125: }
126: }
127:
128: //Element pos = document.createElementNS(GML.pos.getNamespaceURI(), GML.pos.getLocalPart());
129: value.appendChild(document.createTextNode(sb.toString()));
130:
131: return value;
132: }
133: }
|