【学习笔记之Openlayers3】查询分析篇(第五篇)


    这篇主要讲到基于openlayers3实现查询分析功能主要包括:属性查询,几何对象查询,位置关系查询这三类。
    开发环境:openlayers3 geoserver postgresql+postgis

1.属性查询

    其实属性查询没有什么要讲的,就是对于图层要素的的基础属性进行查询(例如名称,地址等等)这些查询就是简单的利用ajax进行请求后台,然后写SQL查询语句即可。这里就不多赘述了。

2.几何对象查询

这里的几何对象查询包括绘制几何对象(多边形,拉框/矩形,圆形)以及缓冲区查询(线缓冲,点缓冲)

  1. 几何对象查询:

     基本实现思路:在地图上绘制几何对象,绘制结束后将几何对象传递到后台,利用postgis提供的函数进行分析哪些对象在这个绘制的对象内部,将结果列表进行分页展示即可。
    

实现代码
1.绘制几何对象,前面已经写过这方面文章,这里就不贴了,详细看【学习笔记之Openlayers3】要素绘制篇(第三篇)
2.将几何对象传入后台进行分析:

/** 几何查询
     * @param arg0
     * @return
     */
    private JSONObject getFeaInGeo(JSONObject arg0) {
        JSONObject result = new JSONObject();
        String geowkt = arg0.getString("geowkt");// 绘制的几何对象的wkt字符串
        String layer = arg0.getString("layer"); // 图层名称
        String page = arg0.getString("page");// 当前页
        String sql;
        int rowCount=0;
        int pageCount=0;
        if(page ==null ){//获取当前页数,如果为空,设为1
            page = "1";
        }
        String countsql = "select count(*) from "+layer+" where ST_DWithin(ST_GeometryFromText('"+geowkt+"',4326),geom,0)";
        try {
            // 获取符合条件的纪录总条数
            rowCount = JdbcServer.getJdbcCount(countsql);
            // 获取总页数
            if(rowCount%5!=0)
            {
                pageCount = rowCount / 5 + 1;
            }else{
                pageCount = rowCount / 5;
            }
sql = "select gid as gid,fname as name,ST_AsText(geom) as           geowkt from "+layer+"  where    ST_DWithin(ST_GeometryFromText('"+geowkt+"',4326),geom,0) order by gid limit 5 offset "+(Integer.parseInt(page)-1)*5;

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            // 获取每页数据
            result = JdbcServer.getFeaList(sql);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        result.put("rowCount", rowCount);
        result.put("pageCount", pageCount);
        result.put("page", page);
        return result;
    }

返回值就是复合条件的要素几何的分页列表,在前台进行循环展示数据即可。

  1. 缓冲区查询:

    基本实现思路:先输入缓冲半径,然后在地图中画点(或者画线),然后生成缓冲区,将生成的缓冲区对象,传入后台进行分析得到结果列表。
    

    1.得到缓冲区:(利用postgis的st_buffer函数)

/** 获取缓冲区
     * @param arg0
     * @return
     */
    private JSONObject getBuffer(JSONObject arg0) {
        JSONObject result = new JSONObject();
        String geowkt = arg0.getString("geowkt");//几何图形wkt字符串
        String bufRadius = arg0.getString("bufRadius");//缓冲半径
        String sql = "select ST_AsText(ST_Buffer('"+geowkt+"',"+bufRadius+")) as geowkt;";
        try {

            result = JdbcServer.getGeoWkt(sql);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

2.根据缓冲区对象进行分析生成结果分页,和上一个几何对象查询代码一样。代码就不贴出来了。


基于位置关系查询分析

    基于位置关系的查询分析主要就是求得满足两个几何对象的关系的要素。举个例子:A图层与B图层中的a要素和b要素相交,求在相交部分中A图层的要素。
    主要思路:判断两个要素是否存在关系(以九交模型为例),若存在求复合关系的涂层要素列表。

代码如下:

/** 基于位置关系的查询
     * @param arg0
     * @return
     */
    private JSONObject getFeaByPois(JSONObject arg0) {
        JSONObject result = new JSONObject();
        int rowCount=0;
        int pageCount=0;
        String sourceName = arg0.getString("sourceName");// A图层
        String relation = arg0.getString("relation");// 位置关系
        String target = arg0.getString("targetId");// B图层要素id
        String page = arg0.getString("page"); // 当前页
        if(page ==null||page.equals("") ){// 获取当前页数,如果为空,设为1
            page = "1";
        }
        String targetId = target.split("\\.")[1];
        String targetName = target.split("\\.")[0];

        String countsql = "select count(*) from "+sourceName+"  where 1=1 ";
        String sql = "select gid as gid,fname as name,ST_AsText(geom) as geowkt from "+sourceName+"  where 1=1 ";

        // 查询所有在这个选中的几何最小外接矩形内的数据
        sql+=" and ST_DWithin(ST_Envelope((select geom from "+targetName+" where gid = "+targetId+") ),geom,0) ";
        countsql+=" and ST_DWithin(ST_Envelope((select geom from "+targetName+" where gid = "+targetId+") ),geom,0) ";
        // 首先判断两者关系,然后拼接对应的sql
        if(relation.equals("intersects")){ // 相交
             sql += " and st_Intersects(geom,(select geom from "+targetName+" where gid = "+targetId+" )) = true "; 
             countsql += " and st_Intersects(geom,(select geom from "+targetName+" where gid = "+targetId+" )) = true "; 
        }else if(relation.equals("within")){ // 范围内
             sql += " and st_Within(geom,(select geom from "+targetName+" where gid = "+targetId+" )) = true  "; 
             countsql += " and st_Intersects(geom,(select geom from "+targetName+" where gid = "+targetId+" )) = true "; 
        }else if(relation.equals("contains")){ //包含
             sql += " and st_Contains(geom,(select geom from "+targetName+" where gid = "+targetId+" )) = true  "; 
             countsql += " and st_Intersects(geom,(select geom from "+targetName+" where gid = "+targetId+" )) = true "; 
        }else if(relation.equals("touches")){ //接触
             sql += " and st_Touches(geom,(select geom from "+targetName+" where gid = "+targetId+" )) = true  "; 
             countsql += " and st_Intersects(geom,(select geom from "+targetName+" where gid = "+targetId+" )) = true "; 
        }
        // 分页
        sql+=" order by gid limit 5 offset "+(Integer.parseInt(page)-1)*5;

        try {
            // 获取符合条件的纪录总条数
            rowCount = JdbcServer.getJdbcCount(countsql);
            // 获取总页数
            if(rowCount%5!=0)
            {
                pageCount = rowCount / 5 + 1;
            }else{
                pageCount = rowCount / 5;
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            // 获取每一页的数据
            result = JdbcServer.getFeaList(sql);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        result.put("rowCount", rowCount);
        result.put("pageCount", pageCount);
        result.put("page", page);
        return result;
    }

结尾

    这样基本上查询分析就算实现了,其实还有一种综合型的查询(包含几何查询和属性查询的一种),这种查询也是比较常见的,举个例子:在某个范围内查询所有的超市,这样就要先确定区域(几何对象),然后查询区域内的所有超市(属性)。这就是几何查询与属性查询相结合进行查询的功能。
    本人也是在学习中,文中若有写的不对的地方,还请留言,帮助我及时纠正,大家共同进步!

转载自:https://blog.csdn.net/u013420816/article/details/55001532

You may also like...