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 org.w3c.dom.Node;
021: import java.net.URI;
022: import java.util.Date;
023: import javax.xml.namespace.QName;
024: import com.vividsolutions.jts.geom.Coordinate;
025: import com.vividsolutions.jts.geom.Envelope;
026: import com.vividsolutions.jts.geom.GeometryFactory;
027: import com.vividsolutions.jts.geom.LineString;
028: import com.vividsolutions.jts.geom.LinearRing;
029: import com.vividsolutions.jts.geom.MultiLineString;
030: import com.vividsolutions.jts.geom.MultiPoint;
031: import com.vividsolutions.jts.geom.MultiPolygon;
032: import com.vividsolutions.jts.geom.Point;
033: import com.vividsolutions.jts.geom.Polygon;
034: import org.opengis.referencing.crs.CoordinateReferenceSystem;
035: import org.geotools.feature.AttributeTypeFactory;
036: import org.geotools.feature.DefaultFeatureTypeFactory;
037: import org.geotools.feature.Feature;
038: import org.geotools.feature.FeatureType;
039: import org.geotools.feature.FeatureTypeBuilder;
040: import org.geotools.geometry.jts.ReferencedEnvelope;
041: import org.geotools.referencing.CRS;
042:
043: /**
044: * Utility class for creating test xml data for gml3 bindings.
045: *
046: * @author Justin Deoliveira, The Open Planning Project
047: *
048: */
049: public class GML3MockData {
050: static GeometryFactory gf = new GeometryFactory();
051:
052: static Element point(Document document, Node parent) {
053: Element point = element(GML.Point, document, parent);
054: point.setAttribute("srsName",
055: "urn:x-ogc:def:crs:EPSG:6.11.2:4326");
056:
057: Element pos = element(GML.pos, document, point);
058: pos.appendChild(document.createTextNode("1.0 2.0 "));
059:
060: return point;
061: }
062:
063: static CoordinateReferenceSystem crs() {
064: try {
065: return CRS.decode("urn:x-ogc:def:crs:EPSG:6.11.2:4326");
066: } catch (Exception e) {
067: throw new RuntimeException(e);
068: }
069: }
070:
071: static Envelope bounds() {
072: return new ReferencedEnvelope(0, 10, 0, 10, crs());
073: }
074:
075: static Point point() {
076: Point p = gf.createPoint(new Coordinate(1, 2));
077: p.setUserData(crs());
078:
079: return p;
080: }
081:
082: static LineString lineString() {
083: return gf.createLineString(new Coordinate[] {
084: new Coordinate(1, 2), new Coordinate(3, 4) });
085: }
086:
087: static Element lineString(Document document, Node parent) {
088: return lineStringWithPos(document, parent);
089: }
090:
091: static Element lineStringWithPos(Document document, Node parent) {
092: Element lineString = element(GML.LineString, document, parent);
093:
094: Element pos = element(GML.pos, document, lineString);
095: pos.appendChild(document.createTextNode("1.0 2.0"));
096:
097: pos = element(GML.pos, document, lineString);
098: pos.appendChild(document.createTextNode("3.0 4.0"));
099:
100: return lineString;
101: }
102:
103: static Element lineStringWithPosList(Document document, Node parent) {
104: Element lineString = element(GML.LineString, document, parent);
105: Element posList = element(GML.posList, document, lineString);
106: posList.appendChild(document.createTextNode("1.0 2.0 3.0 4.0"));
107:
108: return lineString;
109: }
110:
111: static LinearRing linearRing() {
112: return gf.createLinearRing(new Coordinate[] {
113: new Coordinate(1, 1), new Coordinate(2, 2),
114: new Coordinate(3, 3), new Coordinate(1, 1) });
115: }
116:
117: static Element linearRing(Document document, Node parent) {
118: return linearRingWithPos(document, parent);
119: }
120:
121: static Element linearRingWithPos(Document document, Node parent) {
122: Element linearRing = element(GML.LinearRing, document, parent);
123:
124: Element pos = element(GML.pos, document, linearRing);
125: pos.appendChild(document.createTextNode("1.0 2.0"));
126:
127: pos = element(GML.pos, document, linearRing);
128: pos.appendChild(document.createTextNode("3.0 4.0"));
129:
130: pos = element(GML.pos, document, linearRing);
131: pos.appendChild(document.createTextNode("5.0 6.0"));
132:
133: pos = element(GML.pos, document, linearRing);
134: pos.appendChild(document.createTextNode("1.0 2.0"));
135:
136: return linearRing;
137: }
138:
139: static Element linearRingWithPosList(Document document, Node parent) {
140: Element linearRing = element(GML.LinearRing, document, parent);
141:
142: Element posList = element(GML.posList, document, linearRing);
143:
144: linearRing.appendChild(posList);
145: posList.appendChild(document
146: .createTextNode("1.0 2.0 3.0 4.0 5.0 6.0 1.0 2.0"));
147:
148: return linearRing;
149: }
150:
151: static Polygon polygon() {
152: return gf.createPolygon(linearRing(), null);
153: }
154:
155: static Element polygon(Document document, Node parent) {
156: Element polygon = element(GML.Polygon, document, parent);
157:
158: Element exterior = element(GML.exterior, document, polygon);
159: linearRing(document, exterior);
160:
161: return polygon;
162: }
163:
164: static MultiPoint multiPoint() {
165: return gf.createMultiPoint(new Coordinate[] {
166: new Coordinate(1, 1), new Coordinate(2, 2) });
167: }
168:
169: static Element multiPoint(Document document, Node parent) {
170: Element multiPoint = element(GML.MultiPoint, document, parent);
171:
172: // 2 pointMember elements
173: Element pointMember = element(GML.pointMember, document,
174: multiPoint);
175: point(document, pointMember);
176:
177: pointMember = element(GML.pointMember, document, multiPoint);
178: point(document, pointMember);
179:
180: //1 pointMembers elmenet with 2 members
181: Element pointMembers = element(GML.pointMembers, document,
182: multiPoint);
183: point(document, pointMembers);
184: point(document, pointMembers);
185:
186: return multiPoint;
187: }
188:
189: static MultiLineString multiLineString() {
190: return gf.createMultiLineString(new LineString[] {
191: lineString(), lineString() });
192: }
193:
194: static Element multiLineString(Document document, Node parent) {
195: Element multiLineString = element(GML.MultiLineString,
196: document, parent);
197:
198: Element lineStringMember = element(GML.lineStringMember,
199: document, multiLineString);
200: lineString(document, lineStringMember);
201:
202: lineStringMember = element(GML.lineStringMember, document,
203: multiLineString);
204: lineString(document, lineStringMember);
205:
206: return multiLineString;
207: }
208:
209: static MultiPolygon multiPolygon() {
210: return gf.createMultiPolygon(new Polygon[] { polygon(),
211: polygon() });
212: }
213:
214: static Element multiPolygon(Document document, Node parent) {
215: Element multiPolygon = element(GML.MultiPolygon, document,
216: parent);
217:
218: Element polygonMember = element(GML.polygonMember, document,
219: multiPolygon);
220: polygon(document, polygonMember);
221:
222: polygonMember = element(GML.polygonMember, document,
223: multiPolygon);
224: polygon(document, polygonMember);
225:
226: return multiPolygon;
227: }
228:
229: static Element feature(Document document, Node parent) {
230: Element feature = element(TEST.TestFeature, document, parent);
231: Element geom = element(new QName(TEST.NAMESPACE, "geom"),
232: document, feature);
233: point(document, geom);
234:
235: Element count = GML3MockData.element(new QName(TEST.NAMESPACE,
236: "count"), document, feature);
237: count.appendChild(document.createTextNode("1"));
238:
239: return feature;
240: }
241:
242: static Feature feature() throws Exception {
243: FeatureTypeBuilder typeBuilder = new DefaultFeatureTypeFactory();
244: typeBuilder.setName(TEST.TestFeature.getLocalPart());
245: typeBuilder.setNamespace(new URI(TEST.TestFeature
246: .getNamespaceURI()));
247:
248: typeBuilder.addType(AttributeTypeFactory.newAttributeType(
249: "name", String.class));
250: typeBuilder.addType(AttributeTypeFactory.newAttributeType(
251: "description", String.class));
252: typeBuilder.addType(AttributeTypeFactory.newAttributeType(
253: "geom", Point.class));
254: typeBuilder.addType(AttributeTypeFactory.newAttributeType(
255: "count", Integer.class));
256: typeBuilder.addType(AttributeTypeFactory.newAttributeType(
257: "date", Date.class));
258:
259: FeatureType type = typeBuilder.getFeatureType();
260:
261: return type.create(new Object[] { "theName", "theDescription",
262: point(), new Integer(1), new Date() }, "fid.1");
263: }
264:
265: static Element featureMember(Document document, Node parent) {
266: Element featureMember = element(GML.featureMember, document,
267: parent);
268: feature(document, featureMember);
269:
270: return featureMember;
271: }
272:
273: static Element element(QName name, Document document, Node parent) {
274: Element element = document.createElementNS(name
275: .getNamespaceURI(), name.getLocalPart());
276:
277: if (parent != null) {
278: parent.appendChild(element);
279: }
280:
281: return element;
282: }
283: }
|