01: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
02: * This code is licensed under the GPL 2.0 license, availible at the root
03: * application directory.
04: */
05: package org.geoserver.wfs.kvp;
06:
07: import com.vividsolutions.jts.geom.Envelope;
08: import org.geoserver.ows.KvpParser;
09: import org.geoserver.ows.util.KvpUtils;
10: import org.geotools.geometry.jts.ReferencedEnvelope;
11: import org.geotools.referencing.CRS;
12: import org.opengis.referencing.crs.CoordinateReferenceSystem;
13: import org.vfny.geoserver.ServiceException;
14:
15: import java.util.List;
16:
17: public class BBoxKvpParser extends KvpParser {
18: public BBoxKvpParser() {
19: super ("bbox", Envelope.class);
20: }
21:
22: public Object parse(String value) throws Exception {
23: List unparsed = KvpUtils.readFlat(value,
24: KvpUtils.INNER_DELIMETER);
25:
26: // check to make sure that the bounding box has 4 coordinates
27: if (unparsed.size() < 4) {
28: throw new IllegalArgumentException(
29: "Requested bounding box contains wrong"
30: + "number of coordinates (should have "
31: + "4): " + unparsed.size());
32: }
33:
34: //if it does, store them in an array of doubles
35: double[] bbox = new double[4];
36:
37: for (int i = 0; i < 4; i++) {
38: try {
39: bbox[i] = Double.parseDouble((String) unparsed.get(i));
40: } catch (NumberFormatException e) {
41: throw new IllegalArgumentException(
42: "Bounding box coordinate " + i
43: + " is not parsable:" + unparsed.get(i));
44: }
45: }
46:
47: //ensure the values are sane
48: double minx = bbox[0];
49: double miny = bbox[1];
50: double maxx = bbox[2];
51: double maxy = bbox[3];
52:
53: if (minx > maxx) {
54: throw new ServiceException("illegal bbox, minX: " + minx
55: + " is " + "greater than maxX: " + maxx);
56: }
57:
58: if (miny > maxy) {
59: throw new ServiceException("illegal bbox, minY: " + miny
60: + " is " + "greater than maxY: " + maxy);
61: }
62:
63: //check for crs
64: CoordinateReferenceSystem crs = null;
65:
66: if (unparsed.size() > 4) {
67: crs = CRS.decode((String) unparsed.get(4));
68: } else {
69: //TODO: use the default crs of the system
70: }
71:
72: return new ReferencedEnvelope(minx, maxx, miny, maxy, crs);
73: }
74: }
|