Git Product home page Git Product logo

leaflet-geoserver-request's Introduction

Documentation leaflet-geoserver-request

DOI Build Status GitHub release (latest by date) GitHub file size in bytes npm

This is the plugin for Geoserver various kind of requests. Using this plugin, we can make WMS, WFS, getLegendGraphic, WMS-Image request.

demo

Video tutorial

Youtube Video

Installation and setup

  • using NPM:
npm install leaflet-geoserver-request
  • Quick use :
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="[path to js]/L.Geoserver.js"></script>

Web-Map-Service (WMS) Request

WMS request will provides georeferenced map image.

var wmsLayer = L.Geoserver.wms("http://localhost:8080/geoserver/wms", {
  layers: "topp:tasmania_roads",
});
wmsLayer.addTo(map);
WMS options
Option Type Default Description
layers string undefined This is required filed. If you have multiple layers, you can add them in a comma seperated values: "workspace:layer1,workspace:layer2"
format string image/png formate of the wms request. It may be image/jpeg, image/tiff, application/openlayers
transparent boolean true transparent of the map
CQL_FILTER string INCLUDE To filter the layer based on their attribute. read more about this
zIndex integer 1000 zIndex of the layer
attribution string layer attribution of the layer. It will be added at the bottom-left cornor of the map
style string undefined style file associated with that layer. If you didn't provide the style field, it will take default style of the layer

Web-Feature-Service (WFS) Request

In WFS request, you need to enable the jsonp for GeoServer. By default, jsonp for GeoServer was desabled. open $GEOSERVER_HOME\webapps\geoserver\WEB-INF\web.xml file and uncomment following line,

<context-param>
    <param-name>ENABLE_JSONP</param-name>
    <param-value>true</param-value>
</context-param>

Note: WFS request only applicable for vector layer, not for raster

