openlayers实现wfs属性查询和空间查询

概述:

一直在寻求openlayers中wfs加载和属性查询的相关操作,功夫不负有心人,蓦然回首,那人却在灯火阑珊处,找到了这篇博文:http://blog.csdn.net/longshengguoji/article/details/39377931,试了下,在IE8中正常运行,但是在chrom中涉及到跨域的问题,待后期接解决吧。本文讲解如何通过wfs实现属性的查询与展示。


效果:


初始化状态


属性查询结果


空间查询结果

数据表:



关键代码:

添加wfs图层

[javascript] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. wfs = new OpenLayers.Layer.Vector("wfs", {  
  2.     strategies: [new OpenLayers.Strategy.Fixed()],  
  3.     visibility:true,  
  4.     protocol: new OpenLayers.Protocol.WFS({  
  5.         url: "http://localhost:8081/geoserver/lzugis/wfs?",  
  6.         featureType: "capital",  
  7.         featurePrefix : "lzugis",  
  8.         featureNS: "http://www.lzugis.com.cn",  
  9.         srsName : "EPSG:4326",  
  10.         geometryName:"the_geom"  
  11.     })  
  12. });  
  13. map.addLayer(wfs);  

查询条件面板

  1. <pre name="code" class="html"><div class="query-box">  
  2.     <select id="field">  
  3.         <option value="code">编码</option>  
  4.         <option value="pinyin">拼音</option>  
  5.     </select>  
  6.     <input type="text" id="val" value="100032" style="width: 80px;"/>   
  7.     <button id="query">属性查询</button>   
  8.     <button id="boxQuery">空间查询</button>  
  9. </div>  


执行属性查询查询

[javascript] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. $("#query").on("click",function(){  
  2.     var field = $("#field").val();  
  3.     var val = $("#val").val();  
  4.     var filter = new OpenLayers.Filter.Comparison({  
  5.         type : OpenLayers.Filter.Comparison.EQUAL_TO,  
  6.         property : field,  
  7.         value : val  
  8.     });  
  9.     console.log(wfs);  
  10.     wfs.filter = filter;  
  11.     wfs.refresh();  
  12. })  


空间查询

[javascript] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. var drawLayer = new OpenLayers.Layer.Vector("drawLayer",{  
  2.     styleMap: new OpenLayers.StyleMap({'default':{  
  3.         strokeColor: "#ff0000",  
  4.         strokeOpacity: 1,  
  5.         strokeWidth: 1,  
  6.         fillColor: "#000000",  
  7.         fillOpacity: 0.1  
  8.     }})  
  9. });  
  10. map.addLayer(drawLayer);  
  11. var drawBox = new OpenLayers.Control.DrawFeature(drawLayer,  
  12.         OpenLayers.Handler.RegularPolygon,{  
  13.             handlerOptions: {  
  14.                 sides: 4,  
  15.                 irregular: true  
  16.             }  
  17.         }  
  18. );  
  19. map.addControl(drawBox);  
  20. drawBox.featureAdded = onEndDraw;  
  21.   
  22. function onEndDraw(feature){  
  23.     drawBox.deactivate();  
  24.     console.info(feature);  
  25.     var geometry = feature.geometry;  
  26.     var filter = new OpenLayers.Filter.Spatial({  
  27.         type : OpenLayers.Filter.Spatial.INTERSECTS,  
  28.         value : geometry,  
  29.         projection : 'EPSG:4326'  
  30.     });  
  31.     wfs.filter = filter;  
  32.     wfs.refresh();  
  33.     map.zoomToExtent(wfs.getDataExtent());  
  34. }  
  35. $("#boxQuery").on("click",function(){  
  36.     drawLayer.removeAllFeatures();  
  37.     wfs.filter = null;  
  38.     wfs.refresh();  
  39.     drawBox.activate();  
  40. });  



