openlayer+geoserver实现空间查询

代码如下:

  //设置数据源属性
        var wmsSource = new ol.source.TileWMS({
            url: 'http://localhost:9999/geoserver/wfs',//自己的端口号
            params: {
                'LAYERS': 'test:sichuan_xianjie', //自己发布的图层的name
                'TILED': true
            },
            serverType: 'geoserver',
            // crossOrigin: 'anonymous',
            projection: 'EPSG:4326'
        });
        
        var wmsLayer = new ol.layer.Tile({
            source: wmsSource
        });

        //设置视图
        var view = new ol.View({
            center: [102.43652, 30.76172],
            zoom: 7,
            projection : "EPSG:4326"
        });

        //创建地图
        var map = new ol.Map({
            layers: [wmsLayer],
            target: 'map',
            view: view
        });

        //首先定义一个空的矢量图层,设置样式并添加到当前map中
        var vectorSource = new ol.source.Vector;
        var vector = new ol.layer.Vector({
            source: vectorSource,
            style: new ol.style.Style({
                fill: new ol.style.Fill({
                    color: 'white'
                }),
                stroke:new ol.style.Stroke({
                    color: 'red',
                    width:1
                })
            })
        });
        map.addLayer(vector);

        var draw,geometry;
        function addInteraction(){
            draw = new ol.interaction.Draw({
                source : vectorSource,
                type : 'Polygon'
            });
            map.addInteraction(draw);
        };

        function btnQuery(){
            map.removeInteraction(draw);
            addInteraction();
        };

        function btnClick(){
            geometry = vectorSource.getFeatures()[0].getGeometry();
            //设置查询参数与条件
            var featureRequest = new ol.format.WFS().writeGetFeature({
                srsName: 'EPSG:4326',//坐标系统
                featureNS: 'http://geoserver.org',//命名空间 URI
                featurePrefix: 'test',//工作区名称
                featureTypes: ['test:sichuan_xianjie'],//查询图层,可以是同一个工作区下多个图层,逗号隔开
                outputFormat: 'application/json',
                filter: ol.format.filter.intersects("the_geom",geometry)//前者是属性名,后者是对应值
            });

            fetch('http://localhost:9999/geoserver/wfs', {//geoserver wfs地址如localhost:8080/geoserver/wfs,我是9999
                method: 'POST',
                body: new XMLSerializer().serializeToString(featureRequest)
            }).then(function(response) {
                return response.json();
            }).then(function(json) {
                var features = new ol.format.GeoJSON().readFeatures(json);
                vectorSource.addFeatures(features);
                map.getView().fit(vectorSource.getExtent());//缩放到查询出的feature
            });
        }

我这里空间查询只写了多边形的空间查询,大家可以自己加一些其他的。

OpenLayers中,可以通过单击地图来获取要素。下面是一个简单的示例代码: ```javascript // 创建一个地图对象 var map = new ol.Map({ target: 'map', layers: [ // 添加一个OSM图层 new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: ol.proj.fromLonLat([0, 0]), zoom: 2 }) }); // 创建一个WFS源 var wfsSource = new ol.source.Vector({ format: new ol.format.GeoJSON(), url: function(extent) { return 'http://localhost:8080/geoserver/wfs?service=WFS&' + 'version=1.1.0&request=GetFeature&typename=workspace:layername&' + 'outputFormat=application/json&srsname=EPSG:3857&' + 'bbox=' + extent.join(',') + ',EPSG:3857'; }, strategy: ol.loadingstrategy.bbox }); // 创建一个矢量图层 var vectorLayer = new ol.layer.Vector({ source: wfsSource }); // 将矢量图层添加到地图中 map.addLayer(vectorLayer); // 监听地图的单击事件 map.on('click', function(evt) { map.forEachFeatureAtPixel(evt.pixel, function(feature) { // 处理点击事件,获取要素信息 console.log(feature.getProperties()); }); }); ``` 上述代码中,我们首先创建了一个地图对象,并添加了一个OSM图层。然后,我们创建了一个WFS源,并指定了WFS服务的地址和参数。接下来,我们创建了一个矢量图层,并将WFS源添加到该图层中。最后,我们监听地图的单击事件,并在事件处理函数中使用`forEachFeatureAtPixel`方法来获取点击位置的要素信息。 请注意,上述代码中的`workspace:layername`需要替换为实际的工作空间和图层名称,`http://localhost:8080/geoserver/wfs`需要替换为实际的GeoServer的WFS服务地址。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值