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.eclipse.xsd.XSDElementDeclaration;
019: import java.net.URI;
020: import java.util.List;
021: import com.vividsolutions.jts.geom.Coordinate;
022: import com.vividsolutions.jts.geom.CoordinateSequence;
023: import com.vividsolutions.jts.geom.CoordinateSequenceFactory;
024: import com.vividsolutions.jts.geom.GeometryFactory;
025: import com.vividsolutions.jts.geom.LineString;
026: import com.vividsolutions.jts.geom.LinearRing;
027: import com.vividsolutions.jts.geom.Point;
028: import org.opengis.geometry.DirectPosition;
029: import org.opengis.referencing.crs.CoordinateReferenceSystem;
030: import org.geotools.feature.Feature;
031: import org.geotools.feature.FeatureType;
032: import org.geotools.gml2.FeatureTypeCache;
033: import org.geotools.gml2.bindings.GML2ParsingUtils;
034: import org.geotools.referencing.CRS;
035: import org.geotools.xml.BindingWalkerFactory;
036: import org.geotools.xml.ElementInstance;
037: import org.geotools.xml.Node;
038:
039: /**
040: * Utility class for gml3 parsing.
041: *
042: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
043: *
044: */
045: public class GML3ParsingUtils {
046: /**
047: * Utility method to implement Binding.parse for a binding which parses
048: * into A feature.
049: *
050: * @param instance The instance being parsed.
051: * @param node The parse tree.
052: * @param value The value from the last binding in the chain.
053: * @param ftCache The feature type cache.
054: * @param bwFactory Binding walker factory.
055: *
056: * @return A feature.
057: */
058: public static Feature parseFeature(ElementInstance instance,
059: Node node, Object value, FeatureTypeCache ftCache,
060: BindingWalkerFactory bwFactory) throws Exception {
061: return GML2ParsingUtils.parseFeature(instance, node, value,
062: ftCache, bwFactory);
063: }
064:
065: /**
066: * Turns a xml type definition into a geotools feature type.
067: * @param type The xml schema tupe.
068: *
069: * @return The corresponding geotools feature type.
070: */
071: public static FeatureType featureType(
072: XSDElementDeclaration element,
073: BindingWalkerFactory bwFactory) throws Exception {
074: return GML2ParsingUtils.featureType(element, bwFactory);
075: }
076:
077: /**
078: * Turns a parse node + feature type + fid info a feature.
079: */
080: static Feature feature(FeatureType fType, String fid, Node node)
081: throws Exception {
082: return GML2ParsingUtils.feature(fType, fid, node);
083: }
084:
085: static CoordinateReferenceSystem crs(Node node) {
086: return GML2ParsingUtils.crs(node);
087: }
088:
089: static LineString lineString(Node node, GeometryFactory gf,
090: CoordinateSequenceFactory csf) {
091: return line(node, gf, csf, false);
092: }
093:
094: static LinearRing linearRing(Node node, GeometryFactory gf,
095: CoordinateSequenceFactory csf) {
096: return (LinearRing) line(node, gf, csf, true);
097: }
098:
099: static LineString line(Node node, GeometryFactory gf,
100: CoordinateSequenceFactory csf, boolean ring) {
101: if (node.hasChild(DirectPosition.class)) {
102: List dps = node.getChildValues(DirectPosition.class);
103: DirectPosition dp = (DirectPosition) dps.get(0);
104:
105: CoordinateSequence seq = csf.create(dps.size(), dp
106: .getDimension());
107:
108: for (int i = 0; i < dps.size(); i++) {
109: dp = (DirectPosition) dps.get(i);
110:
111: for (int j = 0; j < dp.getDimension(); j++) {
112: seq.setOrdinate(i, j, dp.getOrdinate(j));
113: }
114: }
115:
116: return ring ? gf.createLinearRing(seq) : gf
117: .createLineString(seq);
118: }
119:
120: if (node.hasChild(Point.class)) {
121: List points = node.getChildValues(Point.class);
122: Coordinate[] coordinates = new Coordinate[points.size()];
123:
124: for (int i = 0; i < points.size(); i++) {
125: coordinates[i] = ((Point) points.get(0))
126: .getCoordinate();
127: }
128:
129: return ring ? gf.createLinearRing(coordinates) : gf
130: .createLineString(coordinates);
131: }
132:
133: if (node.hasChild(Coordinate.class)) {
134: List list = node.getChildValues(Coordinate.class);
135: Coordinate[] coordinates = (Coordinate[]) list
136: .toArray(new Coordinate[list.size()]);
137:
138: return ring ? gf.createLinearRing(coordinates) : gf
139: .createLineString(coordinates);
140: }
141:
142: if (node.hasChild(DirectPosition[].class)) {
143: DirectPosition[] dps = (DirectPosition[]) node
144: .getChildValue(DirectPosition[].class);
145:
146: CoordinateSequence seq = null;
147:
148: if (dps.length == 0) {
149: seq = csf.create(0, 0);
150: } else {
151: seq = csf.create(dps.length, dps[0].getDimension());
152:
153: for (int i = 0; i < dps.length; i++) {
154: DirectPosition dp = (DirectPosition) dps[i];
155:
156: for (int j = 0; j < dp.getDimension(); j++) {
157: seq.setOrdinate(i, j, dp.getOrdinate(j));
158: }
159: }
160: }
161:
162: return ring ? gf.createLinearRing(seq) : gf
163: .createLineString(seq);
164: }
165:
166: if (node.hasChild(CoordinateSequence.class)) {
167: CoordinateSequence seq = (CoordinateSequence) node
168: .getChildValue(CoordinateSequence.class);
169:
170: return ring ? gf.createLinearRing(seq) : gf
171: .createLineString(seq);
172: }
173:
174: return null;
175: }
176: }
|