"The explosive growth of the GeoWeb and geographic information has made GIS powerful media for the general public to communicate, but perhaps more importantly, GIS have also become media for constructive dialogs and interactions about social issues." - Sui & Goodchild
This is an old revision of the document!
OpenLayers offre un ensemble de fonctionnalités permettant d'interagir avec la carte. Par défaut, les interactions de zoom et pan sont actives mais il en existe d'autres (ol.control). En plus de la gestion événementielle offerte par ces contrôleurs, OpenLayers permet d'enregistrer des écouteurs sur de nombreux événements. Maintenant que les techniques de visualisation cartographique sont maîtrisées, tant côté serveur que client, il s'agit de découvrir comment interagir avec la carte, ses couches et les objets géographiques représentés.
Cet exemple démontre qu'il est nécessaire qu'un gestionnaire s'occupe spécifiquement d'évenements cartographiques comme ceux en lien avec la vie d'un ol.Map ou ceux relatifs à la vie d'une ol.layer.Vector, d'une ol.source.Vector, etc.
<html> <head> <title>ol3 - 8a - ol.ObjectEvent</title> <script type="text/javascript" src="js/config.js"></script> <script type="text/javascript"> var map, vectorLayer; $(document).ready(function () { map = new ol.Map({ view: new ol.View({ center: [0, 0], zoom: 2 }), target: 'map', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ] }); map.on('pointermove', function (e) { $("#info").html( "Pixel:" + e.pixel + " => Lon/Lat: " + ol.proj.transform(e.coordinate,"EPSG:3857","EPSG:4326") ); }); }); </script> <style type="text/css"> #map { width: 100%; height: 100%; } #info { position: absolute; top: 15px; left: 60px; background-color: #fff; padding: 5px; font-size: small; } p { margin: 0; } </style> </head> <body> <div id="map"></div> <div id="info"></div> </body> </html>
TODO
// Set listener on change:center (ol.ObjectEvent) map.getView().on('change:center', function (e) { $("#info").html("Center of the map: " + e.target.getCenter()); });
// May be we set a listener, but which one ? quakeLayer.getSource().on("???", function (e) { // ??? });
Le gestionnaire d'événements cartographiques va notamment servir à déployer la dernière fonctionnalité de base d'une application de webmapping, celle qui consiste à interroger les objets de la carte. Et d'abord en mode image, c'est-à-dire comment avoir des informations (attributaires) sur l'entité géographique que représente un pixel (“cachée derrière” ce pixel) ?
L'application ci-dessous illustre l'interrogation des objets de la carte par le simple clic d'un pixel sur la carte. On enregistre un écouteur sur la map à l'aide de l'habituel ol.Map.on et afin d'exécuter une action au click sur la map. Cette action utilise alors la méthode ol.source.ImageWMS.getGetFeatureInfoUrl qui est un générateur de requête WMS GetFeatureInfo, il construit pour nous l'URL qui permet d'interroger le WMS selon contexte de clic et de visualisation. Enfin, la fonction getFeatureInfo(url) permet de gérer l'aller/retour avec le serveur WMS avec cette URL précédemment construite.
<html> <head> <title>ol3 - 8b - interaction with WMS GetFeatureInfo operation</title> <script type="text/javascript" src="js/config.js"></script> <script type="text/javascript"> var map, wmsLayer, vectors; $(document).ready(function () { map = new ol.Map({ target: 'map', view: new ol.View({ center: ol.proj.transform([6.7, 46.7], 'EPSG:4326', 'EPSG:3857'), zoom: 4 }) }); var wmsLayer = new ol.layer.Image({ source: new ol.source.ImageWMS({ url: "https://demo.boundlessgeo.com/geoserver/opengeo/wms", ratio: 1, params: { VERSION: "1.0.0", LAYERS: "opengeo:countries", FORMAT: "image/png" } }) }); map.addLayer(wmsLayer); map.on('singleclick', function (evt) { var wmsSource = wmsLayer.getSource(); // WMS GetFeatureInfo request generator var url = wmsSource.getGetFeatureInfoUrl( // clic context evt.coordinate, // map context map.getView().getResolution(), "EPSG:3857", { INFO_FORMAT: "text/html" } ); if (url) getFeatureInfo(url); }); }); function getFeatureInfo(url) { var request = $.ajax({ url: url, dataType: "text" }); request.done(function (data) { $("#info").html(data); }); request.fail(function (jqXHR, textStatus) { alert("Request failed: " + textStatus); }); } </script> <style type="text/css"> #map { width: 100%; height: 100%; } #info { position: absolute; top: 20px; left: 60px; background-color: white; border: solid gray 1px; padding: 5px; font-size: smaller; z-index: 1000000000; } </style> </head> <body> <div id="map"></div> <div id="info">Click on the map to get feature info</div> </body> </html>
TODO
INFO_FORMAT: "application/json"
Que se passe-t-il à l'interrogation de la carte ? Regardez le flux client/serveur … et exploitez-le pour n'afficher que le nom de pays.
vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector() }); map.addLayer(vectorLayer);
request.done(function (data) { decoder = new ol.format.GeoJSON(); features = decoder.readFeatures(data); if (features.length > 0) { vectorLayer.getSource().clear(); vectorLayer.getSource().addFeatures(features); $("#info").html(features[0].get("admin") + " (" + features[0].get("income_grp") + ")"); } });
Après les utilisations possibles pour interagir avec les entités représentées par les services cartographiques, voyons comment interagir directement avec les entités vectors “vivantes” client-side. On utulise pour cela le concept d'ol.interaction, notamment ol.interaction.Select.
<html> <head> <title>ol3 - 8c - Interaction sur entités vectors</title> <script type="text/javascript" src="js/config.js"></script> <script type="text/javascript"> var map; $(document).ready(function () { map = new ol.Map({ view: new ol.View({ center: [738600, 5840171], zoom: 3 }), target: 'map', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ] }); vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector({ url: "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson", format: new ol.format.GeoJSON() }) }); map.addLayer(vectorLayer); var selectInteraction = new ol.interaction.Select({ condition: ol.events.condition.singleClick, // the interactive layers on which the selection is possible (they may be more than one) layers: [vectorLayer] }); map.addInteraction(selectInteraction); // add a listener to fire when one or more feature from the interactive layer(s) is(are) selected selectInteraction.on('select', function (e) { if(e.selected.length > 0) { var title = e.selected[0].get("title"); $("#info").html(title); } }); }); </script> <style type="text/css"> #map { width: 100%; height: 100%; } #info { position: absolute; top: 20px; left: 60px; background-color: white; border: solid gray 1px; padding: 5px; font-size: smaller; z-index: 1000000000; } </style> </head> <body> <div id="map"></div> <div id="info">Select on the map to get feature info</div> </body> </html>
TODO