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.math.BigInteger;
021: import javax.xml.namespace.QName;
022: import org.opengis.geometry.DirectPosition;
023: import org.opengis.referencing.crs.CoordinateReferenceSystem;
024: import org.geotools.geometry.DirectPosition1D;
025: import org.geotools.geometry.DirectPosition2D;
026: import org.geotools.xml.*;
027:
028: /**
029: * Binding object for the type http://www.opengis.net/gml:DirectPositionListType.
030: *
031: * <p>
032: * <pre>
033: * <code>
034: * <complexType name="DirectPositionListType">
035: * <annotation>
036: * <documentation>DirectPositionList instances hold the coordinates for a sequence of direct positions within the same coordinate
037: * reference system (CRS).</documentation>
038: * </annotation>
039: * <simpleContent>
040: * <extension base="gml:doubleList">
041: * <attributeGroup ref="gml:SRSReferenceGroup"/>
042: * <attribute name="count" type="positiveInteger" use="optional">
043: * <annotation>
044: * <documentation>"count" allows to specify the number of direct positions in the list. If the attribute count is present then
045: * the attribute srsDimension shall be present, too.</documentation>
046: * </annotation>
047: * </attribute>
048: * </extension>
049: * </simpleContent>
050: * </complexType>
051: *
052: * </code>
053: * </pre>
054: * </p>
055: *
056: * @generated
057: */
058: public class DirectPositionListTypeBinding extends
059: AbstractComplexBinding {
060: /**
061: * @generated
062: */
063: public QName getTarget() {
064: return GML.DirectPositionListType;
065: }
066:
067: public int getExecutionMode() {
068: return AFTER;
069: }
070:
071: /**
072: * <!-- begin-user-doc -->
073: * <!-- end-user-doc -->
074: *
075: * @generated modifiable
076: */
077: public Class getType() {
078: return DirectPosition[].class;
079: }
080:
081: /**
082: * <!-- begin-user-doc -->
083: * <!-- end-user-doc -->
084: *
085: * @generated modifiable
086: */
087: public Object parse(ElementInstance instance, Node node,
088: Object value) throws Exception {
089: CoordinateReferenceSystem crs = GML3ParsingUtils.crs(node);
090:
091: //double[] values = (double[]) value;
092: Double[] values = (Double[]) value;
093: BigInteger count = (BigInteger) node.getAttributeValue("count");
094:
095: if (count == null) {
096: //assume 2 dimensional
097: count = BigInteger.valueOf(values.length / 2);
098: }
099:
100: if (count.intValue() == 0) {
101: return new DirectPosition[] {};
102: }
103:
104: int dim = values.length / count.intValue();
105:
106: if ((dim < 1) || (dim > 2)) {
107: throw new IllegalArgumentException(
108: "dimension must be 1 or 2");
109: }
110:
111: DirectPosition[] dps = new DirectPosition[count.intValue()];
112:
113: if (dim == 1) {
114: for (int i = 0; i < count.intValue(); i++) {
115: dps[i] = new DirectPosition1D(crs);
116: dps[i].setOrdinate(0, values[i].doubleValue());
117: }
118: } else {
119: int j = 0;
120:
121: for (int i = 0; i < count.intValue(); i++) {
122: dps[i] = new DirectPosition2D(crs);
123: dps[i].setOrdinate(0, values[j++].doubleValue());
124: dps[i].setOrdinate(1, values[j++].doubleValue());
125: }
126: }
127:
128: return dps;
129: }
130:
131: public Element encode(Object object, Document document,
132: Element value) throws Exception {
133: //TODO: remove this when the parser can do lists
134: DirectPosition[] dps = (DirectPosition[]) object;
135: StringBuffer sb = new StringBuffer();
136:
137: for (int i = 0; i < dps.length; i++) {
138: sb.append(dps[i].getOrdinate(0) + " "
139: + dps[i].getOrdinate(1));
140:
141: if (i < (dps.length - 1)) {
142: sb.append(" ");
143: }
144: }
145:
146: value.appendChild(document.createTextNode(sb.toString()));
147:
148: return value;
149: }
150: }
|