用Servlet 写WMS服务
代码:
package demo;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class testWMS extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
@SuppressWarnings("unchecked")
Map<String, String[]> map = req.getParameterMap();
Map<String, String> param = new HashMap<String, String>();
for (String k : map.keySet()) {
String s1 = "";
if (param.containsKey(k.toUpperCase())) {
s1 = param.get(k.toUpperCase()) + ",";
}
String[] s2 = (String[]) map.get(k);
for (int i = 0; i < s2.length; i++) {
s1 += s2[i] + (i == 0 ? "" : ",");
}
param.put(k.toUpperCase(), s1);
}
String[] wmsBBOX = param.get("BBOX").split(",");
String wmsWidth = param.get("WIDTH");
String wmsHeight = param.get("HEIGHT");
double x1, y1, x2;
int width, height;
x1 = Double.parseDouble(wmsBBOX[0]);
y1 = Double.parseDouble(wmsBBOX[1]);
x2 = Double.parseDouble(wmsBBOX[2]);
width = Integer.parseInt(wmsWidth);
height = Integer.parseInt(wmsHeight);
// 计算层级
double res = (x2 - x1) / width;
double res0 = 360 / 256;
double ratio = res0 / res;
double log = Math.log(ratio) / Math.log(2);
long level = Math.round(log);
// 计算行列号
long x = Math.round((x1 + 180) / (res * width));
long y = Math.round((90 - y1) / (res * height));
long yg = y + 7856;
String wmsRequest = param.get("REQUEST");
if (wmsRequest.equals("GetMap")) {
URL url = new URL("http://mt3.google.cn/vt/lyrs=s@81&gl=cn&x=" + x
+ "&y=" + yg + "&z=" + level);
BufferedImage bi = ImageIO.read(url);
ImageIO.write(bi, "JPEG", resp.getOutputStream());
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
这个是仅访问谷歌第15层的,前台可以用OpenLayers进行访问
PS:以前在网上看得一篇用geotools写的WMS,引的包都找不到,晕了老半天
现在,就用servlet写个凑和做个实验的