var wfsLayer = L.Geoserver.wfs("http://localhost:8080/geoserver/wfs", {
  layers: "topp:tasmania_roads",
});
wfsLayer.addTo(map);
WFS options
Options Type Default Description
layers string undefined This is required filed. If you have multiple layers, you can add them in a comma seperated values: "workspace:layer1,workspace:layer2"
style js object/function null The style file can be in form of javascript oject or function. eg `style = {color: "black",fillOpacity: "0", opacity: "0.5" }
onEachFeature function null Common region to add this feature is to add popup when the layer cicked. more detail
CQL_FILTER string INCLUDE To filter the layer based on their attribute. read more about this
fitLayer boolean true The map automatically zoomed into the loaded data region

Web-Mapping-Service-Image (WMSImage) request

This is the custom request naming by me. It will generate the image which will zoomed to the desired location. The main different between wms and wmsImage request is, it can be zoomed to the desired location based on vector layer.

There are certain convention for this request. Please read carefully

  • The first layer must be vector layer in option wmsLayers. Rest of other layers can be vector or raster

  • You need to provide the same number of data list in wmsLayers, wmsCQL_FILTER & wmsStyle fields

  • The map will zoomed to the first parameter of wmsCQL_FILTER region

  • The default value for the style and cql_filter will be include

  • The wmsLayers and wmsId fields are required field

<img src="" id="wmsImage" />
<!-- The id field is required. This id will used in js script option "wmsId"-->
L.Geoserver.wmsImage("http://localhost:8080/geoserver/wms", {
  wmsLayers: [
    "workspace:first_layer",
    "workspace:second_layer",
    "workspace:third_layer",
  ],
  wmsCQL_FILTER: ["district=='Syangja'", "INCLUDE", "INCLUDE"],
  wmsStyle: ["style1", "style2", "INCLUDE"],
  format: "image/png",
  wmsId: "wmsImage",
  width: 300,
  height: 300,
});

Legend Request

The legend request will provide the crossponding legend of the layer to the bottom-left position of the map. In case of not providing style parameter, it will take the default style from GeoServer.

var layerLegend = L.Geoserver.legend("http://localhost:8080/geoserver/wms", {
  layers: "topp:tasmania_roads",
  style: `stylefile`,
});

layerLegend.addTo(map);

Advance WFS request demo

var wfsLayerAdvance = L.Geoserver.wfs("http://localhost:8080/geoserver/wms", {
  layers: "topp:tasmania_roads",
  style: {
    color: "black",
    fillOpacity: "0",
    opacity: "0.5",
  },
  onEachFeature: function (feature, layer) {
    layer.bindPopup("this is popuped");
  },
  CQL_FILTER: "district='Syangja'",
});

map.fitBounds(wfsLayerAdvance.getBounds());
wfsLayerAdvance.addTo(map);

Contribution:

Would ❤️ to see any contributions.

Appreciate:

If you liked my work, show some ❤️ by ⭐ repo.

Also you can appreciate by

Buy Me A Coffee PayPal Logo

leaflet-geoserver-request's People

Contributors

antoniovlx avatar iamtekson avatar iamtekson-server avatar jppcel avatar mrzh4s avatar snailedlt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

leaflet-geoserver-request's Issues

I cannot define more than one L.Geoserver.wfs separate layer

When I try to make two or more layers with L.Geoserver.wfs, my layers break (The first layers don't show in the map, and the last layer shows the first layer instead of the correct one.

I want to fix it, but I need assistance, I don't understand the source of the bug

To reproduce see this fork where I tried to define 2 layers wfs1 and wfs2 https://github.com/nez/leaflet-geoserver-request/blob/e1bd8687147950fbc514d9f07e2f27e985a2b6f8/examples/maps.html#L61-L89
(In this example I am trying to make two layers with the same dataset, the first is red, and the second is white with half transparency, so the color should look pale red but it shows white.

There is also the following error in the browser
parsererror Error: getJson was not called
Uncaught ReferenceError: getJson is not defined

Thanks for the great lib and for your assistance

ReferenceError: $ is not defined at NewClass.wfs (L.Geoserver.js:46:1)

Hi!

I'm using your plugin to load both WMS and WFS layers from a GeoServer instance on a leaflet map inside an Angular app.
WMS are working fine, but when I switched to WFS, I got this error:

image

JSONP for GeoServer is enabled.
If I try to preview the WFS "Levels" layer from GeoServer web UI, I got the following JSONP data back:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "id": "V_Levels.fid-6ecc0c01_1813d891570_169",
            "geometry": {
                "type": "MultiPolygon",
                "coordinates": [
                    [
                        [
                            [
                                7.7826615200000008,
                                45.3871735
                            ],
                            [
                                7.79106001,
                                45.38629957
                            ],
                            ...other coordinates...
                        ]
                    ]
                ]
            },
            "geometry_name": "Geom",
            "properties": {
                "Id": 1,
                "CodRip": 1,
                "CodRegione": 1,
                "CodProvincia": 1,
                "CodComune": 201,
                "CodUts": 201,
                "CodProvinciaComune": 1001,
                "CodIstatComune": "001001",
                "Comune": "Agliè",
                "ComuneAltro": null,
                "Area": 13314604.171518622,
                "Perimetro": 16102.469408787643,
                "ConcentrazioneMedia": 71
            }
        },
        ...other features...
    ],
    "totalFeatures": 7904,
    "numberMatched": 7904,
    "numberReturned": 50,
    "timeStamp": "2022-06-07T10:04:08.658Z",
    "crs": {
        "type": "name",
        "properties": {
            "name": "urn:ogc:def:crs:EPSG::4326"
        }
    }
}

Layers' configuration

// WORKS!
let boundaries = L.Geoserver.wms('https://mygeoserver.dev/geoserver/wms', {
    layers: 'boundaries:all',
    attribution: '<a href="https://mygeoserver.dev">My data</a>'
});
boundaries.addTo(this.map);

// WORKS!
let wmsLevels = L.Geoserver.wms('https://mygeoserver.dev/geoserver/wms', {
     layers: 'rad:Levels',
     attribution: '<a href="https://mygeoserver.dev">My levels</a>'
});
wmsLevels.addTo(this.map);

// DOESN'T WORK!
let wfsLevels = L.Geoserver.wfs("https://mygeoserver.dev/geoserver/wfs", {
    layers: "rad:Levels",
    attribution: '<a href="https://mygeoserver.dev">My levels</a>'
});
wfsLevels.addTo(this.map);

Since the issue seems to be related to $ jQuery function not being found, I've also tried to add a reference to jquery in my module, but with no success. The jquery npm package is already installed in my application.

import * as $_ from 'jquery';
const $ = $_;

Problem with callbacks

Hi there,

I'm getting a odd problem with callbacks that more often than not don't work. If I disable caching and empty cache and force a refresh it will occassionally work which is very frustraing that it's not 100% repeatable. The vast majority of the time though this error is thrown. It's as if it hasn't picked up the definition of getJson before it's used. As I say incognito or forcing clearing cache does seem to work occassionaly then I make a change deploy again and get error back.

I'm using Leaflet 1.9.4 and JQuery 3.5.1.

my code is:

surveyDates = L.Geoserver.wfs("https://maps.redacted-server-name-here.com/geoserver/wfs", {
    layers: "poa:SurveyDates",
    onEachFeature: function (feature, layer) {
        if (feature.properties?.name && feature.properties?.lastSurvey) {
            let survey = " last surveyed: " + new Date(feature.properties.lastSurvey);
            layer.bindTooltip(feature.properties.name + survey);
        }
        if (feature.properties.surveyType == 1) {
            layer.setStyle({ fillColor: '#dc4405' });
        } else {
            layer.setStyle({ fillColor: '#477A7B' });
        }
    }
});

Error is:

Object 'parsererror' Error: getJson was not called
at Function.error (jquery.js:334:9)
at s.converters.script json (jquery.js:10232:12)
at ajaxConvert (jquery.js:9260:19)
at done (jquery.js:9736:15)
at HTMLScriptElement.callback (jquery.js:10170:8)
at HTMLScriptElement.dispatch (jquery.js:5429:27)
at elemData.handle (jquery.js:5233:28)
wfs?callback=getJson&service=WFS&version=1.1.0&request=GetFeature&typename=poa%3ASurveyDates&CQL_FILTER=INCLUDE&srsname=EPSG%3A4326&outputFormat=text%2Fjavascript&format_options=callback%3A%20getJson&=1687773981153:1 Uncaught ReferenceError: getJson is not defined
at wfs?callback=getJson&service=WFS&version=1.1.0&request=GetFeature&typename=poa%3ASurveyDates&CQL_FILTER=INCLUDE&srsname=EPSG%3A4326&outputFormat=text%2Fjavascript&format_options=callback%3A%20getJson&
=1687773981153:1:2
(anonymous) @ wfs?callback=getJson&service=WFS&version=1.1.0&request=GetFeature&typename=poa%3ASurveyDates&CQL_FILTER=INCLUDE&srsname=EPSG%3A4326&outputFormat=text%2Fjavascript&format_options=callback%3A%20getJson&_=1687773981153:1

In case it's relevant I'm also seeing this error

L.Geoserver.js:106 Uncaught TypeError: Cannot read properties of undefined (reading 'fitBounds')
at Object.success (L.Geoserver.js:106:31)
at fire (jquery.js:3496:31)
at Object.fireWith [as resolveWith] (jquery.js:3626:7)
at done (jquery.js:9786:14)
at HTMLScriptElement.callback (jquery.js:10170:8)
at HTMLScriptElement.dispatch (jquery.js:5429:27)
at elemData.handle (jquery.js:5233:28)

Legend Request builds wrong URI for Legend

I'm trying to use the Geoserver with WMS 1.3.0 of the german weather service "DWD".
Getting the layer itself with

    var wmsLayer = L.Geoserver.wms('https://maps.dwd.de/geoserver/ows?', {
        layers: 'dwd:Warnungen_Gemeinden',
        version: '1.3.0',
        opacity: 0.5,
        attribution: 'DWD'
    }).addTo(map);

works.

But when I try to get the legend with

    var layerLegend = L.Geoserver.legend('https://maps.dwd.de/geoserver/ows?', {
        layers: 'dwd:Warnungen_Gemeinden',
        style: `stylefile`,
    });
    layerLegend.addTo(map);

I only get the "broken graphic" symbol. When I inspect the source of the page I see that the wrong URI gets built:

<img src="https://maps.dwd.de/geoserver/ows?/wms?REQUEST=GetLegendGraphic&amp;VERSION=1.1.0&amp;FORMAT=image/png&amp;LAYER=dwd:Warnungen_Gemeinden&amp;style=stylefile" alt="legend" data-toggle="tooltip" title="Map legend">

The right URI according to the "getCapabilities" document is

<LegendURL width="306" height="160">
<Format>image/png</Format>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="https://maps.dwd.de/geoserver/ows?service=WMS&request=GetLegendGraphic&format=image%2Fpng&width=20&height=20&layer=dwd%3AWarnungen_Gemeinden"/>
</LegendURL>

Or short:
Worngly built: https://maps.dwd.de/geoserver/ows?/wms?REQUEST=GetLegendGraphic&amp;VERSION=1.1.0&amp;FORMAT=image/png&amp;LAYER=dwd:Warnungen_Gemeinden&amp;style=stylefile
Correct according to GeoServer; https://maps.dwd.de/geoserver/ows?service=WMS&request=GetLegendGraphic&format=image%2Fpng&width=20&height=20&layer=dwd%3AWarnungen_Gemeinden

Using Multiple LayersLegend with LayerControl

Hello,

I am using your leaflet-geoserver-request code (thank you for providing it !!!!) on a huge bunch of layers that are controled/displayed via a Layer Control.

I would like to open the according Legend each time a Layer is Added to the map.

I tried to use a layerGroup to associate both the WMS layer and it's Legend in a single Layer triggered by the Control Layer, but unfortunately I have a message that states that the "The provided object is not a Layer".

DO you see a work-around to achieve what I tent to do ???

Thank you for your help.

Regards

Raphael

Here is the code I am using :

var norway_map = L.Geoserver.wms('http://15.236.29.28:8080/geoserver/cymo/wms', {
layers: 'cymo:norway_floodmap' ,
format: 'image/png',
transparent: true,});
var norway_Legend = L.Geoserver.legend("http://15.236.29.28:8080/geoserver/cymo/wms", {
layers: "cymo:norway_floodmap",
});
var norway_flood = L.layerGroup([norway_map, norway_Legend]);

the following message appears when I try to display norway_flood.

Layer.js:163 Uncaught Error: The provided object is not a Layer.
at i.addLayer (Layer.js:163)
at i.eachLayer (LayerGroup.js:123)
at i.onAdd (LayerGroup.js:107)
at i._layerAdd (Layer.js:114)
at i.whenReady (Map.js:1465)
at i.addLayer (Layer.js:176)
at i._onInputClick (Control.Layers.js:383)
at HTMLInputElement.c (DomEvent.js:92)

Thank you for your help one more time !!!!!

One issue, two differents messages..

This is my call to L.Geoserver and I have an issue, with two different messages on Chrome and Geoserver
I'm using Leaflet.motion as well

var dataCoords = [];

function doMotion() {
  var seqGroup = L.motion.seq([
    L.motion.polyline(dataCoords, {
    color: "MediumVioletRed",
    weight: 10,
    opacity: 0.8
    }, {
      easing: L.Motion.Ease.easeInOutQuad
    }, {
      removeOnEnd: false,
      icon: L.divIcon({html: "<i class='fa fa-hiking fa-2x' aria-hidden='true'></i>", iconSize: L.point(27.5, 24)})
    }).motionDuration(8000)
  ]).addTo(map);
  seqGroup.on("click", function(){
    seqGroup.motionStart();
  });
  seqGroup.on("dblclick", function(e){
    seqGroup.motionToggle();
  });
  setTimeout(function () {
    seqGroup.motionStart();
  }, 1000);
};



var wfsLayer = L.Geoserver.wfs("https://mappingforyou.eu/geoserver/worldmap/ows", {
 layers: "worldmap:debra",
});


wfsLayer.on('layeradd', function(evt) {
  var coords;
  var geometry = evt.layer.feature.geometry;
  if (geometry.type = 'MultiLineString')
    coords = geometry.coordinates[0];
  else if (geometry.type = 'LineString')
    coords = geometry.coordinates;
  else {
    return;
  }
  coords.forEach(function(coords) {
     dataCoords.push([coords[1], coords[0]]);
  });
  doMotion();
});

Screenshot_22-06-23_11:01:56

Screenshot_22-06-23_11:02:53

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.