基于GeoTools的WMS设计与实现

暑假看OGC标准的时候做了一个简单的WMS(Web Map Service),用的是GeoTools工具包。其实做出来用处也不大,应为已经有GeoServer这个项目在做基于GeoTool的网络GIS应用,并且已经做的比较完善了。我这个纯粹是做着玩,顺便学习Java网络编程和GeoTools的。

OGC的WMS标准我就不多说了,可以直接去看标准文档。GeoTools搞开源GIS开发的应该也不陌生,是一个用Java语言编写的遵循OGC规范的开源GIS工具包,其功能涵盖了地理信息数据读写、处理、坐标转换、查询分析、格式化输出等多个方面,详细的情况请访问GeoTools的主页:www.geotools.org。下面主要介绍一下我的设计方案。

WMS服务器的整体架构
WMS服务器的整体架构主要包括:请求分发模块、数据读取模块、样式设置模块、图层加载模块、地图绘制模块属性查询模块。

首先请求分发模块根据客户端的请求参数判断出操作的种类,并分别调用相应的模块。数据读取模块负责加载地图数据文件。样式设置模块负责获取图层的渲染样式。图层加载模块负责将各个数据集和样式对应并按顺序排列,生成地图对象。地图绘制模块负责将地图对象渲染成为图像。查询模块则根据位置返回指定要素的属性信息。最终将具体操作的结果返回给客户端。

WMS服务器整体架构图

WMS服务器整体架构图

请求分发模块主要实现对用户请求参数的解析。在Servlet中一般通过Request对象的getParameter方法获取客户端的请求参数。在请求的解析中需要首先判断客户端要执行的是GetCapabilities、GetMap、GetFeatureInfo中的哪一种操作,然后根据每种操作的参数列表读取参数值,并对参数的有效性进行检验。对于非必须的请求参数需要设置缺省值。最后生成请求参数对象,传递给各个操作的具体实现方法。对于不符合要求的请求参数,需要返回给客户端错误信息,并停止后续的操作,以避免运行错误。这部分的代码如下:

public void doService(HttpServletRequest request,
        HttpServletResponse response) throws IOException {
    Map map = request.getParameterMap();
    Map param = new HashMap();
    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);
    }
    if (!param.containsKey("REQUEST")) {
        WMSException.exception(response);
    } else {
        String wmsRequest = param.get("REQUEST");
        if (wmsRequest.equals("GetCapabilities")) {
            GetCapabilitiesRequest gcr = new GetCapabilitiesRequest(param);
            doGetCapabilities(gcr, response);
        } else if (wmsRequest.equals("GetMap")) {
            GetMapRequest gmr = new GetMapRequest(param);
            doGetMap(gmr, response);
        } else if (wmsRequest.equals("GetFeatureInfo")) {
            GetFeatureInfoRequest gfr = new GetFeatureInfoRequest(param);
            doGetFeatureInfo(gfr, response);
        } else {
            WMSException.exception(response);
        }
    }
}

数据读取模块主要是利用GeoTools提供的Shape file reader模块来读取指定位置的shp格式地图文件,并创建所需要的Data Store对象。地图样式是用SLD文件定义的。SLD是OGC制定的用于描述图层样式的XML文件格式,通过制定一系列的样式规则对指定的要素类型进行样式化,包括设置显示的符号、颜色、填充样式、线条样式、标注等。GeoTools中图层的管理是通过MapContext对象来实现。调用MapContext对象的addLayer方法,可将图层按照从最底层开始依次加载到地图中。这部分代码如下:

private static void addShapeLayer(String name) throws Exception {
    File file = new File("C:\\data\\" + name + ".shp");
    File sldFile = new File("C:\\data\\" + name + ".sld");
    FileDataStore store = FileDataStoreFinder.getDataStore(file);
    ((ShapefileDataStore) store).setStringCharset(Charset.forName("GB2312"));
    FeatureSource featureSource = store
            .getFeatureSource();
    Configuration config = new SLDConfiguration();
    Parser parser = new Parser(config);
    InputStream sld = new FileInputStream(sldFile);
    StyledLayerDescriptor styleSLD = (StyledLayerDescriptor) parser.parse(sld);
    Style style = SLD.defaultStyle(styleSLD);
    map.addLayer(featureSource, style);
}

WMS服务器的GetMap操作需要根据客户端的请求将地图对象的指定区域进行渲染,并返回图像文件。首先需要根据用户的请求参数生成一个ReferencedEnvelope对象作为地图输出的范围,然后使用StreamingRenderer对象进行渲染,并将输出绘制在用户指定的大小和格式的图像文件中。最后将图像以二进制编码的形式通过Response对象返回给用户。这部分的代码如下:

private void doGetMap(GetMapRequest gmr, HttpServletResponse response)
        throws IOException {
    double x1, y1, x2, y2;
    int width, height;
    try {
        x1 = Double.parseDouble(gmr.getBBOX()[0]);
        y1 = Double.parseDouble(gmr.getBBOX()[1]);
        x2 = Double.parseDouble(gmr.getBBOX()[2]);
        y2 = Double.parseDouble(gmr.getBBOX()[3]);
        width = Integer.parseInt(gmr.getWidth());
        height = Integer.parseInt(gmr.getHeight());
    } catch (Exception e) {
        WMSException.exception(response);
        return;
    }
    // 设置输出范围
    ReferencedEnvelope mapArea = new ReferencedEnvelope(x1, x2, y1, y2, crs);
    // 初始化渲染器
    StreamingRenderer sr = new StreamingRenderer();
    sr.setContext(map);
    // 初始化输出图像
    BufferedImage bi = new BufferedImage(width, height,
            BufferedImage.TYPE_INT_ARGB);
    Graphics g = bi.getGraphics();
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    Rectangle rect = new Rectangle(0, 0, width, height);
    // 绘制地图
    sr.paint((Graphics2D) g, rect, mapArea);
    // 编码图像
    PNGEncodeParam encodeParam = PNGEncodeParam.getDefaultEncodeParam(bi);
    if (encodeParam instanceof PNGEncodeParam.Palette) {
        PNGEncodeParam.Palette p = (PNGEncodeParam.Palette) encodeParam;
        byte[] b = new byte[] { -127 };
        p.setPaletteTransparency(b);
    }
    //将图像数据输出到Servlet相应中
    response.setContentType("image/png");
    ServletOutputStream out = response.getOutputStream();
    com.sun.media.jai.codec.ImageEncoder encoder = ImageCodec
            .createImageEncoder("PNG", out, encodeParam);
    encoder.encode(bi.getData(), bi.getColorModel());
    bi.flush();
}

WMS服务器的GetFeatureInfo操作需要通过位置对要素集进行查询,返回指定要素的属性信息。由于用户所给的查询坐标是图像的像素坐标,因此需要将坐标转换地图要素所使用的实际坐标。知道了实际坐标,就可以创建一个Filter对象来描述查询的约束条件。然后调用要素集的查询方法,就可以获取符合要求的要素子集,最后按一定的格式返回各个要素的属性信息给客户端。这部分的代码如下:

private void doGetFeatureInfo(GetFeatureInfoRequest gfr,
        HttpServletResponse response) throws IOException {
    double x1, y1, x2, y2;
    int width, height, i, j;
    try {
        x1 = Double.parseDouble(gfr.getBBOX()[0]);
        y1 = Double.parseDouble(gfr.getBBOX()[1]);
        x2 = Double.parseDouble(gfr.getBBOX()[2]);
        y2 = Double.parseDouble(gfr.getBBOX()[3]);
        width = Integer.parseInt(gfr.getWidth());
        height = Integer.parseInt(gfr.getHeight());
        i = Integer.parseInt(gfr.getI());
        j = Integer.parseInt(gfr.getJ());
    } catch (Exception e) {
        WMSException.exception(response);
        return;
    }
    // 计算点选范围的地图坐标
    double cx1, cy1, cx2, cy2;
    cx1 = x1 * (width - i + 0.5 + GET_FEATURE_INFO_BUFFUR) / width + x2
            * (i - 0.5 - GET_FEATURE_INFO_BUFFUR) / width;
    cx2 = x1 * (width - i + 0.5 - GET_FEATURE_INFO_BUFFUR) / width + x2
            * (i - 0.5 + GET_FEATURE_INFO_BUFFUR) / width;
    cy1 = y1 * (j - 0.5 + GET_FEATURE_INFO_BUFFUR) / height + y2
            * (height - j + 0.5 - GET_FEATURE_INFO_BUFFUR) / height;
    cy2 = y1 * (j - 0.5 - GET_FEATURE_INFO_BUFFUR) / height + y2
            * (height - j + 0.5 + GET_FEATURE_INFO_BUFFUR) / height;
    ReferencedEnvelope clickArea = new ReferencedEnvelope(cx1, cx2, cy1, cy2, crs);
    MapLayer[] maplayers = map.getLayers();
    FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
    response.setContentType("text/html");
    response.setCharacterEncoding("GBK");
    PrintWriter out = response.getWriter();
    out.println("location: " + ((cx1 + cx2) / 2.0) + ", "
            + ((cy1 + cy2) / 2.0) + "<br/>");
    // 分别在每个图层中查找点选范围内的对象
    for (int k = 0; k < maplayers.length; k++) {
        FeatureSource fs =
            (FeatureSource) maplayers[k].getFeatureSource();
        String geometryPropertyName = fs.getSchema().getGeometryDescriptor().getLocalName();
        Filter filter = ff.bbox(ff.property(geometryPropertyName), clickArea);
        FeatureCollection fc = fs.getFeatures(filter);
        SimpleFeatureType schema = fc.getSchema();
        FeatureIterator fi = fc.features();
        if (fi.hasNext()) {
            out.println("Selected feature(s) in layer ["+schema.getTypeName()+"]:<br/>");
            while (fi.hasNext()) {
                SimpleFeature f = fi.next();
                out.println("id:" + f.getID() + "<br/>");
                for (AttributeDescriptor type : schema
                        .getAttributeDescriptors()) {
                    String name = type.getLocalName();
                    if (!name.equals(geometryPropertyName))
                        out.println(name + ":"
                                + f.getProperty(name).getValue().toString()
                                + "<br/>");
                }
                out.println("<br/>");
            }
        }
    }
    out.flush();
    out.close();
}

最后的效果:

WMS运行效果

WMS运行效果

当时还准备做一个WFS的实例,做了一半,有其他的事情就放下了,以后有时间再做。

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值