完整代码为:

    1. <pre name="code" class="html"><html xmlns="http://www.w3.org/1999/xhtml">  
    2. <head>  
    3.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    4.     <title>china EPSG:4326 image/png</title>  
    5.     <link rel="stylesheet" type="text/css" href="http://localhost/olapi/theme/default/style.css"/>  
    6.     <style type="text/css">  
    7.         body { font-family: sans-serif; font-weight: bold; font-size: .8em; }  
    8.         body { border: 0px; margin: 0px; padding: 0px; }  
    9.         #map { width: 100%; height: 100%; border: 0px; padding: 0px; }  
    10.         .query-box{  
    11.             position: absolute;  
    12.             top: 15pt;  
    13.             right: 15pt;  
    14.             z-index:1001;  
    15.             border: 1px solid #ff0000;  
    16.             border-radius: 3px;  
    17.             background: #f2f2f2;  
    18.             padding: 5px 8px;  
    19.             font-family: "Trebuchet MS", Helvetica, Arial, sans-serif;  
    20.         }  
    21.     </style>  
    22.     <script type="text/javascript" src="http://localhost/olapi/OpenLayers.js"></script>  
    23.     <script type="text/javascript" src="http://localhost/olapi/lib/OpenLayers/Lang/zh-CN.js"></script>  
    24.     <script src="http://localhost/jquery/jquery-1.8.3.js"></script>  
    25.     <script type="text/javascript">  
    26.         var map, wfs;  
    27.         function init(){  
    28.             var bounds = new OpenLayers.Bounds(  
    29.                     87.57582859314434, 19.969920291221204,  
    30.                     126.56713756740385, 45.693810203800794  
    31.             );  
    32.             var options = {  
    33.                 controls: [],  
    34.                 maxExtent: bounds,  
    35.                 maxResolution: 0.1523098006807012,  
    36.                 projection: "EPSG:4326",  
    37.                 units: 'degrees'  
    38.             };  
    39.             map = new OpenLayers.Map('map', options);  
    40.   
    41.   
    42.             var tiled = new OpenLayers.Layer.WMS(  
    43.                     "province", "http://localhost:8081/geoserver/lzugis/wms",  
    44.                     {  
    45.                         "LAYERS": 'province',  
    46.                         "STYLES": '',  
    47.                         format: 'image/png'  
    48.                     },  
    49.                     {  
    50.                         buffer: 0,  
    51.                         displayOutsideMaxExtent: true,  
    52.                         isBaseLayer: true,  
    53.                         yx : {'EPSG:4326' : true}  
    54.                     }  
    55.             );  
    56.             map.addLayer(tiled);  
    57.             map.addControl(new OpenLayers.Control.PanZoomBar({  
    58.                 position: new OpenLayers.Pixel(2, 15)  
    59.             }));  
    60.             map.addControl(new OpenLayers.Control.Navigation());  
    61.             map.zoomToExtent(bounds);  
    62.             wfs = new OpenLayers.Layer.Vector("wfs", {  
    63.                 strategies: [new OpenLayers.Strategy.Fixed()],  
    64.                 visibility:true,  
    65.                 protocol: new OpenLayers.Protocol.WFS({  
    66.                     url: "http://localhost:8081/geoserver/lzugis/wfs?",  
    67.                     featureType: "capital",  
    68.                     featurePrefix : "lzugis",  
    69.                     featureNS: "http://www.lzugis.com.cn",  
    70.                     srsName : "EPSG:4326",  
    71.                     geometryName:"the_geom"  
    72.                 })  
    73.             });  
    74.             map.addLayer(wfs);  
    75.             var drawLayer = new OpenLayers.Layer.Vector("drawLayer",{  
    76.                 styleMap: new OpenLayers.StyleMap({'default':{  
    77.                     strokeColor: "#ff0000",  
    78.                     strokeOpacity: 1,  
    79.                     strokeWidth: 1,  
    80.                     fillColor: "#000000",  
    81.                     fillOpacity: 0.1  
    82.                 }})  
    83.             });  
    84.             map.addLayer(drawLayer);  
    85.             var drawBox = new OpenLayers.Control.DrawFeature(drawLayer,  
    86.                     OpenLayers.Handler.RegularPolygon,{  
    87.                         handlerOptions: {  
    88.                             sides: 4,  
    89.                             irregular: true  
    90.                         }  
    91.                     }  
    92.             );  
    93.             map.addControl(drawBox);  
    94.             drawBox.featureAdded = onEndDraw;  
    95.   
    96.             function onEndDraw(feature){  
    97.                 drawBox.deactivate();  
    98.                 console.info(feature);  
    99.                 var geometry = feature.geometry;  
    100.                 var filter = new OpenLayers.Filter.Spatial({  
    101.                     type : OpenLayers.Filter.Spatial.INTERSECTS,  
    102.                     value : geometry,  
    103.                     projection : 'EPSG:4326'  
    104.                 });  
    105.                 wfs.filter = filter;  
    106.                 wfs.refresh();  
    107.                 map.zoomToExtent(wfs.getDataExtent());  
    108.             }  
    109.             $("#boxQuery").on("click",function(){  
    110.                 drawLayer.removeAllFeatures();  
    111.                 wfs.filter = null;  
    112.                 wfs.refresh();  
    113.                 drawBox.activate();  
    114.             });  
    115.   
    116.             $("#query").on("click",function(){  
    117.                 var field = $("#field").val();  
    118.                 var val = $("#val").val();  
    119.                 var filter = new OpenLayers.Filter.Comparison({  
    120.                     type : OpenLayers.Filter.Comparison.EQUAL_TO,  
    121.                     property : field,  
    122.                     value : val  
    123.                 });  
    124.                 wfs.filter = filter;  
    125.                 wfs.refresh();  
    126. //                map.zoomToExtent(wfs.getDataExtent());  
    127.             });  
    128.         }  
    129.     </script>  
    130. </head>  
    131. <body onLoad="init()">  
    132. <div class="query-box">  
    133.     <select id="field">  
    134.         <option value="code">编码</option>  
    135.         <option value="pinyin">拼音</option>  
    136.     </select>  
    137.     <input type="text" id="val" value="100032" style="width: 80px;"/>   
    138.     <button id="query">属性查询</button>   
    139.     <button id="boxQuery">空间查询</button>  
    140. </div>  
    141. <div id="map"></div>  
    142. </body>  
    143. </html
posted @ 2016-07-18 15:17  韩慧兵  阅读(877)  评论(0编辑  收藏  举报