⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wmsservice.java

📁 联合国农粮署牵头开发的geonetwork源代码最新版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/** * WmsService.java * * @author Stefano Giaccio */package org.wfp.vam.intermap.kernel.map.mapServices.wms;import java.util.*;import org.jdom.*;import jeeves.utils.*;import org.wfp.vam.intermap.Constants;import org.wfp.vam.intermap.kernel.map.mapServices.*;import org.wfp.vam.intermap.kernel.map.mapServices.MapService;import org.wfp.vam.intermap.kernel.map.mapServices.constants.MapServices;import java.io.*;import java.net.*;import org.wfp.vam.intermap.kernel.map.mapServices.wms.dimensions.Extents;public class WmsService extends MapService{	public static final int TYPE = 2;		private boolean visible; // Not used yet => not degugged	private BoundingBox bb = new BoundingBox();	private Element layerInfo; // the part of the getCapabilities response regarding the layer	private String style; // WMS style for this layer	private String wmsVer; // Version of the WMS server	private String imageType;		// Request prefixes	private String getCapabilitiesPrefix;	private String getMapPrefix;	private String getFeatureInfoPrefix;	private String describeLayerPrefix;	private String getLegendGraphicPrefix;	private String getStylesPrefix;	private String putStylesPrefix;		private Element errorElement;		private Hashtable htExtents = new Hashtable();	private HashMap hmVsp = new HashMap();		private static File xslFiles;		public WmsService(String mapServerUrl, String serviceName, Element capabilities)		throws Exception {		super(mapServerUrl, serviceName);				this.info = capabilities;		wmsVer = capabilities.getAttributeValue("version");		imageType = getImageType(capabilities);				// Get layer info		Element root = new Element("root").addContent(new Element("serviceName").setText(serviceName))			.addContent((Element)capabilities.clone());		Element stylesheet = Xml.loadFile(xslFiles + File.separator + "cap_test.xsl");		layerInfo = Xml.transform(root, stylesheet);				Element elStyle = layerInfo.getChild("Style");		if (elStyle != null)			style = elStyle.getChildText("Name");				getPrefixes();	}		/**	 * Method setExtent	 *	 * @param    name                a  String	 * @param    value               a  String	 *	 */	public void setExtent(String name, String value) {		htExtents.put(name, value);//		System.out.println(htExtents); // TEST	}		// Sets the vendor specific parameters	public void setVendorSpecificParams(HashMap hmVspars){		hmVsp  = hmVspars;		// TEST	}		/** Loads the xsl files in a XmlRepository */	public static void init(String path) {		xslFiles = new File(path);	}		/** Returns 2 (-> WMS) */	public int getType() { return TYPE; }		/** Returns the image request URL with  many image names (utility method) */	public String getGroupImageUrl(BoundingBox bBox, int width, int height, Vector imageNames) {		bb = bBox;		String request;				// Build the comma separated list of image names		String stImageNames = "";		for (int i = imageNames.size() - 1; i >= 0; i--) {			stImageNames += URLEncoder.encode((String)imageNames.get(i));			if (i > 0)				stImageNames += ",";		}				String prefix = setPrefix(getMapPrefix);		if (wmsVer=="1.0.0") {			// WMTVER is deprecated but needed for older map servers			// SERVICE should not be required, but servers were found to give errors without it if I remember. FIXME			request = prefix + "SERVICE=WMS&WMTVER=" + wmsVer + "&REQUEST=map&LAYERS=" +				stImageNames + "&SRS=EPSG:4326&BBOX=" + bb.getWest() + ","				+ bb.getSouth() + "," + bb.getEast() + "," + bb.getNorth() + "&WIDTH="				+ width + "&HEIGHT=" + height + "&FORMAT=" + imageType + "&TRANSPARENT=TRUE";		}		else {			/* Dec 15, 2004 - ticheler - Changed the REQUEST=map to REQUEST=GetMap according to specs because the			 * REQUEST=map seems to trigger exceptions (non-compliant behaviour)*/			request = prefix + "SERVICE=WMS&VERSION=" + wmsVer + "&REQUEST=GetMap&LAYERS=" +				stImageNames + "&SRS=EPSG:4326&BBOX=" + bb.getWest() + ","				+ bb.getSouth() + "," + bb.getEast() + "," + bb.getNorth() + "&WIDTH="				+ width + "&HEIGHT=" + height + "&FORMAT=" + imageType + "&TRANSPARENT=TRUE";					}				// set extents		for (Enumeration e = htExtents.keys(); e.hasMoreElements(); ) {			String key = (String)e.nextElement();			String value = (String)htExtents.get(key);			request += "&" + key + "=" + value;		}		//		System.out.println("\n\n\nrequest\n\n\n" + request); // TEST				// Set style		if (style != null) {			request += "&STYLES=" + style;		}				System.out.println("request: " + request);				return request;	}		/**	 * Method getGroupImageUrl	 *	 * @param    bBox                a  BoundingBox	 * @param    width               an int	 * @param    height              an int	 * @param    imageNames          a  Vector	 *	 * @return   an Object	 */	public String getImageUrl(BoundingBox bBox, int width, int height)	{		Vector imageNames = new Vector();		imageNames.add(name);				return getGroupImageUrl(bBox, width, height, imageNames);	}		public int getActiveLayer() { return 1; }		public void identify(int layer, int x, int y, int width, int height, int tolerance, String reqFormat)		throws Exception {		String prefix = setPrefix(getFeatureInfoPrefix);//		System.out.println("Requested format: " + reqFormat); // DEBUG		if (reqFormat == null) reqFormat = ""; //DEBUG		String infoFormat = strFormat(reqFormat);//		System.out.println("Accepted format: " + infoFormat); // DEBUG//		String infoFormat = isGml() ? "application/vnd.ogc.gml" : "text/plain"; //TEST		// FIXME Changed to ensure compatability with WMS 1.1.1 and 1.1.0		String url;				if (wmsVer=="1.0.0") {			url = prefix + "WMTVER=" + wmsVer + "&REQUEST=GetFeatureInfo&LAYERS="				+ name//				+ URLEncoder.encode(name)				+ "&SRS=EPSG:4326&BBOX=" + bb.getWest() + "," + bb.getSouth() + ","				+ bb.getEast() + "," + bb.getNorth() + "&WIDTH=" + width				+ "&HEIGHT=" + height + "&FORMAT=" + imageType				+ "&QUERY_LAYERS=" + name + "&X=" + x + "&Y=" + y				+ "&INFO_FORMAT=" + infoFormat;				    } else {			url = prefix + "SERVICE=WMS&VERSION=" + wmsVer + "&REQUEST=GetFeatureInfo&LAYERS="				+ name//	    		+ URLEncoder.encode(name)				+ "&SRS=EPSG:4326&BBOX=" + bb.getWest() + "," + bb.getSouth() + ","				+ bb.getEast() + "," + bb.getNorth() + "&WIDTH=" + width				+ "&HEIGHT=" + height + "&FORMAT=" + imageType				+ "&QUERY_LAYERS=" + name + "&X=" + x + "&Y=" + y				+ "&INFO_FORMAT=" + infoFormat;	    }		//		System.out.println("GetFeatureInfo request: " + url.toString()); // DEBUG				HttpClient h = new HttpClient(url);				if(infoFormat.equals(Constants.FORMAT_GML) || infoFormat.equals(Constants.FORMAT_XHTML)) {			lastResponse = new Element("gml").addContent(h.getElement());//			System.out.println("lastResponse element, " + infoFormat + ": " + Xml.getString(lastResponse)); // DEBUG		}		else if (infoFormat.equals(Constants.FORMAT_HTML)) {			String strResult = h.getString();//			System.out.println("Server response: " + strResult); // DEBUG			lastResponse = new Element("html").addContent(new CDATA(strResult)); //Add the html as content to an <html> element		}		else {			lastResponse = new Element("text").addContent(new CDATA(h.getString()));		}//		System.out.println("lastResponse element, "+infoFormat+": " + Xml.getString(lastResponse)); // DEBUG			}		private String strFormat(String reqFormat){		/*Returns the available format to be requested.		 The Format requested is checked if available in the capabilities document.		 If requested format is not found, Intermap will try to use another format provided,		 ordered as XHTML, HTML, GML and finally PLAIN		 */		Hashtable htFormat = new Hashtable();		Element getFeatureInfo = info.getChild("Capability").getChild("Request").getChild("GetFeatureInfo");				List formats = getFeatureInfo.getChildren("Format");		for (Iterator i = formats.iterator(); i.hasNext(); ) {			Element elFormat = (Element)i.next();			String text = elFormat.getText();			//System.out.println("format "+i.toString()+text); //TEST			htFormat.put(text,i);		}				if (htFormat.containsKey(reqFormat))			return reqFormat; //return format that was requested, should normally suffice, rest is only to trap faulty requests		else if (htFormat.containsKey(Constants.FORMAT_HTML))			return Constants.FORMAT_HTML;		else if (htFormat.containsKey(Constants.FORMAT_XHTML))			return Constants.FORMAT_XHTML;		else if (htFormat.containsKey(Constants.FORMAT_GML))			return Constants.FORMAT_GML;		else			return Constants.FORMAT_PLAIN;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -