TransWikia.com

Using CQL in OpenLayers 3?

Geographic Information Systems Asked by Sergio_Az on July 12, 2021

I’m trying to make a search engine in OpenLayers 3 by GeoServer features and WFS.

To do this should I use a CQL filter?

This is my connection code if GeoServer mediates OL3 would you be so kind as to show me an example of CQL from OpenLayers.

     vectorWFS = new ol.source.Vector({
            loader: function(extent,resolution,projection){
                $.ajax('http://localhost:8080/geoserver/wfs',{
                    type: 'GET',
                    data:{
                        service:'WFS',
                        version:'1.1.0',
                        request:'GetFeature',
                        typename:'Botanico:Urbana',
                        srsname:'EPSG:3857',
                        outputFormat:'text/javascript'
                    },
                    dataType:'jsonp',
                    jsonpCallback:'callback:loadFeatures',
                    jsonp:'format_options'
                });
            }

        });

        window.loadFeatures=function(response){
            GeoJSON= new ol.format.GeoJSON();
            vectorWFS.addFeatures(GeoJSON.readFeatures(response));
        };

        layerVector = new ol.layer.Vector({
            source: vectorWFS,
            style: new ol.style.Style({
                stroke: new ol.style.Stroke({
                color: 'rgba(0,0,255,1.0)',
                width: 2
                })
            })
            });

2 Answers

I found the solution :

        var geojsonFormat ="";
        var vectorSource = "";
        var cqlFilter = 'INE_MUN=' + $("#bucador").val() ;  
        alert (cqlFilter);
        var geojsonFormat = new ol.format.GeoJSON();
        var vectorSource = new ol.source.Vector({
          loader: function(extent, resolution, projection) {
            var url = 'http://localhost:8080/geoserver/wfs?service=WFS&' +
                'version=1.1.0&request=GetFeature&typename=Botanico:Urbana&' +
                'outputFormat=text/javascript&format_options=callback:loadFeatures&'                           

                  + 'CQL_FILTER={{CQLFILTER}}&' +'&srsname=EPSG:3857';

            // Usamos jsonp:false para prevenir que JQuery cuando se cargue 


            var url2 = url.replace('{{CQLFILTER}}', cqlFilter);
            $.ajax({url: url2, dataType: 'jsonp', jsonp: false});
          },
          strategy: ol.loadingstrategy.tile(ol.tilegrid.createXYZ({
            maxZoom: 22
          }))
        });

        window.loadFeatures = function(response) {
          vectorSource.addFeatures(geojsonFormat.readFeatures(response));
        };

Correct answer by Sergio_Az on July 12, 2021

I use this to add a cql_filter in my layer

var params = layerVector.getSource().getParams();

params.CQL_FILTER = "column_table = '1234'";

layerVector.getSource().updateParams(params);

Answered by DarthSinuhe on July 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP