Mercurial > dive4elements > river
diff flys-client/src/main/java/de/intevation/flys/client/server/GFIServiceImpl.java @ 1400:96708d81eaf6
Added an initial GetFeatureInfo tool to get information about points in the map.
flys-client/trunk@3285 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 17 Nov 2011 16:20:55 +0000 |
parents | |
children | 15ef3d3081b7 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/GFIServiceImpl.java Thu Nov 17 16:20:55 2011 +0000 @@ -0,0 +1,224 @@ +package de.intevation.flys.client.server; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.IOException; +import java.io.StringWriter; +import java.net.URL; +import java.net.URLConnection; +import java.util.List; + +import org.apache.log4j.Logger; + +import com.google.gwt.user.server.rpc.RemoteServiceServlet; + + +import de.intevation.flys.client.shared.exceptions.ServerException; +import de.intevation.flys.client.shared.model.AttributedTheme; +import de.intevation.flys.client.shared.model.Theme; + +import de.intevation.flys.client.client.services.GFIService; + + +/** + * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> + */ +public class GFIServiceImpl +extends RemoteServiceServlet +implements GFIService +{ + public static final String ERR_NO_VALID_GFI_URL = + "error_no_valid_gfi_url"; + + public static final String ERR_GFI_REQUEST_FAILED = + "error_gfi_req_failed"; + + public static final String ERR_PARSING_RESPONSE_FAILED = + "error_gfi_parsing_failed"; + + + private static final Logger logger = + Logger.getLogger(GFIServiceImpl.class); + + + /** + * @param themes + * @param format + * @param bbox + * @param height + * @param width + * @param x + * @param y + * + * @return + */ + public String query( + List<Theme> themes, + String format, + String bbox, + String projection, + int height, + int width, + int x, + int y + ) throws ServerException + { + logger.info("GFIServiceImpl.query"); + + String path = createGetFeautureInfoURL( + themes, format, bbox, projection, height, width, x, y); + logger.debug("URL=" + path); + + try { + URL url = new URL(path); + + URLConnection conn = url.openConnection(); + conn.connect(); + + InputStream is = conn.getInputStream(); + + return getResponseText(is); + + } + catch (IOException ioe) { + logger.warn(ioe, ioe); + } + + throw new ServerException(ERR_GFI_REQUEST_FAILED); + } + + + protected String getResponseText(InputStream is) + throws ServerException { + BufferedReader reader = null; + StringWriter writer = new StringWriter(); + + try { + reader = new BufferedReader(new InputStreamReader(is)); + + String line = null; + + if (reader.ready()) { + while ((line = reader.readLine()) != null) { + String test = line.trim(); + if (test.startsWith("<") && !test.startsWith("</") + && test.indexOf("_feature") > 0) + { + writer.append("<gml:featureMember>"); + } + writer.append(line); + + if (test.startsWith("</") && test.indexOf("_feature") > 0) { + writer.append("</gml:featureMember>"); + } + } + } + } + catch (IOException ioe) { + logger.warn(ioe, ioe); + throw new ServerException(ERR_PARSING_RESPONSE_FAILED); + } + finally { + if (reader != null) { + try { + reader.close(); + } + catch (IOException ioe) { + // do nothing here + } + } + } + + return writer.toString(); + } + + + /** + * @param map + * @param themes + * @param format + * @param x + * @param y + * + * @return + */ + protected String createGetFeautureInfoURL( + List<Theme> themes, + String infoFormat, + String bbox, + String projection, + int height, + int width, + int x, + int y + ) throws ServerException + { + String url = getUrl(themes); + + if (url == null || url.length() == 0) { + throw new ServerException(ERR_NO_VALID_GFI_URL); + } + + String layers = createLayersString(themes); + + StringBuilder sb = new StringBuilder(); + sb.append(url); + + if (url.indexOf("?") < 0) { + sb.append("?SERVICE=WMS"); + } + else { + sb.append("&SERVICE=WMS"); + } + + sb.append("&VERSION=1.1.1"); + sb.append("&REQUEST=GetFeatureInfo"); + sb.append("&LAYERS=" + layers); + sb.append("&QUERY_LAYERS=" + layers); + sb.append("&BBOX=" + bbox); + sb.append("&HEIGHT=" + height); + sb.append("&WIDTH=" + width); + sb.append("&FORMAT=image/png"); + sb.append("&INFO_FORMAT=" + infoFormat); + sb.append("&SRS=" + projection); + sb.append("&X=" + String.valueOf(x)); + sb.append("&Y=" + String.valueOf(y)); + + return sb.toString(); + } + + + protected String getUrl(List<Theme> themes) { + for (Theme t: themes) { + AttributedTheme attr = (AttributedTheme) t; + + if (attr.getAttrAsBoolean("queryable")) { + return attr.getAttr("url"); + } + } + + return null; + } + + + protected String createLayersString(List<Theme> themes) { + StringBuilder sb = new StringBuilder(); + boolean first = true; + + for (Theme theme: themes) { + if (!first) { + sb.append(","); + } + + AttributedTheme layer = (AttributedTheme) theme; + if (layer.getAttrAsBoolean("queryable")) { + sb.append(layer.getAttr("layers")); + first = false; + } + } + + return sb.toString(); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :