读书人

基于GeoTools的WMS设计与兑现

发布时间: 2012-12-19 14:13:15 作者: rapoo

基于GeoTools的WMS设计与实现

转自(http://blog.tigerlihao.cn/2010/01/geotools-based-web-map-service.html)

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

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

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

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

数据读取模块主要是利用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运行效果

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

读书人网 >编程

热点推荐