001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.vfny.geoserver.wcs.requests.readers;
006:
007: import com.vividsolutions.jts.geom.Envelope;
008: import org.vfny.geoserver.Request;
009: import org.vfny.geoserver.ServiceException;
010: import org.vfny.geoserver.util.requests.readers.KvpRequestReader;
011: import org.vfny.geoserver.wcs.WcsException;
012: import org.vfny.geoserver.wcs.requests.CoverageRequest;
013: import org.vfny.geoserver.wcs.servlets.WCService;
014: import java.util.HashMap;
015: import java.util.Map;
016: import java.util.logging.Level;
017: import java.util.logging.Logger;
018: import javax.servlet.http.HttpServletRequest;
019:
020: /**
021: * DOCUMENT ME!
022: *
023: * @author $Author: Alessio Fabiani (alessio.fabiani@gmail.com) $ (last modification)
024: * @author $Author: Simone Giannecchini (simboss1@gmail.com) $ (last modification)
025: */
026: public class GetCoverageKvpReader extends KvpRequestReader {
027: private static final Logger LOGGER = org.geotools.util.logging.Logging
028: .getLogger("org.vfny.geoserver.requests.readers");
029:
030: public GetCoverageKvpReader(Map kvPairs, WCService service) {
031: super (kvPairs, service);
032: }
033:
034: public Request getRequest(HttpServletRequest request)
035: throws ServiceException {
036: return getCoverageRequest(request);
037: }
038:
039: public CoverageRequest getCoverageRequest(
040: HttpServletRequest srequest) throws WcsException {
041: CoverageRequest currentRequest = new CoverageRequest(
042: (WCService) service);
043: currentRequest.setHttpServletRequest(srequest);
044:
045: Map parameters = new HashMap();
046: parameters.putAll(kvpPairs);
047:
048: // set global request parameters
049: if (LOGGER.isLoggable(Level.FINEST)) {
050: LOGGER.finest("setting global request parameters");
051: }
052:
053: if (keyExists("SERVICE")) {
054: final String service = getValue("SERVICE");
055:
056: if (service.trim().toUpperCase().startsWith("WCS")) {
057: currentRequest.setService(service);
058: } else {
059: throw new WcsException("SERVICE parameter is wrong.");
060: }
061:
062: parameters.remove("SERVICE");
063: } else {
064: throw new WcsException("SERVICE parameter is mandatory.");
065: }
066:
067: if (keyExists("VERSION")) {
068: final String version = getValue("VERSION");
069:
070: if (version.equals("1.0.0")) {
071: currentRequest.setVersion(version);
072: } else {
073: throw new WcsException("VERSION parameter is wrong.");
074: }
075:
076: parameters.remove("VERSION");
077: } else {
078: throw new WcsException("VERSION parameter is mandatory.");
079: }
080:
081: if (keyExists("REQUEST")) {
082: final String request = getValue("REQUEST");
083:
084: if (request.equalsIgnoreCase("GetCoverage")) {
085: currentRequest.setRequest(request);
086: } else {
087: throw new WcsException("REQUEST parameter is wrong.");
088: }
089:
090: parameters.remove("REQUEST");
091: } else {
092: throw new WcsException("REQUEST parameter is mandatory.");
093: }
094:
095: if (keyExists("COVERAGE")) {
096: currentRequest.setCoverage(getValue("COVERAGE"));
097: parameters.remove("COVERAGE");
098: } else {
099: throw new WcsException("COVERAGE parameter is mandatory.");
100: }
101:
102: if (keyExists("COVERAGEVERSION")) {
103: currentRequest
104: .setCoverageVersion(getValue("COVERAGEVERSION"));
105: parameters.remove("COVERAGEVERSION");
106: }
107:
108: if (keyExists("FORMAT")) {
109: currentRequest.setOutputFormat(getValue("FORMAT"));
110: parameters.remove("FORMAT");
111: } else {
112: throw new WcsException("FORMAT parameter is mandatory.");
113: }
114:
115: if (keyExists("CRS")) {
116: currentRequest.setCRS(getValue("CRS"));
117: parameters.remove("CRS");
118: } else {
119: throw new WcsException("CRS parameter is mandatory.");
120: }
121:
122: if (keyExists("RESPONSE_CRS")) {
123: currentRequest.setResponseCRS(getValue("RESPONSE_CRS"));
124: parameters.remove("RESPONSE_CRS");
125: } else {
126: currentRequest.setResponseCRS(getValue("CRS"));
127: }
128:
129: if (keyExists("BBOX")) {
130: currentRequest.setEnvelope(getValue("BBOX"));
131: parameters.remove("BBOX");
132: } else {
133: throw new WcsException("BBOX parameter is mandatory.");
134: }
135:
136: if (keyExists("WIDTH") && keyExists("HEIGHT")) {
137: currentRequest.setGridOrigin(new Double[] {
138: new Double(0.0), new Double(0.0) });
139: currentRequest.setGridLow(new Double[] { new Double(0.0),
140: new Double(0.0) });
141: currentRequest.setGridHigh(new Double[] {
142: Double.valueOf(getValue("WIDTH")),
143: Double.valueOf(getValue("HEIGHT")) });
144: parameters.remove("WIDTH");
145: parameters.remove("HEIGHT");
146: } else if ((currentRequest.getEnvelope() != null)
147: && (keyExists("RESX") && keyExists("RESY"))) {
148: final Envelope envelope = currentRequest.getEnvelope();
149: final double envWidth = Math.abs(envelope.getMaxX()
150: - envelope.getMinX());
151: final double envHeight = Math.abs(envelope.getMaxY()
152: - envelope.getMinY());
153: final double width = envWidth
154: / Math.abs(Double.parseDouble(getValue("RESX")));
155: final double height = envHeight
156: / Math.abs(Double.parseDouble(getValue("RESY")));
157:
158: if ((width >= 1.0) && (height >= 1.0)) {
159: currentRequest.setGridOrigin(new Double[] {
160: new Double(0.0), new Double(0.0) });
161: currentRequest.setGridLow(new Double[] {
162: new Double(0.0), new Double(0.0) });
163: currentRequest.setGridHigh(new Double[] {
164: new Double(width), new Double(height) });
165: }
166:
167: parameters.remove("RESX");
168: parameters.remove("RESY");
169: }
170:
171: if (keyExists("INTERPOLATION")) {
172: currentRequest.setInterpolation(getValue("INTERPOLATION"));
173: parameters.remove("INTERPOLATION");
174: }
175:
176: currentRequest.setParameters(parameters);
177:
178: return currentRequest;
179: }
180:
181: protected String getValue(String key) {
182: //JD: this is probably something that the super method should do.
183: String value = super.getValue(key);
184: if (value != null) {
185: value = value.trim();
186: }
187:
188: return value;
189: }
190: }
|