001: package org.geoserver.wfs;
002:
003: import java.util.StringTokenizer;
004:
005: import org.geoserver.data.test.MockData;
006: import org.geotools.referencing.CRS;
007: import org.opengis.referencing.crs.CoordinateReferenceSystem;
008: import org.opengis.referencing.operation.MathTransform;
009: import org.w3c.dom.Document;
010: import org.w3c.dom.Element;
011:
012: public class ReprojectionTest extends WFSTestSupport {
013: private static final String TARGET_CRS_CODE = "EPSG:900913";
014: MathTransform tx;
015:
016: protected void setUp() throws Exception {
017: super .setUp();
018:
019: CoordinateReferenceSystem epsg4326 = CRS
020: .decode(TARGET_CRS_CODE);
021: CoordinateReferenceSystem epsg32615 = CRS.decode("EPSG:32615");
022:
023: tx = CRS.findMathTransform(epsg32615, epsg4326);
024: }
025:
026: public void testGetFeatureGet() throws Exception {
027:
028: Document dom1 = getAsDOM("wfs?request=getfeature&service=wfs&version=1.0.0&typename="
029: + MockData.POLYGONS.getLocalPart());
030: Document dom2 = getAsDOM("wfs?request=getfeature&service=wfs&version=1.0.0&typename="
031: + MockData.POLYGONS.getLocalPart()
032: + "&srsName="
033: + TARGET_CRS_CODE);
034:
035: // print(dom1);
036: // print(dom2);
037:
038: runTest(dom1, dom2);
039: }
040:
041: public void testGetFeaturePost() throws Exception {
042: String xml = "<wfs:GetFeature "
043: + "service=\"WFS\" "
044: + "version=\"1.0.0\" "
045: + "xmlns:cdf=\"http://www.opengis.net/cite/data\" "
046: + "xmlns:ogc=\"http://www.opengis.net/ogc\" "
047: + "xmlns:wfs=\"http://www.opengis.net/wfs\" "
048: + "> "
049: + "<wfs:Query typeName=\""
050: + MockData.POLYGONS.getPrefix()
051: + ":"
052: + MockData.POLYGONS.getLocalPart()
053: + "\"> "
054: + "<wfs:PropertyName>cgf:polygonProperty</wfs:PropertyName> "
055: + "</wfs:Query> " + "</wfs:GetFeature>";
056:
057: Document dom1 = postAsDOM("wfs", xml);
058:
059: xml = "<wfs:GetFeature "
060: + "service=\"WFS\" "
061: + "version=\"1.0.0\" "
062: + "xmlns:cdf=\"http://www.opengis.net/cite/data\" "
063: + "xmlns:ogc=\"http://www.opengis.net/ogc\" "
064: + "xmlns:wfs=\"http://www.opengis.net/wfs\" "
065: + "> "
066: + "<wfs:Query srsName=\""
067: + TARGET_CRS_CODE
068: + "\" typeName=\""
069: + MockData.POLYGONS.getPrefix()
070: + ":"
071: + MockData.POLYGONS.getLocalPart()
072: + "\"> "
073: + "<wfs:PropertyName>cgf:polygonProperty</wfs:PropertyName> "
074: + "</wfs:Query> " + "</wfs:GetFeature>";
075: Document dom2 = postAsDOM("wfs", xml);
076:
077: runTest(dom1, dom2);
078: }
079:
080: public void testGetFeatureWithProjectedBoxGet() throws Exception {
081: String q = "wfs?request=getfeature&service=wfs&version=1.0&typeName="
082: + MockData.POLYGONS.getLocalPart();
083: Document dom = getAsDOM(q);
084:
085: Element envelope = getFirstElementByTagName(dom, "gml:Box");
086: String coordinates = getFirstElementByTagName(envelope,
087: "gml:coordinates").getFirstChild().getNodeValue();
088: String lc = coordinates.split(" ")[0];
089: String uc = coordinates.split(" ")[1];
090: double[] c = new double[] {
091: Double.parseDouble(lc.split(",")[0]),
092: Double.parseDouble(lc.split(",")[1]),
093: Double.parseDouble(uc.split(",")[0]),
094: Double.parseDouble(uc.split(",")[1]) };
095: double[] cr = new double[4];
096: tx.transform(c, 0, cr, 0, 2);
097:
098: q += "&bbox=" + cr[0] + "," + cr[1] + "," + cr[2] + "," + cr[3]
099: + "," + TARGET_CRS_CODE;
100: dom = getAsDOM(q);
101:
102: assertEquals(1, dom.getElementsByTagName(
103: MockData.POLYGONS.getPrefix() + ":"
104: + MockData.POLYGONS.getLocalPart()).getLength());
105: }
106:
107: public void testGetFeatureWithProjectedBoxPost() throws Exception {
108: String q = "wfs?request=getfeature&service=wfs&version=1.0&typeName="
109: + MockData.POLYGONS.getLocalPart();
110: Document dom = getAsDOM(q);
111: Element envelope = getFirstElementByTagName(dom, "gml:Box");
112: String coordinates = getFirstElementByTagName(envelope,
113: "gml:coordinates").getFirstChild().getNodeValue();
114: String lc = coordinates.split(" ")[0];
115: String uc = coordinates.split(" ")[1];
116: double[] c = new double[] {
117: Double.parseDouble(lc.split(",")[0]),
118: Double.parseDouble(lc.split(",")[1]),
119: Double.parseDouble(uc.split(",")[0]),
120: Double.parseDouble(uc.split(",")[1]) };
121: double[] cr = new double[4];
122: tx.transform(c, 0, cr, 0, 2);
123:
124: String xml = "<wfs:GetFeature service=\"WFS\" version=\"1.0.0\""
125: + " xmlns:"
126: + MockData.POLYGONS.getPrefix()
127: + "=\""
128: + MockData.POLYGONS.getNamespaceURI()
129: + "\""
130: + " xmlns:ogc=\"http://www.opengis.net/ogc\" "
131: + " xmlns:gml=\"http://www.opengis.net/gml\" "
132: + " xmlns:wfs=\"http://www.opengis.net/wfs\" "
133: + "> "
134: + "<wfs:Query typeName=\""
135: + MockData.POLYGONS.getPrefix()
136: + ":"
137: + MockData.POLYGONS.getLocalPart()
138: + "\">"
139: + "<wfs:PropertyName>cgf:polygonProperty</wfs:PropertyName> "
140: + "<ogc:Filter>"
141: + "<ogc:BBOX>"
142: + "<ogc:PropertyName>polygonProperty</ogc:PropertyName>"
143: + "<gml:Box srsName=\""
144: + TARGET_CRS_CODE
145: + "\">"
146: + "<gml:coord>"
147: + "<gml:X>"
148: + cr[0]
149: + "</gml:X>"
150: + "<gml:Y>"
151: + cr[1]
152: + "</gml:Y>"
153: + "</gml:coord>"
154: + "<gml:coord>"
155: + "<gml:X>"
156: + cr[2]
157: + "</gml:X>"
158: + "<gml:Y>"
159: + cr[3]
160: + "</gml:Y>"
161: + "</gml:coord>"
162: + "</gml:Box>"
163: + "</ogc:BBOX>"
164: + "</ogc:Filter>"
165: + "</wfs:Query> " + "</wfs:GetFeature>";
166:
167: dom = postAsDOM("wfs", xml);
168:
169: assertEquals(1, dom.getElementsByTagName(
170: MockData.POLYGONS.getPrefix() + ":"
171: + MockData.POLYGONS.getLocalPart()).getLength());
172: }
173:
174: public void testInsertSrsName() throws Exception {
175: String q = "wfs?request=getfeature&service=wfs&version=1.0.0&typeName="
176: + MockData.POLYGONS.getLocalPart();
177: Document dom = getAsDOM(q);
178:
179: Element polygonProperty = getFirstElementByTagName(dom,
180: "cgf:polygonProperty");
181: Element posList = getFirstElementByTagName(polygonProperty,
182: "gml:coordinates");
183:
184: double[] c = coordinates(posList.getFirstChild().getNodeValue());
185: double[] cr = new double[c.length];
186: tx.transform(c, 0, cr, 0, cr.length / 2);
187:
188: String xml = "<wfs:Transaction service=\"WFS\" version=\"1.0.0\" "
189: + " xmlns:wfs=\"http://www.opengis.net/wfs\" "
190: + " xmlns:gml=\"http://www.opengis.net/gml\" "
191: + " xmlns:cgf=\""
192: + MockData.CGF_URI
193: + "\">"
194: + "<wfs:Insert handle=\"insert-1\" srsName=\""
195: + TARGET_CRS_CODE
196: + "\">"
197: + " <cgf:Polygons>"
198: + "<cgf:polygonProperty>"
199: + "<gml:Polygon >"
200: + "<gml:outerBoundaryIs>"
201: + "<gml:LinearRing>"
202: + "<gml:coordinates>";
203: for (int i = 0; i < cr.length;) {
204: xml += cr[i++] + "," + cr[i++];
205: if (i < cr.length - 1) {
206: xml += " ";
207: }
208: }
209: xml += "</gml:coordinates>" + "</gml:LinearRing>"
210: + "</gml:outerBoundaryIs>" + "</gml:Polygon>"
211: + "</cgf:polygonProperty>" + " </cgf:Polygons>"
212: + "</wfs:Insert>" + "</wfs:Transaction>";
213: postAsDOM("wfs", xml);
214:
215: dom = getAsDOM(q);
216:
217: assertEquals(2, dom.getElementsByTagName(
218: MockData.POLYGONS.getPrefix() + ":"
219: + MockData.POLYGONS.getLocalPart()).getLength());
220:
221: }
222:
223: public void testInsertGeomSrsName() throws Exception {
224: String q = "wfs?request=getfeature&service=wfs&version=1.0&typeName="
225: + MockData.POLYGONS.getLocalPart();
226: Document dom = getAsDOM(q);
227:
228: Element polygonProperty = getFirstElementByTagName(dom,
229: "cgf:polygonProperty");
230: Element posList = getFirstElementByTagName(polygonProperty,
231: "gml:coordinates");
232:
233: double[] c = coordinates(posList.getFirstChild().getNodeValue());
234: double[] cr = new double[c.length];
235: tx.transform(c, 0, cr, 0, cr.length / 2);
236:
237: String xml = "<wfs:Transaction service=\"WFS\" version=\"1.0.0\" "
238: + " xmlns:wfs=\"http://www.opengis.net/wfs\" "
239: + " xmlns:gml=\"http://www.opengis.net/gml\" "
240: + " xmlns:cgf=\""
241: + MockData.CGF_URI
242: + "\">"
243: + "<wfs:Insert handle=\"insert-1\">"
244: + " <cgf:Polygons>"
245: + "<cgf:polygonProperty>"
246: + "<gml:Polygon srsName=\""
247: + TARGET_CRS_CODE
248: + "\">"
249: + "<gml:outerBoundaryIs>"
250: + "<gml:LinearRing>"
251: + "<gml:coordinates>";
252: for (int i = 0; i < cr.length;) {
253: xml += cr[i++] + "," + cr[i++];
254: if (i < cr.length - 1) {
255: xml += " ";
256: }
257: }
258: xml += "</gml:coordinates>" + "</gml:LinearRing>"
259: + "</gml:outerBoundaryIs>" + "</gml:Polygon>"
260: + "</cgf:polygonProperty>" + " </cgf:Polygons>"
261: + "</wfs:Insert>" + "</wfs:Transaction>";
262: postAsDOM("wfs", xml);
263:
264: dom = getAsDOM(q);
265:
266: assertEquals(2, dom.getElementsByTagName(
267: MockData.POLYGONS.getPrefix() + ":"
268: + MockData.POLYGONS.getLocalPart()).getLength());
269:
270: }
271:
272: public void testUpdate() throws Exception {
273: String q = "wfs?request=getfeature&service=wfs&version=1.0&typeName="
274: + MockData.POLYGONS.getLocalPart();
275:
276: Document dom = getAsDOM(q);
277:
278: Element polygonProperty = getFirstElementByTagName(dom,
279: "cgf:polygonProperty");
280: Element posList = getFirstElementByTagName(polygonProperty,
281: "gml:coordinates");
282:
283: double[] c = coordinates(posList.getFirstChild().getNodeValue());
284: double[] cr = new double[c.length];
285: tx.transform(c, 0, cr, 0, cr.length / 2);
286:
287: // perform an update
288: String xml = "<wfs:Transaction service=\"WFS\" version=\"1.0.0\" "
289: + "xmlns:cgf=\"http://www.opengis.net/cite/geometry\" "
290: + "xmlns:ogc=\"http://www.opengis.net/ogc\" "
291: + "xmlns:wfs=\"http://www.opengis.net/wfs\" "
292: + "xmlns:gml=\"http://www.opengis.net/gml\"> "
293: + "<wfs:Update typeName=\"cgf:Polygons\" > "
294: + "<wfs:Property>"
295: + "<wfs:Name>polygonProperty</wfs:Name>"
296: + "<wfs:Value>"
297: + "<gml:Polygon srsName=\""
298: + TARGET_CRS_CODE
299: + "\">"
300: + "<gml:outerBoundaryIs>"
301: + "<gml:LinearRing>" + "<gml:coordinates>";
302: for (int i = 0; i < cr.length;) {
303: xml += cr[i++] + "," + cr[i++];
304: if (i < cr.length - 1) {
305: xml += " ";
306: }
307: }
308: xml += "</gml:coordinates>" + "</gml:LinearRing>"
309: + "</gml:outerBoundaryIs>" + "</gml:Polygon>"
310: + "</wfs:Value>" + "</wfs:Property>" + "<ogc:Filter>"
311: + "<ogc:PropertyIsEqualTo>"
312: + "<ogc:PropertyName>id</ogc:PropertyName>"
313: + "<ogc:Literal>t0002</ogc:Literal>"
314: + "</ogc:PropertyIsEqualTo>" + "</ogc:Filter>"
315: + "</wfs:Update>" + "</wfs:Transaction>";
316:
317: dom = postAsDOM("wfs", xml);
318:
319: assertEquals("wfs:WFS_TransactionResponse", dom
320: .getDocumentElement().getNodeName());
321: Element success = getFirstElementByTagName(dom, "wfs:SUCCESS");
322: assertNotNull(success);
323:
324: dom = getAsDOM(q);
325:
326: polygonProperty = getFirstElementByTagName(dom,
327: "cgf:polygonProperty");
328: posList = getFirstElementByTagName(polygonProperty,
329: "gml:coordinates");
330: double[] c1 = coordinates(posList.getFirstChild()
331: .getNodeValue());
332:
333: assertEquals(c.length, c1.length);
334: for (int i = 0; i < c.length; i++) {
335: int x = (int) (c[i] + 0.5);
336: int y = (int) (c1[i] + 0.5);
337:
338: assertEquals(x, y);
339: }
340:
341: }
342:
343: public void runTest(Document dom1, Document dom2) throws Exception {
344: Element box = getFirstElementByTagName(dom1
345: .getDocumentElement(), "gml:Box");
346: Element coordinates = getFirstElementByTagName(box,
347: "gml:coordinates");
348: double[] d1 = coordinates(coordinates.getFirstChild()
349: .getNodeValue());
350:
351: box = getFirstElementByTagName(dom2.getDocumentElement(),
352: "gml:Box");
353: coordinates = getFirstElementByTagName(box, "gml:coordinates");
354: double[] d2 = coordinates(coordinates.getFirstChild()
355: .getNodeValue());
356:
357: double[] d3 = new double[d1.length];
358: tx.transform(d1, 0, d3, 0, d1.length / 2);
359:
360: for (int i = 0; i < d2.length; i++) {
361: assertEquals(d2[i], d3[i], 0.001);
362: }
363: }
364:
365: double[] coordinates(String string) {
366: StringTokenizer st = new StringTokenizer(string, " ");
367: double[] coordinates = new double[st.countTokens() * 2];
368: int i = 0;
369: while (st.hasMoreTokens()) {
370: String tuple = st.nextToken();
371: coordinates[i++] = Double.parseDouble(tuple.split(",")[0]);
372: coordinates[i++] = Double.parseDouble(tuple.split(",")[1]);
373: }
374:
375: return coordinates;
376: }
377:
378: double[] posList(String string) {
379: StringTokenizer st = new StringTokenizer(string, " ");
380: double[] coordinates = new double[st.countTokens()];
381: int i = 0;
382: while (st.hasMoreTokens()) {
383: coordinates[i++] = Double.parseDouble(st.nextToken());
384: }
385:
386: return coordinates;
387: }
388: }
|