diff options
author | Chris Schlaeger <chris@linux.com> | 2014-08-12 21:56:44 +0200 |
---|---|---|
committer | Chris Schlaeger <chris@linux.com> | 2014-08-12 21:56:44 +0200 |
commit | ea346a785dc1b3f7c156f6fc33da634e1f1a627b (patch) | |
tree | af67530553d20b6e82ad60fd79593e9c4abf5565 /misc/openlayers/lib/OpenLayers/Control | |
parent | 59741cd535c47f25971bf8c32b25da25ceadc6d5 (diff) | |
download | postrunner-ea346a785dc1b3f7c156f6fc33da634e1f1a627b.zip |
Adding jquery, flot and openlayers to be included with the GEM.v0.0.4
Diffstat (limited to 'misc/openlayers/lib/OpenLayers/Control')
45 files changed, 13050 insertions, 0 deletions
diff --git a/misc/openlayers/lib/OpenLayers/Control/ArgParser.js b/misc/openlayers/lib/OpenLayers/Control/ArgParser.js new file mode 100644 index 0000000..6b076f5 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/ArgParser.js @@ -0,0 +1,182 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + + +/** + * @requires OpenLayers/Control.js + */ + +/** + * Class: OpenLayers.Control.ArgParser + * The ArgParser control adds location bar query string parsing functionality + * to an OpenLayers Map. + * When added to a Map control, on a page load/refresh, the Map will + * automatically take the href string and parse it for lon, lat, zoom, and + * layers information. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.ArgParser = OpenLayers.Class(OpenLayers.Control, { + + /** + * Property: center + * {<OpenLayers.LonLat>} + */ + center: null, + + /** + * Property: zoom + * {int} + */ + zoom: null, + + /** + * Property: layers + * {String} Each character represents the state of the corresponding layer + * on the map. + */ + layers: null, + + /** + * APIProperty: displayProjection + * {<OpenLayers.Projection>} Requires proj4js support. + * Projection used when reading the coordinates from the URL. This will + * reproject the map coordinates from the URL into the map's + * projection. + * + * If you are using this functionality, be aware that any permalink + * which is added to the map will determine the coordinate type which + * is read from the URL, which means you should not add permalinks with + * different displayProjections to the same map. + */ + displayProjection: null, + + /** + * Constructor: OpenLayers.Control.ArgParser + * + * Parameters: + * options - {Object} + */ + + /** + * Method: getParameters + */ + getParameters: function(url) { + url = url || window.location.href; + var parameters = OpenLayers.Util.getParameters(url); + + // If we have an anchor in the url use it to split the url + var index = url.indexOf('#'); + if (index > 0) { + // create an url to parse on the getParameters + url = '?' + url.substring(index + 1, url.length); + + OpenLayers.Util.extend(parameters, + OpenLayers.Util.getParameters(url)); + } + return parameters; + }, + + /** + * Method: setMap + * Set the map property for the control. + * + * Parameters: + * map - {<OpenLayers.Map>} + */ + setMap: function(map) { + OpenLayers.Control.prototype.setMap.apply(this, arguments); + + //make sure we dont already have an arg parser attached + for(var i=0, len=this.map.controls.length; i<len; i++) { + var control = this.map.controls[i]; + if ( (control != this) && + (control.CLASS_NAME == "OpenLayers.Control.ArgParser") ) { + + // If a second argparser is added to the map, then we + // override the displayProjection to be the one added to the + // map. + if (control.displayProjection != this.displayProjection) { + this.displayProjection = control.displayProjection; + } + + break; + } + } + if (i == this.map.controls.length) { + + var args = this.getParameters(); + // Be careful to set layer first, to not trigger unnecessary layer loads + if (args.layers) { + this.layers = args.layers; + + // when we add a new layer, set its visibility + this.map.events.register('addlayer', this, + this.configureLayers); + this.configureLayers(); + } + if (args.lat && args.lon) { + this.center = new OpenLayers.LonLat(parseFloat(args.lon), + parseFloat(args.lat)); + if (args.zoom) { + this.zoom = parseFloat(args.zoom); + } + + // when we add a new baselayer to see when we can set the center + this.map.events.register('changebaselayer', this, + this.setCenter); + this.setCenter(); + } + } + }, + + /** + * Method: setCenter + * As soon as a baseLayer has been loaded, we center and zoom + * ...and remove the handler. + */ + setCenter: function() { + + if (this.map.baseLayer) { + //dont need to listen for this one anymore + this.map.events.unregister('changebaselayer', this, + this.setCenter); + + if (this.displayProjection) { + this.center.transform(this.displayProjection, + this.map.getProjectionObject()); + } + + this.map.setCenter(this.center, this.zoom); + } + }, + + /** + * Method: configureLayers + * As soon as all the layers are loaded, cycle through them and + * hide or show them. + */ + configureLayers: function() { + + if (this.layers.length == this.map.layers.length) { + this.map.events.unregister('addlayer', this, this.configureLayers); + + for(var i=0, len=this.layers.length; i<len; i++) { + + var layer = this.map.layers[i]; + var c = this.layers.charAt(i); + + if (c == "B") { + this.map.setBaseLayer(layer); + } else if ( (c == "T") || (c == "F") ) { + layer.setVisibility(c == "T"); + } + } + } + }, + + CLASS_NAME: "OpenLayers.Control.ArgParser" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/Attribution.js b/misc/openlayers/lib/OpenLayers/Control/Attribution.js new file mode 100644 index 0000000..e5ea1ce --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/Attribution.js @@ -0,0 +1,104 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + */ + +/** + * Class: OpenLayers.Control.Attribution + * The attribution control adds attribution from layers to the map display. + * It uses 'attribution' property of each layer. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.Attribution = + OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: separator + * {String} String used to separate layers. + */ + separator: ", ", + + /** + * APIProperty: template + * {String} Template for the attribution. This has to include the substring + * "${layers}", which will be replaced by the layer specific + * attributions, separated by <separator>. The default is "${layers}". + */ + template: "${layers}", + + /** + * Constructor: OpenLayers.Control.Attribution + * + * Parameters: + * options - {Object} Options for control. + */ + + /** + * Method: destroy + * Destroy control. + */ + destroy: function() { + this.map.events.un({ + "removelayer": this.updateAttribution, + "addlayer": this.updateAttribution, + "changelayer": this.updateAttribution, + "changebaselayer": this.updateAttribution, + scope: this + }); + + OpenLayers.Control.prototype.destroy.apply(this, arguments); + }, + + /** + * Method: draw + * Initialize control. + * + * Returns: + * {DOMElement} A reference to the DIV DOMElement containing the control + */ + draw: function() { + OpenLayers.Control.prototype.draw.apply(this, arguments); + + this.map.events.on({ + 'changebaselayer': this.updateAttribution, + 'changelayer': this.updateAttribution, + 'addlayer': this.updateAttribution, + 'removelayer': this.updateAttribution, + scope: this + }); + this.updateAttribution(); + + return this.div; + }, + + /** + * Method: updateAttribution + * Update attribution string. + */ + updateAttribution: function() { + var attributions = []; + if (this.map && this.map.layers) { + for(var i=0, len=this.map.layers.length; i<len; i++) { + var layer = this.map.layers[i]; + if (layer.attribution && layer.getVisibility()) { + // add attribution only if attribution text is unique + if (OpenLayers.Util.indexOf( + attributions, layer.attribution) === -1) { + attributions.push( layer.attribution ); + } + } + } + this.div.innerHTML = OpenLayers.String.format(this.template, { + layers: attributions.join(this.separator) + }); + } + }, + + CLASS_NAME: "OpenLayers.Control.Attribution" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/Button.js b/misc/openlayers/lib/OpenLayers/Control/Button.js new file mode 100644 index 0000000..830df6d --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/Button.js @@ -0,0 +1,44 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + */ + +/** + * Class: OpenLayers.Control.Button + * The Button control is a very simple push-button, for use with + * <OpenLayers.Control.Panel>. + * When clicked, the function trigger() is executed. + * + * Inherits from: + * - <OpenLayers.Control> + * + * Use: + * (code) + * var button = new OpenLayers.Control.Button({ + * displayClass: "MyButton", trigger: myFunction + * }); + * panel.addControls([button]); + * (end) + * + * Will create a button with CSS class MyButtonItemInactive, that + * will call the function MyFunction() when clicked. + */ +OpenLayers.Control.Button = OpenLayers.Class(OpenLayers.Control, { + /** + * Property: type + * {Integer} OpenLayers.Control.TYPE_BUTTON. + */ + type: OpenLayers.Control.TYPE_BUTTON, + + /** + * Method: trigger + * Called by a control panel when the button is clicked. + */ + trigger: function() {}, + + CLASS_NAME: "OpenLayers.Control.Button" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/CacheRead.js b/misc/openlayers/lib/OpenLayers/Control/CacheRead.js new file mode 100644 index 0000000..7768bce --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/CacheRead.js @@ -0,0 +1,156 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + */ + +/** + * Class: OpenLayers.Control.CacheRead + * A control for using image tiles cached with <OpenLayers.Control.CacheWrite> + * from the browser's local storage. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.CacheRead = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: fetchEvent + * {String} The layer event to listen to for replacing remote resource tile + * URLs with cached data URIs. Supported values are "tileerror" (try + * remote first, fall back to cached) and "tileloadstart" (try cache + * first, fall back to remote). Default is "tileloadstart". + * + * Note that "tileerror" will not work for CORS enabled images (see + * https://developer.mozilla.org/en/CORS_Enabled_Image), i.e. layers + * configured with a <OpenLayers.Tile.Image.crossOriginKeyword> in + * <OpenLayers.Layer.Grid.tileOptions>. + */ + fetchEvent: "tileloadstart", + + /** + * APIProperty: layers + * {Array(<OpenLayers.Layer.Grid>)}. Optional. If provided, only these + * layers will receive tiles from the cache. + */ + layers: null, + + /** + * APIProperty: autoActivate + * {Boolean} Activate the control when it is added to a map. Default is + * true. + */ + autoActivate: true, + + /** + * Constructor: OpenLayers.Control.CacheRead + * + * Parameters: + * options - {Object} Object with API properties for this control + */ + + /** + * Method: setMap + * Set the map property for the control. + * + * Parameters: + * map - {<OpenLayers.Map>} + */ + setMap: function(map) { + OpenLayers.Control.prototype.setMap.apply(this, arguments); + var i, layers = this.layers || map.layers; + for (i=layers.length-1; i>=0; --i) { + this.addLayer({layer: layers[i]}); + } + if (!this.layers) { + map.events.on({ + addlayer: this.addLayer, + removeLayer: this.removeLayer, + scope: this + }); + } + }, + + /** + * Method: addLayer + * Adds a layer to the control. Once added, tiles requested for this layer + * will be cached. + * + * Parameters: + * evt - {Object} Object with a layer property referencing an + * <OpenLayers.Layer> instance + */ + addLayer: function(evt) { + evt.layer.events.register(this.fetchEvent, this, this.fetch); + }, + + /** + * Method: removeLayer + * Removes a layer from the control. Once removed, tiles requested for this + * layer will no longer be cached. + * + * Parameters: + * evt - {Object} Object with a layer property referencing an + * <OpenLayers.Layer> instance + */ + removeLayer: function(evt) { + evt.layer.events.unregister(this.fetchEvent, this, this.fetch); + }, + + /** + * Method: fetch + * Listener to the <fetchEvent> event. Replaces a tile's url with a data + * URI from the cache. + * + * Parameters: + * evt - {Object} Event object with a tile property. + */ + fetch: function(evt) { + if (this.active && window.localStorage && + evt.tile instanceof OpenLayers.Tile.Image) { + var tile = evt.tile, + url = tile.url; + // deal with modified tile urls when both CacheWrite and CacheRead + // are active + if (!tile.layer.crossOriginKeyword && OpenLayers.ProxyHost && + url.indexOf(OpenLayers.ProxyHost) === 0) { + url = OpenLayers.Control.CacheWrite.urlMap[url]; + } + var dataURI = window.localStorage.getItem("olCache_" + url); + if (dataURI) { + tile.url = dataURI; + if (evt.type === "tileerror") { + tile.setImgSrc(dataURI); + } + } + } + }, + + /** + * Method: destroy + * The destroy method is used to perform any clean up before the control + * is dereferenced. Typically this is where event listeners are removed + * to prevent memory leaks. + */ + destroy: function() { + if (this.layers || this.map) { + var i, layers = this.layers || this.map.layers; + for (i=layers.length-1; i>=0; --i) { + this.removeLayer({layer: layers[i]}); + } + } + if (this.map) { + this.map.events.un({ + addlayer: this.addLayer, + removeLayer: this.removeLayer, + scope: this + }); + } + OpenLayers.Control.prototype.destroy.apply(this, arguments); + }, + + CLASS_NAME: "OpenLayers.Control.CacheRead" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/CacheWrite.js b/misc/openlayers/lib/OpenLayers/Control/CacheWrite.js new file mode 100644 index 0000000..3d4ecf5 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/CacheWrite.js @@ -0,0 +1,257 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Request.js + * @requires OpenLayers/Console.js + */ + +/** + * Class: OpenLayers.Control.CacheWrite + * A control for caching image tiles in the browser's local storage. The + * <OpenLayers.Control.CacheRead> control is used to fetch and use the cached + * tile images. + * + * Note: Before using this control on any layer that is not your own, make sure + * that the terms of service of the tile provider allow local storage of tiles. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.CacheWrite = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: events + * {<OpenLayers.Events>} Events instance for listeners and triggering + * control specific events. + * + * To register events in the constructor, configure <eventListeners>. + * + * Register a listener for a particular event with the following syntax: + * (code) + * control.events.register(type, obj, listener); + * (end) + * + * Supported event types (in addition to those from <OpenLayers.Control.events>): + * cachefull - Triggered when the cache is full. Listeners receive an + * object with a tile property as first argument. The tile references + * the tile that couldn't be cached. + */ + + /** + * APIProperty: eventListeners + * {Object} Object with event listeners, keyed by event name. An optional + * scope property defines the scope that listeners will be executed in. + */ + + /** + * APIProperty: layers + * {Array(<OpenLayers.Layer.Grid>)}. Optional. If provided, caching + * will be enabled for these layers only, otherwise for all cacheable + * layers. + */ + layers: null, + + /** + * APIProperty: imageFormat + * {String} The image format used for caching. The default is "image/png". + * Supported formats depend on the user agent. If an unsupported + * <imageFormat> is provided, "image/png" will be used. For aerial + * imagery, "image/jpeg" is recommended. + */ + imageFormat: "image/png", + + /** + * Property: quotaRegEx + * {RegExp} + */ + quotaRegEx: (/quota/i), + + /** + * Constructor: OpenLayers.Control.CacheWrite + * + * Parameters: + * options - {Object} Object with API properties for this control. + */ + + /** + * Method: setMap + * Set the map property for the control. + * + * Parameters: + * map - {<OpenLayers.Map>} + */ + setMap: function(map) { + OpenLayers.Control.prototype.setMap.apply(this, arguments); + var i, layers = this.layers || map.layers; + for (i=layers.length-1; i>=0; --i) { + this.addLayer({layer: layers[i]}); + } + if (!this.layers) { + map.events.on({ + addlayer: this.addLayer, + removeLayer: this.removeLayer, + scope: this + }); + } + }, + + /** + * Method: addLayer + * Adds a layer to the control. Once added, tiles requested for this layer + * will be cached. + * + * Parameters: + * evt - {Object} Object with a layer property referencing an + * <OpenLayers.Layer> instance + */ + addLayer: function(evt) { + evt.layer.events.on({ + tileloadstart: this.makeSameOrigin, + tileloaded: this.onTileLoaded, + scope: this + }); + }, + + /** + * Method: removeLayer + * Removes a layer from the control. Once removed, tiles requested for this + * layer will no longer be cached. + * + * Parameters: + * evt - {Object} Object with a layer property referencing an + * <OpenLayers.Layer> instance + */ + removeLayer: function(evt) { + evt.layer.events.un({ + tileloadstart: this.makeSameOrigin, + tileloaded: this.onTileLoaded, + scope: this + }); + }, + + /** + * Method: makeSameOrigin + * If the tile does not have CORS image loading enabled and is from a + * different origin, use OpenLayers.ProxyHost to make it a same origin url. + * + * Parameters: + * evt - {<OpenLayers.Event>} + */ + makeSameOrigin: function(evt) { + if (this.active) { + var tile = evt.tile; + if (tile instanceof OpenLayers.Tile.Image && + !tile.crossOriginKeyword && + tile.url.substr(0, 5) !== "data:") { + var sameOriginUrl = OpenLayers.Request.makeSameOrigin( + tile.url, OpenLayers.ProxyHost + ); + OpenLayers.Control.CacheWrite.urlMap[sameOriginUrl] = tile.url; + tile.url = sameOriginUrl; + } + } + }, + + /** + * Method: onTileLoaded + * Decides whether a tile can be cached and calls the cache method. + * + * Parameters: + * evt - {Event} + */ + onTileLoaded: function(evt) { + if (this.active && !evt.aborted && + evt.tile instanceof OpenLayers.Tile.Image && + evt.tile.url.substr(0, 5) !== 'data:') { + this.cache({tile: evt.tile}); + delete OpenLayers.Control.CacheWrite.urlMap[evt.tile.url]; + } + }, + + /** + * Method: cache + * Adds a tile to the cache. When the cache is full, the "cachefull" event + * is triggered. + * + * Parameters: + * obj - {Object} Object with a tile property, tile being the + * <OpenLayers.Tile.Image> with the data to add to the cache + */ + cache: function(obj) { + if (window.localStorage) { + var tile = obj.tile; + try { + var canvasContext = tile.getCanvasContext(); + if (canvasContext) { + var urlMap = OpenLayers.Control.CacheWrite.urlMap; + var url = urlMap[tile.url] || tile.url; + window.localStorage.setItem( + "olCache_" + url, + canvasContext.canvas.toDataURL(this.imageFormat) + ); + } + } catch(e) { + // local storage full or CORS violation + var reason = e.name || e.message; + if (reason && this.quotaRegEx.test(reason)) { + this.events.triggerEvent("cachefull", {tile: tile}); + } else { + OpenLayers.Console.error(e.toString()); + } + } + } + }, + + /** + * Method: destroy + * The destroy method is used to perform any clean up before the control + * is dereferenced. Typically this is where event listeners are removed + * to prevent memory leaks. + */ + destroy: function() { + if (this.layers || this.map) { + var i, layers = this.layers || this.map.layers; + for (i=layers.length-1; i>=0; --i) { + this.removeLayer({layer: layers[i]}); + } + } + if (this.map) { + this.map.events.un({ + addlayer: this.addLayer, + removeLayer: this.removeLayer, + scope: this + }); + } + OpenLayers.Control.prototype.destroy.apply(this, arguments); + }, + + CLASS_NAME: "OpenLayers.Control.CacheWrite" +}); + +/** + * APIFunction: OpenLayers.Control.CacheWrite.clearCache + * Clears all tiles cached with <OpenLayers.Control.CacheWrite> from the cache. + */ +OpenLayers.Control.CacheWrite.clearCache = function() { + if (!window.localStorage) { return; } + var i, key; + for (i=window.localStorage.length-1; i>=0; --i) { + key = window.localStorage.key(i); + if (key.substr(0, 8) === "olCache_") { + window.localStorage.removeItem(key); + } + } +}; + +/** + * Property: OpenLayers.Control.CacheWrite.urlMap + * {Object} Mapping of same origin urls to cache url keys. Entries will be + * deleted as soon as a tile was cached. + */ +OpenLayers.Control.CacheWrite.urlMap = {}; + + diff --git a/misc/openlayers/lib/OpenLayers/Control/DragFeature.js b/misc/openlayers/lib/OpenLayers/Control/DragFeature.js new file mode 100644 index 0000000..d8fb15f --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/DragFeature.js @@ -0,0 +1,366 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Handler/Drag.js + * @requires OpenLayers/Handler/Feature.js + */ + +/** + * Class: OpenLayers.Control.DragFeature + * The DragFeature control moves a feature with a drag of the mouse. Create a + * new control with the <OpenLayers.Control.DragFeature> constructor. + * + * Inherits From: + * - <OpenLayers.Control> + */ +OpenLayers.Control.DragFeature = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: geometryTypes + * {Array(String)} To restrict dragging to a limited set of geometry types, + * send a list of strings corresponding to the geometry class names. + */ + geometryTypes: null, + + /** + * APIProperty: onStart + * {Function} Define this function if you want to know when a drag starts. + * The function should expect to receive two arguments: the feature + * that is about to be dragged and the pixel location of the mouse. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} The feature that is about to be + * dragged. + * pixel - {<OpenLayers.Pixel>} The pixel location of the mouse. + */ + onStart: function(feature, pixel) {}, + + /** + * APIProperty: onDrag + * {Function} Define this function if you want to know about each move of a + * feature. The function should expect to receive two arguments: the + * feature that is being dragged and the pixel location of the mouse. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} The feature that was dragged. + * pixel - {<OpenLayers.Pixel>} The pixel location of the mouse. + */ + onDrag: function(feature, pixel) {}, + + /** + * APIProperty: onComplete + * {Function} Define this function if you want to know when a feature is + * done dragging. The function should expect to receive two arguments: + * the feature that is being dragged and the pixel location of the + * mouse. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} The feature that was dragged. + * pixel - {<OpenLayers.Pixel>} The pixel location of the mouse. + */ + onComplete: function(feature, pixel) {}, + + /** + * APIProperty: onEnter + * {Function} Define this function if you want to know when the mouse + * goes over a feature and thereby makes this feature a candidate + * for dragging. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} The feature that is ready + * to be dragged. + */ + onEnter: function(feature) {}, + + /** + * APIProperty: onLeave + * {Function} Define this function if you want to know when the mouse + * goes out of the feature that was dragged. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} The feature that was dragged. + */ + onLeave: function(feature) {}, + + /** + * APIProperty: documentDrag + * {Boolean} If set to true, mouse dragging will continue even if the + * mouse cursor leaves the map viewport. Default is false. + */ + documentDrag: false, + + /** + * Property: layer + * {<OpenLayers.Layer.Vector>} + */ + layer: null, + + /** + * Property: feature + * {<OpenLayers.Feature.Vector>} + */ + feature: null, + + /** + * Property: dragCallbacks + * {Object} The functions that are sent to the drag handler for callback. + */ + dragCallbacks: {}, + + /** + * Property: featureCallbacks + * {Object} The functions that are sent to the feature handler for callback. + */ + featureCallbacks: {}, + + /** + * Property: lastPixel + * {<OpenLayers.Pixel>} + */ + lastPixel: null, + + /** + * Constructor: OpenLayers.Control.DragFeature + * Create a new control to drag features. + * + * Parameters: + * layer - {<OpenLayers.Layer.Vector>} The layer containing features to be + * dragged. + * options - {Object} Optional object whose properties will be set on the + * control. + */ + initialize: function(layer, options) { + OpenLayers.Control.prototype.initialize.apply(this, [options]); + this.layer = layer; + this.handlers = { + drag: new OpenLayers.Handler.Drag( + this, OpenLayers.Util.extend({ + down: this.downFeature, + move: this.moveFeature, + up: this.upFeature, + out: this.cancel, + done: this.doneDragging + }, this.dragCallbacks), { + documentDrag: this.documentDrag + } + ), + feature: new OpenLayers.Handler.Feature( + this, this.layer, OpenLayers.Util.extend({ + // 'click' and 'clickout' callback are for the mobile + // support: no 'over' or 'out' in touch based browsers. + click: this.clickFeature, + clickout: this.clickoutFeature, + over: this.overFeature, + out: this.outFeature + }, this.featureCallbacks), + {geometryTypes: this.geometryTypes} + ) + }; + }, + + /** + * Method: clickFeature + * Called when the feature handler detects a click-in on a feature. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} + */ + clickFeature: function(feature) { + if (this.handlers.feature.touch && !this.over && this.overFeature(feature)) { + this.handlers.drag.dragstart(this.handlers.feature.evt); + // to let the events propagate to the feature handler (click callback) + this.handlers.drag.stopDown = false; + } + }, + + /** + * Method: clickoutFeature + * Called when the feature handler detects a click-out on a feature. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} + */ + clickoutFeature: function(feature) { + if (this.handlers.feature.touch && this.over) { + this.outFeature(feature); + this.handlers.drag.stopDown = true; + } + }, + + /** + * APIMethod: destroy + * Take care of things that are not handled in superclass + */ + destroy: function() { + this.layer = null; + OpenLayers.Control.prototype.destroy.apply(this, []); + }, + + /** + * APIMethod: activate + * Activate the control and the feature handler. + * + * Returns: + * {Boolean} Successfully activated the control and feature handler. + */ + activate: function() { + return (this.handlers.feature.activate() && + OpenLayers.Control.prototype.activate.apply(this, arguments)); + }, + + /** + * APIMethod: deactivate + * Deactivate the control and all handlers. + * + * Returns: + * {Boolean} Successfully deactivated the control. + */ + deactivate: function() { + // the return from the handlers is unimportant in this case + this.handlers.drag.deactivate(); + this.handlers.feature.deactivate(); + this.feature = null; + this.dragging = false; + this.lastPixel = null; + OpenLayers.Element.removeClass( + this.map.viewPortDiv, this.displayClass + "Over" + ); + return OpenLayers.Control.prototype.deactivate.apply(this, arguments); + }, + + /** + * Method: overFeature + * Called when the feature handler detects a mouse-over on a feature. + * This activates the drag handler. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} The selected feature. + * + * Returns: + * {Boolean} Successfully activated the drag handler. + */ + overFeature: function(feature) { + var activated = false; + if(!this.handlers.drag.dragging) { + this.feature = feature; + this.handlers.drag.activate(); + activated = true; + this.over = true; + OpenLayers.Element.addClass(this.map.viewPortDiv, this.displayClass + "Over"); + this.onEnter(feature); + } else { + if(this.feature.id == feature.id) { + this.over = true; + } else { + this.over = false; + } + } + return activated; + }, + + /** + * Method: downFeature + * Called when the drag handler detects a mouse-down. + * + * Parameters: + * pixel - {<OpenLayers.Pixel>} Location of the mouse event. + */ + downFeature: function(pixel) { + this.lastPixel = pixel; + this.onStart(this.feature, pixel); + }, + + /** + * Method: moveFeature + * Called when the drag handler detects a mouse-move. Also calls the + * optional onDrag method. + * + * Parameters: + * pixel - {<OpenLayers.Pixel>} Location of the mouse event. + */ + moveFeature: function(pixel) { + var res = this.map.getResolution(); + this.feature.geometry.move(res * (pixel.x - this.lastPixel.x), + res * (this.lastPixel.y - pixel.y)); + this.layer.drawFeature(this.feature); + this.lastPixel = pixel; + this.onDrag(this.feature, pixel); + }, + + /** + * Method: upFeature + * Called when the drag handler detects a mouse-up. + * + * Parameters: + * pixel - {<OpenLayers.Pixel>} Location of the mouse event. + */ + upFeature: function(pixel) { + if(!this.over) { + this.handlers.drag.deactivate(); + } + }, + + /** + * Method: doneDragging + * Called when the drag handler is done dragging. + * + * Parameters: + * pixel - {<OpenLayers.Pixel>} The last event pixel location. If this event + * came from a mouseout, this may not be in the map viewport. + */ + doneDragging: function(pixel) { + this.onComplete(this.feature, pixel); + }, + + /** + * Method: outFeature + * Called when the feature handler detects a mouse-out on a feature. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} The feature that the mouse left. + */ + outFeature: function(feature) { + if(!this.handlers.drag.dragging) { + this.over = false; + this.handlers.drag.deactivate(); + OpenLayers.Element.removeClass( + this.map.viewPortDiv, this.displayClass + "Over" + ); + this.onLeave(feature); + this.feature = null; + } else { + if(this.feature.id == feature.id) { + this.over = false; + } + } + }, + + /** + * Method: cancel + * Called when the drag handler detects a mouse-out (from the map viewport). + */ + cancel: function() { + this.handlers.drag.deactivate(); + this.over = false; + }, + + /** + * Method: setMap + * Set the map property for the control and all handlers. + * + * Parameters: + * map - {<OpenLayers.Map>} The control's map. + */ + setMap: function(map) { + this.handlers.drag.setMap(map); + this.handlers.feature.setMap(map); + OpenLayers.Control.prototype.setMap.apply(this, arguments); + }, + + CLASS_NAME: "OpenLayers.Control.DragFeature" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/DragPan.js b/misc/openlayers/lib/OpenLayers/Control/DragPan.js new file mode 100644 index 0000000..981a649 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/DragPan.js @@ -0,0 +1,156 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Handler/Drag.js + */ + +/** + * Class: OpenLayers.Control.DragPan + * The DragPan control pans the map with a drag of the mouse. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.DragPan = OpenLayers.Class(OpenLayers.Control, { + + /** + * Property: type + * {OpenLayers.Control.TYPES} + */ + type: OpenLayers.Control.TYPE_TOOL, + + /** + * Property: panned + * {Boolean} The map moved. + */ + panned: false, + + /** + * Property: interval + * {Integer} The number of milliseconds that should ellapse before + * panning the map again. Defaults to 0 milliseconds, which means that + * no separate cycle is used for panning. In most cases you won't want + * to change this value. For slow machines/devices larger values can be + * tried out. + */ + interval: 0, + + /** + * APIProperty: documentDrag + * {Boolean} If set to true, mouse dragging will continue even if the + * mouse cursor leaves the map viewport. Default is false. + */ + documentDrag: false, + + /** + * Property: kinetic + * {<OpenLayers.Kinetic>} The OpenLayers.Kinetic object. + */ + kinetic: null, + + /** + * APIProperty: enableKinetic + * {Boolean} Set this option to enable "kinetic dragging". Can be + * set to true or to an object. If set to an object this + * object will be passed to the {<OpenLayers.Kinetic>} + * constructor. Defaults to true. + * To get kinetic dragging, ensure that OpenLayers/Kinetic.js is + * included in your build config. + */ + enableKinetic: true, + + /** + * APIProperty: kineticInterval + * {Integer} Interval in milliseconds between 2 steps in the "kinetic + * scrolling". Applies only if enableKinetic is set. Defaults + * to 10 milliseconds. + */ + kineticInterval: 10, + + + /** + * Method: draw + * Creates a Drag handler, using <panMap> and + * <panMapDone> as callbacks. + */ + draw: function() { + if (this.enableKinetic && OpenLayers.Kinetic) { + var config = {interval: this.kineticInterval}; + if(typeof this.enableKinetic === "object") { + config = OpenLayers.Util.extend(config, this.enableKinetic); + } + this.kinetic = new OpenLayers.Kinetic(config); + } + this.handler = new OpenLayers.Handler.Drag(this, { + "move": this.panMap, + "done": this.panMapDone, + "down": this.panMapStart + }, { + interval: this.interval, + documentDrag: this.documentDrag + } + ); + }, + + /** + * Method: panMapStart + */ + panMapStart: function() { + if(this.kinetic) { + this.kinetic.begin(); + } + }, + + /** + * Method: panMap + * + * Parameters: + * xy - {<OpenLayers.Pixel>} Pixel of the mouse position + */ + panMap: function(xy) { + if(this.kinetic) { + this.kinetic.update(xy); + } + this.panned = true; + this.map.pan( + this.handler.last.x - xy.x, + this.handler.last.y - xy.y, + {dragging: true, animate: false} + ); + }, + + /** + * Method: panMapDone + * Finish the panning operation. Only call setCenter (through <panMap>) + * if the map has actually been moved. + * + * Parameters: + * xy - {<OpenLayers.Pixel>} Pixel of the mouse position + */ + panMapDone: function(xy) { + if(this.panned) { + var res = null; + if (this.kinetic) { + res = this.kinetic.end(xy); + } + this.map.pan( + this.handler.last.x - xy.x, + this.handler.last.y - xy.y, + {dragging: !!res, animate: false} + ); + if (res) { + var self = this; + this.kinetic.move(res, function(x, y, end) { + self.map.pan(x, y, {dragging: !end, animate: false}); + }); + } + this.panned = false; + } + }, + + CLASS_NAME: "OpenLayers.Control.DragPan" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/DrawFeature.js b/misc/openlayers/lib/OpenLayers/Control/DrawFeature.js new file mode 100644 index 0000000..b0afc71 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/DrawFeature.js @@ -0,0 +1,229 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Feature/Vector.js + */ + +/** + * Class: OpenLayers.Control.DrawFeature + * The DrawFeature control draws point, line or polygon features on a vector + * layer when active. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.DrawFeature = OpenLayers.Class(OpenLayers.Control, { + + /** + * Property: layer + * {<OpenLayers.Layer.Vector>} + */ + layer: null, + + /** + * Property: callbacks + * {Object} The functions that are sent to the handler for callback + */ + callbacks: null, + + /** + * APIProperty: events + * {<OpenLayers.Events>} Events instance for listeners and triggering + * control specific events. + * + * Register a listener for a particular event with the following syntax: + * (code) + * control.events.register(type, obj, listener); + * (end) + * + * Supported event types (in addition to those from <OpenLayers.Control.events>): + * featureadded - Triggered when a feature is added + */ + + /** + * APIProperty: multi + * {Boolean} Cast features to multi-part geometries before passing to the + * layer. Default is false. + */ + multi: false, + + /** + * APIProperty: featureAdded + * {Function} Called after each feature is added + */ + featureAdded: function() {}, + + /** + * APIProperty: handlerOptions + * {Object} Used to set non-default properties on the control's handler + */ + + /** + * Constructor: OpenLayers.Control.DrawFeature + * + * Parameters: + * layer - {<OpenLayers.Layer.Vector>} + * handler - {<OpenLayers.Handler>} + * options - {Object} + */ + initialize: function(layer, handler, options) { + OpenLayers.Control.prototype.initialize.apply(this, [options]); + this.callbacks = OpenLayers.Util.extend( + { + done: this.drawFeature, + modify: function(vertex, feature) { + this.layer.events.triggerEvent( + "sketchmodified", {vertex: vertex, feature: feature} + ); + }, + create: function(vertex, feature) { + this.layer.events.triggerEvent( + "sketchstarted", {vertex: vertex, feature: feature} + ); + } + }, + this.callbacks + ); + this.layer = layer; + this.handlerOptions = this.handlerOptions || {}; + this.handlerOptions.layerOptions = OpenLayers.Util.applyDefaults( + this.handlerOptions.layerOptions, { + renderers: layer.renderers, rendererOptions: layer.rendererOptions + } + ); + if (!("multi" in this.handlerOptions)) { + this.handlerOptions.multi = this.multi; + } + var sketchStyle = this.layer.styleMap && this.layer.styleMap.styles.temporary; + if(sketchStyle) { + this.handlerOptions.layerOptions = OpenLayers.Util.applyDefaults( + this.handlerOptions.layerOptions, + {styleMap: new OpenLayers.StyleMap({"default": sketchStyle})} + ); + } + this.handler = new handler(this, this.callbacks, this.handlerOptions); + }, + + /** + * Method: drawFeature + */ + drawFeature: function(geometry) { + var feature = new OpenLayers.Feature.Vector(geometry); + var proceed = this.layer.events.triggerEvent( + "sketchcomplete", {feature: feature} + ); + if(proceed !== false) { + feature.state = OpenLayers.State.INSERT; + this.layer.addFeatures([feature]); + this.featureAdded(feature); + this.events.triggerEvent("featureadded",{feature : feature}); + } + }, + + /** + * APIMethod: insertXY + * Insert a point in the current sketch given x & y coordinates. + * + * Parameters: + * x - {Number} The x-coordinate of the point. + * y - {Number} The y-coordinate of the point. + */ + insertXY: function(x, y) { + if (this.handler && this.handler.line) { + this.handler.insertXY(x, y); + } + }, + + /** + * APIMethod: insertDeltaXY + * Insert a point given offsets from the previously inserted point. + * + * Parameters: + * dx - {Number} The x-coordinate offset of the point. + * dy - {Number} The y-coordinate offset of the point. + */ + insertDeltaXY: function(dx, dy) { + if (this.handler && this.handler.line) { + this.handler.insertDeltaXY(dx, dy); + } + }, + + /** + * APIMethod: insertDirectionLength + * Insert a point in the current sketch given a direction and a length. + * + * Parameters: + * direction - {Number} Degrees clockwise from the positive x-axis. + * length - {Number} Distance from the previously drawn point. + */ + insertDirectionLength: function(direction, length) { + if (this.handler && this.handler.line) { + this.handler.insertDirectionLength(direction, length); + } + }, + + /** + * APIMethod: insertDeflectionLength + * Insert a point in the current sketch given a deflection and a length. + * The deflection should be degrees clockwise from the previously + * digitized segment. + * + * Parameters: + * deflection - {Number} Degrees clockwise from the previous segment. + * length - {Number} Distance from the previously drawn point. + */ + insertDeflectionLength: function(deflection, length) { + if (this.handler && this.handler.line) { + this.handler.insertDeflectionLength(deflection, length); + } + }, + + /** + * APIMethod: undo + * Remove the most recently added point in the current sketch geometry. + * + * Returns: + * {Boolean} An edit was undone. + */ + undo: function() { + return this.handler.undo && this.handler.undo(); + }, + + /** + * APIMethod: redo + * Reinsert the most recently removed point resulting from an <undo> call. + * The undo stack is deleted whenever a point is added by other means. + * + * Returns: + * {Boolean} An edit was redone. + */ + redo: function() { + return this.handler.redo && this.handler.redo(); + }, + + /** + * APIMethod: finishSketch + * Finishes the sketch without including the currently drawn point. + * This method can be called to terminate drawing programmatically + * instead of waiting for the user to end the sketch. + */ + finishSketch: function() { + this.handler.finishGeometry(); + }, + + /** + * APIMethod: cancel + * Cancel the current sketch. This removes the current sketch and keeps + * the drawing control active. + */ + cancel: function() { + this.handler.cancel(); + }, + + CLASS_NAME: "OpenLayers.Control.DrawFeature" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/EditingToolbar.js b/misc/openlayers/lib/OpenLayers/Control/EditingToolbar.js new file mode 100644 index 0000000..ba7ca40 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/EditingToolbar.js @@ -0,0 +1,81 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control/Panel.js + * @requires OpenLayers/Control/Navigation.js + * @requires OpenLayers/Control/DrawFeature.js + * @requires OpenLayers/Handler/Point.js + * @requires OpenLayers/Handler/Path.js + * @requires OpenLayers/Handler/Polygon.js + */ + +/** + * Class: OpenLayers.Control.EditingToolbar + * The EditingToolbar is a panel of 4 controls to draw polygons, lines, + * points, or to navigate the map by panning. By default it appears in the + * upper right corner of the map. + * + * Inherits from: + * - <OpenLayers.Control.Panel> + */ +OpenLayers.Control.EditingToolbar = OpenLayers.Class( + OpenLayers.Control.Panel, { + + /** + * APIProperty: citeCompliant + * {Boolean} If set to true, coordinates of features drawn in a map extent + * crossing the date line won't exceed the world bounds. Default is false. + */ + citeCompliant: false, + + /** + * Constructor: OpenLayers.Control.EditingToolbar + * Create an editing toolbar for a given layer. + * + * Parameters: + * layer - {<OpenLayers.Layer.Vector>} + * options - {Object} + */ + initialize: function(layer, options) { + OpenLayers.Control.Panel.prototype.initialize.apply(this, [options]); + + this.addControls( + [ new OpenLayers.Control.Navigation() ] + ); + var controls = [ + new OpenLayers.Control.DrawFeature(layer, OpenLayers.Handler.Point, { + displayClass: 'olControlDrawFeaturePoint', + handlerOptions: {citeCompliant: this.citeCompliant} + }), + new OpenLayers.Control.DrawFeature(layer, OpenLayers.Handler.Path, { + displayClass: 'olControlDrawFeaturePath', + handlerOptions: {citeCompliant: this.citeCompliant} + }), + new OpenLayers.Control.DrawFeature(layer, OpenLayers.Handler.Polygon, { + displayClass: 'olControlDrawFeaturePolygon', + handlerOptions: {citeCompliant: this.citeCompliant} + }) + ]; + this.addControls(controls); + }, + + /** + * Method: draw + * calls the default draw, and then activates mouse defaults. + * + * Returns: + * {DOMElement} + */ + draw: function() { + var div = OpenLayers.Control.Panel.prototype.draw.apply(this, arguments); + if (this.defaultControl === null) { + this.defaultControl = this.controls[0]; + } + return div; + }, + + CLASS_NAME: "OpenLayers.Control.EditingToolbar" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/Geolocate.js b/misc/openlayers/lib/OpenLayers/Control/Geolocate.js new file mode 100644 index 0000000..4b5b439 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/Geolocate.js @@ -0,0 +1,192 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Geometry/Point.js + * @requires OpenLayers/Projection.js + */ + +/** + * Class: OpenLayers.Control.Geolocate + * The Geolocate control wraps w3c geolocation API into control that can be + * bound to a map, and generate events on location update + * + * To use this control requires to load the proj4js library if the projection + * of the map is not EPSG:4326 or EPSG:900913. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.Geolocate = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: events + * {<OpenLayers.Events>} Events instance for listeners and triggering + * control specific events. + * + * Register a listener for a particular event with the following syntax: + * (code) + * control.events.register(type, obj, listener); + * (end) + * + * Supported event types (in addition to those from <OpenLayers.Control.events>): + * locationupdated - Triggered when browser return a new position. Listeners will + * receive an object with a 'position' property which is the browser.geolocation.position + * native object, as well as a 'point' property which is the location transformed in the + * current map projection. + * locationfailed - Triggered when geolocation has failed + * locationuncapable - Triggered when control is activated on a browser + * which doesn't support geolocation + */ + + /** + * Property: geolocation + * {Object} The geolocation engine, as a property to be possibly mocked. + * This is set lazily to avoid a memory leak in IE9. + */ + geolocation: null, + + /** + * Property: available + * {Boolean} The navigator.geolocation object is available. + */ + available: ('geolocation' in navigator), + + /** + * APIProperty: bind + * {Boolean} If true, map center will be set on location update. + */ + bind: true, + + /** + * APIProperty: watch + * {Boolean} If true, position will be update regularly. + */ + watch: false, + + /** + * APIProperty: geolocationOptions + * {Object} Options to pass to the navigator's geolocation API. See + * <http://dev.w3.org/geo/api/spec-source.html>. No specific + * option is passed to the geolocation API by default. + */ + geolocationOptions: null, + + /** + * Constructor: OpenLayers.Control.Geolocate + * Create a new control to deal with browser geolocation API + * + */ + + /** + * Method: destroy + */ + destroy: function() { + this.deactivate(); + OpenLayers.Control.prototype.destroy.apply(this, arguments); + }, + + /** + * Method: activate + * Activates the control. + * + * Returns: + * {Boolean} The control was effectively activated. + */ + activate: function () { + if (this.available && !this.geolocation) { + // set lazily to avoid IE9 memory leak + this.geolocation = navigator.geolocation; + } + if (!this.geolocation) { + this.events.triggerEvent("locationuncapable"); + return false; + } + if (OpenLayers.Control.prototype.activate.apply(this, arguments)) { + if (this.watch) { + this.watchId = this.geolocation.watchPosition( + OpenLayers.Function.bind(this.geolocate, this), + OpenLayers.Function.bind(this.failure, this), + this.geolocationOptions + ); + } else { + this.getCurrentLocation(); + } + return true; + } + return false; + }, + + /** + * Method: deactivate + * Deactivates the control. + * + * Returns: + * {Boolean} The control was effectively deactivated. + */ + deactivate: function () { + if (this.active && this.watchId !== null) { + this.geolocation.clearWatch(this.watchId); + } + return OpenLayers.Control.prototype.deactivate.apply( + this, arguments + ); + }, + + /** + * Method: geolocate + * Activates the control. + * + */ + geolocate: function (position) { + var center = new OpenLayers.LonLat( + position.coords.longitude, + position.coords.latitude + ).transform( + new OpenLayers.Projection("EPSG:4326"), + this.map.getProjectionObject() + ); + if (this.bind) { + this.map.setCenter(center); + } + this.events.triggerEvent("locationupdated", { + position: position, + point: new OpenLayers.Geometry.Point( + center.lon, center.lat + ) + }); + }, + + /** + * APIMethod: getCurrentLocation + * + * Returns: + * {Boolean} Returns true if a event will be fired (successfull + * registration) + */ + getCurrentLocation: function() { + if (!this.active || this.watch) { + return false; + } + this.geolocation.getCurrentPosition( + OpenLayers.Function.bind(this.geolocate, this), + OpenLayers.Function.bind(this.failure, this), + this.geolocationOptions + ); + return true; + }, + + /** + * Method: failure + * method called on browser's geolocation failure + * + */ + failure: function (error) { + this.events.triggerEvent("locationfailed", {error: error}); + }, + + CLASS_NAME: "OpenLayers.Control.Geolocate" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/GetFeature.js b/misc/openlayers/lib/OpenLayers/Control/GetFeature.js new file mode 100644 index 0000000..144e87f --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/GetFeature.js @@ -0,0 +1,597 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Handler/Click.js + * @requires OpenLayers/Handler/Box.js + * @requires OpenLayers/Handler/Hover.js + * @requires OpenLayers/Filter/Spatial.js + */ + +/** + * Class: OpenLayers.Control.GetFeature + * Gets vector features for locations underneath the mouse cursor. Can be + * configured to act on click, hover or dragged boxes. Uses an + * <OpenLayers.Protocol> that supports spatial filters to retrieve + * features from a server and fires events that notify applications of the + * selected features. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.GetFeature = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: protocol + * {<OpenLayers.Protocol>} Required. The protocol used for fetching + * features. + */ + protocol: null, + + /** + * APIProperty: multipleKey + * {String} An event modifier ('altKey' or 'shiftKey') that temporarily sets + * the <multiple> property to true. Default is null. + */ + multipleKey: null, + + /** + * APIProperty: toggleKey + * {String} An event modifier ('altKey' or 'shiftKey') that temporarily sets + * the <toggle> property to true. Default is null. + */ + toggleKey: null, + + /** + * Property: modifiers + * {Object} The event modifiers to use, according to the current event + * being handled by this control's handlers + */ + modifiers: null, + + /** + * APIProperty: multiple + * {Boolean} Allow selection of multiple geometries. Default is false. + */ + multiple: false, + + /** + * APIProperty: click + * {Boolean} Use a click handler for selecting/unselecting features. If + * both <click> and <box> are set to true, the click handler takes + * precedence over the box handler if a box with zero extent was + * selected. Default is true. + */ + click: true, + + /** + * APIProperty: single + * {Boolean} Tells whether select by click should select a single + * feature. If set to false, all matching features are selected. + * If set to true, only the best matching feature is selected. + * This option has an effect only of the <click> option is set + * to true. Default is true. + */ + single: true, + + /** + * APIProperty: clickout + * {Boolean} Unselect features when clicking outside any feature. + * Applies only if <click> is true. Default is true. + */ + clickout: true, + + /** + * APIProperty: toggle + * {Boolean} Unselect a selected feature on click. Applies only if + * <click> is true. Default is false. + */ + toggle: false, + + /** + * APIProperty: clickTolerance + * {Integer} Tolerance for the filter query in pixels. This has the + * same effect as the tolerance parameter on WMS GetFeatureInfo + * requests. Will be ignored for box selections. Applies only if + * <click> or <hover> is true. Default is 5. Note that this not + * only affects requests on click, but also on hover. + */ + clickTolerance: 5, + + /** + * APIProperty: hover + * {Boolean} Send feature requests on mouse moves. Default is false. + */ + hover: false, + + /** + * APIProperty: box + * {Boolean} Allow feature selection by drawing a box. If set to + * true set <click> to false to disable the click handler and + * rely on the box handler only, even for "zero extent" boxes. + * See the description of the <click> option for additional + * information. Default is false. + */ + box: false, + + /** + * APIProperty: maxFeatures + * {Integer} Maximum number of features to return from a query in single mode + * if supported by the <protocol>. This set of features is then used to + * determine the best match client-side. Default is 10. + */ + maxFeatures: 10, + + /** + * Property: features + * {Object} Hash of {<OpenLayers.Feature.Vector>}, keyed by fid, holding + * the currently selected features + */ + features: null, + + /** + * Proeprty: hoverFeature + * {<OpenLayers.Feature.Vector>} The feature currently selected by the + * hover handler + */ + hoverFeature: null, + + /** + * APIProperty: handlerOptions + * {Object} Additional options for the handlers used by this control. This + * is a hash with the keys "click", "box" and "hover". + */ + + /** + * Property: handlers + * {Object} Object with references to multiple <OpenLayers.Handler> + * instances. + */ + handlers: null, + + /** + * Property: hoverResponse + * {<OpenLayers.Protocol.Response>} The response object associated with + * the currently running hover request (if any). + */ + hoverResponse: null, + + /** + * Property: filterType + * {<String>} The type of filter to use when sending off a request. + * Possible values: + * OpenLayers.Filter.Spatial.<BBOX|INTERSECTS|WITHIN|CONTAINS> + * Defaults to: OpenLayers.Filter.Spatial.BBOX + */ + filterType: OpenLayers.Filter.Spatial.BBOX, + + /** + * APIProperty: events + * {<OpenLayers.Events>} Events instance for listeners and triggering + * control specific events. + * + * Register a listener for a particular event with the following syntax: + * (code) + * control.events.register(type, obj, listener); + * (end) + * + * Supported event types (in addition to those from <OpenLayers.Control.events>): + * beforefeatureselected - Triggered when <click> is true before a + * feature is selected. The event object has a feature property with + * the feature about to select + * featureselected - Triggered when <click> is true and a feature is + * selected. The event object has a feature property with the + * selected feature + * beforefeaturesselected - Triggered when <click> is true before a + * set of features is selected. The event object is an array of + * feature properties with the features about to be selected. + * Return false after receiving this event to discontinue processing + * of all featureselected events and the featuresselected event. + * featuresselected - Triggered when <click> is true and a set of + * features is selected. The event object is an array of feature + * properties of the selected features + * featureunselected - Triggered when <click> is true and a feature is + * unselected. The event object has a feature property with the + * unselected feature + * clickout - Triggered when when <click> is true and no feature was + * selected. + * hoverfeature - Triggered when <hover> is true and the mouse has + * stopped over a feature + * outfeature - Triggered when <hover> is true and the mouse moves + * moved away from a hover-selected feature + */ + + /** + * Constructor: OpenLayers.Control.GetFeature + * Create a new control for fetching remote features. + * + * Parameters: + * options - {Object} A configuration object which at least has to contain + * a <protocol> property (if not, it has to be set before a request is + * made) + */ + initialize: function(options) { + options.handlerOptions = options.handlerOptions || {}; + + OpenLayers.Control.prototype.initialize.apply(this, [options]); + + this.features = {}; + + this.handlers = {}; + + if(this.click) { + this.handlers.click = new OpenLayers.Handler.Click(this, + {click: this.selectClick}, this.handlerOptions.click || {}); + } + + if(this.box) { + this.handlers.box = new OpenLayers.Handler.Box( + this, {done: this.selectBox}, + OpenLayers.Util.extend(this.handlerOptions.box, { + boxDivClassName: "olHandlerBoxSelectFeature" + }) + ); + } + + if(this.hover) { + this.handlers.hover = new OpenLayers.Handler.Hover( + this, {'move': this.cancelHover, 'pause': this.selectHover}, + OpenLayers.Util.extend(this.handlerOptions.hover, { + 'delay': 250, + 'pixelTolerance': 2 + }) + ); + } + }, + + /** + * Method: activate + * Activates the control. + * + * Returns: + * {Boolean} The control was effectively activated. + */ + activate: function () { + if (!this.active) { + for(var i in this.handlers) { + this.handlers[i].activate(); + } + } + return OpenLayers.Control.prototype.activate.apply( + this, arguments + ); + }, + + /** + * Method: deactivate + * Deactivates the control. + * + * Returns: + * {Boolean} The control was effectively deactivated. + */ + deactivate: function () { + if (this.active) { + for(var i in this.handlers) { + this.handlers[i].deactivate(); + } + } + return OpenLayers.Control.prototype.deactivate.apply( + this, arguments + ); + }, + + /** + * Method: selectClick + * Called on click + * + * Parameters: + * evt - {<OpenLayers.Event>} + */ + selectClick: function(evt) { + var bounds = this.pixelToBounds(evt.xy); + + this.setModifiers(evt); + this.request(bounds, {single: this.single}); + }, + + /** + * Method: selectBox + * Callback from the handlers.box set up when <box> selection is on + * + * Parameters: + * position - {<OpenLayers.Bounds>|Object} An OpenLayers.Bounds or + * an object with a 'left', 'bottom', 'right' and 'top' properties. + */ + selectBox: function(position) { + var bounds; + if (position instanceof OpenLayers.Bounds) { + var minXY = this.map.getLonLatFromPixel({ + x: position.left, + y: position.bottom + }); + var maxXY = this.map.getLonLatFromPixel({ + x: position.right, + y: position.top + }); + bounds = new OpenLayers.Bounds( + minXY.lon, minXY.lat, maxXY.lon, maxXY.lat + ); + + } else { + if(this.click) { + // box without extent - let the click handler take care of it + return; + } + bounds = this.pixelToBounds(position); + } + this.setModifiers(this.handlers.box.dragHandler.evt); + this.request(bounds); + }, + + /** + * Method: selectHover + * Callback from the handlers.hover set up when <hover> selection is on + * + * Parameters: + * evt - {Object} event object with an xy property + */ + selectHover: function(evt) { + var bounds = this.pixelToBounds(evt.xy); + this.request(bounds, {single: true, hover: true}); + }, + + /** + * Method: cancelHover + * Callback from the handlers.hover set up when <hover> selection is on + */ + cancelHover: function() { + if (this.hoverResponse) { + this.protocol.abort(this.hoverResponse); + this.hoverResponse = null; + + OpenLayers.Element.removeClass(this.map.viewPortDiv, "olCursorWait"); + } + }, + + /** + * Method: request + * Sends a GetFeature request to the WFS + * + * Parameters: + * bounds - {<OpenLayers.Bounds>} bounds for the request's BBOX filter + * options - {Object} additional options for this method. + * + * Supported options include: + * single - {Boolean} A single feature should be returned. + * Note that this will be ignored if the protocol does not + * return the geometries of the features. + * hover - {Boolean} Do the request for the hover handler. + */ + request: function(bounds, options) { + options = options || {}; + var filter = new OpenLayers.Filter.Spatial({ + type: this.filterType, + value: bounds + }); + + // Set the cursor to "wait" to tell the user we're working. + OpenLayers.Element.addClass(this.map.viewPortDiv, "olCursorWait"); + + var response = this.protocol.read({ + maxFeatures: options.single == true ? this.maxFeatures : undefined, + filter: filter, + callback: function(result) { + if(result.success()) { + if(result.features.length) { + if(options.single == true) { + this.selectBestFeature(result.features, + bounds.getCenterLonLat(), options); + } else { + this.select(result.features); + } + } else if(options.hover) { + this.hoverSelect(); + } else { + this.events.triggerEvent("clickout"); + if(this.clickout) { + this.unselectAll(); + } + } + } + // Reset the cursor. + OpenLayers.Element.removeClass(this.map.viewPortDiv, "olCursorWait"); + }, + scope: this + }); + if(options.hover == true) { + this.hoverResponse = response; + } + }, + + /** + * Method: selectBestFeature + * Selects the feature from an array of features that is the best match + * for the click position. + * + * Parameters: + * features - {Array(<OpenLayers.Feature.Vector>)} + * clickPosition - {<OpenLayers.LonLat>} + * options - {Object} additional options for this method + * + * Supported options include: + * hover - {Boolean} Do the selection for the hover handler. + */ + selectBestFeature: function(features, clickPosition, options) { + options = options || {}; + if(features.length) { + var point = new OpenLayers.Geometry.Point(clickPosition.lon, + clickPosition.lat); + var feature, resultFeature, dist; + var minDist = Number.MAX_VALUE; + for(var i=0; i<features.length; ++i) { + feature = features[i]; + if(feature.geometry) { + dist = point.distanceTo(feature.geometry, {edge: false}); + if(dist < minDist) { + minDist = dist; + resultFeature = feature; + if(minDist == 0) { + break; + } + } + } + } + + if(options.hover == true) { + this.hoverSelect(resultFeature); + } else { + this.select(resultFeature || features); + } + } + }, + + /** + * Method: setModifiers + * Sets the multiple and toggle modifiers according to the current event + * + * Parameters: + * evt - {<OpenLayers.Event>} + */ + setModifiers: function(evt) { + this.modifiers = { + multiple: this.multiple || (this.multipleKey && evt[this.multipleKey]), + toggle: this.toggle || (this.toggleKey && evt[this.toggleKey]) + }; + }, + + /** + * Method: select + * Add feature to the hash of selected features and trigger the + * featureselected and featuresselected events. + * + * Parameters: + * features - {<OpenLayers.Feature.Vector>} or an array of features + */ + select: function(features) { + if(!this.modifiers.multiple && !this.modifiers.toggle) { + this.unselectAll(); + } + if(!(OpenLayers.Util.isArray(features))) { + features = [features]; + } + + var cont = this.events.triggerEvent("beforefeaturesselected", { + features: features + }); + if(cont !== false) { + var selectedFeatures = []; + var feature; + for(var i=0, len=features.length; i<len; ++i) { + feature = features[i]; + if(this.features[feature.fid || feature.id]) { + if(this.modifiers.toggle) { + this.unselect(this.features[feature.fid || feature.id]); + } + } else { + cont = this.events.triggerEvent("beforefeatureselected", { + feature: feature + }); + if(cont !== false) { + this.features[feature.fid || feature.id] = feature; + selectedFeatures.push(feature); + + this.events.triggerEvent("featureselected", + {feature: feature}); + } + } + } + this.events.triggerEvent("featuresselected", { + features: selectedFeatures + }); + } + }, + + /** + * Method: hoverSelect + * Sets/unsets the <hoverFeature> + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} the feature to hover-select. + * If none is provided, the current <hoverFeature> will be nulled and + * the outfeature event will be triggered. + */ + hoverSelect: function(feature) { + var fid = feature ? feature.fid || feature.id : null; + var hfid = this.hoverFeature ? + this.hoverFeature.fid || this.hoverFeature.id : null; + + if(hfid && hfid != fid) { + this.events.triggerEvent("outfeature", + {feature: this.hoverFeature}); + this.hoverFeature = null; + } + if(fid && fid != hfid) { + this.events.triggerEvent("hoverfeature", {feature: feature}); + this.hoverFeature = feature; + } + }, + + /** + * Method: unselect + * Remove feature from the hash of selected features and trigger the + * featureunselected event. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} + */ + unselect: function(feature) { + delete this.features[feature.fid || feature.id]; + this.events.triggerEvent("featureunselected", {feature: feature}); + }, + + /** + * Method: unselectAll + * Unselect all selected features. + */ + unselectAll: function() { + // we'll want an option to supress notification here + for(var fid in this.features) { + this.unselect(this.features[fid]); + } + }, + + /** + * Method: setMap + * Set the map property for the control. + * + * Parameters: + * map - {<OpenLayers.Map>} + */ + setMap: function(map) { + for(var i in this.handlers) { + this.handlers[i].setMap(map); + } + OpenLayers.Control.prototype.setMap.apply(this, arguments); + }, + + /** + * Method: pixelToBounds + * Takes a pixel as argument and creates bounds after adding the + * <clickTolerance>. + * + * Parameters: + * pixel - {<OpenLayers.Pixel>} + */ + pixelToBounds: function(pixel) { + var llPx = pixel.add(-this.clickTolerance/2, this.clickTolerance/2); + var urPx = pixel.add(this.clickTolerance/2, -this.clickTolerance/2); + var ll = this.map.getLonLatFromPixel(llPx); + var ur = this.map.getLonLatFromPixel(urPx); + return new OpenLayers.Bounds(ll.lon, ll.lat, ur.lon, ur.lat); + }, + + CLASS_NAME: "OpenLayers.Control.GetFeature" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/Graticule.js b/misc/openlayers/lib/OpenLayers/Control/Graticule.js new file mode 100644 index 0000000..2fce50d --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/Graticule.js @@ -0,0 +1,377 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Lang.js + * @requires OpenLayers/Rule.js + * @requires OpenLayers/StyleMap.js + * @requires OpenLayers/Layer/Vector.js + */ + +/** + * Class: OpenLayers.Control.Graticule + * The Graticule displays a grid of latitude/longitude lines reprojected on + * the map. + * + * Inherits from: + * - <OpenLayers.Control> + * + */ +OpenLayers.Control.Graticule = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: autoActivate + * {Boolean} Activate the control when it is added to a map. Default is + * true. + */ + autoActivate: true, + + /** + * APIProperty: intervals + * {Array(Float)} A list of possible graticule widths in degrees. + */ + intervals: [ 45, 30, 20, 10, 5, 2, 1, + 0.5, 0.2, 0.1, 0.05, 0.01, + 0.005, 0.002, 0.001 ], + + /** + * APIProperty: displayInLayerSwitcher + * {Boolean} Allows the Graticule control to be switched on and off by + * LayerSwitcher control. Defaults is true. + */ + displayInLayerSwitcher: true, + + /** + * APIProperty: visible + * {Boolean} should the graticule be initially visible (default=true) + */ + visible: true, + + /** + * APIProperty: numPoints + * {Integer} The number of points to use in each graticule line. Higher + * numbers result in a smoother curve for projected maps + */ + numPoints: 50, + + /** + * APIProperty: targetSize + * {Integer} The maximum size of the grid in pixels on the map + */ + targetSize: 200, + + /** + * APIProperty: layerName + * {String} The name to be displayed in the layer switcher, default is set + * by {<OpenLayers.Lang>}. + */ + layerName: null, + + /** + * APIProperty: labelled + * {Boolean} Should the graticule lines be labelled?. default=true + */ + labelled: true, + + /** + * APIProperty: labelFormat + * {String} the format of the labels, default = 'dm'. See + * <OpenLayers.Util.getFormattedLonLat> for other options. + */ + labelFormat: 'dm', + + /** + * APIProperty: lineSymbolizer + * {symbolizer} the symbolizer used to render lines + */ + lineSymbolizer: { + strokeColor: "#333", + strokeWidth: 1, + strokeOpacity: 0.5 + }, + + /** + * APIProperty: labelSymbolizer + * {symbolizer} the symbolizer used to render labels + */ + labelSymbolizer: {}, + + /** + * Property: gratLayer + * {<OpenLayers.Layer.Vector>} vector layer used to draw the graticule on + */ + gratLayer: null, + + /** + * Constructor: OpenLayers.Control.Graticule + * Create a new graticule control to display a grid of latitude longitude + * lines. + * + * Parameters: + * options - {Object} An optional object whose properties will be used + * to extend the control. + */ + initialize: function(options) { + options = options || {}; + options.layerName = options.layerName || OpenLayers.i18n("Graticule"); + OpenLayers.Control.prototype.initialize.apply(this, [options]); + + this.labelSymbolizer.stroke = false; + this.labelSymbolizer.fill = false; + this.labelSymbolizer.label = "${label}"; + this.labelSymbolizer.labelAlign = "${labelAlign}"; + this.labelSymbolizer.labelXOffset = "${xOffset}"; + this.labelSymbolizer.labelYOffset = "${yOffset}"; + }, + + /** + * APIMethod: destroy + */ + destroy: function() { + this.deactivate(); + OpenLayers.Control.prototype.destroy.apply(this, arguments); + if (this.gratLayer) { + this.gratLayer.destroy(); + this.gratLayer = null; + } + }, + + /** + * Method: draw + * + * initializes the graticule layer and does the initial update + * + * Returns: + * {DOMElement} + */ + draw: function() { + OpenLayers.Control.prototype.draw.apply(this, arguments); + if (!this.gratLayer) { + var gratStyle = new OpenLayers.Style({},{ + rules: [new OpenLayers.Rule({'symbolizer': + {"Point":this.labelSymbolizer, + "Line":this.lineSymbolizer} + })] + }); + this.gratLayer = new OpenLayers.Layer.Vector(this.layerName, { + styleMap: new OpenLayers.StyleMap({'default':gratStyle}), + visibility: this.visible, + displayInLayerSwitcher: this.displayInLayerSwitcher + }); + } + return this.div; + }, + + /** + * APIMethod: activate + */ + activate: function() { + if (OpenLayers.Control.prototype.activate.apply(this, arguments)) { + this.map.addLayer(this.gratLayer); + this.map.events.register('moveend', this, this.update); + this.update(); + return true; + } else { + return false; + } + }, + + /** + * APIMethod: deactivate + */ + deactivate: function() { + if (OpenLayers.Control.prototype.deactivate.apply(this, arguments)) { + this.map.events.unregister('moveend', this, this.update); + this.map.removeLayer(this.gratLayer); + return true; + } else { + return false; + } + }, + /** + * Method: update + * + * calculates the grid to be displayed and actually draws it + * + * Returns: + * {DOMElement} + */ + update: function() { + //wait for the map to be initialized before proceeding + var mapBounds = this.map.getExtent(); + if (!mapBounds) { + return; + } + + //clear out the old grid + this.gratLayer.destroyFeatures(); + + //get the projection objects required + var llProj = new OpenLayers.Projection("EPSG:4326"); + var mapProj = this.map.getProjectionObject(); + var mapRes = this.map.getResolution(); + + //if the map is in lon/lat, then the lines are straight and only one + //point is required + if (mapProj.proj && mapProj.proj.projName == "longlat") { + this.numPoints = 1; + } + + //get the map center in EPSG:4326 + var mapCenter = this.map.getCenter(); //lon and lat here are really map x and y + var mapCenterLL = new OpenLayers.Pixel(mapCenter.lon, mapCenter.lat); + OpenLayers.Projection.transform(mapCenterLL, mapProj, llProj); + + /* This block of code determines the lon/lat interval to use for the + * grid by calculating the diagonal size of one grid cell at the map + * center. Iterates through the intervals array until the diagonal + * length is less than the targetSize option. + */ + //find lat/lon interval that results in a grid of less than the target size + var testSq = this.targetSize*mapRes; + testSq *= testSq; //compare squares rather than doing a square root to save time + var llInterval; + for (var i=0; i<this.intervals.length; ++i) { + llInterval = this.intervals[i]; //could do this for both x and y?? + var delta = llInterval/2; + var p1 = mapCenterLL.offset({x: -delta, y: -delta}); //test coords in EPSG:4326 space + var p2 = mapCenterLL.offset({x: delta, y: delta}); + OpenLayers.Projection.transform(p1, llProj, mapProj); // convert them back to map projection + OpenLayers.Projection.transform(p2, llProj, mapProj); + var distSq = (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y); + if (distSq <= testSq) { + break; + } + } + //alert(llInterval); + + //round the LL center to an even number based on the interval + mapCenterLL.x = Math.floor(mapCenterLL.x/llInterval)*llInterval; + mapCenterLL.y = Math.floor(mapCenterLL.y/llInterval)*llInterval; + //TODO adjust for minutses/seconds? + + /* The following 2 blocks calculate the nodes of the grid along a + * line of constant longitude (then latitiude) running through the + * center of the map until it reaches the map edge. The calculation + * goes from the center in both directions to the edge. + */ + //get the central longitude line, increment the latitude + var iter = 0; + var centerLonPoints = [mapCenterLL.clone()]; + var newPoint = mapCenterLL.clone(); + var mapXY; + do { + newPoint = newPoint.offset({x: 0, y: llInterval}); + mapXY = OpenLayers.Projection.transform(newPoint.clone(), llProj, mapProj); + centerLonPoints.unshift(newPoint); + } while (mapBounds.containsPixel(mapXY) && ++iter<1000); + newPoint = mapCenterLL.clone(); + do { + newPoint = newPoint.offset({x: 0, y: -llInterval}); + mapXY = OpenLayers.Projection.transform(newPoint.clone(), llProj, mapProj); + centerLonPoints.push(newPoint); + } while (mapBounds.containsPixel(mapXY) && ++iter<1000); + + //get the central latitude line, increment the longitude + iter = 0; + var centerLatPoints = [mapCenterLL.clone()]; + newPoint = mapCenterLL.clone(); + do { + newPoint = newPoint.offset({x: -llInterval, y: 0}); + mapXY = OpenLayers.Projection.transform(newPoint.clone(), llProj, mapProj); + centerLatPoints.unshift(newPoint); + } while (mapBounds.containsPixel(mapXY) && ++iter<1000); + newPoint = mapCenterLL.clone(); + do { + newPoint = newPoint.offset({x: llInterval, y: 0}); + mapXY = OpenLayers.Projection.transform(newPoint.clone(), llProj, mapProj); + centerLatPoints.push(newPoint); + } while (mapBounds.containsPixel(mapXY) && ++iter<1000); + + //now generate a line for each node in the central lat and lon lines + //first loop over constant longitude + var lines = []; + for(var i=0; i < centerLatPoints.length; ++i) { + var lon = centerLatPoints[i].x; + var pointList = []; + var labelPoint = null; + var latEnd = Math.min(centerLonPoints[0].y, 90); + var latStart = Math.max(centerLonPoints[centerLonPoints.length - 1].y, -90); + var latDelta = (latEnd - latStart)/this.numPoints; + var lat = latStart; + for(var j=0; j<= this.numPoints; ++j) { + var gridPoint = new OpenLayers.Geometry.Point(lon,lat); + gridPoint.transform(llProj, mapProj); + pointList.push(gridPoint); + lat += latDelta; + if (gridPoint.y >= mapBounds.bottom && !labelPoint) { + labelPoint = gridPoint; + } + } + if (this.labelled) { + //keep track of when this grid line crosses the map bounds to set + //the label position + //labels along the bottom, add 10 pixel offset up into the map + //TODO add option for labels on top + var labelPos = new OpenLayers.Geometry.Point(labelPoint.x,mapBounds.bottom); + var labelAttrs = { + value: lon, + label: this.labelled?OpenLayers.Util.getFormattedLonLat(lon, "lon", this.labelFormat):"", + labelAlign: "cb", + xOffset: 0, + yOffset: 2 + }; + this.gratLayer.addFeatures(new OpenLayers.Feature.Vector(labelPos,labelAttrs)); + } + var geom = new OpenLayers.Geometry.LineString(pointList); + lines.push(new OpenLayers.Feature.Vector(geom)); + } + + //now draw the lines of constant latitude + for (var j=0; j < centerLonPoints.length; ++j) { + lat = centerLonPoints[j].y; + if (lat<-90 || lat>90) { //latitudes only valid between -90 and 90 + continue; + } + var pointList = []; + var lonStart = centerLatPoints[0].x; + var lonEnd = centerLatPoints[centerLatPoints.length - 1].x; + var lonDelta = (lonEnd - lonStart)/this.numPoints; + var lon = lonStart; + var labelPoint = null; + for(var i=0; i <= this.numPoints ; ++i) { + var gridPoint = new OpenLayers.Geometry.Point(lon,lat); + gridPoint.transform(llProj, mapProj); + pointList.push(gridPoint); + lon += lonDelta; + if (gridPoint.x < mapBounds.right) { + labelPoint = gridPoint; + } + } + if (this.labelled) { + //keep track of when this grid line crosses the map bounds to set + //the label position + //labels along the right, 30 pixel offset left into the map + //TODO add option for labels on left + var labelPos = new OpenLayers.Geometry.Point(mapBounds.right, labelPoint.y); + var labelAttrs = { + value: lat, + label: this.labelled?OpenLayers.Util.getFormattedLonLat(lat, "lat", this.labelFormat):"", + labelAlign: "rb", + xOffset: -2, + yOffset: 2 + }; + this.gratLayer.addFeatures(new OpenLayers.Feature.Vector(labelPos,labelAttrs)); + } + var geom = new OpenLayers.Geometry.LineString(pointList); + lines.push(new OpenLayers.Feature.Vector(geom)); + } + this.gratLayer.addFeatures(lines); + }, + + CLASS_NAME: "OpenLayers.Control.Graticule" +}); + diff --git a/misc/openlayers/lib/OpenLayers/Control/KeyboardDefaults.js b/misc/openlayers/lib/OpenLayers/Control/KeyboardDefaults.js new file mode 100644 index 0000000..3af8831 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/KeyboardDefaults.js @@ -0,0 +1,142 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Handler/Keyboard.js + * @requires OpenLayers/Events.js + */ + +/** + * Class: OpenLayers.Control.KeyboardDefaults + * The KeyboardDefaults control adds panning and zooming functions, controlled + * with the keyboard. By default arrow keys pan, +/- keys zoom & Page Up/Page + * Down/Home/End scroll by three quarters of a page. + * + * This control has no visible appearance. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.KeyboardDefaults = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: autoActivate + * {Boolean} Activate the control when it is added to a map. Default is + * true. + */ + autoActivate: true, + + /** + * APIProperty: slideFactor + * Pixels to slide by. + */ + slideFactor: 75, + + /** + * APIProperty: observeElement + * {DOMelement|String} The DOM element to handle keys for. You + * can use the map div here, to have the navigation keys + * work when the map div has the focus. If undefined the + * document is used. + */ + observeElement: null, + + /** + * Constructor: OpenLayers.Control.KeyboardDefaults + */ + + /** + * Method: draw + * Create handler. + */ + draw: function() { + var observeElement = this.observeElement || document; + this.handler = new OpenLayers.Handler.Keyboard( this, + {"keydown": this.defaultKeyPress}, + {observeElement: observeElement} + ); + }, + + /** + * Method: defaultKeyPress + * When handling the key event, we only use evt.keyCode. This holds + * some drawbacks, though we get around them below. When interpretting + * the keycodes below (including the comments associated with them), + * consult the URL below. For instance, the Safari browser returns + * "IE keycodes", and so is supported by any keycode labeled "IE". + * + * Very informative URL: + * http://unixpapa.com/js/key.html + * + * Parameters: + * evt - {Event} + */ + defaultKeyPress: function (evt) { + var size, handled = true; + + var target = OpenLayers.Event.element(evt); + if (target && + (target.tagName == 'INPUT' || + target.tagName == 'TEXTAREA' || + target.tagName == 'SELECT')) { + return; + } + + switch (evt.keyCode) { + case OpenLayers.Event.KEY_LEFT: + this.map.pan(-this.slideFactor, 0); + break; + case OpenLayers.Event.KEY_RIGHT: + this.map.pan(this.slideFactor, 0); + break; + case OpenLayers.Event.KEY_UP: + this.map.pan(0, -this.slideFactor); + break; + case OpenLayers.Event.KEY_DOWN: + this.map.pan(0, this.slideFactor); + break; + + case 33: // Page Up. Same in all browsers. + size = this.map.getSize(); + this.map.pan(0, -0.75*size.h); + break; + case 34: // Page Down. Same in all browsers. + size = this.map.getSize(); + this.map.pan(0, 0.75*size.h); + break; + case 35: // End. Same in all browsers. + size = this.map.getSize(); + this.map.pan(0.75*size.w, 0); + break; + case 36: // Home. Same in all browsers. + size = this.map.getSize(); + this.map.pan(-0.75*size.w, 0); + break; + + case 43: // +/= (ASCII), keypad + (ASCII, Opera) + case 61: // +/= (Mozilla, Opera, some ASCII) + case 187: // +/= (IE) + case 107: // keypad + (IE, Mozilla) + this.map.zoomIn(); + break; + case 45: // -/_ (ASCII, Opera), keypad - (ASCII, Opera) + case 109: // -/_ (Mozilla), keypad - (Mozilla, IE) + case 189: // -/_ (IE) + case 95: // -/_ (some ASCII) + this.map.zoomOut(); + break; + default: + handled = false; + } + if (handled) { + // prevent browser default not to move the page + // when moving the page with the keyboard + OpenLayers.Event.stop(evt); + } + }, + + CLASS_NAME: "OpenLayers.Control.KeyboardDefaults" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/LayerSwitcher.js b/misc/openlayers/lib/OpenLayers/Control/LayerSwitcher.js new file mode 100644 index 0000000..668f5c3 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/LayerSwitcher.js @@ -0,0 +1,521 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Lang.js + * @requires OpenLayers/Util.js + * @requires OpenLayers/Events/buttonclick.js + */ + +/** + * Class: OpenLayers.Control.LayerSwitcher + * The LayerSwitcher control displays a table of contents for the map. This + * allows the user interface to switch between BaseLasyers and to show or hide + * Overlays. By default the switcher is shown minimized on the right edge of + * the map, the user may expand it by clicking on the handle. + * + * To create the LayerSwitcher outside of the map, pass the Id of a html div + * as the first argument to the constructor. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.LayerSwitcher = OpenLayers.Class(OpenLayers.Control, { + + /** + * Property: layerStates + * {Array(Object)} Basically a copy of the "state" of the map's layers + * the last time the control was drawn. We have this in order to avoid + * unnecessarily redrawing the control. + */ + layerStates: null, + + // DOM Elements + + /** + * Property: layersDiv + * {DOMElement} + */ + layersDiv: null, + + /** + * Property: baseLayersDiv + * {DOMElement} + */ + baseLayersDiv: null, + + /** + * Property: baseLayers + * {Array(Object)} + */ + baseLayers: null, + + + /** + * Property: dataLbl + * {DOMElement} + */ + dataLbl: null, + + /** + * Property: dataLayersDiv + * {DOMElement} + */ + dataLayersDiv: null, + + /** + * Property: dataLayers + * {Array(Object)} + */ + dataLayers: null, + + + /** + * Property: minimizeDiv + * {DOMElement} + */ + minimizeDiv: null, + + /** + * Property: maximizeDiv + * {DOMElement} + */ + maximizeDiv: null, + + /** + * APIProperty: ascending + * {Boolean} + */ + ascending: true, + + /** + * Constructor: OpenLayers.Control.LayerSwitcher + * + * Parameters: + * options - {Object} + */ + initialize: function(options) { + OpenLayers.Control.prototype.initialize.apply(this, arguments); + this.layerStates = []; + }, + + /** + * APIMethod: destroy + */ + destroy: function() { + + //clear out layers info and unregister their events + this.clearLayersArray("base"); + this.clearLayersArray("data"); + + this.map.events.un({ + buttonclick: this.onButtonClick, + addlayer: this.redraw, + changelayer: this.redraw, + removelayer: this.redraw, + changebaselayer: this.redraw, + scope: this + }); + this.events.unregister("buttonclick", this, this.onButtonClick); + + OpenLayers.Control.prototype.destroy.apply(this, arguments); + }, + + /** + * Method: setMap + * + * Properties: + * map - {<OpenLayers.Map>} + */ + setMap: function(map) { + OpenLayers.Control.prototype.setMap.apply(this, arguments); + + this.map.events.on({ + addlayer: this.redraw, + changelayer: this.redraw, + removelayer: this.redraw, + changebaselayer: this.redraw, + scope: this + }); + if (this.outsideViewport) { + this.events.attachToElement(this.div); + this.events.register("buttonclick", this, this.onButtonClick); + } else { + this.map.events.register("buttonclick", this, this.onButtonClick); + } + }, + + /** + * Method: draw + * + * Returns: + * {DOMElement} A reference to the DIV DOMElement containing the + * switcher tabs. + */ + draw: function() { + OpenLayers.Control.prototype.draw.apply(this); + + // create layout divs + this.loadContents(); + + // set mode to minimize + if(!this.outsideViewport) { + this.minimizeControl(); + } + + // populate div with current info + this.redraw(); + + return this.div; + }, + + /** + * Method: onButtonClick + * + * Parameters: + * evt - {Event} + */ + onButtonClick: function(evt) { + var button = evt.buttonElement; + if (button === this.minimizeDiv) { + this.minimizeControl(); + } else if (button === this.maximizeDiv) { + this.maximizeControl(); + } else if (button._layerSwitcher === this.id) { + if (button["for"]) { + button = document.getElementById(button["for"]); + } + if (!button.disabled) { + if (button.type == "radio") { + button.checked = true; + this.map.setBaseLayer(this.map.getLayer(button._layer)); + } else { + button.checked = !button.checked; + this.updateMap(); + } + } + } + }, + + /** + * Method: clearLayersArray + * User specifies either "base" or "data". we then clear all the + * corresponding listeners, the div, and reinitialize a new array. + * + * Parameters: + * layersType - {String} + */ + clearLayersArray: function(layersType) { + this[layersType + "LayersDiv"].innerHTML = ""; + this[layersType + "Layers"] = []; + }, + + + /** + * Method: checkRedraw + * Checks if the layer state has changed since the last redraw() call. + * + * Returns: + * {Boolean} The layer state changed since the last redraw() call. + */ + checkRedraw: function() { + if ( !this.layerStates.length || + (this.map.layers.length != this.layerStates.length) ) { + return true; + } + + for (var i = 0, len = this.layerStates.length; i < len; i++) { + var layerState = this.layerStates[i]; + var layer = this.map.layers[i]; + if ( (layerState.name != layer.name) || + (layerState.inRange != layer.inRange) || + (layerState.id != layer.id) || + (layerState.visibility != layer.visibility) ) { + return true; + } + } + + return false; + }, + + /** + * Method: redraw + * Goes through and takes the current state of the Map and rebuilds the + * control to display that state. Groups base layers into a + * radio-button group and lists each data layer with a checkbox. + * + * Returns: + * {DOMElement} A reference to the DIV DOMElement containing the control + */ + redraw: function() { + //if the state hasn't changed since last redraw, no need + // to do anything. Just return the existing div. + if (!this.checkRedraw()) { + return this.div; + } + + //clear out previous layers + this.clearLayersArray("base"); + this.clearLayersArray("data"); + + var containsOverlays = false; + var containsBaseLayers = false; + + // Save state -- for checking layer if the map state changed. + // We save this before redrawing, because in the process of redrawing + // we will trigger more visibility changes, and we want to not redraw + // and enter an infinite loop. + var len = this.map.layers.length; + this.layerStates = new Array(len); + for (var i=0; i <len; i++) { + var layer = this.map.layers[i]; + this.layerStates[i] = { + 'name': layer.name, + 'visibility': layer.visibility, + 'inRange': layer.inRange, + 'id': layer.id + }; + } + + var layers = this.map.layers.slice(); + if (!this.ascending) { layers.reverse(); } + for(var i=0, len=layers.length; i<len; i++) { + var layer = layers[i]; + var baseLayer = layer.isBaseLayer; + + if (layer.displayInLayerSwitcher) { + + if (baseLayer) { + containsBaseLayers = true; + } else { + containsOverlays = true; + } + + // only check a baselayer if it is *the* baselayer, check data + // layers if they are visible + var checked = (baseLayer) ? (layer == this.map.baseLayer) + : layer.getVisibility(); + + // create input element + var inputElem = document.createElement("input"), + // The input shall have an id attribute so we can use + // labels to interact with them. + inputId = OpenLayers.Util.createUniqueID( + this.id + "_input_" + ); + + inputElem.id = inputId; + inputElem.name = (baseLayer) ? this.id + "_baseLayers" : layer.name; + inputElem.type = (baseLayer) ? "radio" : "checkbox"; + inputElem.value = layer.name; + inputElem.checked = checked; + inputElem.defaultChecked = checked; + inputElem.className = "olButton"; + inputElem._layer = layer.id; + inputElem._layerSwitcher = this.id; + + if (!baseLayer && !layer.inRange) { + inputElem.disabled = true; + } + + // create span + var labelSpan = document.createElement("label"); + // this isn't the DOM attribute 'for', but an arbitrary name we + // use to find the appropriate input element in <onButtonClick> + labelSpan["for"] = inputElem.id; + OpenLayers.Element.addClass(labelSpan, "labelSpan olButton"); + labelSpan._layer = layer.id; + labelSpan._layerSwitcher = this.id; + if (!baseLayer && !layer.inRange) { + labelSpan.style.color = "gray"; + } + labelSpan.innerHTML = layer.name; + labelSpan.style.verticalAlign = (baseLayer) ? "bottom" + : "baseline"; + // create line break + var br = document.createElement("br"); + + + var groupArray = (baseLayer) ? this.baseLayers + : this.dataLayers; + groupArray.push({ + 'layer': layer, + 'inputElem': inputElem, + 'labelSpan': labelSpan + }); + + + var groupDiv = (baseLayer) ? this.baseLayersDiv + : this.dataLayersDiv; + groupDiv.appendChild(inputElem); + groupDiv.appendChild(labelSpan); + groupDiv.appendChild(br); + } + } + + // if no overlays, dont display the overlay label + this.dataLbl.style.display = (containsOverlays) ? "" : "none"; + + // if no baselayers, dont display the baselayer label + this.baseLbl.style.display = (containsBaseLayers) ? "" : "none"; + + return this.div; + }, + + /** + * Method: updateMap + * Cycles through the loaded data and base layer input arrays and makes + * the necessary calls to the Map object such that that the map's + * visual state corresponds to what the user has selected in + * the control. + */ + updateMap: function() { + + // set the newly selected base layer + for(var i=0, len=this.baseLayers.length; i<len; i++) { + var layerEntry = this.baseLayers[i]; + if (layerEntry.inputElem.checked) { + this.map.setBaseLayer(layerEntry.layer, false); + } + } + + // set the correct visibilities for the overlays + for(var i=0, len=this.dataLayers.length; i<len; i++) { + var layerEntry = this.dataLayers[i]; + layerEntry.layer.setVisibility(layerEntry.inputElem.checked); + } + + }, + + /** + * Method: maximizeControl + * Set up the labels and divs for the control + * + * Parameters: + * e - {Event} + */ + maximizeControl: function(e) { + + // set the div's width and height to empty values, so + // the div dimensions can be controlled by CSS + this.div.style.width = ""; + this.div.style.height = ""; + + this.showControls(false); + + if (e != null) { + OpenLayers.Event.stop(e); + } + }, + + /** + * Method: minimizeControl + * Hide all the contents of the control, shrink the size, + * add the maximize icon + * + * Parameters: + * e - {Event} + */ + minimizeControl: function(e) { + + // to minimize the control we set its div's width + // and height to 0px, we cannot just set "display" + // to "none" because it would hide the maximize + // div + this.div.style.width = "0px"; + this.div.style.height = "0px"; + + this.showControls(true); + + if (e != null) { + OpenLayers.Event.stop(e); + } + }, + + /** + * Method: showControls + * Hide/Show all LayerSwitcher controls depending on whether we are + * minimized or not + * + * Parameters: + * minimize - {Boolean} + */ + showControls: function(minimize) { + + this.maximizeDiv.style.display = minimize ? "" : "none"; + this.minimizeDiv.style.display = minimize ? "none" : ""; + + this.layersDiv.style.display = minimize ? "none" : ""; + }, + + /** + * Method: loadContents + * Set up the labels and divs for the control + */ + loadContents: function() { + + // layers list div + this.layersDiv = document.createElement("div"); + this.layersDiv.id = this.id + "_layersDiv"; + OpenLayers.Element.addClass(this.layersDiv, "layersDiv"); + + this.baseLbl = document.createElement("div"); + this.baseLbl.innerHTML = OpenLayers.i18n("Base Layer"); + OpenLayers.Element.addClass(this.baseLbl, "baseLbl"); + + this.baseLayersDiv = document.createElement("div"); + OpenLayers.Element.addClass(this.baseLayersDiv, "baseLayersDiv"); + + this.dataLbl = document.createElement("div"); + this.dataLbl.innerHTML = OpenLayers.i18n("Overlays"); + OpenLayers.Element.addClass(this.dataLbl, "dataLbl"); + + this.dataLayersDiv = document.createElement("div"); + OpenLayers.Element.addClass(this.dataLayersDiv, "dataLayersDiv"); + + if (this.ascending) { + this.layersDiv.appendChild(this.baseLbl); + this.layersDiv.appendChild(this.baseLayersDiv); + this.layersDiv.appendChild(this.dataLbl); + this.layersDiv.appendChild(this.dataLayersDiv); + } else { + this.layersDiv.appendChild(this.dataLbl); + this.layersDiv.appendChild(this.dataLayersDiv); + this.layersDiv.appendChild(this.baseLbl); + this.layersDiv.appendChild(this.baseLayersDiv); + } + + this.div.appendChild(this.layersDiv); + + // maximize button div + var img = OpenLayers.Util.getImageLocation('layer-switcher-maximize.png'); + this.maximizeDiv = OpenLayers.Util.createAlphaImageDiv( + "OpenLayers_Control_MaximizeDiv", + null, + null, + img, + "absolute"); + OpenLayers.Element.addClass(this.maximizeDiv, "maximizeDiv olButton"); + this.maximizeDiv.style.display = "none"; + + this.div.appendChild(this.maximizeDiv); + + // minimize button div + var img = OpenLayers.Util.getImageLocation('layer-switcher-minimize.png'); + this.minimizeDiv = OpenLayers.Util.createAlphaImageDiv( + "OpenLayers_Control_MinimizeDiv", + null, + null, + img, + "absolute"); + OpenLayers.Element.addClass(this.minimizeDiv, "minimizeDiv olButton"); + this.minimizeDiv.style.display = "none"; + + this.div.appendChild(this.minimizeDiv); + }, + + CLASS_NAME: "OpenLayers.Control.LayerSwitcher" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/Measure.js b/misc/openlayers/lib/OpenLayers/Control/Measure.js new file mode 100644 index 0000000..03c22b8 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/Measure.js @@ -0,0 +1,379 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Feature/Vector.js + */ + +/** + * Class: OpenLayers.Control.Measure + * Allows for drawing of features for measurements. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.Measure = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: events + * {<OpenLayers.Events>} Events instance for listeners and triggering + * control specific events. + * + * Register a listener for a particular event with the following syntax: + * (code) + * control.events.register(type, obj, listener); + * (end) + * + * Supported event types (in addition to those from <OpenLayers.Control.events>): + * measure - Triggered when a measurement sketch is complete. Listeners + * will receive an event with measure, units, order, and geometry + * properties. + * measurepartial - Triggered when a new point is added to the + * measurement sketch or if the <immediate> property is true and the + * measurement sketch is modified. Listeners receive an event with measure, + * units, order, and geometry. + */ + + /** + * APIProperty: handlerOptions + * {Object} Used to set non-default properties on the control's handler + */ + + /** + * Property: callbacks + * {Object} The functions that are sent to the handler for callback + */ + callbacks: null, + + /** + * APIProperty: displaySystem + * {String} Display system for output measurements. Supported values + * are 'english', 'metric', and 'geographic'. Default is 'metric'. + */ + displaySystem: 'metric', + + /** + * APIProperty: geodesic + * {Boolean} Calculate geodesic metrics instead of planar metrics. This + * requires that geometries can be transformed into Geographic/WGS84 + * (if that is not already the map projection). Default is false. + */ + geodesic: false, + + /** + * Property: displaySystemUnits + * {Object} Units for various measurement systems. Values are arrays + * of unit abbreviations (from OpenLayers.INCHES_PER_UNIT) in decreasing + * order of length. + */ + displaySystemUnits: { + geographic: ['dd'], + english: ['mi', 'ft', 'in'], + metric: ['km', 'm'] + }, + + /** + * Property: delay + * {Number} Number of milliseconds between clicks before the event is + * considered a double-click. The "measurepartial" event will not + * be triggered if the sketch is completed within this time. This + * is required for IE where creating a browser reflow (if a listener + * is modifying the DOM by displaying the measurement values) messes + * with the dblclick listener in the sketch handler. + */ + partialDelay: 300, + + /** + * Property: delayedTrigger + * {Number} Timeout id of trigger for measurepartial. + */ + delayedTrigger: null, + + /** + * APIProperty: persist + * {Boolean} Keep the temporary measurement sketch drawn after the + * measurement is complete. The geometry will persist until a new + * measurement is started, the control is deactivated, or <cancel> is + * called. + */ + persist: false, + + /** + * APIProperty: immediate + * {Boolean} Activates the immediate measurement so that the "measurepartial" + * event is also fired once the measurement sketch is modified. + * Default is false. + */ + immediate : false, + + /** + * Constructor: OpenLayers.Control.Measure + * + * Parameters: + * handler - {<OpenLayers.Handler>} + * options - {Object} + */ + initialize: function(handler, options) { + OpenLayers.Control.prototype.initialize.apply(this, [options]); + var callbacks = {done: this.measureComplete, + point: this.measurePartial}; + if (this.immediate){ + callbacks.modify = this.measureImmediate; + } + this.callbacks = OpenLayers.Util.extend(callbacks, this.callbacks); + + // let the handler options override, so old code that passes 'persist' + // directly to the handler does not need an update + this.handlerOptions = OpenLayers.Util.extend( + {persist: this.persist}, this.handlerOptions + ); + this.handler = new handler(this, this.callbacks, this.handlerOptions); + }, + + /** + * APIMethod: deactivate + */ + deactivate: function() { + this.cancelDelay(); + return OpenLayers.Control.prototype.deactivate.apply(this, arguments); + }, + + /** + * APIMethod: cancel + * Stop the control from measuring. If <persist> is true, the temporary + * sketch will be erased. + */ + cancel: function() { + this.cancelDelay(); + this.handler.cancel(); + }, + + /** + * APIMethod: setImmediate + * Sets the <immediate> property. Changes the activity of immediate + * measurement. + */ + setImmediate: function(immediate) { + this.immediate = immediate; + if (this.immediate){ + this.callbacks.modify = this.measureImmediate; + } else { + delete this.callbacks.modify; + } + }, + + /** + * Method: updateHandler + * + * Parameters: + * handler - {Function} One of the sketch handler constructors. + * options - {Object} Options for the handler. + */ + updateHandler: function(handler, options) { + var active = this.active; + if(active) { + this.deactivate(); + } + this.handler = new handler(this, this.callbacks, options); + if(active) { + this.activate(); + } + }, + + /** + * Method: measureComplete + * Called when the measurement sketch is done. + * + * Parameters: + * geometry - {<OpenLayers.Geometry>} + */ + measureComplete: function(geometry) { + this.cancelDelay(); + this.measure(geometry, "measure"); + }, + + /** + * Method: measurePartial + * Called each time a new point is added to the measurement sketch. + * + * Parameters: + * point - {<OpenLayers.Geometry.Point>} The last point added. + * geometry - {<OpenLayers.Geometry>} The sketch geometry. + */ + measurePartial: function(point, geometry) { + this.cancelDelay(); + geometry = geometry.clone(); + // when we're wating for a dblclick, we have to trigger measurepartial + // after some delay to deal with reflow issues in IE + if (this.handler.freehandMode(this.handler.evt)) { + // no dblclick in freehand mode + this.measure(geometry, "measurepartial"); + } else { + this.delayedTrigger = window.setTimeout( + OpenLayers.Function.bind(function() { + this.delayedTrigger = null; + this.measure(geometry, "measurepartial"); + }, this), + this.partialDelay + ); + } + }, + + /** + * Method: measureImmediate + * Called each time the measurement sketch is modified. + * + * Parameters: + * point - {<OpenLayers.Geometry.Point>} The point at the mouse position. + * feature - {<OpenLayers.Feature.Vector>} The sketch feature. + * drawing - {Boolean} Indicates whether we're currently drawing. + */ + measureImmediate : function(point, feature, drawing) { + if (drawing && !this.handler.freehandMode(this.handler.evt)) { + this.cancelDelay(); + this.measure(feature.geometry, "measurepartial"); + } + }, + + /** + * Method: cancelDelay + * Cancels the delay measurement that measurePartial began. + */ + cancelDelay: function() { + if (this.delayedTrigger !== null) { + window.clearTimeout(this.delayedTrigger); + this.delayedTrigger = null; + } + }, + + /** + * Method: measure + * + * Parameters: + * geometry - {<OpenLayers.Geometry>} + * eventType - {String} + */ + measure: function(geometry, eventType) { + var stat, order; + if(geometry.CLASS_NAME.indexOf('LineString') > -1) { + stat = this.getBestLength(geometry); + order = 1; + } else { + stat = this.getBestArea(geometry); + order = 2; + } + this.events.triggerEvent(eventType, { + measure: stat[0], + units: stat[1], + order: order, + geometry: geometry + }); + }, + + /** + * Method: getBestArea + * Based on the <displaySystem> returns the area of a geometry. + * + * Parameters: + * geometry - {<OpenLayers.Geometry>} + * + * Returns: + * {Array([Float, String])} Returns a two item array containing the + * area and the units abbreviation. + */ + getBestArea: function(geometry) { + var units = this.displaySystemUnits[this.displaySystem]; + var unit, area; + for(var i=0, len=units.length; i<len; ++i) { + unit = units[i]; + area = this.getArea(geometry, unit); + if(area > 1) { + break; + } + } + return [area, unit]; + }, + + /** + * Method: getArea + * + * Parameters: + * geometry - {<OpenLayers.Geometry>} + * units - {String} Unit abbreviation + * + * Returns: + * {Float} The geometry area in the given units. + */ + getArea: function(geometry, units) { + var area, geomUnits; + if(this.geodesic) { + area = geometry.getGeodesicArea(this.map.getProjectionObject()); + geomUnits = "m"; + } else { + area = geometry.getArea(); + geomUnits = this.map.getUnits(); + } + var inPerDisplayUnit = OpenLayers.INCHES_PER_UNIT[units]; + if(inPerDisplayUnit) { + var inPerMapUnit = OpenLayers.INCHES_PER_UNIT[geomUnits]; + area *= Math.pow((inPerMapUnit / inPerDisplayUnit), 2); + } + return area; + }, + + /** + * Method: getBestLength + * Based on the <displaySystem> returns the length of a geometry. + * + * Parameters: + * geometry - {<OpenLayers.Geometry>} + * + * Returns: + * {Array([Float, String])} Returns a two item array containing the + * length and the units abbreviation. + */ + getBestLength: function(geometry) { + var units = this.displaySystemUnits[this.displaySystem]; + var unit, length; + for(var i=0, len=units.length; i<len; ++i) { + unit = units[i]; + length = this.getLength(geometry, unit); + if(length > 1) { + break; + } + } + return [length, unit]; + }, + + /** + * Method: getLength + * + * Parameters: + * geometry - {<OpenLayers.Geometry>} + * units - {String} Unit abbreviation + * + * Returns: + * {Float} The geometry length in the given units. + */ + getLength: function(geometry, units) { + var length, geomUnits; + if(this.geodesic) { + length = geometry.getGeodesicLength(this.map.getProjectionObject()); + geomUnits = "m"; + } else { + length = geometry.getLength(); + geomUnits = this.map.getUnits(); + } + var inPerDisplayUnit = OpenLayers.INCHES_PER_UNIT[units]; + if(inPerDisplayUnit) { + var inPerMapUnit = OpenLayers.INCHES_PER_UNIT[geomUnits]; + length *= (inPerMapUnit / inPerDisplayUnit); + } + return length; + }, + + CLASS_NAME: "OpenLayers.Control.Measure" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/ModifyFeature.js b/misc/openlayers/lib/OpenLayers/Control/ModifyFeature.js new file mode 100644 index 0000000..e574608 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/ModifyFeature.js @@ -0,0 +1,835 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Handler/Drag.js + * @requires OpenLayers/Handler/Keyboard.js + */ + +/** + * Class: OpenLayers.Control.ModifyFeature + * Control to modify features. When activated, a click renders the vertices + * of a feature - these vertices can then be dragged. By default, the + * delete key will delete the vertex under the mouse. New features are + * added by dragging "virtual vertices" between vertices. Create a new + * control with the <OpenLayers.Control.ModifyFeature> constructor. + * + * Inherits From: + * - <OpenLayers.Control> + */ +OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: documentDrag + * {Boolean} If set to true, dragging vertices will continue even if the + * mouse cursor leaves the map viewport. Default is false. + */ + documentDrag: false, + + /** + * APIProperty: geometryTypes + * {Array(String)} To restrict modification to a limited set of geometry + * types, send a list of strings corresponding to the geometry class + * names. + */ + geometryTypes: null, + + /** + * APIProperty: clickout + * {Boolean} Unselect features when clicking outside any feature. + * Default is true. + */ + clickout: true, + + /** + * APIProperty: toggle + * {Boolean} Unselect a selected feature on click. + * Default is true. + */ + toggle: true, + + /** + * APIProperty: standalone + * {Boolean} Set to true to create a control without SelectFeature + * capabilities. Default is false. If standalone is true, to modify + * a feature, call the <selectFeature> method with the target feature. + * Note that you must call the <unselectFeature> method to finish + * feature modification in standalone mode (before starting to modify + * another feature). + */ + standalone: false, + + /** + * Property: layer + * {<OpenLayers.Layer.Vector>} + */ + layer: null, + + /** + * Property: feature + * {<OpenLayers.Feature.Vector>} Feature currently available for modification. + */ + feature: null, + + /** + * Property: vertex + * {<OpenLayers.Feature.Vector>} Vertex currently being modified. + */ + vertex: null, + + /** + * Property: vertices + * {Array(<OpenLayers.Feature.Vector>)} Verticies currently available + * for dragging. + */ + vertices: null, + + /** + * Property: virtualVertices + * {Array(<OpenLayers.Feature.Vector>)} Virtual vertices in the middle + * of each edge. + */ + virtualVertices: null, + + /** + * Property: handlers + * {Object} + */ + handlers: null, + + /** + * APIProperty: deleteCodes + * {Array(Integer)} Keycodes for deleting verticies. Set to null to disable + * vertex deltion by keypress. If non-null, keypresses with codes + * in this array will delete vertices under the mouse. Default + * is 46 and 68, the 'delete' and lowercase 'd' keys. + */ + deleteCodes: null, + + /** + * APIProperty: virtualStyle + * {Object} A symbolizer to be used for virtual vertices. + */ + virtualStyle: null, + + /** + * APIProperty: vertexRenderIntent + * {String} The renderIntent to use for vertices. If no <virtualStyle> is + * provided, this renderIntent will also be used for virtual vertices, with + * a fillOpacity and strokeOpacity of 0.3. Default is null, which means + * that the layer's default style will be used for vertices. + */ + vertexRenderIntent: null, + + /** + * APIProperty: mode + * {Integer} Bitfields specifying the modification mode. Defaults to + * OpenLayers.Control.ModifyFeature.RESHAPE. To set the mode to a + * combination of options, use the | operator. For example, to allow + * the control to both resize and rotate features, use the following + * syntax + * (code) + * control.mode = OpenLayers.Control.ModifyFeature.RESIZE | + * OpenLayers.Control.ModifyFeature.ROTATE; + * (end) + */ + mode: null, + + /** + * APIProperty: createVertices + * {Boolean} Create new vertices by dragging the virtual vertices + * in the middle of each edge. Default is true. + */ + createVertices: true, + + /** + * Property: modified + * {Boolean} The currently selected feature has been modified. + */ + modified: false, + + /** + * Property: radiusHandle + * {<OpenLayers.Feature.Vector>} A handle for rotating/resizing a feature. + */ + radiusHandle: null, + + /** + * Property: dragHandle + * {<OpenLayers.Feature.Vector>} A handle for dragging a feature. + */ + dragHandle: null, + + /** + * APIProperty: onModificationStart + * {Function} *Deprecated*. Register for "beforefeaturemodified" instead. + * The "beforefeaturemodified" event is triggered on the layer before + * any modification begins. + * + * Optional function to be called when a feature is selected + * to be modified. The function should expect to be called with a + * feature. This could be used for example to allow to lock the + * feature on server-side. + */ + onModificationStart: function() {}, + + /** + * APIProperty: onModification + * {Function} *Deprecated*. Register for "featuremodified" instead. + * The "featuremodified" event is triggered on the layer with each + * feature modification. + * + * Optional function to be called when a feature has been + * modified. The function should expect to be called with a feature. + */ + onModification: function() {}, + + /** + * APIProperty: onModificationEnd + * {Function} *Deprecated*. Register for "afterfeaturemodified" instead. + * The "afterfeaturemodified" event is triggered on the layer after + * a feature has been modified. + * + * Optional function to be called when a feature is finished + * being modified. The function should expect to be called with a + * feature. + */ + onModificationEnd: function() {}, + + /** + * Constructor: OpenLayers.Control.ModifyFeature + * Create a new modify feature control. + * + * Parameters: + * layer - {<OpenLayers.Layer.Vector>} Layer that contains features that + * will be modified. + * options - {Object} Optional object whose properties will be set on the + * control. + */ + initialize: function(layer, options) { + options = options || {}; + this.layer = layer; + this.vertices = []; + this.virtualVertices = []; + this.virtualStyle = OpenLayers.Util.extend({}, + this.layer.style || + this.layer.styleMap.createSymbolizer(null, options.vertexRenderIntent) + ); + this.virtualStyle.fillOpacity = 0.3; + this.virtualStyle.strokeOpacity = 0.3; + this.deleteCodes = [46, 68]; + this.mode = OpenLayers.Control.ModifyFeature.RESHAPE; + OpenLayers.Control.prototype.initialize.apply(this, [options]); + if(!(OpenLayers.Util.isArray(this.deleteCodes))) { + this.deleteCodes = [this.deleteCodes]; + } + + // configure the drag handler + var dragCallbacks = { + down: function(pixel) { + this.vertex = null; + var feature = this.layer.getFeatureFromEvent( + this.handlers.drag.evt); + if (feature) { + this.dragStart(feature); + } else if (this.clickout) { + this._unselect = this.feature; + } + }, + move: function(pixel) { + delete this._unselect; + if (this.vertex) { + this.dragVertex(this.vertex, pixel); + } + }, + up: function() { + this.handlers.drag.stopDown = false; + if (this._unselect) { + this.unselectFeature(this._unselect); + delete this._unselect; + } + }, + done: function(pixel) { + if (this.vertex) { + this.dragComplete(this.vertex); + } + } + }; + var dragOptions = { + documentDrag: this.documentDrag, + stopDown: false + }; + + // configure the keyboard handler + var keyboardOptions = { + keydown: this.handleKeypress + }; + this.handlers = { + keyboard: new OpenLayers.Handler.Keyboard(this, keyboardOptions), + drag: new OpenLayers.Handler.Drag(this, dragCallbacks, dragOptions) + }; + }, + + /** + * APIMethod: destroy + * Take care of things that are not handled in superclass. + */ + destroy: function() { + if (this.map) { + this.map.events.un({ + "removelayer": this.handleMapEvents, + "changelayer": this.handleMapEvents, + scope: this + }); + } + this.layer = null; + OpenLayers.Control.prototype.destroy.apply(this, []); + }, + + /** + * APIMethod: activate + * Activate the control. + * + * Returns: + * {Boolean} Successfully activated the control. + */ + activate: function() { + this.moveLayerToTop(); + this.map.events.on({ + "removelayer": this.handleMapEvents, + "changelayer": this.handleMapEvents, + scope: this + }); + return (this.handlers.keyboard.activate() && + this.handlers.drag.activate() && + OpenLayers.Control.prototype.activate.apply(this, arguments)); + }, + + /** + * APIMethod: deactivate + * Deactivate the control. + * + * Returns: + * {Boolean} Successfully deactivated the control. + */ + deactivate: function() { + var deactivated = false; + // the return from the controls is unimportant in this case + if(OpenLayers.Control.prototype.deactivate.apply(this, arguments)) { + this.moveLayerBack(); + this.map.events.un({ + "removelayer": this.handleMapEvents, + "changelayer": this.handleMapEvents, + scope: this + }); + this.layer.removeFeatures(this.vertices, {silent: true}); + this.layer.removeFeatures(this.virtualVertices, {silent: true}); + this.vertices = []; + this.handlers.drag.deactivate(); + this.handlers.keyboard.deactivate(); + var feature = this.feature; + if (feature && feature.geometry && feature.layer) { + this.unselectFeature(feature); + } + deactivated = true; + } + return deactivated; + }, + + /** + * Method: beforeSelectFeature + * Called before a feature is selected. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} The feature about to be selected. + */ + beforeSelectFeature: function(feature) { + return this.layer.events.triggerEvent( + "beforefeaturemodified", {feature: feature} + ); + }, + + /** + * APIMethod: selectFeature + * Select a feature for modification in standalone mode. In non-standalone + * mode, this method is called when a feature is selected by clicking. + * Register a listener to the beforefeaturemodified event and return false + * to prevent feature modification. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} the selected feature. + */ + selectFeature: function(feature) { + if (this.feature === feature || + (this.geometryTypes && OpenLayers.Util.indexOf(this.geometryTypes, + feature.geometry.CLASS_NAME) == -1)) { + return; + } + if (this.beforeSelectFeature(feature) !== false) { + if (this.feature) { + this.unselectFeature(this.feature); + } + this.feature = feature; + this.layer.selectedFeatures.push(feature); + this.layer.drawFeature(feature, 'select'); + this.modified = false; + this.resetVertices(); + this.onModificationStart(this.feature); + } + // keep track of geometry modifications + var modified = feature.modified; + if (feature.geometry && !(modified && modified.geometry)) { + this._originalGeometry = feature.geometry.clone(); + } + }, + + /** + * APIMethod: unselectFeature + * Called when the select feature control unselects a feature. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} The unselected feature. + */ + unselectFeature: function(feature) { + this.layer.removeFeatures(this.vertices, {silent: true}); + this.vertices = []; + this.layer.destroyFeatures(this.virtualVertices, {silent: true}); + this.virtualVertices = []; + if(this.dragHandle) { + this.layer.destroyFeatures([this.dragHandle], {silent: true}); + delete this.dragHandle; + } + if(this.radiusHandle) { + this.layer.destroyFeatures([this.radiusHandle], {silent: true}); + delete this.radiusHandle; + } + this.layer.drawFeature(this.feature, 'default'); + this.feature = null; + OpenLayers.Util.removeItem(this.layer.selectedFeatures, feature); + this.onModificationEnd(feature); + this.layer.events.triggerEvent("afterfeaturemodified", { + feature: feature, + modified: this.modified + }); + this.modified = false; + }, + + + /** + * Method: dragStart + * Called by the drag handler before a feature is dragged. This method is + * used to differentiate between points and vertices + * of higher order geometries. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} The point or vertex about to be + * dragged. + */ + dragStart: function(feature) { + var isPoint = feature.geometry.CLASS_NAME == + 'OpenLayers.Geometry.Point'; + if (!this.standalone && + ((!feature._sketch && isPoint) || !feature._sketch)) { + if (this.toggle && this.feature === feature) { + // mark feature for unselection + this._unselect = feature; + } + this.selectFeature(feature); + } + if (feature._sketch || isPoint) { + // feature is a drag or virtual handle or point + this.vertex = feature; + this.handlers.drag.stopDown = true; + } + }, + + /** + * Method: dragVertex + * Called by the drag handler with each drag move of a vertex. + * + * Parameters: + * vertex - {<OpenLayers.Feature.Vector>} The vertex being dragged. + * pixel - {<OpenLayers.Pixel>} Pixel location of the mouse event. + */ + dragVertex: function(vertex, pixel) { + var pos = this.map.getLonLatFromViewPortPx(pixel); + var geom = vertex.geometry; + geom.move(pos.lon - geom.x, pos.lat - geom.y); + this.modified = true; + /** + * Five cases: + * 1) dragging a simple point + * 2) dragging a virtual vertex + * 3) dragging a drag handle + * 4) dragging a real vertex + * 5) dragging a radius handle + */ + if(this.feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") { + // dragging a simple point + this.layer.events.triggerEvent("vertexmodified", { + vertex: vertex.geometry, + feature: this.feature, + pixel: pixel + }); + } else { + if(vertex._index) { + // dragging a virtual vertex + vertex.geometry.parent.addComponent(vertex.geometry, + vertex._index); + // move from virtual to real vertex + delete vertex._index; + OpenLayers.Util.removeItem(this.virtualVertices, vertex); + this.vertices.push(vertex); + } else if(vertex == this.dragHandle) { + // dragging a drag handle + this.layer.removeFeatures(this.vertices, {silent: true}); + this.vertices = []; + if(this.radiusHandle) { + this.layer.destroyFeatures([this.radiusHandle], {silent: true}); + this.radiusHandle = null; + } + } else if(vertex !== this.radiusHandle) { + // dragging a real vertex + this.layer.events.triggerEvent("vertexmodified", { + vertex: vertex.geometry, + feature: this.feature, + pixel: pixel + }); + } + // dragging a radius handle - no special treatment + if(this.virtualVertices.length > 0) { + this.layer.destroyFeatures(this.virtualVertices, {silent: true}); + this.virtualVertices = []; + } + this.layer.drawFeature(this.feature, this.standalone ? undefined : + 'select'); + } + // keep the vertex on top so it gets the mouseout after dragging + // this should be removed in favor of an option to draw under or + // maintain node z-index + this.layer.drawFeature(vertex); + }, + + /** + * Method: dragComplete + * Called by the drag handler when the feature dragging is complete. + * + * Parameters: + * vertex - {<OpenLayers.Feature.Vector>} The vertex being dragged. + */ + dragComplete: function(vertex) { + this.resetVertices(); + this.setFeatureState(); + this.onModification(this.feature); + this.layer.events.triggerEvent("featuremodified", + {feature: this.feature}); + }, + + /** + * Method: setFeatureState + * Called when the feature is modified. If the current state is not + * INSERT or DELETE, the state is set to UPDATE. + */ + setFeatureState: function() { + if(this.feature.state != OpenLayers.State.INSERT && + this.feature.state != OpenLayers.State.DELETE) { + this.feature.state = OpenLayers.State.UPDATE; + if (this.modified && this._originalGeometry) { + var feature = this.feature; + feature.modified = OpenLayers.Util.extend(feature.modified, { + geometry: this._originalGeometry + }); + delete this._originalGeometry; + } + } + }, + + /** + * Method: resetVertices + */ + resetVertices: function() { + if(this.vertices.length > 0) { + this.layer.removeFeatures(this.vertices, {silent: true}); + this.vertices = []; + } + if(this.virtualVertices.length > 0) { + this.layer.removeFeatures(this.virtualVertices, {silent: true}); + this.virtualVertices = []; + } + if(this.dragHandle) { + this.layer.destroyFeatures([this.dragHandle], {silent: true}); + this.dragHandle = null; + } + if(this.radiusHandle) { + this.layer.destroyFeatures([this.radiusHandle], {silent: true}); + this.radiusHandle = null; + } + if(this.feature && + this.feature.geometry.CLASS_NAME != "OpenLayers.Geometry.Point") { + if((this.mode & OpenLayers.Control.ModifyFeature.DRAG)) { + this.collectDragHandle(); + } + if((this.mode & (OpenLayers.Control.ModifyFeature.ROTATE | + OpenLayers.Control.ModifyFeature.RESIZE))) { + this.collectRadiusHandle(); + } + if(this.mode & OpenLayers.Control.ModifyFeature.RESHAPE){ + // Don't collect vertices when we're resizing + if (!(this.mode & OpenLayers.Control.ModifyFeature.RESIZE)){ + this.collectVertices(); + } + } + } + }, + + /** + * Method: handleKeypress + * Called by the feature handler on keypress. This is used to delete + * vertices. If the <deleteCode> property is set, vertices will + * be deleted when a feature is selected for modification and + * the mouse is over a vertex. + * + * Parameters: + * evt - {Event} Keypress event. + */ + handleKeypress: function(evt) { + var code = evt.keyCode; + + // check for delete key + if(this.feature && + OpenLayers.Util.indexOf(this.deleteCodes, code) != -1) { + var vertex = this.layer.getFeatureFromEvent(this.handlers.drag.evt); + if (vertex && + OpenLayers.Util.indexOf(this.vertices, vertex) != -1 && + !this.handlers.drag.dragging && vertex.geometry.parent) { + // remove the vertex + vertex.geometry.parent.removeComponent(vertex.geometry); + this.layer.events.triggerEvent("vertexremoved", { + vertex: vertex.geometry, + feature: this.feature, + pixel: evt.xy + }); + this.layer.drawFeature(this.feature, this.standalone ? + undefined : 'select'); + this.modified = true; + this.resetVertices(); + this.setFeatureState(); + this.onModification(this.feature); + this.layer.events.triggerEvent("featuremodified", + {feature: this.feature}); + } + } + }, + + /** + * Method: collectVertices + * Collect the vertices from the modifiable feature's geometry and push + * them on to the control's vertices array. + */ + collectVertices: function() { + this.vertices = []; + this.virtualVertices = []; + var control = this; + function collectComponentVertices(geometry) { + var i, vertex, component, len; + if(geometry.CLASS_NAME == "OpenLayers.Geometry.Point") { + vertex = new OpenLayers.Feature.Vector(geometry); + vertex._sketch = true; + vertex.renderIntent = control.vertexRenderIntent; + control.vertices.push(vertex); + } else { + var numVert = geometry.components.length; + if(geometry.CLASS_NAME == "OpenLayers.Geometry.LinearRing") { + numVert -= 1; + } + for(i=0; i<numVert; ++i) { + component = geometry.components[i]; + if(component.CLASS_NAME == "OpenLayers.Geometry.Point") { + vertex = new OpenLayers.Feature.Vector(component); + vertex._sketch = true; + vertex.renderIntent = control.vertexRenderIntent; + control.vertices.push(vertex); + } else { + collectComponentVertices(component); + } + } + + // add virtual vertices in the middle of each edge + if (control.createVertices && geometry.CLASS_NAME != "OpenLayers.Geometry.MultiPoint") { + for(i=0, len=geometry.components.length; i<len-1; ++i) { + var prevVertex = geometry.components[i]; + var nextVertex = geometry.components[i + 1]; + if(prevVertex.CLASS_NAME == "OpenLayers.Geometry.Point" && + nextVertex.CLASS_NAME == "OpenLayers.Geometry.Point") { + var x = (prevVertex.x + nextVertex.x) / 2; + var y = (prevVertex.y + nextVertex.y) / 2; + var point = new OpenLayers.Feature.Vector( + new OpenLayers.Geometry.Point(x, y), + null, control.virtualStyle + ); + // set the virtual parent and intended index + point.geometry.parent = geometry; + point._index = i + 1; + point._sketch = true; + control.virtualVertices.push(point); + } + } + } + } + } + collectComponentVertices.call(this, this.feature.geometry); + this.layer.addFeatures(this.virtualVertices, {silent: true}); + this.layer.addFeatures(this.vertices, {silent: true}); + }, + + /** + * Method: collectDragHandle + * Collect the drag handle for the selected geometry. + */ + collectDragHandle: function() { + var geometry = this.feature.geometry; + var center = geometry.getBounds().getCenterLonLat(); + var originGeometry = new OpenLayers.Geometry.Point( + center.lon, center.lat + ); + var origin = new OpenLayers.Feature.Vector(originGeometry); + originGeometry.move = function(x, y) { + OpenLayers.Geometry.Point.prototype.move.call(this, x, y); + geometry.move(x, y); + }; + origin._sketch = true; + this.dragHandle = origin; + this.dragHandle.renderIntent = this.vertexRenderIntent; + this.layer.addFeatures([this.dragHandle], {silent: true}); + }, + + /** + * Method: collectRadiusHandle + * Collect the radius handle for the selected geometry. + */ + collectRadiusHandle: function() { + var geometry = this.feature.geometry; + var bounds = geometry.getBounds(); + var center = bounds.getCenterLonLat(); + var originGeometry = new OpenLayers.Geometry.Point( + center.lon, center.lat + ); + var radiusGeometry = new OpenLayers.Geometry.Point( + bounds.right, bounds.bottom + ); + var radius = new OpenLayers.Feature.Vector(radiusGeometry); + var resize = (this.mode & OpenLayers.Control.ModifyFeature.RESIZE); + var reshape = (this.mode & OpenLayers.Control.ModifyFeature.RESHAPE); + var rotate = (this.mode & OpenLayers.Control.ModifyFeature.ROTATE); + + radiusGeometry.move = function(x, y) { + OpenLayers.Geometry.Point.prototype.move.call(this, x, y); + var dx1 = this.x - originGeometry.x; + var dy1 = this.y - originGeometry.y; + var dx0 = dx1 - x; + var dy0 = dy1 - y; + if(rotate) { + var a0 = Math.atan2(dy0, dx0); + var a1 = Math.atan2(dy1, dx1); + var angle = a1 - a0; + angle *= 180 / Math.PI; + geometry.rotate(angle, originGeometry); + } + if(resize) { + var scale, ratio; + // 'resize' together with 'reshape' implies that the aspect + // ratio of the geometry will not be preserved whilst resizing + if (reshape) { + scale = dy1 / dy0; + ratio = (dx1 / dx0) / scale; + } else { + var l0 = Math.sqrt((dx0 * dx0) + (dy0 * dy0)); + var l1 = Math.sqrt((dx1 * dx1) + (dy1 * dy1)); + scale = l1 / l0; + } + geometry.resize(scale, originGeometry, ratio); + } + }; + radius._sketch = true; + this.radiusHandle = radius; + this.radiusHandle.renderIntent = this.vertexRenderIntent; + this.layer.addFeatures([this.radiusHandle], {silent: true}); + }, + + /** + * Method: setMap + * Set the map property for the control and all handlers. + * + * Parameters: + * map - {<OpenLayers.Map>} The control's map. + */ + setMap: function(map) { + this.handlers.drag.setMap(map); + OpenLayers.Control.prototype.setMap.apply(this, arguments); + }, + + /** + * Method: handleMapEvents + * + * Parameters: + * evt - {Object} + */ + handleMapEvents: function(evt) { + if (evt.type == "removelayer" || evt.property == "order") { + this.moveLayerToTop(); + } + }, + + /** + * Method: moveLayerToTop + * Moves the layer for this handler to the top, so mouse events can reach + * it. + */ + moveLayerToTop: function() { + var index = Math.max(this.map.Z_INDEX_BASE['Feature'] - 1, + this.layer.getZIndex()) + 1; + this.layer.setZIndex(index); + + }, + + /** + * Method: moveLayerBack + * Moves the layer back to the position determined by the map's layers + * array. + */ + moveLayerBack: function() { + var index = this.layer.getZIndex() - 1; + if (index >= this.map.Z_INDEX_BASE['Feature']) { + this.layer.setZIndex(index); + } else { + this.map.setLayerZIndex(this.layer, + this.map.getLayerIndex(this.layer)); + } + }, + + CLASS_NAME: "OpenLayers.Control.ModifyFeature" +}); + +/** + * Constant: RESHAPE + * {Integer} Constant used to make the control work in reshape mode + */ +OpenLayers.Control.ModifyFeature.RESHAPE = 1; +/** + * Constant: RESIZE + * {Integer} Constant used to make the control work in resize mode + */ +OpenLayers.Control.ModifyFeature.RESIZE = 2; +/** + * Constant: ROTATE + * {Integer} Constant used to make the control work in rotate mode + */ +OpenLayers.Control.ModifyFeature.ROTATE = 4; +/** + * Constant: DRAG + * {Integer} Constant used to make the control work in drag mode + */ +OpenLayers.Control.ModifyFeature.DRAG = 8; diff --git a/misc/openlayers/lib/OpenLayers/Control/MousePosition.js b/misc/openlayers/lib/OpenLayers/Control/MousePosition.js new file mode 100644 index 0000000..0c88fcf --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/MousePosition.js @@ -0,0 +1,227 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + + +/** + * @requires OpenLayers/Control.js + */ + +/** + * Class: OpenLayers.Control.MousePosition + * The MousePosition control displays geographic coordinates of the mouse + * pointer, as it is moved about the map. + * + * You can use the <prefix>- or <suffix>-properties to provide more information + * about the displayed coordinates to the user: + * + * (code) + * var mousePositionCtrl = new OpenLayers.Control.MousePosition({ + * prefix: '<a target="_blank" ' + + * 'href="http://spatialreference.org/ref/epsg/4326/">' + + * 'EPSG:4326</a> coordinates: ' + * } + * ); + * (end code) + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.MousePosition = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: autoActivate + * {Boolean} Activate the control when it is added to a map. Default is + * true. + */ + autoActivate: true, + + /** + * Property: element + * {DOMElement} + */ + element: null, + + /** + * APIProperty: prefix + * {String} A string to be prepended to the current pointers coordinates + * when it is rendered. Defaults to the empty string ''. + */ + prefix: '', + + /** + * APIProperty: separator + * {String} A string to be used to seperate the two coordinates from each + * other. Defaults to the string ', ', which will result in a + * rendered coordinate of e.g. '42.12, 21.22'. + */ + separator: ', ', + + /** + * APIProperty: suffix + * {String} A string to be appended to the current pointers coordinates + * when it is rendered. Defaults to the empty string ''. + */ + suffix: '', + + /** + * APIProperty: numDigits + * {Integer} The number of digits each coordinate shall have when being + * rendered, Defaults to 5. + */ + numDigits: 5, + + /** + * APIProperty: granularity + * {Integer} + */ + granularity: 10, + + /** + * APIProperty: emptyString + * {String} Set this to some value to set when the mouse is outside the + * map. + */ + emptyString: null, + + /** + * Property: lastXy + * {<OpenLayers.Pixel>} + */ + lastXy: null, + + /** + * APIProperty: displayProjection + * {<OpenLayers.Projection>} The projection in which the mouse position is + * displayed. + */ + displayProjection: null, + + /** + * Constructor: OpenLayers.Control.MousePosition + * + * Parameters: + * options - {Object} Options for control. + */ + + /** + * Method: destroy + */ + destroy: function() { + this.deactivate(); + OpenLayers.Control.prototype.destroy.apply(this, arguments); + }, + + /** + * APIMethod: activate + */ + activate: function() { + if (OpenLayers.Control.prototype.activate.apply(this, arguments)) { + this.map.events.register('mousemove', this, this.redraw); + this.map.events.register('mouseout', this, this.reset); + this.redraw(); + return true; + } else { + return false; + } + }, + + /** + * APIMethod: deactivate + */ + deactivate: function() { + if (OpenLayers.Control.prototype.deactivate.apply(this, arguments)) { + this.map.events.unregister('mousemove', this, this.redraw); + this.map.events.unregister('mouseout', this, this.reset); + this.element.innerHTML = ""; + return true; + } else { + return false; + } + }, + + /** + * Method: draw + * {DOMElement} + */ + draw: function() { + OpenLayers.Control.prototype.draw.apply(this, arguments); + + if (!this.element) { + this.div.left = ""; + this.div.top = ""; + this.element = this.div; + } + + return this.div; + }, + + /** + * Method: redraw + */ + redraw: function(evt) { + + var lonLat; + + if (evt == null) { + this.reset(); + return; + } else { + if (this.lastXy == null || + Math.abs(evt.xy.x - this.lastXy.x) > this.granularity || + Math.abs(evt.xy.y - this.lastXy.y) > this.granularity) + { + this.lastXy = evt.xy; + return; + } + + lonLat = this.map.getLonLatFromPixel(evt.xy); + if (!lonLat) { + // map has not yet been properly initialized + return; + } + if (this.displayProjection) { + lonLat.transform(this.map.getProjectionObject(), + this.displayProjection ); + } + this.lastXy = evt.xy; + + } + + var newHtml = this.formatOutput(lonLat); + + if (newHtml != this.element.innerHTML) { + this.element.innerHTML = newHtml; + } + }, + + /** + * Method: reset + */ + reset: function(evt) { + if (this.emptyString != null) { + this.element.innerHTML = this.emptyString; + } + }, + + /** + * Method: formatOutput + * Override to provide custom display output + * + * Parameters: + * lonLat - {<OpenLayers.LonLat>} Location to display + */ + formatOutput: function(lonLat) { + var digits = parseInt(this.numDigits); + var newHtml = + this.prefix + + lonLat.lon.toFixed(digits) + + this.separator + + lonLat.lat.toFixed(digits) + + this.suffix; + return newHtml; + }, + + CLASS_NAME: "OpenLayers.Control.MousePosition" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/NavToolbar.js b/misc/openlayers/lib/OpenLayers/Control/NavToolbar.js new file mode 100644 index 0000000..b6bc2aa --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/NavToolbar.js @@ -0,0 +1,57 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control/Panel.js + * @requires OpenLayers/Control/Navigation.js + * @requires OpenLayers/Control/ZoomBox.js + */ + +/** + * Class: OpenLayers.Control.NavToolbar + * This Toolbar is an alternative to the Navigation control that displays + * the state of the control, and provides a UI for changing state to + * use the zoomBox via a Panel control. + * + * If you wish to change the properties of the Navigation control used + * in the NavToolbar, see: + * http://trac.openlayers.org/wiki/Toolbars#SubclassingNavToolbar + * + * + * Inherits from: + * - <OpenLayers.Control.Panel> + */ +OpenLayers.Control.NavToolbar = OpenLayers.Class(OpenLayers.Control.Panel, { + + /** + * Constructor: OpenLayers.Control.NavToolbar + * Add our two mousedefaults controls. + * + * Parameters: + * options - {Object} An optional object whose properties will be used + * to extend the control. + */ + initialize: function(options) { + OpenLayers.Control.Panel.prototype.initialize.apply(this, [options]); + this.addControls([ + new OpenLayers.Control.Navigation(), + new OpenLayers.Control.ZoomBox() + ]); + }, + + /** + * Method: draw + * calls the default draw, and then activates mouse defaults. + */ + draw: function() { + var div = OpenLayers.Control.Panel.prototype.draw.apply(this, arguments); + if (this.defaultControl === null) { + this.defaultControl = this.controls[0]; + } + return div; + }, + + CLASS_NAME: "OpenLayers.Control.NavToolbar" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/Navigation.js b/misc/openlayers/lib/OpenLayers/Control/Navigation.js new file mode 100644 index 0000000..d50e131 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/Navigation.js @@ -0,0 +1,345 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control/ZoomBox.js + * @requires OpenLayers/Control/DragPan.js + * @requires OpenLayers/Handler/MouseWheel.js + * @requires OpenLayers/Handler/Click.js + */ + +/** + * Class: OpenLayers.Control.Navigation + * The navigation control handles map browsing with mouse events (dragging, + * double-clicking, and scrolling the wheel). Create a new navigation + * control with the <OpenLayers.Control.Navigation> control. + * + * Note that this control is added to the map by default (if no controls + * array is sent in the options object to the <OpenLayers.Map> + * constructor). + * + * Inherits: + * - <OpenLayers.Control> + */ +OpenLayers.Control.Navigation = OpenLayers.Class(OpenLayers.Control, { + + /** + * Property: dragPan + * {<OpenLayers.Control.DragPan>} + */ + dragPan: null, + + /** + * APIProperty: dragPanOptions + * {Object} Options passed to the DragPan control. + */ + dragPanOptions: null, + + /** + * Property: pinchZoom + * {<OpenLayers.Control.PinchZoom>} + */ + pinchZoom: null, + + /** + * APIProperty: pinchZoomOptions + * {Object} Options passed to the PinchZoom control. + */ + pinchZoomOptions: null, + + /** + * APIProperty: documentDrag + * {Boolean} Allow panning of the map by dragging outside map viewport. + * Default is false. + */ + documentDrag: false, + + /** + * Property: zoomBox + * {<OpenLayers.Control.ZoomBox>} + */ + zoomBox: null, + + /** + * APIProperty: zoomBoxEnabled + * {Boolean} Whether the user can draw a box to zoom + */ + zoomBoxEnabled: true, + + /** + * APIProperty: zoomWheelEnabled + * {Boolean} Whether the mousewheel should zoom the map + */ + zoomWheelEnabled: true, + + /** + * Property: mouseWheelOptions + * {Object} Options passed to the MouseWheel control (only useful if + * <zoomWheelEnabled> is set to true). Default is no options for maps + * with fractionalZoom set to true, otherwise + * {cumulative: false, interval: 50, maxDelta: 6} + */ + mouseWheelOptions: null, + + /** + * APIProperty: handleRightClicks + * {Boolean} Whether or not to handle right clicks. Default is false. + */ + handleRightClicks: false, + + /** + * APIProperty: zoomBoxKeyMask + * {Integer} <OpenLayers.Handler> key code of the key, which has to be + * pressed, while drawing the zoom box with the mouse on the screen. + * You should probably set handleRightClicks to true if you use this + * with MOD_CTRL, to disable the context menu for machines which use + * CTRL-Click as a right click. + * Default: <OpenLayers.Handler.MOD_SHIFT> + */ + zoomBoxKeyMask: OpenLayers.Handler.MOD_SHIFT, + + /** + * APIProperty: autoActivate + * {Boolean} Activate the control when it is added to a map. Default is + * true. + */ + autoActivate: true, + + /** + * Constructor: OpenLayers.Control.Navigation + * Create a new navigation control + * + * Parameters: + * options - {Object} An optional object whose properties will be set on + * the control + */ + initialize: function(options) { + this.handlers = {}; + OpenLayers.Control.prototype.initialize.apply(this, arguments); + }, + + /** + * Method: destroy + * The destroy method is used to perform any clean up before the control + * is dereferenced. Typically this is where event listeners are removed + * to prevent memory leaks. + */ + destroy: function() { + this.deactivate(); + + if (this.dragPan) { + this.dragPan.destroy(); + } + this.dragPan = null; + + if (this.zoomBox) { + this.zoomBox.destroy(); + } + this.zoomBox = null; + + if (this.pinchZoom) { + this.pinchZoom.destroy(); + } + this.pinchZoom = null; + + OpenLayers.Control.prototype.destroy.apply(this,arguments); + }, + + /** + * Method: activate + */ + activate: function() { + this.dragPan.activate(); + if (this.zoomWheelEnabled) { + this.handlers.wheel.activate(); + } + this.handlers.click.activate(); + if (this.zoomBoxEnabled) { + this.zoomBox.activate(); + } + if (this.pinchZoom) { + this.pinchZoom.activate(); + } + return OpenLayers.Control.prototype.activate.apply(this,arguments); + }, + + /** + * Method: deactivate + */ + deactivate: function() { + if (this.pinchZoom) { + this.pinchZoom.deactivate(); + } + this.zoomBox.deactivate(); + this.dragPan.deactivate(); + this.handlers.click.deactivate(); + this.handlers.wheel.deactivate(); + return OpenLayers.Control.prototype.deactivate.apply(this,arguments); + }, + + /** + * Method: draw + */ + draw: function() { + // disable right mouse context menu for support of right click events + if (this.handleRightClicks) { + this.map.viewPortDiv.oncontextmenu = OpenLayers.Function.False; + } + + var clickCallbacks = { + 'click': this.defaultClick, + 'dblclick': this.defaultDblClick, + 'dblrightclick': this.defaultDblRightClick + }; + var clickOptions = { + 'double': true, + 'stopDouble': true + }; + this.handlers.click = new OpenLayers.Handler.Click( + this, clickCallbacks, clickOptions + ); + this.dragPan = new OpenLayers.Control.DragPan( + OpenLayers.Util.extend({ + map: this.map, + documentDrag: this.documentDrag + }, this.dragPanOptions) + ); + this.zoomBox = new OpenLayers.Control.ZoomBox( + {map: this.map, keyMask: this.zoomBoxKeyMask}); + this.dragPan.draw(); + this.zoomBox.draw(); + var wheelOptions = this.map.fractionalZoom ? {} : { + cumulative: false, + interval: 50, + maxDelta: 6 + }; + this.handlers.wheel = new OpenLayers.Handler.MouseWheel( + this, {up : this.wheelUp, down: this.wheelDown}, + OpenLayers.Util.extend(wheelOptions, this.mouseWheelOptions) + ); + if (OpenLayers.Control.PinchZoom) { + this.pinchZoom = new OpenLayers.Control.PinchZoom( + OpenLayers.Util.extend( + {map: this.map}, this.pinchZoomOptions)); + } + }, + + /** + * Method: defaultClick + * + * Parameters: + * evt - {Event} + */ + defaultClick: function (evt) { + if (evt.lastTouches && evt.lastTouches.length == 2) { + this.map.zoomOut(); + } + }, + + /** + * Method: defaultDblClick + * + * Parameters: + * evt - {Event} + */ + defaultDblClick: function (evt) { + this.map.zoomTo(this.map.zoom + 1, evt.xy); + }, + + /** + * Method: defaultDblRightClick + * + * Parameters: + * evt - {Event} + */ + defaultDblRightClick: function (evt) { + this.map.zoomTo(this.map.zoom - 1, evt.xy); + }, + + /** + * Method: wheelChange + * + * Parameters: + * evt - {Event} + * deltaZ - {Integer} + */ + wheelChange: function(evt, deltaZ) { + if (!this.map.fractionalZoom) { + deltaZ = Math.round(deltaZ); + } + var currentZoom = this.map.getZoom(), + newZoom = currentZoom + deltaZ; + newZoom = Math.max(newZoom, 0); + newZoom = Math.min(newZoom, this.map.getNumZoomLevels()); + if (newZoom === currentZoom) { + return; + } + this.map.zoomTo(newZoom, evt.xy); + }, + + /** + * Method: wheelUp + * User spun scroll wheel up + * + * Parameters: + * evt - {Event} + * delta - {Integer} + */ + wheelUp: function(evt, delta) { + this.wheelChange(evt, delta || 1); + }, + + /** + * Method: wheelDown + * User spun scroll wheel down + * + * Parameters: + * evt - {Event} + * delta - {Integer} + */ + wheelDown: function(evt, delta) { + this.wheelChange(evt, delta || -1); + }, + + /** + * Method: disableZoomBox + */ + disableZoomBox : function() { + this.zoomBoxEnabled = false; + this.zoomBox.deactivate(); + }, + + /** + * Method: enableZoomBox + */ + enableZoomBox : function() { + this.zoomBoxEnabled = true; + if (this.active) { + this.zoomBox.activate(); + } + }, + + /** + * Method: disableZoomWheel + */ + + disableZoomWheel : function() { + this.zoomWheelEnabled = false; + this.handlers.wheel.deactivate(); + }, + + /** + * Method: enableZoomWheel + */ + + enableZoomWheel : function() { + this.zoomWheelEnabled = true; + if (this.active) { + this.handlers.wheel.activate(); + } + }, + + CLASS_NAME: "OpenLayers.Control.Navigation" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/NavigationHistory.js b/misc/openlayers/lib/OpenLayers/Control/NavigationHistory.js new file mode 100644 index 0000000..bf2f95a --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/NavigationHistory.js @@ -0,0 +1,423 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Control/Button.js + */ + +/** + * Class: OpenLayers.Control.NavigationHistory + * A navigation history control. This is a meta-control, that creates two + * dependent controls: <previous> and <next>. Call the trigger method + * on the <previous> and <next> controls to restore previous and next + * history states. The previous and next controls will become active + * when there are available states to restore and will become deactive + * when there are no states to restore. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.NavigationHistory = OpenLayers.Class(OpenLayers.Control, { + + /** + * Property: type + * {String} Note that this control is not intended to be added directly + * to a control panel. Instead, add the sub-controls previous and + * next. These sub-controls are button type controls that activate + * and deactivate themselves. If this parent control is added to + * a panel, it will act as a toggle. + */ + type: OpenLayers.Control.TYPE_TOGGLE, + + /** + * APIProperty: previous + * {<OpenLayers.Control>} A button type control whose trigger method restores + * the previous state managed by this control. + */ + previous: null, + + /** + * APIProperty: previousOptions + * {Object} Set this property on the options argument of the constructor + * to set optional properties on the <previous> control. + */ + previousOptions: null, + + /** + * APIProperty: next + * {<OpenLayers.Control>} A button type control whose trigger method restores + * the next state managed by this control. + */ + next: null, + + /** + * APIProperty: nextOptions + * {Object} Set this property on the options argument of the constructor + * to set optional properties on the <next> control. + */ + nextOptions: null, + + /** + * APIProperty: limit + * {Integer} Optional limit on the number of history items to retain. If + * null, there is no limit. Default is 50. + */ + limit: 50, + + /** + * APIProperty: autoActivate + * {Boolean} Activate the control when it is added to a map. Default is + * true. + */ + autoActivate: true, + + /** + * Property: clearOnDeactivate + * {Boolean} Clear the history when the control is deactivated. Default + * is false. + */ + clearOnDeactivate: false, + + /** + * Property: registry + * {Object} An object with keys corresponding to event types. Values + * are functions that return an object representing the current state. + */ + registry: null, + + /** + * Property: nextStack + * {Array} Array of items in the history. + */ + nextStack: null, + + /** + * Property: previousStack + * {Array} List of items in the history. First item represents the current + * state. + */ + previousStack: null, + + /** + * Property: listeners + * {Object} An object containing properties corresponding to event types. + * This object is used to configure the control and is modified on + * construction. + */ + listeners: null, + + /** + * Property: restoring + * {Boolean} Currently restoring a history state. This is set to true + * before calling restore and set to false after restore returns. + */ + restoring: false, + + /** + * Constructor: OpenLayers.Control.NavigationHistory + * + * Parameters: + * options - {Object} An optional object whose properties will be used + * to extend the control. + */ + initialize: function(options) { + OpenLayers.Control.prototype.initialize.apply(this, [options]); + + this.registry = OpenLayers.Util.extend({ + "moveend": this.getState + }, this.registry); + + var previousOptions = { + trigger: OpenLayers.Function.bind(this.previousTrigger, this), + displayClass: this.displayClass + " " + this.displayClass + "Previous" + }; + OpenLayers.Util.extend(previousOptions, this.previousOptions); + this.previous = new OpenLayers.Control.Button(previousOptions); + + var nextOptions = { + trigger: OpenLayers.Function.bind(this.nextTrigger, this), + displayClass: this.displayClass + " " + this.displayClass + "Next" + }; + OpenLayers.Util.extend(nextOptions, this.nextOptions); + this.next = new OpenLayers.Control.Button(nextOptions); + + this.clear(); + }, + + /** + * Method: onPreviousChange + * Called when the previous history stack changes. + * + * Parameters: + * state - {Object} An object representing the state to be restored + * if previous is triggered again or null if no previous states remain. + * length - {Integer} The number of remaining previous states that can + * be restored. + */ + onPreviousChange: function(state, length) { + if(state && !this.previous.active) { + this.previous.activate(); + } else if(!state && this.previous.active) { + this.previous.deactivate(); + } + }, + + /** + * Method: onNextChange + * Called when the next history stack changes. + * + * Parameters: + * state - {Object} An object representing the state to be restored + * if next is triggered again or null if no next states remain. + * length - {Integer} The number of remaining next states that can + * be restored. + */ + onNextChange: function(state, length) { + if(state && !this.next.active) { + this.next.activate(); + } else if(!state && this.next.active) { + this.next.deactivate(); + } + }, + + /** + * APIMethod: destroy + * Destroy the control. + */ + destroy: function() { + OpenLayers.Control.prototype.destroy.apply(this); + this.previous.destroy(); + this.next.destroy(); + this.deactivate(); + for(var prop in this) { + this[prop] = null; + } + }, + + /** + * Method: setMap + * Set the map property for the control and <previous> and <next> child + * controls. + * + * Parameters: + * map - {<OpenLayers.Map>} + */ + setMap: function(map) { + this.map = map; + this.next.setMap(map); + this.previous.setMap(map); + }, + + /** + * Method: draw + * Called when the control is added to the map. + */ + draw: function() { + OpenLayers.Control.prototype.draw.apply(this, arguments); + this.next.draw(); + this.previous.draw(); + }, + + /** + * Method: previousTrigger + * Restore the previous state. If no items are in the previous history + * stack, this has no effect. + * + * Returns: + * {Object} Item representing state that was restored. Undefined if no + * items are in the previous history stack. + */ + previousTrigger: function() { + var current = this.previousStack.shift(); + var state = this.previousStack.shift(); + if(state != undefined) { + this.nextStack.unshift(current); + this.previousStack.unshift(state); + this.restoring = true; + this.restore(state); + this.restoring = false; + this.onNextChange(this.nextStack[0], this.nextStack.length); + this.onPreviousChange( + this.previousStack[1], this.previousStack.length - 1 + ); + } else { + this.previousStack.unshift(current); + } + return state; + }, + + /** + * APIMethod: nextTrigger + * Restore the next state. If no items are in the next history + * stack, this has no effect. The next history stack is populated + * as states are restored from the previous history stack. + * + * Returns: + * {Object} Item representing state that was restored. Undefined if no + * items are in the next history stack. + */ + nextTrigger: function() { + var state = this.nextStack.shift(); + if(state != undefined) { + this.previousStack.unshift(state); + this.restoring = true; + this.restore(state); + this.restoring = false; + this.onNextChange(this.nextStack[0], this.nextStack.length); + this.onPreviousChange( + this.previousStack[1], this.previousStack.length - 1 + ); + } + return state; + }, + + /** + * APIMethod: clear + * Clear history. + */ + clear: function() { + this.previousStack = []; + this.previous.deactivate(); + this.nextStack = []; + this.next.deactivate(); + }, + + /** + * Method: getState + * Get the current state and return it. + * + * Returns: + * {Object} An object representing the current state. + */ + getState: function() { + return { + center: this.map.getCenter(), + resolution: this.map.getResolution(), + projection: this.map.getProjectionObject(), + units: this.map.getProjectionObject().getUnits() || + this.map.units || this.map.baseLayer.units + }; + }, + + /** + * Method: restore + * Update the state with the given object. + * + * Parameters: + * state - {Object} An object representing the state to restore. + */ + restore: function(state) { + var center, zoom; + if (this.map.getProjectionObject() == state.projection) { + zoom = this.map.getZoomForResolution(state.resolution); + center = state.center; + } else { + center = state.center.clone(); + center.transform(state.projection, this.map.getProjectionObject()); + var sourceUnits = state.units; + var targetUnits = this.map.getProjectionObject().getUnits() || + this.map.units || this.map.baseLayer.units; + var resolutionFactor = sourceUnits && targetUnits ? + OpenLayers.INCHES_PER_UNIT[sourceUnits] / OpenLayers.INCHES_PER_UNIT[targetUnits] : 1; + zoom = this.map.getZoomForResolution(resolutionFactor*state.resolution); + } + this.map.setCenter(center, zoom); + }, + + /** + * Method: setListeners + * Sets functions to be registered in the listeners object. + */ + setListeners: function() { + this.listeners = {}; + for(var type in this.registry) { + this.listeners[type] = OpenLayers.Function.bind(function() { + if(!this.restoring) { + var state = this.registry[type].apply(this, arguments); + this.previousStack.unshift(state); + if(this.previousStack.length > 1) { + this.onPreviousChange( + this.previousStack[1], this.previousStack.length - 1 + ); + } + if(this.previousStack.length > (this.limit + 1)) { + this.previousStack.pop(); + } + if(this.nextStack.length > 0) { + this.nextStack = []; + this.onNextChange(null, 0); + } + } + return true; + }, this); + } + }, + + /** + * APIMethod: activate + * Activate the control. This registers any listeners. + * + * Returns: + * {Boolean} Control successfully activated. + */ + activate: function() { + var activated = false; + if(this.map) { + if(OpenLayers.Control.prototype.activate.apply(this)) { + if(this.listeners == null) { + this.setListeners(); + } + for(var type in this.listeners) { + this.map.events.register(type, this, this.listeners[type]); + } + activated = true; + if(this.previousStack.length == 0) { + this.initStack(); + } + } + } + return activated; + }, + + /** + * Method: initStack + * Called after the control is activated if the previous history stack is + * empty. + */ + initStack: function() { + if(this.map.getCenter()) { + this.listeners.moveend(); + } + }, + + /** + * APIMethod: deactivate + * Deactivate the control. This unregisters any listeners. + * + * Returns: + * {Boolean} Control successfully deactivated. + */ + deactivate: function() { + var deactivated = false; + if(this.map) { + if(OpenLayers.Control.prototype.deactivate.apply(this)) { + for(var type in this.listeners) { + this.map.events.unregister( + type, this, this.listeners[type] + ); + } + if(this.clearOnDeactivate) { + this.clear(); + } + deactivated = true; + } + } + return deactivated; + }, + + CLASS_NAME: "OpenLayers.Control.NavigationHistory" +}); + diff --git a/misc/openlayers/lib/OpenLayers/Control/OverviewMap.js b/misc/openlayers/lib/OpenLayers/Control/OverviewMap.js new file mode 100644 index 0000000..50b9300 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/OverviewMap.js @@ -0,0 +1,750 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/BaseTypes.js + * @requires OpenLayers/Events/buttonclick.js + * @requires OpenLayers/Map.js + * @requires OpenLayers/Handler/Click.js + * @requires OpenLayers/Handler/Drag.js + */ + +/** + * Class: OpenLayers.Control.OverviewMap + * The OverMap control creates a small overview map, useful to display the + * extent of a zoomed map and your main map and provide additional + * navigation options to the User. By default the overview map is drawn in + * the lower right corner of the main map. Create a new overview map with the + * <OpenLayers.Control.OverviewMap> constructor. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.OverviewMap = OpenLayers.Class(OpenLayers.Control, { + + /** + * Property: element + * {DOMElement} The DOM element that contains the overview map + */ + element: null, + + /** + * APIProperty: ovmap + * {<OpenLayers.Map>} A reference to the overview map itself. + */ + ovmap: null, + + /** + * APIProperty: size + * {<OpenLayers.Size>} The overvew map size in pixels. Note that this is + * the size of the map itself - the element that contains the map (default + * class name olControlOverviewMapElement) may have padding or other style + * attributes added via CSS. + */ + size: {w: 180, h: 90}, + + /** + * APIProperty: layers + * {Array(<OpenLayers.Layer>)} Ordered list of layers in the overview map. + * If none are sent at construction, the base layer for the main map is used. + */ + layers: null, + + /** + * APIProperty: minRectSize + * {Integer} The minimum width or height (in pixels) of the extent + * rectangle on the overview map. When the extent rectangle reaches + * this size, it will be replaced depending on the value of the + * <minRectDisplayClass> property. Default is 15 pixels. + */ + minRectSize: 15, + + /** + * APIProperty: minRectDisplayClass + * {String} Replacement style class name for the extent rectangle when + * <minRectSize> is reached. This string will be suffixed on to the + * displayClass. Default is "RectReplacement". + * + * Example CSS declaration: + * (code) + * .olControlOverviewMapRectReplacement { + * overflow: hidden; + * cursor: move; + * background-image: url("img/overview_replacement.gif"); + * background-repeat: no-repeat; + * background-position: center; + * } + * (end) + */ + minRectDisplayClass: "RectReplacement", + + /** + * APIProperty: minRatio + * {Float} The ratio of the overview map resolution to the main map + * resolution at which to zoom farther out on the overview map. + */ + minRatio: 8, + + /** + * APIProperty: maxRatio + * {Float} The ratio of the overview map resolution to the main map + * resolution at which to zoom farther in on the overview map. + */ + maxRatio: 32, + + /** + * APIProperty: mapOptions + * {Object} An object containing any non-default properties to be sent to + * the overview map's map constructor. These should include any + * non-default options that the main map was constructed with. + */ + mapOptions: null, + + /** + * APIProperty: autoPan + * {Boolean} Always pan the overview map, so the extent marker remains in + * the center. Default is false. If true, when you drag the extent + * marker, the overview map will update itself so the marker returns + * to the center. + */ + autoPan: false, + + /** + * Property: handlers + * {Object} + */ + handlers: null, + + /** + * Property: resolutionFactor + * {Object} + */ + resolutionFactor: 1, + + /** + * APIProperty: maximized + * {Boolean} Start as maximized (visible). Defaults to false. + */ + maximized: false, + + /** + * APIProperty: maximizeTitle + * {String} This property is used for showing a tooltip over the + * maximize div. Defaults to "" (no title). + */ + maximizeTitle: "", + + /** + * APIProperty: minimizeTitle + * {String} This property is used for showing a tooltip over the + * minimize div. Defaults to "" (no title). + */ + minimizeTitle: "", + + /** + * Constructor: OpenLayers.Control.OverviewMap + * Create a new overview map + * + * Parameters: + * options - {Object} Properties of this object will be set on the overview + * map object. Note, to set options on the map object contained in this + * control, set <mapOptions> as one of the options properties. + */ + initialize: function(options) { + this.layers = []; + this.handlers = {}; + OpenLayers.Control.prototype.initialize.apply(this, [options]); + }, + + /** + * APIMethod: destroy + * Deconstruct the control + */ + destroy: function() { + if (!this.mapDiv) { // we've already been destroyed + return; + } + if (this.handlers.click) { + this.handlers.click.destroy(); + } + if (this.handlers.drag) { + this.handlers.drag.destroy(); + } + + this.ovmap && this.ovmap.viewPortDiv.removeChild(this.extentRectangle); + this.extentRectangle = null; + + if (this.rectEvents) { + this.rectEvents.destroy(); + this.rectEvents = null; + } + + if (this.ovmap) { + this.ovmap.destroy(); + this.ovmap = null; + } + + this.element.removeChild(this.mapDiv); + this.mapDiv = null; + + this.div.removeChild(this.element); + this.element = null; + + if (this.maximizeDiv) { + this.div.removeChild(this.maximizeDiv); + this.maximizeDiv = null; + } + + if (this.minimizeDiv) { + this.div.removeChild(this.minimizeDiv); + this.minimizeDiv = null; + } + + this.map.events.un({ + buttonclick: this.onButtonClick, + moveend: this.update, + changebaselayer: this.baseLayerDraw, + scope: this + }); + + OpenLayers.Control.prototype.destroy.apply(this, arguments); + }, + + /** + * Method: draw + * Render the control in the browser. + */ + draw: function() { + OpenLayers.Control.prototype.draw.apply(this, arguments); + if (this.layers.length === 0) { + if (this.map.baseLayer) { + var layer = this.map.baseLayer.clone(); + this.layers = [layer]; + } else { + this.map.events.register("changebaselayer", this, this.baseLayerDraw); + return this.div; + } + } + + // create overview map DOM elements + this.element = document.createElement('div'); + this.element.className = this.displayClass + 'Element'; + this.element.style.display = 'none'; + + this.mapDiv = document.createElement('div'); + this.mapDiv.style.width = this.size.w + 'px'; + this.mapDiv.style.height = this.size.h + 'px'; + this.mapDiv.style.position = 'relative'; + this.mapDiv.style.overflow = 'hidden'; + this.mapDiv.id = OpenLayers.Util.createUniqueID('overviewMap'); + + this.extentRectangle = document.createElement('div'); + this.extentRectangle.style.position = 'absolute'; + this.extentRectangle.style.zIndex = 1000; //HACK + this.extentRectangle.className = this.displayClass+'ExtentRectangle'; + + this.element.appendChild(this.mapDiv); + + this.div.appendChild(this.element); + + // Optionally add min/max buttons if the control will go in the + // map viewport. + if(!this.outsideViewport) { + this.div.className += " " + this.displayClass + 'Container'; + // maximize button div + var img = OpenLayers.Util.getImageLocation('layer-switcher-maximize.png'); + this.maximizeDiv = OpenLayers.Util.createAlphaImageDiv( + this.displayClass + 'MaximizeButton', + null, + null, + img, + 'absolute'); + this.maximizeDiv.style.display = 'none'; + this.maximizeDiv.className = this.displayClass + 'MaximizeButton olButton'; + if (this.maximizeTitle) { + this.maximizeDiv.title = this.maximizeTitle; + } + this.div.appendChild(this.maximizeDiv); + + // minimize button div + var img = OpenLayers.Util.getImageLocation('layer-switcher-minimize.png'); + this.minimizeDiv = OpenLayers.Util.createAlphaImageDiv( + 'OpenLayers_Control_minimizeDiv', + null, + null, + img, + 'absolute'); + this.minimizeDiv.style.display = 'none'; + this.minimizeDiv.className = this.displayClass + 'MinimizeButton olButton'; + if (this.minimizeTitle) { + this.minimizeDiv.title = this.minimizeTitle; + } + this.div.appendChild(this.minimizeDiv); + this.minimizeControl(); + } else { + // show the overview map + this.element.style.display = ''; + } + if(this.map.getExtent()) { + this.update(); + } + + this.map.events.on({ + buttonclick: this.onButtonClick, + moveend: this.update, + scope: this + }); + + if (this.maximized) { + this.maximizeControl(); + } + return this.div; + }, + + /** + * Method: baseLayerDraw + * Draw the base layer - called if unable to complete in the initial draw + */ + baseLayerDraw: function() { + this.draw(); + this.map.events.unregister("changebaselayer", this, this.baseLayerDraw); + }, + + /** + * Method: rectDrag + * Handle extent rectangle drag + * + * Parameters: + * px - {<OpenLayers.Pixel>} The pixel location of the drag. + */ + rectDrag: function(px) { + var deltaX = this.handlers.drag.last.x - px.x; + var deltaY = this.handlers.drag.last.y - px.y; + if(deltaX != 0 || deltaY != 0) { + var rectTop = this.rectPxBounds.top; + var rectLeft = this.rectPxBounds.left; + var rectHeight = Math.abs(this.rectPxBounds.getHeight()); + var rectWidth = this.rectPxBounds.getWidth(); + // don't allow dragging off of parent element + var newTop = Math.max(0, (rectTop - deltaY)); + newTop = Math.min(newTop, + this.ovmap.size.h - this.hComp - rectHeight); + var newLeft = Math.max(0, (rectLeft - deltaX)); + newLeft = Math.min(newLeft, + this.ovmap.size.w - this.wComp - rectWidth); + this.setRectPxBounds(new OpenLayers.Bounds(newLeft, + newTop + rectHeight, + newLeft + rectWidth, + newTop)); + } + }, + + /** + * Method: mapDivClick + * Handle browser events + * + * Parameters: + * evt - {<OpenLayers.Event>} evt + */ + mapDivClick: function(evt) { + var pxCenter = this.rectPxBounds.getCenterPixel(); + var deltaX = evt.xy.x - pxCenter.x; + var deltaY = evt.xy.y - pxCenter.y; + var top = this.rectPxBounds.top; + var left = this.rectPxBounds.left; + var height = Math.abs(this.rectPxBounds.getHeight()); + var width = this.rectPxBounds.getWidth(); + var newTop = Math.max(0, (top + deltaY)); + newTop = Math.min(newTop, this.ovmap.size.h - height); + var newLeft = Math.max(0, (left + deltaX)); + newLeft = Math.min(newLeft, this.ovmap.size.w - width); + this.setRectPxBounds(new OpenLayers.Bounds(newLeft, + newTop + height, + newLeft + width, + newTop)); + this.updateMapToRect(); + }, + + /** + * Method: onButtonClick + * + * Parameters: + * evt - {Event} + */ + onButtonClick: function(evt) { + if (evt.buttonElement === this.minimizeDiv) { + this.minimizeControl(); + } else if (evt.buttonElement === this.maximizeDiv) { + this.maximizeControl(); + } + }, + + /** + * Method: maximizeControl + * Unhide the control. Called when the control is in the map viewport. + * + * Parameters: + * e - {<OpenLayers.Event>} + */ + maximizeControl: function(e) { + this.element.style.display = ''; + this.showToggle(false); + if (e != null) { + OpenLayers.Event.stop(e); + } + }, + + /** + * Method: minimizeControl + * Hide all the contents of the control, shrink the size, + * add the maximize icon + * + * Parameters: + * e - {<OpenLayers.Event>} + */ + minimizeControl: function(e) { + this.element.style.display = 'none'; + this.showToggle(true); + if (e != null) { + OpenLayers.Event.stop(e); + } + }, + + /** + * Method: showToggle + * Hide/Show the toggle depending on whether the control is minimized + * + * Parameters: + * minimize - {Boolean} + */ + showToggle: function(minimize) { + if (this.maximizeDiv) { + this.maximizeDiv.style.display = minimize ? '' : 'none'; + } + if (this.minimizeDiv) { + this.minimizeDiv.style.display = minimize ? 'none' : ''; + } + }, + + /** + * Method: update + * Update the overview map after layers move. + */ + update: function() { + if(this.ovmap == null) { + this.createMap(); + } + + if(this.autoPan || !this.isSuitableOverview()) { + this.updateOverview(); + } + + // update extent rectangle + this.updateRectToMap(); + }, + + /** + * Method: isSuitableOverview + * Determines if the overview map is suitable given the extent and + * resolution of the main map. + */ + isSuitableOverview: function() { + var mapExtent = this.map.getExtent(); + var maxExtent = this.map.getMaxExtent(); + var testExtent = new OpenLayers.Bounds( + Math.max(mapExtent.left, maxExtent.left), + Math.max(mapExtent.bottom, maxExtent.bottom), + Math.min(mapExtent.right, maxExtent.right), + Math.min(mapExtent.top, maxExtent.top)); + + if (this.ovmap.getProjection() != this.map.getProjection()) { + testExtent = testExtent.transform( + this.map.getProjectionObject(), + this.ovmap.getProjectionObject() ); + } + + var resRatio = this.ovmap.getResolution() / this.map.getResolution(); + return ((resRatio > this.minRatio) && + (resRatio <= this.maxRatio) && + (this.ovmap.getExtent().containsBounds(testExtent))); + }, + + /** + * Method updateOverview + * Called by <update> if <isSuitableOverview> returns true + */ + updateOverview: function() { + var mapRes = this.map.getResolution(); + var targetRes = this.ovmap.getResolution(); + var resRatio = targetRes / mapRes; + if(resRatio > this.maxRatio) { + // zoom in overview map + targetRes = this.minRatio * mapRes; + } else if(resRatio <= this.minRatio) { + // zoom out overview map + targetRes = this.maxRatio * mapRes; + } + var center; + if (this.ovmap.getProjection() != this.map.getProjection()) { + center = this.map.center.clone(); + center.transform(this.map.getProjectionObject(), + this.ovmap.getProjectionObject() ); + } else { + center = this.map.center; + } + this.ovmap.setCenter(center, this.ovmap.getZoomForResolution( + targetRes * this.resolutionFactor)); + this.updateRectToMap(); + }, + + /** + * Method: createMap + * Construct the map that this control contains + */ + createMap: function() { + // create the overview map + var options = OpenLayers.Util.extend( + {controls: [], maxResolution: 'auto', + fallThrough: false}, this.mapOptions); + this.ovmap = new OpenLayers.Map(this.mapDiv, options); + this.ovmap.viewPortDiv.appendChild(this.extentRectangle); + + // prevent ovmap from being destroyed when the page unloads, because + // the OverviewMap control has to do this (and does it). + OpenLayers.Event.stopObserving(window, 'unload', this.ovmap.unloadDestroy); + + this.ovmap.addLayers(this.layers); + this.ovmap.zoomToMaxExtent(); + // check extent rectangle border width + this.wComp = parseInt(OpenLayers.Element.getStyle(this.extentRectangle, + 'border-left-width')) + + parseInt(OpenLayers.Element.getStyle(this.extentRectangle, + 'border-right-width')); + this.wComp = (this.wComp) ? this.wComp : 2; + this.hComp = parseInt(OpenLayers.Element.getStyle(this.extentRectangle, + 'border-top-width')) + + parseInt(OpenLayers.Element.getStyle(this.extentRectangle, + 'border-bottom-width')); + this.hComp = (this.hComp) ? this.hComp : 2; + + this.handlers.drag = new OpenLayers.Handler.Drag( + this, {move: this.rectDrag, done: this.updateMapToRect}, + {map: this.ovmap} + ); + this.handlers.click = new OpenLayers.Handler.Click( + this, { + "click": this.mapDivClick + },{ + "single": true, "double": false, + "stopSingle": true, "stopDouble": true, + "pixelTolerance": 1, + map: this.ovmap + } + ); + this.handlers.click.activate(); + + this.rectEvents = new OpenLayers.Events(this, this.extentRectangle, + null, true); + this.rectEvents.register("mouseover", this, function(e) { + if(!this.handlers.drag.active && !this.map.dragging) { + this.handlers.drag.activate(); + } + }); + this.rectEvents.register("mouseout", this, function(e) { + if(!this.handlers.drag.dragging) { + this.handlers.drag.deactivate(); + } + }); + + if (this.ovmap.getProjection() != this.map.getProjection()) { + var sourceUnits = this.map.getProjectionObject().getUnits() || + this.map.units || this.map.baseLayer.units; + var targetUnits = this.ovmap.getProjectionObject().getUnits() || + this.ovmap.units || this.ovmap.baseLayer.units; + this.resolutionFactor = sourceUnits && targetUnits ? + OpenLayers.INCHES_PER_UNIT[sourceUnits] / + OpenLayers.INCHES_PER_UNIT[targetUnits] : 1; + } + }, + + /** + * Method: updateRectToMap + * Updates the extent rectangle position and size to match the map extent + */ + updateRectToMap: function() { + // If the projections differ we need to reproject + var bounds; + if (this.ovmap.getProjection() != this.map.getProjection()) { + bounds = this.map.getExtent().transform( + this.map.getProjectionObject(), + this.ovmap.getProjectionObject() ); + } else { + bounds = this.map.getExtent(); + } + var pxBounds = this.getRectBoundsFromMapBounds(bounds); + if (pxBounds) { + this.setRectPxBounds(pxBounds); + } + }, + + /** + * Method: updateMapToRect + * Updates the map extent to match the extent rectangle position and size + */ + updateMapToRect: function() { + var lonLatBounds = this.getMapBoundsFromRectBounds(this.rectPxBounds); + if (this.ovmap.getProjection() != this.map.getProjection()) { + lonLatBounds = lonLatBounds.transform( + this.ovmap.getProjectionObject(), + this.map.getProjectionObject() ); + } + this.map.panTo(lonLatBounds.getCenterLonLat()); + }, + + /** + * Method: setRectPxBounds + * Set extent rectangle pixel bounds. + * + * Parameters: + * pxBounds - {<OpenLayers.Bounds>} + */ + setRectPxBounds: function(pxBounds) { + var top = Math.max(pxBounds.top, 0); + var left = Math.max(pxBounds.left, 0); + var bottom = Math.min(pxBounds.top + Math.abs(pxBounds.getHeight()), + this.ovmap.size.h - this.hComp); + var right = Math.min(pxBounds.left + pxBounds.getWidth(), + this.ovmap.size.w - this.wComp); + var width = Math.max(right - left, 0); + var height = Math.max(bottom - top, 0); + if(width < this.minRectSize || height < this.minRectSize) { + this.extentRectangle.className = this.displayClass + + this.minRectDisplayClass; + var rLeft = left + (width / 2) - (this.minRectSize / 2); + var rTop = top + (height / 2) - (this.minRectSize / 2); + this.extentRectangle.style.top = Math.round(rTop) + 'px'; + this.extentRectangle.style.left = Math.round(rLeft) + 'px'; + this.extentRectangle.style.height = this.minRectSize + 'px'; + this.extentRectangle.style.width = this.minRectSize + 'px'; + } else { + this.extentRectangle.className = this.displayClass + + 'ExtentRectangle'; + this.extentRectangle.style.top = Math.round(top) + 'px'; + this.extentRectangle.style.left = Math.round(left) + 'px'; + this.extentRectangle.style.height = Math.round(height) + 'px'; + this.extentRectangle.style.width = Math.round(width) + 'px'; + } + this.rectPxBounds = new OpenLayers.Bounds( + Math.round(left), Math.round(bottom), + Math.round(right), Math.round(top) + ); + }, + + /** + * Method: getRectBoundsFromMapBounds + * Get the rect bounds from the map bounds. + * + * Parameters: + * lonLatBounds - {<OpenLayers.Bounds>} + * + * Returns: + * {<OpenLayers.Bounds>}A bounds which is the passed-in map lon/lat extent + * translated into pixel bounds for the overview map + */ + getRectBoundsFromMapBounds: function(lonLatBounds) { + var leftBottomPx = this.getOverviewPxFromLonLat({ + lon: lonLatBounds.left, + lat: lonLatBounds.bottom + }); + var rightTopPx = this.getOverviewPxFromLonLat({ + lon: lonLatBounds.right, + lat: lonLatBounds.top + }); + var bounds = null; + if (leftBottomPx && rightTopPx) { + bounds = new OpenLayers.Bounds(leftBottomPx.x, leftBottomPx.y, + rightTopPx.x, rightTopPx.y); + } + return bounds; + }, + + /** + * Method: getMapBoundsFromRectBounds + * Get the map bounds from the rect bounds. + * + * Parameters: + * pxBounds - {<OpenLayers.Bounds>} + * + * Returns: + * {<OpenLayers.Bounds>} Bounds which is the passed-in overview rect bounds + * translated into lon/lat bounds for the overview map + */ + getMapBoundsFromRectBounds: function(pxBounds) { + var leftBottomLonLat = this.getLonLatFromOverviewPx({ + x: pxBounds.left, + y: pxBounds.bottom + }); + var rightTopLonLat = this.getLonLatFromOverviewPx({ + x: pxBounds.right, + y: pxBounds.top + }); + return new OpenLayers.Bounds(leftBottomLonLat.lon, leftBottomLonLat.lat, + rightTopLonLat.lon, rightTopLonLat.lat); + }, + + /** + * Method: getLonLatFromOverviewPx + * Get a map location from a pixel location + * + * Parameters: + * overviewMapPx - {<OpenLayers.Pixel>|Object} OpenLayers.Pixel or + * an object with a + * 'x' and 'y' properties. + * + * Returns: + * {Object} Location which is the passed-in overview map + * OpenLayers.Pixel, translated into lon/lat by the overview + * map. An object with a 'lon' and 'lat' properties. + */ + getLonLatFromOverviewPx: function(overviewMapPx) { + var size = this.ovmap.size; + var res = this.ovmap.getResolution(); + var center = this.ovmap.getExtent().getCenterLonLat(); + + var deltaX = overviewMapPx.x - (size.w / 2); + var deltaY = overviewMapPx.y - (size.h / 2); + + return { + lon: center.lon + deltaX * res, + lat: center.lat - deltaY * res + }; + }, + + /** + * Method: getOverviewPxFromLonLat + * Get a pixel location from a map location + * + * Parameters: + * lonlat - {<OpenLayers.LonLat>|Object} OpenLayers.LonLat or an + * object with a 'lon' and 'lat' properties. + * + * Returns: + * {Object} Location which is the passed-in OpenLayers.LonLat, + * translated into overview map pixels + */ + getOverviewPxFromLonLat: function(lonlat) { + var res = this.ovmap.getResolution(); + var extent = this.ovmap.getExtent(); + if (extent) { + return { + x: Math.round(1/res * (lonlat.lon - extent.left)), + y: Math.round(1/res * (extent.top - lonlat.lat)) + }; + } + }, + + CLASS_NAME: 'OpenLayers.Control.OverviewMap' +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/Pan.js b/misc/openlayers/lib/OpenLayers/Control/Pan.js new file mode 100644 index 0000000..d7fcc07 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/Pan.js @@ -0,0 +1,95 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control/Button.js + */ + +/** + * Class: OpenLayers.Control.Pan + * The Pan control is a single button to pan the map in one direction. For + * a more complete control see <OpenLayers.Control.PanPanel>. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.Pan = OpenLayers.Class(OpenLayers.Control.Button, { + + /** + * APIProperty: slideFactor + * {Integer} Number of pixels by which we'll pan the map in any direction + * on clicking the arrow buttons, defaults to 50. If you want to pan + * by some ratio of the map dimensions, use <slideRatio> instead. + */ + slideFactor: 50, + + /** + * APIProperty: slideRatio + * {Number} The fraction of map width/height by which we'll pan the map + * on clicking the arrow buttons. Default is null. If set, will + * override <slideFactor>. E.g. if slideRatio is .5, then Pan Up will + * pan up half the map height. + */ + slideRatio: null, + + /** + * Property: direction + * {String} in {'North', 'South', 'East', 'West'} + */ + direction: null, + + /** + * Constructor: OpenLayers.Control.Pan + * Control which handles the panning (in any of the cardinal directions) + * of the map by a set px distance. + * + * Parameters: + * direction - {String} The direction this button should pan. + * options - {Object} An optional object whose properties will be used + * to extend the control. + */ + initialize: function(direction, options) { + + this.direction = direction; + this.CLASS_NAME += this.direction; + + OpenLayers.Control.prototype.initialize.apply(this, [options]); + }, + + /** + * Method: trigger + */ + trigger: function(){ + if (this.map) { + var getSlideFactor = OpenLayers.Function.bind(function (dim) { + return this.slideRatio ? + this.map.getSize()[dim] * this.slideRatio : + this.slideFactor; + }, this); + + switch (this.direction) { + case OpenLayers.Control.Pan.NORTH: + this.map.pan(0, -getSlideFactor("h")); + break; + case OpenLayers.Control.Pan.SOUTH: + this.map.pan(0, getSlideFactor("h")); + break; + case OpenLayers.Control.Pan.WEST: + this.map.pan(-getSlideFactor("w"), 0); + break; + case OpenLayers.Control.Pan.EAST: + this.map.pan(getSlideFactor("w"), 0); + break; + } + } + }, + + CLASS_NAME: "OpenLayers.Control.Pan" +}); + +OpenLayers.Control.Pan.NORTH = "North"; +OpenLayers.Control.Pan.SOUTH = "South"; +OpenLayers.Control.Pan.EAST = "East"; +OpenLayers.Control.Pan.WEST = "West"; diff --git a/misc/openlayers/lib/OpenLayers/Control/PanPanel.js b/misc/openlayers/lib/OpenLayers/Control/PanPanel.js new file mode 100644 index 0000000..eeedbd0 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/PanPanel.js @@ -0,0 +1,73 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control/Panel.js + * @requires OpenLayers/Control/Pan.js + */ + +/** + * Class: OpenLayers.Control.PanPanel + * The PanPanel is visible control for panning the map North, South, East or + * West in small steps. By default it is drawn in the top left corner of the + * map. + * + * Note: + * If you wish to use this class with the default images and you want + * it to look nice in ie6, you should add the following, conditionally + * added css stylesheet to your HTML file: + * + * (code) + * <!--[if lte IE 6]> + * <link rel="stylesheet" href="../theme/default/ie6-style.css" type="text/css" /> + * <![endif]--> + * (end) + * + * Inherits from: + * - <OpenLayers.Control.Panel> + */ +OpenLayers.Control.PanPanel = OpenLayers.Class(OpenLayers.Control.Panel, { + + /** + * APIProperty: slideFactor + * {Integer} Number of pixels by which we'll pan the map in any direction + * on clicking the arrow buttons, defaults to 50. If you want to pan + * by some ratio of the map dimensions, use <slideRatio> instead. + */ + slideFactor: 50, + + /** + * APIProperty: slideRatio + * {Number} The fraction of map width/height by which we'll pan the map + * on clicking the arrow buttons. Default is null. If set, will + * override <slideFactor>. E.g. if slideRatio is .5, then Pan Up will + * pan up half the map height. + */ + slideRatio: null, + + /** + * Constructor: OpenLayers.Control.PanPanel + * Add the four directional pan buttons. + * + * Parameters: + * options - {Object} An optional object whose properties will be used + * to extend the control. + */ + initialize: function(options) { + OpenLayers.Control.Panel.prototype.initialize.apply(this, [options]); + var options = { + slideFactor: this.slideFactor, + slideRatio: this.slideRatio + }; + this.addControls([ + new OpenLayers.Control.Pan(OpenLayers.Control.Pan.NORTH, options), + new OpenLayers.Control.Pan(OpenLayers.Control.Pan.SOUTH, options), + new OpenLayers.Control.Pan(OpenLayers.Control.Pan.EAST, options), + new OpenLayers.Control.Pan(OpenLayers.Control.Pan.WEST, options) + ]); + }, + + CLASS_NAME: "OpenLayers.Control.PanPanel" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/PanZoom.js b/misc/openlayers/lib/OpenLayers/Control/PanZoom.js new file mode 100644 index 0000000..dd007cf --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/PanZoom.js @@ -0,0 +1,233 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Events/buttonclick.js + */ + +/** + * Class: OpenLayers.Control.PanZoom + * The PanZoom is a visible control, composed of a + * <OpenLayers.Control.PanPanel> and a <OpenLayers.Control.ZoomPanel>. By + * default it is drawn in the upper left corner of the map. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.PanZoom = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: slideFactor + * {Integer} Number of pixels by which we'll pan the map in any direction + * on clicking the arrow buttons. If you want to pan by some ratio + * of the map dimensions, use <slideRatio> instead. + */ + slideFactor: 50, + + /** + * APIProperty: slideRatio + * {Number} The fraction of map width/height by which we'll pan the map + * on clicking the arrow buttons. Default is null. If set, will + * override <slideFactor>. E.g. if slideRatio is .5, then the Pan Up + * button will pan up half the map height. + */ + slideRatio: null, + + /** + * Property: buttons + * {Array(DOMElement)} Array of Button Divs + */ + buttons: null, + + /** + * Property: position + * {<OpenLayers.Pixel>} + */ + position: null, + + /** + * Constructor: OpenLayers.Control.PanZoom + * + * Parameters: + * options - {Object} + */ + initialize: function(options) { + this.position = new OpenLayers.Pixel(OpenLayers.Control.PanZoom.X, + OpenLayers.Control.PanZoom.Y); + OpenLayers.Control.prototype.initialize.apply(this, arguments); + }, + + /** + * APIMethod: destroy + */ + destroy: function() { + if (this.map) { + this.map.events.unregister("buttonclick", this, this.onButtonClick); + } + this.removeButtons(); + this.buttons = null; + this.position = null; + OpenLayers.Control.prototype.destroy.apply(this, arguments); + }, + + /** + * Method: setMap + * + * Properties: + * map - {<OpenLayers.Map>} + */ + setMap: function(map) { + OpenLayers.Control.prototype.setMap.apply(this, arguments); + this.map.events.register("buttonclick", this, this.onButtonClick); + }, + + /** + * Method: draw + * + * Parameters: + * px - {<OpenLayers.Pixel>} + * + * Returns: + * {DOMElement} A reference to the container div for the PanZoom control. + */ + draw: function(px) { + // initialize our internal div + OpenLayers.Control.prototype.draw.apply(this, arguments); + px = this.position; + + // place the controls + this.buttons = []; + + var sz = {w: 18, h: 18}; + var centered = new OpenLayers.Pixel(px.x+sz.w/2, px.y); + + this._addButton("panup", "north-mini.png", centered, sz); + px.y = centered.y+sz.h; + this._addButton("panleft", "west-mini.png", px, sz); + this._addButton("panright", "east-mini.png", px.add(sz.w, 0), sz); + this._addButton("pandown", "south-mini.png", + centered.add(0, sz.h*2), sz); + this._addButton("zoomin", "zoom-plus-mini.png", + centered.add(0, sz.h*3+5), sz); + this._addButton("zoomworld", "zoom-world-mini.png", + centered.add(0, sz.h*4+5), sz); + this._addButton("zoomout", "zoom-minus-mini.png", + centered.add(0, sz.h*5+5), sz); + return this.div; + }, + + /** + * Method: _addButton + * + * Parameters: + * id - {String} + * img - {String} + * xy - {<OpenLayers.Pixel>} + * sz - {<OpenLayers.Size>} + * + * Returns: + * {DOMElement} A Div (an alphaImageDiv, to be precise) that contains the + * image of the button, and has all the proper event handlers set. + */ + _addButton:function(id, img, xy, sz) { + var imgLocation = OpenLayers.Util.getImageLocation(img); + var btn = OpenLayers.Util.createAlphaImageDiv( + this.id + "_" + id, + xy, sz, imgLocation, "absolute"); + btn.style.cursor = "pointer"; + //we want to add the outer div + this.div.appendChild(btn); + btn.action = id; + btn.className = "olButton"; + + //we want to remember/reference the outer div + this.buttons.push(btn); + return btn; + }, + + /** + * Method: _removeButton + * + * Parameters: + * btn - {Object} + */ + _removeButton: function(btn) { + this.div.removeChild(btn); + OpenLayers.Util.removeItem(this.buttons, btn); + }, + + /** + * Method: removeButtons + */ + removeButtons: function() { + for(var i=this.buttons.length-1; i>=0; --i) { + this._removeButton(this.buttons[i]); + } + }, + + /** + * Method: onButtonClick + * + * Parameters: + * evt - {Event} + */ + onButtonClick: function(evt) { + var btn = evt.buttonElement; + switch (btn.action) { + case "panup": + this.map.pan(0, -this.getSlideFactor("h")); + break; + case "pandown": + this.map.pan(0, this.getSlideFactor("h")); + break; + case "panleft": + this.map.pan(-this.getSlideFactor("w"), 0); + break; + case "panright": + this.map.pan(this.getSlideFactor("w"), 0); + break; + case "zoomin": + this.map.zoomIn(); + break; + case "zoomout": + this.map.zoomOut(); + break; + case "zoomworld": + this.map.zoomToMaxExtent(); + break; + } + }, + + /** + * Method: getSlideFactor + * + * Parameters: + * dim - {String} "w" or "h" (for width or height). + * + * Returns: + * {Number} The slide factor for panning in the requested direction. + */ + getSlideFactor: function(dim) { + return this.slideRatio ? + this.map.getSize()[dim] * this.slideRatio : + this.slideFactor; + }, + + CLASS_NAME: "OpenLayers.Control.PanZoom" +}); + +/** + * Constant: X + * {Integer} + */ +OpenLayers.Control.PanZoom.X = 4; + +/** + * Constant: Y + * {Integer} + */ +OpenLayers.Control.PanZoom.Y = 4; diff --git a/misc/openlayers/lib/OpenLayers/Control/PanZoomBar.js b/misc/openlayers/lib/OpenLayers/Control/PanZoomBar.js new file mode 100644 index 0000000..ebf2964 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/PanZoomBar.js @@ -0,0 +1,408 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + + +/** + * @requires OpenLayers/Control/PanZoom.js + */ + +/** + * Class: OpenLayers.Control.PanZoomBar + * The PanZoomBar is a visible control composed of a + * <OpenLayers.Control.PanPanel> and a <OpenLayers.Control.ZoomBar>. + * By default it is displayed in the upper left corner of the map as 4 + * directional arrows above a vertical slider. + * + * Inherits from: + * - <OpenLayers.Control.PanZoom> + */ +OpenLayers.Control.PanZoomBar = OpenLayers.Class(OpenLayers.Control.PanZoom, { + + /** + * APIProperty: zoomStopWidth + */ + zoomStopWidth: 18, + + /** + * APIProperty: zoomStopHeight + */ + zoomStopHeight: 11, + + /** + * Property: slider + */ + slider: null, + + /** + * Property: sliderEvents + * {<OpenLayers.Events>} + */ + sliderEvents: null, + + /** + * Property: zoombarDiv + * {DOMElement} + */ + zoombarDiv: null, + + /** + * APIProperty: zoomWorldIcon + * {Boolean} + */ + zoomWorldIcon: false, + + /** + * APIProperty: panIcons + * {Boolean} Set this property to false not to display the pan icons. If + * false the zoom world icon is placed under the zoom bar. Defaults to + * true. + */ + panIcons: true, + + /** + * APIProperty: forceFixedZoomLevel + * {Boolean} Force a fixed zoom level even though the map has + * fractionalZoom + */ + forceFixedZoomLevel: false, + + /** + * Property: mouseDragStart + * {<OpenLayers.Pixel>} + */ + mouseDragStart: null, + + /** + * Property: deltaY + * {Number} The cumulative vertical pixel offset during a zoom bar drag. + */ + deltaY: null, + + /** + * Property: zoomStart + * {<OpenLayers.Pixel>} + */ + zoomStart: null, + + /** + * Constructor: OpenLayers.Control.PanZoomBar + */ + + /** + * APIMethod: destroy + */ + destroy: function() { + + this._removeZoomBar(); + + this.map.events.un({ + "changebaselayer": this.redraw, + "updatesize": this.redraw, + scope: this + }); + + OpenLayers.Control.PanZoom.prototype.destroy.apply(this, arguments); + + delete this.mouseDragStart; + delete this.zoomStart; + }, + + /** + * Method: setMap + * + * Parameters: + * map - {<OpenLayers.Map>} + */ + setMap: function(map) { + OpenLayers.Control.PanZoom.prototype.setMap.apply(this, arguments); + this.map.events.on({ + "changebaselayer": this.redraw, + "updatesize": this.redraw, + scope: this + }); + }, + + /** + * Method: redraw + * clear the div and start over. + */ + redraw: function() { + if (this.div != null) { + this.removeButtons(); + this._removeZoomBar(); + } + this.draw(); + }, + + /** + * Method: draw + * + * Parameters: + * px - {<OpenLayers.Pixel>} + */ + draw: function(px) { + // initialize our internal div + OpenLayers.Control.prototype.draw.apply(this, arguments); + px = this.position.clone(); + + // place the controls + this.buttons = []; + + var sz = {w: 18, h: 18}; + if (this.panIcons) { + var centered = new OpenLayers.Pixel(px.x+sz.w/2, px.y); + var wposition = sz.w; + + if (this.zoomWorldIcon) { + centered = new OpenLayers.Pixel(px.x+sz.w, px.y); + } + + this._addButton("panup", "north-mini.png", centered, sz); + px.y = centered.y+sz.h; + this._addButton("panleft", "west-mini.png", px, sz); + if (this.zoomWorldIcon) { + this._addButton("zoomworld", "zoom-world-mini.png", px.add(sz.w, 0), sz); + + wposition *= 2; + } + this._addButton("panright", "east-mini.png", px.add(wposition, 0), sz); + this._addButton("pandown", "south-mini.png", centered.add(0, sz.h*2), sz); + this._addButton("zoomin", "zoom-plus-mini.png", centered.add(0, sz.h*3+5), sz); + centered = this._addZoomBar(centered.add(0, sz.h*4 + 5)); + this._addButton("zoomout", "zoom-minus-mini.png", centered, sz); + } + else { + this._addButton("zoomin", "zoom-plus-mini.png", px, sz); + centered = this._addZoomBar(px.add(0, sz.h)); + this._addButton("zoomout", "zoom-minus-mini.png", centered, sz); + if (this.zoomWorldIcon) { + centered = centered.add(0, sz.h+3); + this._addButton("zoomworld", "zoom-world-mini.png", centered, sz); + } + } + return this.div; + }, + + /** + * Method: _addZoomBar + * + * Parameters: + * centered - {<OpenLayers.Pixel>} where zoombar drawing is to start. + */ + _addZoomBar:function(centered) { + var imgLocation = OpenLayers.Util.getImageLocation("slider.png"); + var id = this.id + "_" + this.map.id; + var minZoom = this.map.getMinZoom(); + var zoomsToEnd = this.map.getNumZoomLevels() - 1 - this.map.getZoom(); + var slider = OpenLayers.Util.createAlphaImageDiv(id, + centered.add(-1, zoomsToEnd * this.zoomStopHeight), + {w: 20, h: 9}, + imgLocation, + "absolute"); + slider.style.cursor = "move"; + this.slider = slider; + + this.sliderEvents = new OpenLayers.Events(this, slider, null, true, + {includeXY: true}); + this.sliderEvents.on({ + "touchstart": this.zoomBarDown, + "touchmove": this.zoomBarDrag, + "touchend": this.zoomBarUp, + "mousedown": this.zoomBarDown, + "mousemove": this.zoomBarDrag, + "mouseup": this.zoomBarUp + }); + + var sz = { + w: this.zoomStopWidth, + h: this.zoomStopHeight * (this.map.getNumZoomLevels() - minZoom) + }; + var imgLocation = OpenLayers.Util.getImageLocation("zoombar.png"); + var div = null; + + if (OpenLayers.Util.alphaHack()) { + var id = this.id + "_" + this.map.id; + div = OpenLayers.Util.createAlphaImageDiv(id, centered, + {w: sz.w, h: this.zoomStopHeight}, + imgLocation, + "absolute", null, "crop"); + div.style.height = sz.h + "px"; + } else { + div = OpenLayers.Util.createDiv( + 'OpenLayers_Control_PanZoomBar_Zoombar' + this.map.id, + centered, + sz, + imgLocation); + } + div.style.cursor = "pointer"; + div.className = "olButton"; + this.zoombarDiv = div; + + this.div.appendChild(div); + + this.startTop = parseInt(div.style.top); + this.div.appendChild(slider); + + this.map.events.register("zoomend", this, this.moveZoomBar); + + centered = centered.add(0, + this.zoomStopHeight * (this.map.getNumZoomLevels() - minZoom)); + return centered; + }, + + /** + * Method: _removeZoomBar + */ + _removeZoomBar: function() { + this.sliderEvents.un({ + "touchstart": this.zoomBarDown, + "touchmove": this.zoomBarDrag, + "touchend": this.zoomBarUp, + "mousedown": this.zoomBarDown, + "mousemove": this.zoomBarDrag, + "mouseup": this.zoomBarUp + }); + this.sliderEvents.destroy(); + + this.div.removeChild(this.zoombarDiv); + this.zoombarDiv = null; + this.div.removeChild(this.slider); + this.slider = null; + + this.map.events.unregister("zoomend", this, this.moveZoomBar); + }, + + /** + * Method: onButtonClick + * + * Parameters: + * evt - {Event} + */ + onButtonClick: function(evt) { + OpenLayers.Control.PanZoom.prototype.onButtonClick.apply(this, arguments); + if (evt.buttonElement === this.zoombarDiv) { + var levels = evt.buttonXY.y / this.zoomStopHeight; + if(this.forceFixedZoomLevel || !this.map.fractionalZoom) { + levels = Math.floor(levels); + } + var zoom = (this.map.getNumZoomLevels() - 1) - levels; + zoom = Math.min(Math.max(zoom, 0), this.map.getNumZoomLevels() - 1); + this.map.zoomTo(zoom); + } + }, + + /** + * Method: passEventToSlider + * This function is used to pass events that happen on the div, or the map, + * through to the slider, which then does its moving thing. + * + * Parameters: + * evt - {<OpenLayers.Event>} + */ + passEventToSlider:function(evt) { + this.sliderEvents.handleBrowserEvent(evt); + }, + + /* + * Method: zoomBarDown + * event listener for clicks on the slider + * + * Parameters: + * evt - {<OpenLayers.Event>} + */ + zoomBarDown:function(evt) { + if (!OpenLayers.Event.isLeftClick(evt) && !OpenLayers.Event.isSingleTouch(evt)) { + return; + } + this.map.events.on({ + "touchmove": this.passEventToSlider, + "mousemove": this.passEventToSlider, + "mouseup": this.passEventToSlider, + scope: this + }); + this.mouseDragStart = evt.xy.clone(); + this.zoomStart = evt.xy.clone(); + this.div.style.cursor = "move"; + // reset the div offsets just in case the div moved + this.zoombarDiv.offsets = null; + OpenLayers.Event.stop(evt); + }, + + /* + * Method: zoomBarDrag + * This is what happens when a click has occurred, and the client is + * dragging. Here we must ensure that the slider doesn't go beyond the + * bottom/top of the zoombar div, as well as moving the slider to its new + * visual location + * + * Parameters: + * evt - {<OpenLayers.Event>} + */ + zoomBarDrag:function(evt) { + if (this.mouseDragStart != null) { + var deltaY = this.mouseDragStart.y - evt.xy.y; + var offsets = OpenLayers.Util.pagePosition(this.zoombarDiv); + if ((evt.clientY - offsets[1]) > 0 && + (evt.clientY - offsets[1]) < parseInt(this.zoombarDiv.style.height) - 2) { + var newTop = parseInt(this.slider.style.top) - deltaY; + this.slider.style.top = newTop+"px"; + this.mouseDragStart = evt.xy.clone(); + } + // set cumulative displacement + this.deltaY = this.zoomStart.y - evt.xy.y; + OpenLayers.Event.stop(evt); + } + }, + + /* + * Method: zoomBarUp + * Perform cleanup when a mouseup event is received -- discover new zoom + * level and switch to it. + * + * Parameters: + * evt - {<OpenLayers.Event>} + */ + zoomBarUp:function(evt) { + if (!OpenLayers.Event.isLeftClick(evt) && evt.type !== "touchend") { + return; + } + if (this.mouseDragStart) { + this.div.style.cursor=""; + this.map.events.un({ + "touchmove": this.passEventToSlider, + "mouseup": this.passEventToSlider, + "mousemove": this.passEventToSlider, + scope: this + }); + var zoomLevel = this.map.zoom; + if (!this.forceFixedZoomLevel && this.map.fractionalZoom) { + zoomLevel += this.deltaY/this.zoomStopHeight; + zoomLevel = Math.min(Math.max(zoomLevel, 0), + this.map.getNumZoomLevels() - 1); + } else { + zoomLevel += this.deltaY/this.zoomStopHeight; + zoomLevel = Math.max(Math.round(zoomLevel), 0); + } + this.map.zoomTo(zoomLevel); + this.mouseDragStart = null; + this.zoomStart = null; + this.deltaY = 0; + OpenLayers.Event.stop(evt); + } + }, + + /* + * Method: moveZoomBar + * Change the location of the slider to match the current zoom level. + */ + moveZoomBar:function() { + var newTop = + ((this.map.getNumZoomLevels()-1) - this.map.getZoom()) * + this.zoomStopHeight + this.startTop + 1; + this.slider.style.top = newTop + "px"; + }, + + CLASS_NAME: "OpenLayers.Control.PanZoomBar" +});
\ No newline at end of file diff --git a/misc/openlayers/lib/OpenLayers/Control/Panel.js b/misc/openlayers/lib/OpenLayers/Control/Panel.js new file mode 100644 index 0000000..150afa7 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/Panel.js @@ -0,0 +1,431 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Events/buttonclick.js + */ + +/** + * Class: OpenLayers.Control.Panel + * The Panel control is a container for other controls. With it toolbars + * may be composed. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.Panel = OpenLayers.Class(OpenLayers.Control, { + /** + * Property: controls + * {Array(<OpenLayers.Control>)} + */ + controls: null, + + /** + * APIProperty: autoActivate + * {Boolean} Activate the control when it is added to a map. Default is + * true. + */ + autoActivate: true, + + /** + * APIProperty: defaultControl + * {<OpenLayers.Control>} The control which is activated when the control is + * activated (turned on), which also happens at instantiation. + * If <saveState> is true, <defaultControl> will be nullified after the + * first activation of the panel. + */ + defaultControl: null, + + /** + * APIProperty: saveState + * {Boolean} If set to true, the active state of this panel's controls will + * be stored on panel deactivation, and restored on reactivation. Default + * is false. + */ + saveState: false, + + /** + * APIProperty: allowDepress + * {Boolean} If is true the <OpenLayers.Control.TYPE_TOOL> controls can + * be deactivated by clicking the icon that represents them. Default + * is false. + */ + allowDepress: false, + + /** + * Property: activeState + * {Object} stores the active state of this panel's controls. + */ + activeState: null, + + /** + * Constructor: OpenLayers.Control.Panel + * Create a new control panel. + * + * Each control in the panel is represented by an icon. When clicking + * on an icon, the <activateControl> method is called. + * + * Specific properties for controls on a panel: + * type - {Number} One of <OpenLayers.Control.TYPE_TOOL>, + * <OpenLayers.Control.TYPE_TOGGLE>, <OpenLayers.Control.TYPE_BUTTON>. + * If not provided, <OpenLayers.Control.TYPE_TOOL> is assumed. + * title - {string} Text displayed when mouse is over the icon that + * represents the control. + * + * The <OpenLayers.Control.type> of a control determines the behavior when + * clicking its icon: + * <OpenLayers.Control.TYPE_TOOL> - The control is activated and other + * controls of this type in the same panel are deactivated. This is + * the default type. + * <OpenLayers.Control.TYPE_TOGGLE> - The active state of the control is + * toggled. + * <OpenLayers.Control.TYPE_BUTTON> - The + * <OpenLayers.Control.Button.trigger> method of the control is called, + * but its active state is not changed. + * + * If a control is <OpenLayers.Control.active>, it will be drawn with the + * olControl[Name]ItemActive class, otherwise with the + * olControl[Name]ItemInactive class. + * + * Parameters: + * options - {Object} An optional object whose properties will be used + * to extend the control. + */ + initialize: function(options) { + OpenLayers.Control.prototype.initialize.apply(this, [options]); + this.controls = []; + this.activeState = {}; + }, + + /** + * APIMethod: destroy + */ + destroy: function() { + if (this.map) { + this.map.events.unregister("buttonclick", this, this.onButtonClick); + } + OpenLayers.Control.prototype.destroy.apply(this, arguments); + for (var ctl, i = this.controls.length - 1; i >= 0; i--) { + ctl = this.controls[i]; + if (ctl.events) { + ctl.events.un({ + activate: this.iconOn, + deactivate: this.iconOff + }); + } + ctl.panel_div = null; + } + this.activeState = null; + }, + + /** + * APIMethod: activate + */ + activate: function() { + if (OpenLayers.Control.prototype.activate.apply(this, arguments)) { + var control; + for (var i=0, len=this.controls.length; i<len; i++) { + control = this.controls[i]; + if (control === this.defaultControl || + (this.saveState && this.activeState[control.id])) { + control.activate(); + } + } + if (this.saveState === true) { + this.defaultControl = null; + } + this.redraw(); + return true; + } else { + return false; + } + }, + + /** + * APIMethod: deactivate + */ + deactivate: function() { + if (OpenLayers.Control.prototype.deactivate.apply(this, arguments)) { + var control; + for (var i=0, len=this.controls.length; i<len; i++) { + control = this.controls[i]; + this.activeState[control.id] = control.deactivate(); + } + this.redraw(); + return true; + } else { + return false; + } + }, + + /** + * Method: draw + * + * Returns: + * {DOMElement} + */ + draw: function() { + OpenLayers.Control.prototype.draw.apply(this, arguments); + if (this.outsideViewport) { + this.events.attachToElement(this.div); + this.events.register("buttonclick", this, this.onButtonClick); + } else { + this.map.events.register("buttonclick", this, this.onButtonClick); + } + this.addControlsToMap(this.controls); + return this.div; + }, + + /** + * Method: redraw + */ + redraw: function() { + for (var l=this.div.childNodes.length, i=l-1; i>=0; i--) { + this.div.removeChild(this.div.childNodes[i]); + } + this.div.innerHTML = ""; + if (this.active) { + for (var i=0, len=this.controls.length; i<len; i++) { + this.div.appendChild(this.controls[i].panel_div); + } + } + }, + + /** + * APIMethod: activateControl + * This method is called when the user click on the icon representing a + * control in the panel. + * + * Parameters: + * control - {<OpenLayers.Control>} + */ + activateControl: function (control) { + if (!this.active) { return false; } + if (control.type == OpenLayers.Control.TYPE_BUTTON) { + control.trigger(); + return; + } + if (control.type == OpenLayers.Control.TYPE_TOGGLE) { + if (control.active) { + control.deactivate(); + } else { + control.activate(); + } + return; + } + if (this.allowDepress && control.active) { + control.deactivate(); + } else { + var c; + for (var i=0, len=this.controls.length; i<len; i++) { + c = this.controls[i]; + if (c != control && + (c.type === OpenLayers.Control.TYPE_TOOL || c.type == null)) { + c.deactivate(); + } + } + control.activate(); + } + }, + + /** + * APIMethod: addControls + * To build a toolbar, you add a set of controls to it. addControls + * lets you add a single control or a list of controls to the + * Control Panel. + * + * Parameters: + * controls - {<OpenLayers.Control>} Controls to add in the panel. + */ + addControls: function(controls) { + if (!(OpenLayers.Util.isArray(controls))) { + controls = [controls]; + } + this.controls = this.controls.concat(controls); + + for (var i=0, len=controls.length; i<len; i++) { + var control = controls[i], + element = this.createControlMarkup(control); + OpenLayers.Element.addClass(element, + control.displayClass + "ItemInactive"); + OpenLayers.Element.addClass(element, "olButton"); + if (control.title != "" && !element.title) { + element.title = control.title; + } + control.panel_div = element; + } + + if (this.map) { // map.addControl() has already been called on the panel + this.addControlsToMap(controls); + this.redraw(); + } + }, + + /** + * APIMethod: createControlMarkup + * This function just creates a div for the control. If specific HTML + * markup is needed this function can be overridden in specific classes, + * or at panel instantiation time: + * + * Example: + * (code) + * var panel = new OpenLayers.Control.Panel({ + * defaultControl: control, + * // ovverride createControlMarkup to create actual buttons + * // including texts wrapped into span elements. + * createControlMarkup: function(control) { + * var button = document.createElement('button'), + * span = document.createElement('span'); + * if (control.text) { + * span.innerHTML = control.text; + * } + * return button; + * } + * }); + * (end) + * + * Parameters: + * control - {<OpenLayers.Control>} The control to create the HTML + * markup for. + * + * Returns: + * {DOMElement} The markup. + */ + createControlMarkup: function(control) { + return document.createElement("div"); + }, + + /** + * Method: addControlsToMap + * Only for internal use in draw() and addControls() methods. + * + * Parameters: + * controls - {Array(<OpenLayers.Control>)} Controls to add into map. + */ + addControlsToMap: function (controls) { + var control; + for (var i=0, len=controls.length; i<len; i++) { + control = controls[i]; + if (control.autoActivate === true) { + control.autoActivate = false; + this.map.addControl(control); + control.autoActivate = true; + } else { + this.map.addControl(control); + control.deactivate(); + } + control.events.on({ + activate: this.iconOn, + deactivate: this.iconOff + }); + } + }, + + /** + * Method: iconOn + * Internal use, for use only with "controls[i].events.on/un". + */ + iconOn: function() { + var d = this.panel_div; // "this" refers to a control on panel! + var re = new RegExp("\\b(" + this.displayClass + "Item)Inactive\\b"); + d.className = d.className.replace(re, "$1Active"); + }, + + /** + * Method: iconOff + * Internal use, for use only with "controls[i].events.on/un". + */ + iconOff: function() { + var d = this.panel_div; // "this" refers to a control on panel! + var re = new RegExp("\\b(" + this.displayClass + "Item)Active\\b"); + d.className = d.className.replace(re, "$1Inactive"); + }, + + /** + * Method: onButtonClick + * + * Parameters: + * evt - {Event} + */ + onButtonClick: function (evt) { + var controls = this.controls, + button = evt.buttonElement; + for (var i=controls.length-1; i>=0; --i) { + if (controls[i].panel_div === button) { + this.activateControl(controls[i]); + break; + } + } + }, + + /** + * APIMethod: getControlsBy + * Get a list of controls with properties matching the given criteria. + * + * Parameters: + * property - {String} A control property to be matched. + * match - {String | Object} A string to match. Can also be a regular + * expression literal or object. In addition, it can be any object + * with a method named test. For reqular expressions or other, if + * match.test(control[property]) evaluates to true, the control will be + * included in the array returned. If no controls are found, an empty + * array is returned. + * + * Returns: + * {Array(<OpenLayers.Control>)} A list of controls matching the given criteria. + * An empty array is returned if no matches are found. + */ + getControlsBy: function(property, match) { + var test = (typeof match.test == "function"); + var found = OpenLayers.Array.filter(this.controls, function(item) { + return item[property] == match || (test && match.test(item[property])); + }); + return found; + }, + + /** + * APIMethod: getControlsByName + * Get a list of contorls with names matching the given name. + * + * Parameters: + * match - {String | Object} A control name. The name can also be a regular + * expression literal or object. In addition, it can be any object + * with a method named test. For reqular expressions or other, if + * name.test(control.name) evaluates to true, the control will be included + * in the list of controls returned. If no controls are found, an empty + * array is returned. + * + * Returns: + * {Array(<OpenLayers.Control>)} A list of controls matching the given name. + * An empty array is returned if no matches are found. + */ + getControlsByName: function(match) { + return this.getControlsBy("name", match); + }, + + /** + * APIMethod: getControlsByClass + * Get a list of controls of a given type (CLASS_NAME). + * + * Parameters: + * match - {String | Object} A control class name. The type can also be a + * regular expression literal or object. In addition, it can be any + * object with a method named test. For reqular expressions or other, + * if type.test(control.CLASS_NAME) evaluates to true, the control will + * be included in the list of controls returned. If no controls are + * found, an empty array is returned. + * + * Returns: + * {Array(<OpenLayers.Control>)} A list of controls matching the given type. + * An empty array is returned if no matches are found. + */ + getControlsByClass: function(match) { + return this.getControlsBy("CLASS_NAME", match); + }, + + CLASS_NAME: "OpenLayers.Control.Panel" +}); + diff --git a/misc/openlayers/lib/OpenLayers/Control/Permalink.js b/misc/openlayers/lib/OpenLayers/Control/Permalink.js new file mode 100644 index 0000000..3d5d7a2 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/Permalink.js @@ -0,0 +1,257 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Control/ArgParser.js + * @requires OpenLayers/Lang.js + */ + +/** + * Class: OpenLayers.Control.Permalink + * The Permalink control is hyperlink that will return the user to the + * current map view. By default it is drawn in the lower right corner of the + * map. The href is updated as the map is zoomed, panned and whilst layers + * are switched. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.Permalink = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: argParserClass + * {Class} The ArgParser control class (not instance) to use with this + * control. + */ + argParserClass: OpenLayers.Control.ArgParser, + + /** + * Property: element + * {DOMElement} + */ + element: null, + + /** + * APIProperty: anchor + * {Boolean} This option changes 3 things: + * the character '#' is used in place of the character '?', + * the window.href is updated if no element is provided. + * When this option is set to true it's not recommend to provide + * a base without provide an element. + */ + anchor: false, + + /** + * APIProperty: base + * {String} + */ + base: '', + + /** + * APIProperty: displayProjection + * {<OpenLayers.Projection>} Requires proj4js support. Projection used + * when creating the coordinates in the link. This will reproject the + * map coordinates into display coordinates. If you are using this + * functionality, the permalink which is last added to the map will + * determine the coordinate type which is read from the URL, which + * means you should not add permalinks with different + * displayProjections to the same map. + */ + displayProjection: null, + + /** + * Constructor: OpenLayers.Control.Permalink + * + * Parameters: + * element - {DOMElement} + * base - {String} + * options - {Object} options to the control. + * + * Or for anchor: + * options - {Object} options to the control. + */ + initialize: function(element, base, options) { + if (element !== null && typeof element == 'object' && !OpenLayers.Util.isElement(element)) { + options = element; + this.base = document.location.href; + OpenLayers.Control.prototype.initialize.apply(this, [options]); + if (this.element != null) { + this.element = OpenLayers.Util.getElement(this.element); + } + } + else { + OpenLayers.Control.prototype.initialize.apply(this, [options]); + this.element = OpenLayers.Util.getElement(element); + this.base = base || document.location.href; + } + }, + + /** + * APIMethod: destroy + */ + destroy: function() { + if (this.element && this.element.parentNode == this.div) { + this.div.removeChild(this.element); + this.element = null; + } + if (this.map) { + this.map.events.unregister('moveend', this, this.updateLink); + } + + OpenLayers.Control.prototype.destroy.apply(this, arguments); + }, + + /** + * Method: setMap + * Set the map property for the control. + * + * Parameters: + * map - {<OpenLayers.Map>} + */ + setMap: function(map) { + OpenLayers.Control.prototype.setMap.apply(this, arguments); + + //make sure we have an arg parser attached + for(var i=0, len=this.map.controls.length; i<len; i++) { + var control = this.map.controls[i]; + if (control.CLASS_NAME == this.argParserClass.CLASS_NAME) { + + // If a permalink is added to the map, and an ArgParser already + // exists, we override the displayProjection to be the one + // on the permalink. + if (control.displayProjection != this.displayProjection) { + this.displayProjection = control.displayProjection; + } + + break; + } + } + if (i == this.map.controls.length) { + this.map.addControl(new this.argParserClass( + { 'displayProjection': this.displayProjection })); + } + + }, + + /** + * Method: draw + * + * Returns: + * {DOMElement} + */ + draw: function() { + OpenLayers.Control.prototype.draw.apply(this, arguments); + + if (!this.element && !this.anchor) { + this.element = document.createElement("a"); + this.element.innerHTML = OpenLayers.i18n("Permalink"); + this.element.href=""; + this.div.appendChild(this.element); + } + this.map.events.on({ + 'moveend': this.updateLink, + 'changelayer': this.updateLink, + 'changebaselayer': this.updateLink, + scope: this + }); + + // Make it so there is at least a link even though the map may not have + // moved yet. + this.updateLink(); + + return this.div; + }, + + /** + * Method: updateLink + */ + updateLink: function() { + var separator = this.anchor ? '#' : '?'; + var href = this.base; + var anchor = null; + if (href.indexOf("#") != -1 && this.anchor == false) { + anchor = href.substring( href.indexOf("#"), href.length); + } + if (href.indexOf(separator) != -1) { + href = href.substring( 0, href.indexOf(separator) ); + } + var splits = href.split("#"); + href = splits[0] + separator+ OpenLayers.Util.getParameterString(this.createParams()); + if (anchor) { + href += anchor; + } + if (this.anchor && !this.element) { + window.location.href = href; + } + else { + this.element.href = href; + } + }, + + /** + * APIMethod: createParams + * Creates the parameters that need to be encoded into the permalink url. + * + * Parameters: + * center - {<OpenLayers.LonLat>} center to encode in the permalink. + * Defaults to the current map center. + * zoom - {Integer} zoom level to encode in the permalink. Defaults to the + * current map zoom level. + * layers - {Array(<OpenLayers.Layer>)} layers to encode in the permalink. + * Defaults to the current map layers. + * + * Returns: + * {Object} Hash of parameters that will be url-encoded into the + * permalink. + */ + createParams: function(center, zoom, layers) { + center = center || this.map.getCenter(); + + var params = OpenLayers.Util.getParameters(this.base); + + // If there's still no center, map is not initialized yet. + // Break out of this function, and simply return the params from the + // base link. + if (center) { + + //zoom + params.zoom = zoom || this.map.getZoom(); + + //lon,lat + var lat = center.lat; + var lon = center.lon; + + if (this.displayProjection) { + var mapPosition = OpenLayers.Projection.transform( + { x: lon, y: lat }, + this.map.getProjectionObject(), + this.displayProjection ); + lon = mapPosition.x; + lat = mapPosition.y; + } + params.lat = Math.round(lat*100000)/100000; + params.lon = Math.round(lon*100000)/100000; + + //layers + layers = layers || this.map.layers; + params.layers = ''; + for (var i=0, len=layers.length; i<len; i++) { + var layer = layers[i]; + + if (layer.isBaseLayer) { + params.layers += (layer == this.map.baseLayer) ? "B" : "0"; + } else { + params.layers += (layer.getVisibility()) ? "T" : "F"; + } + } + } + + return params; + }, + + CLASS_NAME: "OpenLayers.Control.Permalink" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/PinchZoom.js b/misc/openlayers/lib/OpenLayers/Control/PinchZoom.js new file mode 100644 index 0000000..13c1104 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/PinchZoom.js @@ -0,0 +1,157 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Handler/Pinch.js + */ + +/** + * Class: OpenLayers.Control.PinchZoom + * + * Inherits: + * - <OpenLayers.Control> + */ +OpenLayers.Control.PinchZoom = OpenLayers.Class(OpenLayers.Control, { + + /** + * Property: type + * {OpenLayers.Control.TYPES} + */ + type: OpenLayers.Control.TYPE_TOOL, + + /** + * Property: pinchOrigin + * {Object} Cached object representing the pinch start (in pixels). + */ + pinchOrigin: null, + + /** + * Property: currentCenter + * {Object} Cached object representing the latest pinch center (in pixels). + */ + currentCenter: null, + + /** + * APIProperty: autoActivate + * {Boolean} Activate the control when it is added to a map. Default is + * true. + */ + autoActivate: true, + + /** + * APIProperty: preserveCenter + * {Boolean} Set this to true if you don't want the map center to change + * while pinching. For example you may want to set preserveCenter to + * true when the user location is being watched and you want to preserve + * the user location at the center of the map even if he zooms in or + * out using pinch. This property's value can be changed any time on an + * existing instance. Default is false. + */ + preserveCenter: false, + + /** + * APIProperty: handlerOptions + * {Object} Used to set non-default properties on the pinch handler + */ + + /** + * Constructor: OpenLayers.Control.PinchZoom + * Create a control for zooming with pinch gestures. This works on devices + * with multi-touch support. + * + * Parameters: + * options - {Object} An optional object whose properties will be set on + * the control + */ + initialize: function(options) { + OpenLayers.Control.prototype.initialize.apply(this, arguments); + this.handler = new OpenLayers.Handler.Pinch(this, { + start: this.pinchStart, + move: this.pinchMove, + done: this.pinchDone + }, this.handlerOptions); + }, + + /** + * Method: pinchStart + * + * Parameters: + * evt - {Event} + * pinchData - {Object} pinch data object related to the current touchmove + * of the pinch gesture. This give us the current scale of the pinch. + */ + pinchStart: function(evt, pinchData) { + var xy = (this.preserveCenter) ? + this.map.getPixelFromLonLat(this.map.getCenter()) : evt.xy; + this.pinchOrigin = xy; + this.currentCenter = xy; + }, + + /** + * Method: pinchMove + * + * Parameters: + * evt - {Event} + * pinchData - {Object} pinch data object related to the current touchmove + * of the pinch gesture. This give us the current scale of the pinch. + */ + pinchMove: function(evt, pinchData) { + var scale = pinchData.scale; + var containerOrigin = this.map.layerContainerOriginPx; + var pinchOrigin = this.pinchOrigin; + var current = (this.preserveCenter) ? + this.map.getPixelFromLonLat(this.map.getCenter()) : evt.xy; + + var dx = Math.round((containerOrigin.x + current.x - pinchOrigin.x) + (scale - 1) * (containerOrigin.x - pinchOrigin.x)); + var dy = Math.round((containerOrigin.y + current.y - pinchOrigin.y) + (scale - 1) * (containerOrigin.y - pinchOrigin.y)); + + this.map.applyTransform(dx, dy, scale); + this.currentCenter = current; + }, + + /** + * Method: pinchDone + * + * Parameters: + * evt - {Event} + * start - {Object} pinch data object related to the touchstart event that + * started the pinch gesture. + * last - {Object} pinch data object related to the last touchmove event + * of the pinch gesture. This give us the final scale of the pinch. + */ + pinchDone: function(evt, start, last) { + this.map.applyTransform(); + var zoom = this.map.getZoomForResolution(this.map.getResolution() / last.scale, true); + if (zoom !== this.map.getZoom() || !this.currentCenter.equals(this.pinchOrigin)) { + var resolution = this.map.getResolutionForZoom(zoom); + + var location = this.map.getLonLatFromPixel(this.pinchOrigin); + var zoomPixel = this.currentCenter; + var size = this.map.getSize(); + + location.lon += resolution * ((size.w / 2) - zoomPixel.x); + location.lat -= resolution * ((size.h / 2) - zoomPixel.y); + + // Force a reflow before calling setCenter. This is to work + // around an issue occuring in iOS. + // + // See https://github.com/openlayers/openlayers/pull/351. + // + // Without a reflow setting the layer container div's top left + // style properties to "0px" - as done in Map.moveTo when zoom + // is changed - won't actually correctly reposition the layer + // container div. + // + // Also, we need to use a statement that the Google Closure + // compiler won't optimize away. + this.map.div.clientWidth = this.map.div.clientWidth; + + this.map.setCenter(location, zoom); + } + }, + + CLASS_NAME: "OpenLayers.Control.PinchZoom" + +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/SLDSelect.js b/misc/openlayers/lib/OpenLayers/Control/SLDSelect.js new file mode 100644 index 0000000..cd348a7 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/SLDSelect.js @@ -0,0 +1,567 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Layer/WMS.js + * @requires OpenLayers/Handler/RegularPolygon.js + * @requires OpenLayers/Handler/Polygon.js + * @requires OpenLayers/Handler/Path.js + * @requires OpenLayers/Handler/Click.js + * @requires OpenLayers/Filter/Spatial.js + * @requires OpenLayers/Format/SLD/v1_0_0.js + */ + +/** + * Class: OpenLayers.Control.SLDSelect + * Perform selections on WMS layers using Styled Layer Descriptor (SLD) + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.SLDSelect = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: events + * {<OpenLayers.Events>} Events instance for listeners and triggering + * control specific events. + * + * Register a listener for a particular event with the following syntax: + * (code) + * control.events.register(type, obj, listener); + * (end) + * + * Supported event types (in addition to those from <OpenLayers.Control.events>): + * selected - Triggered when a selection occurs. Listeners receive an + * event with *filters* and *layer* properties. Filters will be an + * array of OpenLayers.Filter objects created in order to perform + * the particular selection. + */ + + /** + * APIProperty: clearOnDeactivate + * {Boolean} Should the selection be cleared when the control is + * deactivated. Default value is false. + */ + clearOnDeactivate: false, + + /** + * APIProperty: layers + * {Array(<OpenLayers.Layer.WMS>)} The WMS layers this control will work + * on. + */ + layers: null, + + /** + * Property: callbacks + * {Object} The functions that are sent to the handler for callback + */ + callbacks: null, + + /** + * APIProperty: selectionSymbolizer + * {Object} Determines the styling of the selected objects. Default is + * a selection in red. + */ + selectionSymbolizer: { + 'Polygon': {fillColor: '#FF0000', stroke: false}, + 'Line': {strokeColor: '#FF0000', strokeWidth: 2}, + 'Point': {graphicName: 'square', fillColor: '#FF0000', pointRadius: 5} + }, + + /** + * APIProperty: layerOptions + * {Object} The options to apply to the selection layer, by default the + * selection layer will be kept out of the layer switcher. + */ + layerOptions: null, + + /** + * APIProperty: handlerOptions + * {Object} Used to set non-default properties on the control's handler + */ + + /** + * APIProperty: sketchStyle + * {<OpenLayers.Style>|Object} Style or symbolizer to use for the sketch + * handler. The recommended way of styling the sketch layer, however, is + * to configure an <OpenLayers.StyleMap> in the layerOptions of the + * <handlerOptions>: + * + * (code) + * new OpenLayers.Control.SLDSelect(OpenLayers.Handler.Path, { + * handlerOptions: { + * layerOptions: { + * styleMap: new OpenLayers.StyleMap({ + * "default": {strokeColor: "yellow"} + * }) + * } + * } + * }); + * (end) + */ + sketchStyle: null, + + /** + * APIProperty: wfsCache + * {Object} Cache to use for storing parsed results from + * <OpenLayers.Format.WFSDescribeFeatureType.read>. If not provided, + * these will be cached on the prototype. + */ + wfsCache: {}, + + /** + * APIProperty: layerCache + * {Object} Cache to use for storing references to the selection layers. + * Normally each source layer will have exactly 1 selection layer of + * type OpenLayers.Layer.WMS. If not provided, layers will + * be cached on the prototype. Note that if <clearOnDeactivate> is + * true, the layer will no longer be cached after deactivating the + * control. + */ + layerCache: {}, + + /** + * Constructor: OpenLayers.Control.SLDSelect + * Create a new control for selecting features in WMS layers using + * Styled Layer Descriptor (SLD). + * + * Parameters: + * handler - {<OpenLayers.Class>} A sketch handler class. This determines + * the type of selection, e.g. box (<OpenLayers.Handler.Box>), point + * (<OpenLayers.Handler.Point>), path (<OpenLayers.Handler.Path>) or + * polygon (<OpenLayers.Handler.Polygon>) selection. To use circle + * type selection, use <OpenLayers.Handler.RegularPolygon> and pass + * the number of desired sides (e.g. 40) as "sides" property to the + * <handlerOptions>. + * options - {Object} An object containing all configuration properties for + * the control. + * + * Valid options: + * layers - Array({<OpenLayers.Layer.WMS>}) The layers to perform the + * selection on. + */ + initialize: function(handler, options) { + OpenLayers.Control.prototype.initialize.apply(this, [options]); + + this.callbacks = OpenLayers.Util.extend({done: this.select, + click: this.select}, this.callbacks); + this.handlerOptions = this.handlerOptions || {}; + this.layerOptions = OpenLayers.Util.applyDefaults(this.layerOptions, { + displayInLayerSwitcher: false, + tileOptions: {maxGetUrlLength: 2048} + }); + if (this.sketchStyle) { + this.handlerOptions.layerOptions = OpenLayers.Util.applyDefaults( + this.handlerOptions.layerOptions, + {styleMap: new OpenLayers.StyleMap({"default": this.sketchStyle})} + ); + } + this.handler = new handler(this, this.callbacks, this.handlerOptions); + }, + + /** + * APIMethod: destroy + * Take care of things that are not handled in superclass. + */ + destroy: function() { + for (var key in this.layerCache) { + delete this.layerCache[key]; + } + for (var key in this.wfsCache) { + delete this.wfsCache[key]; + } + OpenLayers.Control.prototype.destroy.apply(this, arguments); + }, + + /** + * Method: coupleLayerVisiblity + * Couple the selection layer and the source layer with respect to + * layer visibility. So if the source layer is turned off, the + * selection layer is also turned off. + * + * Context: + * - {<OpenLayers.Layer>} + * + * Parameters: + * evt - {Object} + */ + coupleLayerVisiblity: function(evt) { + this.setVisibility(evt.object.getVisibility()); + }, + + /** + * Method: createSelectionLayer + * Creates a "clone" from the source layer in which the selection can + * be drawn. This ensures both the source layer and the selection are + * visible and not only the selection. + * + * Parameters: + * source - {<OpenLayers.Layer.WMS>} The source layer on which the selection + * is performed. + * + * Returns: + * {<OpenLayers.Layer.WMS>} A WMS layer with maxGetUrlLength configured to 2048 + * since SLD selections can easily get quite long. + */ + createSelectionLayer: function(source) { + // check if we already have a selection layer for the source layer + var selectionLayer; + if (!this.layerCache[source.id]) { + selectionLayer = new OpenLayers.Layer.WMS(source.name, + source.url, source.params, + OpenLayers.Util.applyDefaults( + this.layerOptions, + source.getOptions()) + ); + this.layerCache[source.id] = selectionLayer; + // make sure the layers are coupled wrt visibility, but only + // if they are not displayed in the layer switcher, because in + // that case the user cannot control visibility. + if (this.layerOptions.displayInLayerSwitcher === false) { + source.events.on({ + "visibilitychanged": this.coupleLayerVisiblity, + scope: selectionLayer}); + } + this.map.addLayer(selectionLayer); + } else { + selectionLayer = this.layerCache[source.id]; + } + return selectionLayer; + }, + + /** + * Method: createSLD + * Create the SLD document for the layer using the supplied filters. + * + * Parameters: + * layer - {<OpenLayers.Layer.WMS>} + * filters - Array({<OpenLayers.Filter>}) The filters to be applied. + * geometryAttributes - Array({Object}) The geometry attributes of the + * layer. + * + * Returns: + * {String} The SLD document generated as a string. + */ + createSLD: function(layer, filters, geometryAttributes) { + var sld = {version: "1.0.0", namedLayers: {}}; + var layerNames = [layer.params.LAYERS].join(",").split(","); + for (var i=0, len=layerNames.length; i<len; i++) { + var name = layerNames[i]; + sld.namedLayers[name] = {name: name, userStyles: []}; + var symbolizer = this.selectionSymbolizer; + var geometryAttribute = geometryAttributes[i]; + if (geometryAttribute.type.indexOf('Polygon') >= 0) { + symbolizer = {Polygon: this.selectionSymbolizer['Polygon']}; + } else if (geometryAttribute.type.indexOf('LineString') >= 0) { + symbolizer = {Line: this.selectionSymbolizer['Line']}; + } else if (geometryAttribute.type.indexOf('Point') >= 0) { + symbolizer = {Point: this.selectionSymbolizer['Point']}; + } + var filter = filters[i]; + sld.namedLayers[name].userStyles.push({name: 'default', rules: [ + new OpenLayers.Rule({symbolizer: symbolizer, + filter: filter, + maxScaleDenominator: layer.options.minScale}) + ]}); + } + return new OpenLayers.Format.SLD({srsName: this.map.getProjection()}).write(sld); + }, + + /** + * Method: parseDescribeLayer + * Parse the SLD WMS DescribeLayer response and issue the corresponding + * WFS DescribeFeatureType request + * + * request - {XMLHttpRequest} The request object. + */ + parseDescribeLayer: function(request) { + var format = new OpenLayers.Format.WMSDescribeLayer(); + var doc = request.responseXML; + if(!doc || !doc.documentElement) { + doc = request.responseText; + } + var describeLayer = format.read(doc); + var typeNames = []; + var url = null; + for (var i=0, len=describeLayer.length; i<len; i++) { + // perform a WFS DescribeFeatureType request + if (describeLayer[i].owsType == "WFS") { + typeNames.push(describeLayer[i].typeName); + url = describeLayer[i].owsURL; + } + } + var options = { + url: url, + params: { + SERVICE: "WFS", + TYPENAME: typeNames.toString(), + REQUEST: "DescribeFeatureType", + VERSION: "1.0.0" + }, + callback: function(request) { + var format = new OpenLayers.Format.WFSDescribeFeatureType(); + var doc = request.responseXML; + if(!doc || !doc.documentElement) { + doc = request.responseText; + } + var describeFeatureType = format.read(doc); + this.control.wfsCache[this.layer.id] = describeFeatureType; + this.control._queue && this.control.applySelection(); + }, + scope: this + }; + OpenLayers.Request.GET(options); + }, + + /** + * Method: getGeometryAttributes + * Look up the geometry attributes from the WFS DescribeFeatureType response + * + * Parameters: + * layer - {<OpenLayers.Layer.WMS>} The layer for which to look up the + * geometry attributes. + * + * Returns: + * Array({Object}) Array of geometry attributes + */ + getGeometryAttributes: function(layer) { + var result = []; + var cache = this.wfsCache[layer.id]; + for (var i=0, len=cache.featureTypes.length; i<len; i++) { + var typeName = cache.featureTypes[i]; + var properties = typeName.properties; + for (var j=0, lenj=properties.length; j < lenj; j++) { + var property = properties[j]; + var type = property.type; + if ((type.indexOf('LineString') >= 0) || + (type.indexOf('GeometryAssociationType') >=0) || + (type.indexOf('GeometryPropertyType') >= 0) || + (type.indexOf('Point') >= 0) || + (type.indexOf('Polygon') >= 0) ) { + result.push(property); + } + } + } + return result; + }, + + /** + * APIMethod: activate + * Activate the control. Activating the control will perform a SLD WMS + * DescribeLayer request followed by a WFS DescribeFeatureType request + * so that the proper symbolizers can be chosen based on the geometry + * type. + */ + activate: function() { + var activated = OpenLayers.Control.prototype.activate.call(this); + if(activated) { + for (var i=0, len=this.layers.length; i<len; i++) { + var layer = this.layers[i]; + if (layer && !this.wfsCache[layer.id]) { + var options = { + url: layer.url, + params: { + SERVICE: "WMS", + VERSION: layer.params.VERSION, + LAYERS: layer.params.LAYERS, + REQUEST: "DescribeLayer" + }, + callback: this.parseDescribeLayer, + scope: {layer: layer, control: this} + }; + OpenLayers.Request.GET(options); + } + } + } + return activated; + }, + + /** + * APIMethod: deactivate + * Deactivate the control. If clearOnDeactivate is true, remove the + * selection layer(s). + */ + deactivate: function() { + var deactivated = OpenLayers.Control.prototype.deactivate.call(this); + if(deactivated) { + for (var i=0, len=this.layers.length; i<len; i++) { + var layer = this.layers[i]; + if (layer && this.clearOnDeactivate === true) { + var layerCache = this.layerCache; + var selectionLayer = layerCache[layer.id]; + if (selectionLayer) { + layer.events.un({ + "visibilitychanged": this.coupleLayerVisiblity, + scope: selectionLayer}); + selectionLayer.destroy(); + delete layerCache[layer.id]; + } + } + } + } + return deactivated; + }, + + /** + * APIMethod: setLayers + * Set the layers on which the selection should be performed. Call the + * setLayers method if the layer(s) to be used change and the same + * control should be used on a new set of layers. + * If the control is already active, it will be active after the new + * set of layers is set. + * + * Parameters: + * layers - {Array(<OpenLayers.Layer.WMS>)} The new set of layers on which + * the selection should be performed. + */ + setLayers: function(layers) { + if(this.active) { + this.deactivate(); + this.layers = layers; + this.activate(); + } else { + this.layers = layers; + } + }, + + /** + * Function: createFilter + * Create the filter to be used in the SLD. + * + * Parameters: + * geometryAttribute - {Object} Used to get the name of the geometry + * attribute which is needed for constructing the spatial filter. + * geometry - {<OpenLayers.Geometry>} The geometry to use. + * + * Returns: + * {<OpenLayers.Filter.Spatial>} The spatial filter created. + */ + createFilter: function(geometryAttribute, geometry) { + var filter = null; + if (this.handler instanceof OpenLayers.Handler.RegularPolygon) { + // box + if (this.handler.irregular === true) { + filter = new OpenLayers.Filter.Spatial({ + type: OpenLayers.Filter.Spatial.BBOX, + property: geometryAttribute.name, + value: geometry.getBounds()} + ); + } else { + filter = new OpenLayers.Filter.Spatial({ + type: OpenLayers.Filter.Spatial.INTERSECTS, + property: geometryAttribute.name, + value: geometry} + ); + } + } else if (this.handler instanceof OpenLayers.Handler.Polygon) { + filter = new OpenLayers.Filter.Spatial({ + type: OpenLayers.Filter.Spatial.INTERSECTS, + property: geometryAttribute.name, + value: geometry} + ); + } else if (this.handler instanceof OpenLayers.Handler.Path) { + // if source layer is point based, use DWITHIN instead + if (geometryAttribute.type.indexOf('Point') >= 0) { + filter = new OpenLayers.Filter.Spatial({ + type: OpenLayers.Filter.Spatial.DWITHIN, + property: geometryAttribute.name, + distance: this.map.getExtent().getWidth()*0.01 , + distanceUnits: this.map.getUnits(), + value: geometry} + ); + } else { + filter = new OpenLayers.Filter.Spatial({ + type: OpenLayers.Filter.Spatial.INTERSECTS, + property: geometryAttribute.name, + value: geometry} + ); + } + } else if (this.handler instanceof OpenLayers.Handler.Click) { + if (geometryAttribute.type.indexOf('Polygon') >= 0) { + filter = new OpenLayers.Filter.Spatial({ + type: OpenLayers.Filter.Spatial.INTERSECTS, + property: geometryAttribute.name, + value: geometry} + ); + } else { + filter = new OpenLayers.Filter.Spatial({ + type: OpenLayers.Filter.Spatial.DWITHIN, + property: geometryAttribute.name, + distance: this.map.getExtent().getWidth()*0.01 , + distanceUnits: this.map.getUnits(), + value: geometry} + ); + } + } + return filter; + }, + + /** + * Method: select + * When the handler is done, use SLD_BODY on the selection layer to + * display the selection in the map. + * + * Parameters: + * geometry - {Object} or {<OpenLayers.Geometry>} + */ + select: function(geometry) { + this._queue = function() { + for (var i=0, len=this.layers.length; i<len; i++) { + var layer = this.layers[i]; + var geometryAttributes = this.getGeometryAttributes(layer); + var filters = []; + for (var j=0, lenj=geometryAttributes.length; j<lenj; j++) { + var geometryAttribute = geometryAttributes[j]; + if (geometryAttribute !== null) { + // from the click handler we will not get an actual + // geometry so transform + if (!(geometry instanceof OpenLayers.Geometry)) { + var point = this.map.getLonLatFromPixel( + geometry.xy); + geometry = new OpenLayers.Geometry.Point( + point.lon, point.lat); + } + var filter = this.createFilter(geometryAttribute, + geometry); + if (filter !== null) { + filters.push(filter); + } + } + } + + var selectionLayer = this.createSelectionLayer(layer); + + this.events.triggerEvent("selected", { + layer: layer, + filters: filters + }); + + var sld = this.createSLD(layer, filters, geometryAttributes); + + selectionLayer.mergeNewParams({SLD_BODY: sld}); + delete this._queue; + } + }; + this.applySelection(); + }, + + /** + * Method: applySelection + * Checks if all required wfs data is cached, and applies the selection + */ + applySelection: function() { + var canApply = true; + for (var i=0, len=this.layers.length; i<len; i++) { + if(!this.wfsCache[this.layers[i].id]) { + canApply = false; + break; + } + } + canApply && this._queue.call(this); + }, + + CLASS_NAME: "OpenLayers.Control.SLDSelect" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/Scale.js b/misc/openlayers/lib/OpenLayers/Control/Scale.js new file mode 100644 index 0000000..c9f2d2b --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/Scale.js @@ -0,0 +1,100 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Lang.js + */ + +/** + * Class: OpenLayers.Control.Scale + * The Scale control displays the current map scale as a ratio (e.g. Scale = + * 1:1M). By default it is displayed in the lower right corner of the map. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.Scale = OpenLayers.Class(OpenLayers.Control, { + + /** + * Property: element + * {DOMElement} + */ + element: null, + + /** + * APIProperty: geodesic + * {Boolean} Use geodesic measurement. Default is false. The recommended + * setting for maps in EPSG:4326 is false, and true EPSG:900913. If set to + * true, the scale will be calculated based on the horizontal size of the + * pixel in the center of the map viewport. + */ + geodesic: false, + + /** + * Constructor: OpenLayers.Control.Scale + * + * Parameters: + * element - {DOMElement} + * options - {Object} + */ + initialize: function(element, options) { + OpenLayers.Control.prototype.initialize.apply(this, [options]); + this.element = OpenLayers.Util.getElement(element); + }, + + /** + * Method: draw + * + * Returns: + * {DOMElement} + */ + draw: function() { + OpenLayers.Control.prototype.draw.apply(this, arguments); + if (!this.element) { + this.element = document.createElement("div"); + this.div.appendChild(this.element); + } + this.map.events.register( 'moveend', this, this.updateScale); + this.updateScale(); + return this.div; + }, + + /** + * Method: updateScale + */ + updateScale: function() { + var scale; + if(this.geodesic === true) { + var units = this.map.getUnits(); + if(!units) { + return; + } + var inches = OpenLayers.INCHES_PER_UNIT; + scale = (this.map.getGeodesicPixelSize().w || 0.000001) * + inches["km"] * OpenLayers.DOTS_PER_INCH; + } else { + scale = this.map.getScale(); + } + + if (!scale) { + return; + } + + if (scale >= 9500 && scale <= 950000) { + scale = Math.round(scale / 1000) + "K"; + } else if (scale >= 950000) { + scale = Math.round(scale / 1000000) + "M"; + } else { + scale = Math.round(scale); + } + + this.element.innerHTML = OpenLayers.i18n("Scale = 1 : ${scaleDenom}", {'scaleDenom':scale}); + }, + + CLASS_NAME: "OpenLayers.Control.Scale" +}); + diff --git a/misc/openlayers/lib/OpenLayers/Control/ScaleLine.js b/misc/openlayers/lib/OpenLayers/Control/ScaleLine.js new file mode 100644 index 0000000..9262414 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/ScaleLine.js @@ -0,0 +1,220 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + */ + +/** + * Class: OpenLayers.Control.ScaleLine + * The ScaleLine displays a small line indicator representing the current + * map scale on the map. By default it is drawn in the lower left corner of + * the map. + * + * Inherits from: + * - <OpenLayers.Control> + * + * Is a very close copy of: + * - <OpenLayers.Control.Scale> + */ +OpenLayers.Control.ScaleLine = OpenLayers.Class(OpenLayers.Control, { + + /** + * Property: maxWidth + * {Integer} Maximum width of the scale line in pixels. Default is 100. + */ + maxWidth: 100, + + /** + * Property: topOutUnits + * {String} Units for zoomed out on top bar. Default is km. + */ + topOutUnits: "km", + + /** + * Property: topInUnits + * {String} Units for zoomed in on top bar. Default is m. + */ + topInUnits: "m", + + /** + * Property: bottomOutUnits + * {String} Units for zoomed out on bottom bar. Default is mi. + */ + bottomOutUnits: "mi", + + /** + * Property: bottomInUnits + * {String} Units for zoomed in on bottom bar. Default is ft. + */ + bottomInUnits: "ft", + + /** + * Property: eTop + * {DOMElement} + */ + eTop: null, + + /** + * Property: eBottom + * {DOMElement} + */ + eBottom:null, + + /** + * APIProperty: geodesic + * {Boolean} Use geodesic measurement. Default is false. The recommended + * setting for maps in EPSG:4326 is false, and true EPSG:900913. If set to + * true, the scale will be calculated based on the horizontal size of the + * pixel in the center of the map viewport. + */ + geodesic: false, + + /** + * Constructor: OpenLayers.Control.ScaleLine + * Create a new scale line control. + * + * Parameters: + * options - {Object} An optional object whose properties will be used + * to extend the control. + */ + + /** + * Method: draw + * + * Returns: + * {DOMElement} + */ + draw: function() { + OpenLayers.Control.prototype.draw.apply(this, arguments); + if (!this.eTop) { + // stick in the top bar + this.eTop = document.createElement("div"); + this.eTop.className = this.displayClass + "Top"; + var theLen = this.topInUnits.length; + this.div.appendChild(this.eTop); + if((this.topOutUnits == "") || (this.topInUnits == "")) { + this.eTop.style.visibility = "hidden"; + } else { + this.eTop.style.visibility = "visible"; + } + + // and the bottom bar + this.eBottom = document.createElement("div"); + this.eBottom.className = this.displayClass + "Bottom"; + this.div.appendChild(this.eBottom); + if((this.bottomOutUnits == "") || (this.bottomInUnits == "")) { + this.eBottom.style.visibility = "hidden"; + } else { + this.eBottom.style.visibility = "visible"; + } + } + this.map.events.register('moveend', this, this.update); + this.update(); + return this.div; + }, + + /** + * Method: getBarLen + * Given a number, round it down to the nearest 1,2,5 times a power of 10. + * That seems a fairly useful set of number groups to use. + * + * Parameters: + * maxLen - {float} the number we're rounding down from + * + * Returns: + * {Float} the rounded number (less than or equal to maxLen) + */ + getBarLen: function(maxLen) { + // nearest power of 10 lower than maxLen + var digits = parseInt(Math.log(maxLen) / Math.log(10)); + var pow10 = Math.pow(10, digits); + + // ok, find first character + var firstChar = parseInt(maxLen / pow10); + + // right, put it into the correct bracket + var barLen; + if(firstChar > 5) { + barLen = 5; + } else if(firstChar > 2) { + barLen = 2; + } else { + barLen = 1; + } + + // scale it up the correct power of 10 + return barLen * pow10; + }, + + /** + * Method: update + * Update the size of the bars, and the labels they contain. + */ + update: function() { + var res = this.map.getResolution(); + if (!res) { + return; + } + + var curMapUnits = this.map.getUnits(); + var inches = OpenLayers.INCHES_PER_UNIT; + + // convert maxWidth to map units + var maxSizeData = this.maxWidth * res * inches[curMapUnits]; + var geodesicRatio = 1; + if(this.geodesic === true) { + var maxSizeGeodesic = (this.map.getGeodesicPixelSize().w || + 0.000001) * this.maxWidth; + var maxSizeKilometers = maxSizeData / inches["km"]; + geodesicRatio = maxSizeGeodesic / maxSizeKilometers; + maxSizeData *= geodesicRatio; + } + + // decide whether to use large or small scale units + var topUnits; + var bottomUnits; + if(maxSizeData > 100000) { + topUnits = this.topOutUnits; + bottomUnits = this.bottomOutUnits; + } else { + topUnits = this.topInUnits; + bottomUnits = this.bottomInUnits; + } + + // and to map units units + var topMax = maxSizeData / inches[topUnits]; + var bottomMax = maxSizeData / inches[bottomUnits]; + + // now trim this down to useful block length + var topRounded = this.getBarLen(topMax); + var bottomRounded = this.getBarLen(bottomMax); + + // and back to display units + topMax = topRounded / inches[curMapUnits] * inches[topUnits]; + bottomMax = bottomRounded / inches[curMapUnits] * inches[bottomUnits]; + + // and to pixel units + var topPx = topMax / res / geodesicRatio; + var bottomPx = bottomMax / res / geodesicRatio; + + // now set the pixel widths + // and the values inside them + + if (this.eBottom.style.visibility == "visible"){ + this.eBottom.style.width = Math.round(bottomPx) + "px"; + this.eBottom.innerHTML = bottomRounded + " " + bottomUnits ; + } + + if (this.eTop.style.visibility == "visible"){ + this.eTop.style.width = Math.round(topPx) + "px"; + this.eTop.innerHTML = topRounded + " " + topUnits; + } + + }, + + CLASS_NAME: "OpenLayers.Control.ScaleLine" +}); + diff --git a/misc/openlayers/lib/OpenLayers/Control/SelectFeature.js b/misc/openlayers/lib/OpenLayers/Control/SelectFeature.js new file mode 100644 index 0000000..5467267 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/SelectFeature.js @@ -0,0 +1,643 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Feature/Vector.js + * @requires OpenLayers/Handler/Feature.js + * @requires OpenLayers/Layer/Vector/RootContainer.js + */ + +/** + * Class: OpenLayers.Control.SelectFeature + * The SelectFeature control selects vector features from a given layer on + * click or hover. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: events + * {<OpenLayers.Events>} Events instance for listeners and triggering + * control specific events. + * + * Register a listener for a particular event with the following syntax: + * (code) + * control.events.register(type, obj, listener); + * (end) + * + * Supported event types (in addition to those from <OpenLayers.Control.events>): + * beforefeaturehighlighted - Triggered before a feature is highlighted + * featurehighlighted - Triggered when a feature is highlighted + * featureunhighlighted - Triggered when a feature is unhighlighted + * boxselectionstart - Triggered before box selection starts + * boxselectionend - Triggered after box selection ends + */ + + /** + * Property: multipleKey + * {String} An event modifier ('altKey' or 'shiftKey') that temporarily sets + * the <multiple> property to true. Default is null. + */ + multipleKey: null, + + /** + * Property: toggleKey + * {String} An event modifier ('altKey' or 'shiftKey') that temporarily sets + * the <toggle> property to true. Default is null. + */ + toggleKey: null, + + /** + * APIProperty: multiple + * {Boolean} Allow selection of multiple geometries. Default is false. + */ + multiple: false, + + /** + * APIProperty: clickout + * {Boolean} Unselect features when clicking outside any feature. + * Default is true. + */ + clickout: true, + + /** + * APIProperty: toggle + * {Boolean} Unselect a selected feature on click. Default is false. Only + * has meaning if hover is false. + */ + toggle: false, + + /** + * APIProperty: hover + * {Boolean} Select on mouse over and deselect on mouse out. If true, this + * ignores clicks and only listens to mouse moves. + */ + hover: false, + + /** + * APIProperty: highlightOnly + * {Boolean} If true do not actually select features (that is place them in + * the layer's selected features array), just highlight them. This property + * has no effect if hover is false. Defaults to false. + */ + highlightOnly: false, + + /** + * APIProperty: box + * {Boolean} Allow feature selection by drawing a box. + */ + box: false, + + /** + * Property: onBeforeSelect + * {Function} Optional function to be called before a feature is selected. + * The function should expect to be called with a feature. + */ + onBeforeSelect: function() {}, + + /** + * APIProperty: onSelect + * {Function} Optional function to be called when a feature is selected. + * The function should expect to be called with a feature. + */ + onSelect: function() {}, + + /** + * APIProperty: onUnselect + * {Function} Optional function to be called when a feature is unselected. + * The function should expect to be called with a feature. + */ + onUnselect: function() {}, + + /** + * Property: scope + * {Object} The scope to use with the onBeforeSelect, onSelect, onUnselect + * callbacks. If null the scope will be this control. + */ + scope: null, + + /** + * APIProperty: geometryTypes + * {Array(String)} To restrict selecting to a limited set of geometry types, + * send a list of strings corresponding to the geometry class names. + */ + geometryTypes: null, + + /** + * Property: layer + * {<OpenLayers.Layer.Vector>} The vector layer with a common renderer + * root for all layers this control is configured with (if an array of + * layers was passed to the constructor), or the vector layer the control + * was configured with (if a single layer was passed to the constructor). + */ + layer: null, + + /** + * Property: layers + * {Array(<OpenLayers.Layer.Vector>)} The layers this control will work on, + * or null if the control was configured with a single layer + */ + layers: null, + + /** + * APIProperty: callbacks + * {Object} The functions that are sent to the handlers.feature for callback + */ + callbacks: null, + + /** + * APIProperty: selectStyle + * {Object} Hash of styles + */ + selectStyle: null, + + /** + * Property: renderIntent + * {String} key used to retrieve the select style from the layer's + * style map. + */ + renderIntent: "select", + + /** + * Property: handlers + * {Object} Object with references to multiple <OpenLayers.Handler> + * instances. + */ + handlers: null, + + /** + * Constructor: OpenLayers.Control.SelectFeature + * Create a new control for selecting features. + * + * Parameters: + * layers - {<OpenLayers.Layer.Vector>}, or an array of vector layers. The + * layer(s) this control will select features from. + * options - {Object} + */ + initialize: function(layers, options) { + OpenLayers.Control.prototype.initialize.apply(this, [options]); + + if(this.scope === null) { + this.scope = this; + } + this.initLayer(layers); + var callbacks = { + click: this.clickFeature, + clickout: this.clickoutFeature + }; + if (this.hover) { + callbacks.over = this.overFeature; + callbacks.out = this.outFeature; + } + + this.callbacks = OpenLayers.Util.extend(callbacks, this.callbacks); + this.handlers = { + feature: new OpenLayers.Handler.Feature( + this, this.layer, this.callbacks, + {geometryTypes: this.geometryTypes} + ) + }; + + if (this.box) { + this.handlers.box = new OpenLayers.Handler.Box( + this, {done: this.selectBox}, + {boxDivClassName: "olHandlerBoxSelectFeature"} + ); + } + }, + + /** + * Method: initLayer + * Assign the layer property. If layers is an array, we need to use + * a RootContainer. + * + * Parameters: + * layers - {<OpenLayers.Layer.Vector>}, or an array of vector layers. + */ + initLayer: function(layers) { + if(OpenLayers.Util.isArray(layers)) { + this.layers = layers; + this.layer = new OpenLayers.Layer.Vector.RootContainer( + this.id + "_container", { + layers: layers + } + ); + } else { + this.layer = layers; + } + }, + + /** + * Method: destroy + */ + destroy: function() { + if(this.active && this.layers) { + this.map.removeLayer(this.layer); + } + OpenLayers.Control.prototype.destroy.apply(this, arguments); + if(this.layers) { + this.layer.destroy(); + } + }, + + /** + * Method: activate + * Activates the control. + * + * Returns: + * {Boolean} The control was effectively activated. + */ + activate: function () { + if (!this.active) { + if(this.layers) { + this.map.addLayer(this.layer); + } + this.handlers.feature.activate(); + if(this.box && this.handlers.box) { + this.handlers.box.activate(); + } + } + return OpenLayers.Control.prototype.activate.apply( + this, arguments + ); + }, + + /** + * Method: deactivate + * Deactivates the control. + * + * Returns: + * {Boolean} The control was effectively deactivated. + */ + deactivate: function () { + if (this.active) { + this.handlers.feature.deactivate(); + if(this.handlers.box) { + this.handlers.box.deactivate(); + } + if(this.layers) { + this.map.removeLayer(this.layer); + } + } + return OpenLayers.Control.prototype.deactivate.apply( + this, arguments + ); + }, + + /** + * Method: unselectAll + * Unselect all selected features. To unselect all except for a single + * feature, set the options.except property to the feature. + * + * Parameters: + * options - {Object} Optional configuration object. + */ + unselectAll: function(options) { + // we'll want an option to supress notification here + var layers = this.layers || [this.layer], + layer, feature, l, numExcept; + for(l=0; l<layers.length; ++l) { + layer = layers[l]; + numExcept = 0; + //layer.selectedFeatures is null when layer is destroyed and + //one of it's preremovelayer listener calls setLayer + //with another layer on this control + if(layer.selectedFeatures != null) { + while(layer.selectedFeatures.length > numExcept) { + feature = layer.selectedFeatures[numExcept]; + if(!options || options.except != feature) { + this.unselect(feature); + } else { + ++numExcept; + } + } + } + } + }, + + /** + * Method: clickFeature + * Called on click in a feature + * Only responds if this.hover is false. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} + */ + clickFeature: function(feature) { + if(!this.hover) { + var selected = (OpenLayers.Util.indexOf( + feature.layer.selectedFeatures, feature) > -1); + if(selected) { + if(this.toggleSelect()) { + this.unselect(feature); + } else if(!this.multipleSelect()) { + this.unselectAll({except: feature}); + } + } else { + if(!this.multipleSelect()) { + this.unselectAll({except: feature}); + } + this.select(feature); + } + } + }, + + /** + * Method: multipleSelect + * Allow for multiple selected features based on <multiple> property and + * <multipleKey> event modifier. + * + * Returns: + * {Boolean} Allow for multiple selected features. + */ + multipleSelect: function() { + return this.multiple || (this.handlers.feature.evt && + this.handlers.feature.evt[this.multipleKey]); + }, + + /** + * Method: toggleSelect + * Event should toggle the selected state of a feature based on <toggle> + * property and <toggleKey> event modifier. + * + * Returns: + * {Boolean} Toggle the selected state of a feature. + */ + toggleSelect: function() { + return this.toggle || (this.handlers.feature.evt && + this.handlers.feature.evt[this.toggleKey]); + }, + + /** + * Method: clickoutFeature + * Called on click outside a previously clicked (selected) feature. + * Only responds if this.hover is false. + * + * Parameters: + * feature - {<OpenLayers.Vector.Feature>} + */ + clickoutFeature: function(feature) { + if(!this.hover && this.clickout) { + this.unselectAll(); + } + }, + + /** + * Method: overFeature + * Called on over a feature. + * Only responds if this.hover is true. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} + */ + overFeature: function(feature) { + var layer = feature.layer; + if(this.hover) { + if(this.highlightOnly) { + this.highlight(feature); + } else if(OpenLayers.Util.indexOf( + layer.selectedFeatures, feature) == -1) { + this.select(feature); + } + } + }, + + /** + * Method: outFeature + * Called on out of a selected feature. + * Only responds if this.hover is true. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} + */ + outFeature: function(feature) { + if(this.hover) { + if(this.highlightOnly) { + // we do nothing if we're not the last highlighter of the + // feature + if(feature._lastHighlighter == this.id) { + // if another select control had highlighted the feature before + // we did it ourself then we use that control to highlight the + // feature as it was before we highlighted it, else we just + // unhighlight it + if(feature._prevHighlighter && + feature._prevHighlighter != this.id) { + delete feature._lastHighlighter; + var control = this.map.getControl( + feature._prevHighlighter); + if(control) { + control.highlight(feature); + } + } else { + this.unhighlight(feature); + } + } + } else { + this.unselect(feature); + } + } + }, + + /** + * Method: highlight + * Redraw feature with the select style. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} + */ + highlight: function(feature) { + var layer = feature.layer; + var cont = this.events.triggerEvent("beforefeaturehighlighted", { + feature : feature + }); + if(cont !== false) { + feature._prevHighlighter = feature._lastHighlighter; + feature._lastHighlighter = this.id; + var style = this.selectStyle || this.renderIntent; + layer.drawFeature(feature, style); + this.events.triggerEvent("featurehighlighted", {feature : feature}); + } + }, + + /** + * Method: unhighlight + * Redraw feature with the "default" style + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} + */ + unhighlight: function(feature) { + var layer = feature.layer; + // three cases: + // 1. there's no other highlighter, in that case _prev is undefined, + // and we just need to undef _last + // 2. another control highlighted the feature after we did it, in + // that case _last references this other control, and we just + // need to undef _prev + // 3. another control highlighted the feature before we did it, in + // that case _prev references this other control, and we need to + // set _last to _prev and undef _prev + if(feature._prevHighlighter == undefined) { + delete feature._lastHighlighter; + } else if(feature._prevHighlighter == this.id) { + delete feature._prevHighlighter; + } else { + feature._lastHighlighter = feature._prevHighlighter; + delete feature._prevHighlighter; + } + layer.drawFeature(feature, feature.style || feature.layer.style || + "default"); + this.events.triggerEvent("featureunhighlighted", {feature : feature}); + }, + + /** + * Method: select + * Add feature to the layer's selectedFeature array, render the feature as + * selected, and call the onSelect function. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} + */ + select: function(feature) { + var cont = this.onBeforeSelect.call(this.scope, feature); + var layer = feature.layer; + if(cont !== false) { + cont = layer.events.triggerEvent("beforefeatureselected", { + feature: feature + }); + if(cont !== false) { + layer.selectedFeatures.push(feature); + this.highlight(feature); + // if the feature handler isn't involved in the feature + // selection (because the box handler is used or the + // feature is selected programatically) we fake the + // feature handler to allow unselecting on click + if(!this.handlers.feature.lastFeature) { + this.handlers.feature.lastFeature = layer.selectedFeatures[0]; + } + layer.events.triggerEvent("featureselected", {feature: feature}); + this.onSelect.call(this.scope, feature); + } + } + }, + + /** + * Method: unselect + * Remove feature from the layer's selectedFeature array, render the feature as + * normal, and call the onUnselect function. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} + */ + unselect: function(feature) { + var layer = feature.layer; + // Store feature style for restoration later + this.unhighlight(feature); + OpenLayers.Util.removeItem(layer.selectedFeatures, feature); + layer.events.triggerEvent("featureunselected", {feature: feature}); + this.onUnselect.call(this.scope, feature); + }, + + /** + * Method: selectBox + * Callback from the handlers.box set up when <box> selection is true + * on. + * + * Parameters: + * position - {<OpenLayers.Bounds> || <OpenLayers.Pixel> } + */ + selectBox: function(position) { + if (position instanceof OpenLayers.Bounds) { + var minXY = this.map.getLonLatFromPixel({ + x: position.left, + y: position.bottom + }); + var maxXY = this.map.getLonLatFromPixel({ + x: position.right, + y: position.top + }); + var bounds = new OpenLayers.Bounds( + minXY.lon, minXY.lat, maxXY.lon, maxXY.lat + ); + + // if multiple is false, first deselect currently selected features + if (!this.multipleSelect()) { + this.unselectAll(); + } + + // because we're using a box, we consider we want multiple selection + var prevMultiple = this.multiple; + this.multiple = true; + var layers = this.layers || [this.layer]; + this.events.triggerEvent("boxselectionstart", {layers: layers}); + var layer; + for(var l=0; l<layers.length; ++l) { + layer = layers[l]; + for(var i=0, len = layer.features.length; i<len; ++i) { + var feature = layer.features[i]; + // check if the feature is displayed + if (!feature.getVisibility()) { + continue; + } + + if (this.geometryTypes == null || OpenLayers.Util.indexOf( + this.geometryTypes, feature.geometry.CLASS_NAME) > -1) { + if (bounds.toGeometry().intersects(feature.geometry)) { + if (OpenLayers.Util.indexOf(layer.selectedFeatures, feature) == -1) { + this.select(feature); + } + } + } + } + } + this.multiple = prevMultiple; + this.events.triggerEvent("boxselectionend", {layers: layers}); + } + }, + + /** + * Method: setMap + * Set the map property for the control. + * + * Parameters: + * map - {<OpenLayers.Map>} + */ + setMap: function(map) { + this.handlers.feature.setMap(map); + if (this.box) { + this.handlers.box.setMap(map); + } + OpenLayers.Control.prototype.setMap.apply(this, arguments); + }, + + /** + * APIMethod: setLayer + * Attach a new layer to the control, overriding any existing layers. + * + * Parameters: + * layers - Array of {<OpenLayers.Layer.Vector>} or a single + * {<OpenLayers.Layer.Vector>} + */ + setLayer: function(layers) { + var isActive = this.active; + this.unselectAll(); + this.deactivate(); + if(this.layers) { + this.layer.destroy(); + this.layers = null; + } + this.initLayer(layers); + this.handlers.feature.layer = this.layer; + if (isActive) { + this.activate(); + } + }, + + CLASS_NAME: "OpenLayers.Control.SelectFeature" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/Snapping.js b/misc/openlayers/lib/OpenLayers/Control/Snapping.js new file mode 100644 index 0000000..2173114 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/Snapping.js @@ -0,0 +1,560 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Layer/Vector.js + */ + +/** + * Class: OpenLayers.Control.Snapping + * Acts as a snapping agent while editing vector features. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.Snapping = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: events + * {<OpenLayers.Events>} Events instance for listeners and triggering + * control specific events. + * + * Register a listener for a particular event with the following syntax: + * (code) + * control.events.register(type, obj, listener); + * (end) + * + * Supported event types (in addition to those from <OpenLayers.Control.events>): + * beforesnap - Triggered before a snap occurs. Listeners receive an + * event object with *point*, *x*, *y*, *distance*, *layer*, and + * *snapType* properties. The point property will be original point + * geometry considered for snapping. The x and y properties represent + * coordinates the point will receive. The distance is the distance + * of the snap. The layer is the target layer. The snapType property + * will be one of "node", "vertex", or "edge". Return false to stop + * snapping from occurring. + * snap - Triggered when a snap occurs. Listeners receive an event with + * *point*, *snapType*, *layer*, and *distance* properties. The point + * will be the location snapped to. The snapType will be one of "node", + * "vertex", or "edge". The layer will be the target layer. The + * distance will be the distance of the snap in map units. + * unsnap - Triggered when a vertex is unsnapped. Listeners receive an + * event with a *point* property. + */ + + /** + * CONSTANT: DEFAULTS + * Default target properties. + */ + DEFAULTS: { + tolerance: 10, + node: true, + edge: true, + vertex: true + }, + + /** + * Property: greedy + * {Boolean} Snap to closest feature in first layer with an eligible + * feature. Default is true. + */ + greedy: true, + + /** + * Property: precedence + * {Array} List representing precedence of different snapping types. + * Default is "node", "vertex", "edge". + */ + precedence: ["node", "vertex", "edge"], + + /** + * Property: resolution + * {Float} The map resolution for the previously considered snap. + */ + resolution: null, + + /** + * Property: geoToleranceCache + * {Object} A cache of geo-tolerances. Tolerance values (in map units) are + * calculated when the map resolution changes. + */ + geoToleranceCache: null, + + /** + * Property: layer + * {<OpenLayers.Layer.Vector>} The current editable layer. Set at + * construction or after construction with <setLayer>. + */ + layer: null, + + /** + * Property: feature + * {<OpenLayers.Feature.Vector>} The current editable feature. + */ + feature: null, + + /** + * Property: point + * {<OpenLayers.Geometry.Point>} The currently snapped vertex. + */ + point: null, + + /** + * Constructor: OpenLayers.Control.Snapping + * Creates a new snapping control. A control is constructed with an editable + * layer and a set of configuration objects for target layers. While the + * control is active, dragging vertices while drawing new features or + * modifying existing features on the editable layer will engage + * snapping to features on the target layers. Whether a vertex snaps to + * a feature on a target layer depends on the target layer configuration. + * + * Parameters: + * options - {Object} An object containing all configuration properties for + * the control. + * + * Valid options: + * layer - {<OpenLayers.Layer.Vector>} The editable layer. Features from this + * layer that are digitized or modified may have vertices snapped to + * features from any of the target layers. + * targets - {Array(Object | OpenLayers.Layer.Vector)} A list of objects for + * configuring target layers. See valid properties of the target + * objects below. If the items in the targets list are vector layers + * (instead of configuration objects), the defaults from the <defaults> + * property will apply. The editable layer itself may be a target + * layer, allowing newly created or edited features to be snapped to + * existing features from the same layer. If no targets are provided + * the layer given in the constructor (as <layer>) will become the + * initial target. + * defaults - {Object} An object with default properties to be applied + * to all target objects. + * greedy - {Boolean} Snap to closest feature in first target layer that + * applies. Default is true. If false, all features in all target + * layers will be checked and the closest feature in all target layers + * will be chosen. The greedy property determines if the order of the + * target layers is significant. By default, the order of the target + * layers is significant where layers earlier in the target layer list + * have precedence over layers later in the list. Within a single + * layer, the closest feature is always chosen for snapping. This + * property only determines whether the search for a closer feature + * continues after an eligible feature is found in a target layer. + * + * Valid target properties: + * layer - {<OpenLayers.Layer.Vector>} A target layer. Features from this + * layer will be eligible to act as snapping target for the editable + * layer. + * tolerance - {Float} The distance (in pixels) at which snapping may occur. + * Default is 10. + * node - {Boolean} Snap to nodes (first or last point in a geometry) in + * target layer. Default is true. + * nodeTolerance - {Float} Optional distance at which snapping may occur + * for nodes specifically. If none is provided, <tolerance> will be + * used. + * vertex - {Boolean} Snap to vertices in target layer. Default is true. + * vertexTolerance - {Float} Optional distance at which snapping may occur + * for vertices specifically. If none is provided, <tolerance> will be + * used. + * edge - {Boolean} Snap to edges in target layer. Default is true. + * edgeTolerance - {Float} Optional distance at which snapping may occur + * for edges specifically. If none is provided, <tolerance> will be + * used. + * filter - {<OpenLayers.Filter>} Optional filter to evaluate to determine if + * feature is eligible for snapping. If filter evaluates to true for a + * target feature a vertex may be snapped to the feature. + * minResolution - {Number} If a minResolution is provided, snapping to this + * target will only be considered if the map resolution is greater than + * or equal to this value (the minResolution is inclusive). Default is + * no minimum resolution limit. + * maxResolution - {Number} If a maxResolution is provided, snapping to this + * target will only be considered if the map resolution is strictly + * less than this value (the maxResolution is exclusive). Default is + * no maximum resolution limit. + */ + initialize: function(options) { + OpenLayers.Control.prototype.initialize.apply(this, [options]); + this.options = options || {}; // TODO: this could be done by the super + + // set the editable layer if provided + if(this.options.layer) { + this.setLayer(this.options.layer); + } + // configure target layers + var defaults = OpenLayers.Util.extend({}, this.options.defaults); + this.defaults = OpenLayers.Util.applyDefaults(defaults, this.DEFAULTS); + this.setTargets(this.options.targets); + if(this.targets.length === 0 && this.layer) { + this.addTargetLayer(this.layer); + } + + this.geoToleranceCache = {}; + }, + + /** + * APIMethod: setLayer + * Set the editable layer. Call the setLayer method if the editable layer + * changes and the same control should be used on a new editable layer. + * If the control is already active, it will be active after the new + * layer is set. + * + * Parameters: + * layer - {<OpenLayers.Layer.Vector>} The new editable layer. + */ + setLayer: function(layer) { + if(this.active) { + this.deactivate(); + this.layer = layer; + this.activate(); + } else { + this.layer = layer; + } + }, + + /** + * Method: setTargets + * Set the targets for the snapping agent. + * + * Parameters: + * targets - {Array} An array of target configs or target layers. + */ + setTargets: function(targets) { + this.targets = []; + if(targets && targets.length) { + var target; + for(var i=0, len=targets.length; i<len; ++i) { + target = targets[i]; + if(target instanceof OpenLayers.Layer.Vector) { + this.addTargetLayer(target); + } else { + this.addTarget(target); + } + } + } + }, + + /** + * Method: addTargetLayer + * Add a target layer with the default target config. + * + * Parameters: + * layer - {<OpenLayers.Layer.Vector>} A target layer. + */ + addTargetLayer: function(layer) { + this.addTarget({layer: layer}); + }, + + /** + * Method: addTarget + * Add a configured target layer. + * + * Parameters: + * target - {Object} A target config. + */ + addTarget: function(target) { + target = OpenLayers.Util.applyDefaults(target, this.defaults); + target.nodeTolerance = target.nodeTolerance || target.tolerance; + target.vertexTolerance = target.vertexTolerance || target.tolerance; + target.edgeTolerance = target.edgeTolerance || target.tolerance; + this.targets.push(target); + }, + + /** + * Method: removeTargetLayer + * Remove a target layer. + * + * Parameters: + * layer - {<OpenLayers.Layer.Vector>} The target layer to remove. + */ + removeTargetLayer: function(layer) { + var target; + for(var i=this.targets.length-1; i>=0; --i) { + target = this.targets[i]; + if(target.layer === layer) { + this.removeTarget(target); + } + } + }, + + /** + * Method: removeTarget + * Remove a target. + * + * Parameters: + * target - {Object} A target config. + * + * Returns: + * {Array} The targets array. + */ + removeTarget: function(target) { + return OpenLayers.Util.removeItem(this.targets, target); + }, + + /** + * APIMethod: activate + * Activate the control. Activating the control registers listeners for + * editing related events so that during feature creation and + * modification, moving vertices will trigger snapping. + */ + activate: function() { + var activated = OpenLayers.Control.prototype.activate.call(this); + if(activated) { + if(this.layer && this.layer.events) { + this.layer.events.on({ + sketchstarted: this.onSketchModified, + sketchmodified: this.onSketchModified, + vertexmodified: this.onVertexModified, + scope: this + }); + } + } + return activated; + }, + + /** + * APIMethod: deactivate + * Deactivate the control. Deactivating the control unregisters listeners + * so feature editing may proceed without engaging the snapping agent. + */ + deactivate: function() { + var deactivated = OpenLayers.Control.prototype.deactivate.call(this); + if(deactivated) { + if(this.layer && this.layer.events) { + this.layer.events.un({ + sketchstarted: this.onSketchModified, + sketchmodified: this.onSketchModified, + vertexmodified: this.onVertexModified, + scope: this + }); + } + } + this.feature = null; + this.point = null; + return deactivated; + }, + + /** + * Method: onSketchModified + * Registered as a listener for the sketchmodified event on the editable + * layer. + * + * Parameters: + * event - {Object} The sketch modified event. + */ + onSketchModified: function(event) { + this.feature = event.feature; + this.considerSnapping(event.vertex, event.vertex); + }, + + /** + * Method: onVertexModified + * Registered as a listener for the vertexmodified event on the editable + * layer. + * + * Parameters: + * event - {Object} The vertex modified event. + */ + onVertexModified: function(event) { + this.feature = event.feature; + var loc = this.layer.map.getLonLatFromViewPortPx(event.pixel); + this.considerSnapping( + event.vertex, new OpenLayers.Geometry.Point(loc.lon, loc.lat) + ); + }, + + /** + * Method: considerSnapping + * + * Parameters: + * point - {<OpenLayers.Geometry.Point>} The vertex to be snapped (or + * unsnapped). + * loc - {<OpenLayers.Geometry.Point>} The location of the mouse in map + * coords. + */ + considerSnapping: function(point, loc) { + var best = { + rank: Number.POSITIVE_INFINITY, + dist: Number.POSITIVE_INFINITY, + x: null, y: null + }; + var snapped = false; + var result, target; + for(var i=0, len=this.targets.length; i<len; ++i) { + target = this.targets[i]; + result = this.testTarget(target, loc); + if(result) { + if(this.greedy) { + best = result; + best.target = target; + snapped = true; + break; + } else { + if((result.rank < best.rank) || + (result.rank === best.rank && result.dist < best.dist)) { + best = result; + best.target = target; + snapped = true; + } + } + } + } + if(snapped) { + var proceed = this.events.triggerEvent("beforesnap", { + point: point, x: best.x, y: best.y, distance: best.dist, + layer: best.target.layer, snapType: this.precedence[best.rank] + }); + if(proceed !== false) { + point.x = best.x; + point.y = best.y; + this.point = point; + this.events.triggerEvent("snap", { + point: point, + snapType: this.precedence[best.rank], + layer: best.target.layer, + distance: best.dist + }); + } else { + snapped = false; + } + } + if(this.point && !snapped) { + point.x = loc.x; + point.y = loc.y; + this.point = null; + this.events.triggerEvent("unsnap", {point: point}); + } + }, + + /** + * Method: testTarget + * + * Parameters: + * target - {Object} Object with target layer configuration. + * loc - {<OpenLayers.Geometry.Point>} The location of the mouse in map + * coords. + * + * Returns: + * {Object} A result object with rank, dist, x, and y properties. + * Returns null if candidate is not eligible for snapping. + */ + testTarget: function(target, loc) { + var resolution = this.layer.map.getResolution(); + if ("minResolution" in target) { + if (resolution < target.minResolution) { + return null; + } + } + if ("maxResolution" in target) { + if (resolution >= target.maxResolution) { + return null; + } + } + var tolerance = { + node: this.getGeoTolerance(target.nodeTolerance, resolution), + vertex: this.getGeoTolerance(target.vertexTolerance, resolution), + edge: this.getGeoTolerance(target.edgeTolerance, resolution) + }; + // this could be cached if we don't support setting tolerance values directly + var maxTolerance = Math.max( + tolerance.node, tolerance.vertex, tolerance.edge + ); + var result = { + rank: Number.POSITIVE_INFINITY, dist: Number.POSITIVE_INFINITY + }; + var eligible = false; + var features = target.layer.features; + var feature, type, vertices, vertex, closest, dist, found; + var numTypes = this.precedence.length; + var ll = new OpenLayers.LonLat(loc.x, loc.y); + for(var i=0, len=features.length; i<len; ++i) { + feature = features[i]; + if(feature !== this.feature && !feature._sketch && + feature.state !== OpenLayers.State.DELETE && + (!target.filter || target.filter.evaluate(feature))) { + if(feature.atPoint(ll, maxTolerance, maxTolerance)) { + for(var j=0, stop=Math.min(result.rank+1, numTypes); j<stop; ++j) { + type = this.precedence[j]; + if(target[type]) { + if(type === "edge") { + closest = feature.geometry.distanceTo(loc, {details: true}); + dist = closest.distance; + if(dist <= tolerance[type] && dist < result.dist) { + result = { + rank: j, dist: dist, + x: closest.x0, y: closest.y0 // closest coords on feature + }; + eligible = true; + // don't look for lower precedence types for this feature + break; + } + } else { + // look for nodes or vertices + vertices = feature.geometry.getVertices(type === "node"); + found = false; + for(var k=0, klen=vertices.length; k<klen; ++k) { + vertex = vertices[k]; + dist = vertex.distanceTo(loc); + if(dist <= tolerance[type] && + (j < result.rank || (j === result.rank && dist < result.dist))) { + result = { + rank: j, dist: dist, + x: vertex.x, y: vertex.y + }; + eligible = true; + found = true; + } + } + if(found) { + // don't look for lower precedence types for this feature + break; + } + } + } + } + } + } + } + return eligible ? result : null; + }, + + /** + * Method: getGeoTolerance + * Calculate a tolerance in map units given a tolerance in pixels. This + * takes advantage of the <geoToleranceCache> when the map resolution + * has not changed. + * + * Parameters: + * tolerance - {Number} A tolerance value in pixels. + * resolution - {Number} Map resolution. + * + * Returns: + * {Number} A tolerance value in map units. + */ + getGeoTolerance: function(tolerance, resolution) { + if(resolution !== this.resolution) { + this.resolution = resolution; + this.geoToleranceCache = {}; + } + var geoTolerance = this.geoToleranceCache[tolerance]; + if(geoTolerance === undefined) { + geoTolerance = tolerance * resolution; + this.geoToleranceCache[tolerance] = geoTolerance; + } + return geoTolerance; + }, + + /** + * Method: destroy + * Clean up the control. + */ + destroy: function() { + if(this.active) { + this.deactivate(); // TODO: this should be handled by the super + } + delete this.layer; + delete this.targets; + OpenLayers.Control.prototype.destroy.call(this); + }, + + CLASS_NAME: "OpenLayers.Control.Snapping" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/Split.js b/misc/openlayers/lib/OpenLayers/Control/Split.js new file mode 100644 index 0000000..de19eb7 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/Split.js @@ -0,0 +1,494 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Handler/Path.js + * @requires OpenLayers/Layer/Vector.js + */ + +/** + * Class: OpenLayers.Control.Split + * Acts as a split feature agent while editing vector features. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.Split = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: events + * {<OpenLayers.Events>} Events instance for listeners and triggering + * control specific events. + * + * Register a listener for a particular event with the following syntax: + * (code) + * control.events.register(type, obj, listener); + * (end) + * + * Supported event types (in addition to those from <OpenLayers.Control.events>): + * beforesplit - Triggered before a split occurs. Listeners receive an + * event object with *source* and *target* properties. + * split - Triggered when a split occurs. Listeners receive an event with + * an *original* property and a *features* property. The original + * is a reference to the target feature that the sketch or modified + * feature intersects. The features property is a list of all features + * that result from this single split. This event is triggered before + * the resulting features are added to the layer (while the layer still + * has a reference to the original). + * aftersplit - Triggered after all splits resulting from a single sketch + * or feature modification have occurred. The original features + * have been destroyed and features that result from the split + * have already been added to the layer. Listeners receive an event + * with a *source* and *features* property. The source references the + * sketch or modified feature used as a splitter. The features + * property is a list of all resulting features. + */ + + /** + * APIProperty: layer + * {<OpenLayers.Layer.Vector>} The target layer with features to be split. + * Set at construction or after construction with <setLayer>. + */ + layer: null, + + /** + * Property: source + * {<OpenLayers.Layer.Vector>} Optional source layer. Any newly created + * or modified features from this layer will be used to split features + * on the target layer. If not provided, a temporary sketch layer will + * be created. + */ + source: null, + + /** + * Property: sourceOptions + * {Options} If a temporary sketch layer is created, these layer options + * will be applied. + */ + sourceOptions: null, + + /** + * APIProperty: tolerance + * {Number} Distance between the calculated intersection and a vertex on + * the source geometry below which the existing vertex will be used + * for the split. Default is null. + */ + tolerance: null, + + /** + * APIProperty: edge + * {Boolean} Allow splits given intersection of edges only. Default is + * true. If false, a vertex on the source must be within the + * <tolerance> distance of the calculated intersection for a split + * to occur. + */ + edge: true, + + /** + * APIProperty: deferDelete + * {Boolean} Instead of removing features from the layer, set feature + * states of split features to DELETE. This assumes a save strategy + * or other component is in charge of removing features from the + * layer. Default is false. If false, split features will be + * immediately deleted from the layer. + */ + deferDelete: false, + + /** + * APIProperty: mutual + * {Boolean} If source and target layers are the same, split source + * features and target features where they intersect. Default is + * true. If false, only target features will be split. + */ + mutual: true, + + /** + * APIProperty: targetFilter + * {<OpenLayers.Filter>} Optional filter that will be evaluated + * to determine if a feature from the target layer is eligible for + * splitting. + */ + targetFilter: null, + + /** + * APIProperty: sourceFilter + * {<OpenLayers.Filter>} Optional filter that will be evaluated + * to determine if a feature from the source layer is eligible for + * splitting. + */ + sourceFilter: null, + + /** + * Property: handler + * {<OpenLayers.Handler.Path>} The temporary sketch handler created if + * no source layer is provided. + */ + handler: null, + + /** + * Constructor: OpenLayers.Control.Split + * Creates a new split control. A control is constructed with a target + * layer and an optional source layer. While the control is active, + * creating new features or modifying existing features on the source + * layer will result in splitting any eligible features on the target + * layer. If no source layer is provided, a temporary sketch layer will + * be created to create lines for splitting features on the target. + * + * Parameters: + * options - {Object} An object containing all configuration properties for + * the control. + * + * Valid options: + * layer - {<OpenLayers.Layer.Vector>} The target layer. Features from this + * layer will be split by new or modified features on the source layer + * or temporary sketch layer. + * source - {<OpenLayers.Layer.Vector>} Optional source layer. If provided + * newly created features or modified features will be used to split + * features on the target layer. If not provided, a temporary sketch + * layer will be created for drawing lines. + * tolerance - {Number} Optional value for the distance between a source + * vertex and the calculated intersection below which the split will + * occur at the vertex. + * edge - {Boolean} Allow splits given intersection of edges only. Default + * is true. If false, a vertex on the source must be within the + * <tolerance> distance of the calculated intersection for a split + * to occur. + * mutual - {Boolean} If source and target are the same, split source + * features and target features where they intersect. Default is + * true. If false, only target features will be split. + * targetFilter - {<OpenLayers.Filter>} Optional filter that will be evaluated + * to determine if a feature from the target layer is eligible for + * splitting. + * sourceFilter - {<OpenLayers.Filter>} Optional filter that will be evaluated + * to determine if a feature from the target layer is eligible for + * splitting. + */ + initialize: function(options) { + OpenLayers.Control.prototype.initialize.apply(this, [options]); + this.options = options || {}; // TODO: this could be done by the super + + // set the source layer if provided + if(this.options.source) { + this.setSource(this.options.source); + } + }, + + /** + * APIMethod: setSource + * Set the source layer for edits layer. + * + * Parameters: + * layer - {<OpenLayers.Layer.Vector>} The new source layer layer. If + * null, a temporary sketch layer will be created. + */ + setSource: function(layer) { + if(this.active) { + this.deactivate(); + if(this.handler) { + this.handler.destroy(); + delete this.handler; + } + this.source = layer; + this.activate(); + } else { + this.source = layer; + } + }, + + /** + * APIMethod: activate + * Activate the control. Activating the control registers listeners for + * editing related events so that during feature creation and + * modification, features in the target will be considered for + * splitting. + */ + activate: function() { + var activated = OpenLayers.Control.prototype.activate.call(this); + if(activated) { + if(!this.source) { + if(!this.handler) { + this.handler = new OpenLayers.Handler.Path(this, + {done: function(geometry) { + this.onSketchComplete({ + feature: new OpenLayers.Feature.Vector(geometry) + }); + }}, + {layerOptions: this.sourceOptions} + ); + } + this.handler.activate(); + } else if(this.source.events) { + this.source.events.on({ + sketchcomplete: this.onSketchComplete, + afterfeaturemodified: this.afterFeatureModified, + scope: this + }); + } + } + return activated; + }, + + /** + * APIMethod: deactivate + * Deactivate the control. Deactivating the control unregisters listeners + * so feature editing may proceed without engaging the split agent. + */ + deactivate: function() { + var deactivated = OpenLayers.Control.prototype.deactivate.call(this); + if(deactivated) { + if(this.source && this.source.events) { + this.source.events.un({ + sketchcomplete: this.onSketchComplete, + afterfeaturemodified: this.afterFeatureModified, + scope: this + }); + } + } + return deactivated; + }, + + /** + * Method: onSketchComplete + * Registered as a listener for the sketchcomplete event on the editable + * layer. + * + * Parameters: + * event - {Object} The sketch complete event. + * + * Returns: + * {Boolean} Stop the sketch from being added to the layer (it has been + * split). + */ + onSketchComplete: function(event) { + this.feature = null; + return !this.considerSplit(event.feature); + }, + + /** + * Method: afterFeatureModified + * Registered as a listener for the afterfeaturemodified event on the + * editable layer. + * + * Parameters: + * event - {Object} The after feature modified event. + */ + afterFeatureModified: function(event) { + if(event.modified) { + var feature = event.feature; + if (typeof feature.geometry.split === "function") { + this.feature = event.feature; + this.considerSplit(event.feature); + } + } + }, + + /** + * Method: removeByGeometry + * Remove a feature from a list based on the given geometry. + * + * Parameters: + * features - {Array(<OpenLayers.Feature.Vector>)} A list of features. + * geometry - {<OpenLayers.Geometry>} A geometry. + */ + removeByGeometry: function(features, geometry) { + for(var i=0, len=features.length; i<len; ++i) { + if(features[i].geometry === geometry) { + features.splice(i, 1); + break; + } + } + }, + + /** + * Method: isEligible + * Test if a target feature is eligible for splitting. + * + * Parameters: + * target - {<OpenLayers.Feature.Vector>} The target feature. + * + * Returns: + * {Boolean} The target is eligible for splitting. + */ + isEligible: function(target) { + if (!target.geometry) { + return false; + } else { + return ( + target.state !== OpenLayers.State.DELETE + ) && ( + typeof target.geometry.split === "function" + ) && ( + this.feature !== target + ) && ( + !this.targetFilter || + this.targetFilter.evaluate(target.attributes) + ); + } + }, + + /** + * Method: considerSplit + * Decide whether or not to split target features with the supplied + * feature. If <mutual> is true, both the source and target features + * will be split if eligible. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} The newly created or modified + * feature. + * + * Returns: + * {Boolean} The supplied feature was split (and destroyed). + */ + considerSplit: function(feature) { + var sourceSplit = false; + var targetSplit = false; + if(!this.sourceFilter || + this.sourceFilter.evaluate(feature.attributes)) { + var features = this.layer && this.layer.features || []; + var target, results, proceed; + var additions = [], removals = []; + var mutual = (this.layer === this.source) && this.mutual; + var options = { + edge: this.edge, + tolerance: this.tolerance, + mutual: mutual + }; + var sourceParts = [feature.geometry]; + var targetFeature, targetParts; + var source, parts; + for(var i=0, len=features.length; i<len; ++i) { + targetFeature = features[i]; + if(this.isEligible(targetFeature)) { + targetParts = [targetFeature.geometry]; + // work through source geoms - this array may change + for(var j=0; j<sourceParts.length; ++j) { + source = sourceParts[j]; + // work through target parts - this array may change + for(var k=0; k<targetParts.length; ++k) { + target = targetParts[k]; + if(source.getBounds().intersectsBounds(target.getBounds())) { + results = source.split(target, options); + if(results) { + proceed = this.events.triggerEvent( + "beforesplit", {source: feature, target: targetFeature} + ); + if(proceed !== false) { + if(mutual) { + parts = results[0]; + // handle parts that result from source splitting + if(parts.length > 1) { + // splice in new source parts + parts.unshift(j, 1); // add args for splice below + Array.prototype.splice.apply(sourceParts, parts); + j += parts.length - 3; + } + results = results[1]; + } + // handle parts that result from target splitting + if(results.length > 1) { + // splice in new target parts + results.unshift(k, 1); // add args for splice below + Array.prototype.splice.apply(targetParts, results); + k += results.length - 3; + } + } + } + } + } + } + if(targetParts && targetParts.length > 1) { + this.geomsToFeatures(targetFeature, targetParts); + this.events.triggerEvent("split", { + original: targetFeature, + features: targetParts + }); + Array.prototype.push.apply(additions, targetParts); + removals.push(targetFeature); + targetSplit = true; + } + } + } + if(sourceParts && sourceParts.length > 1) { + this.geomsToFeatures(feature, sourceParts); + this.events.triggerEvent("split", { + original: feature, + features: sourceParts + }); + Array.prototype.push.apply(additions, sourceParts); + removals.push(feature); + sourceSplit = true; + } + if(sourceSplit || targetSplit) { + // remove and add feature events are suppressed + // listen for split event on this control instead + if(this.deferDelete) { + // Set state instead of removing. Take care to avoid + // setting delete for features that have not yet been + // inserted - those should be destroyed immediately. + var feat, destroys = []; + for(var i=0, len=removals.length; i<len; ++i) { + feat = removals[i]; + if(feat.state === OpenLayers.State.INSERT) { + destroys.push(feat); + } else { + feat.state = OpenLayers.State.DELETE; + this.layer.drawFeature(feat); + } + } + this.layer.destroyFeatures(destroys, {silent: true}); + for(var i=0, len=additions.length; i<len; ++i) { + additions[i].state = OpenLayers.State.INSERT; + } + } else { + this.layer.destroyFeatures(removals, {silent: true}); + } + this.layer.addFeatures(additions, {silent: true}); + this.events.triggerEvent("aftersplit", { + source: feature, + features: additions + }); + } + } + return sourceSplit; + }, + + /** + * Method: geomsToFeatures + * Create new features given a template feature and a list of geometries. + * The list of geometries is modified in place. The result will be + * a list of new features. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} The feature to be cloned. + * geoms - {Array(<OpenLayers.Geometry>)} List of goemetries. This will + * become a list of new features. + */ + geomsToFeatures: function(feature, geoms) { + var clone = feature.clone(); + delete clone.geometry; + var newFeature; + for(var i=0, len=geoms.length; i<len; ++i) { + // turn results list from geoms to features + newFeature = clone.clone(); + newFeature.geometry = geoms[i]; + newFeature.state = OpenLayers.State.INSERT; + geoms[i] = newFeature; + } + }, + + /** + * Method: destroy + * Clean up the control. + */ + destroy: function() { + if(this.active) { + this.deactivate(); // TODO: this should be handled by the super + } + OpenLayers.Control.prototype.destroy.call(this); + }, + + CLASS_NAME: "OpenLayers.Control.Split" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/TouchNavigation.js b/misc/openlayers/lib/OpenLayers/Control/TouchNavigation.js new file mode 100644 index 0000000..cd5f926 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/TouchNavigation.js @@ -0,0 +1,182 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control/DragPan.js + * @requires OpenLayers/Control/PinchZoom.js + * @requires OpenLayers/Handler/Click.js + */ + +/** + * Class: OpenLayers.Control.TouchNavigation + * The navigation control handles map browsing with touch events (dragging, + * double-tapping, tap with two fingers, and pinch zoom). Create a new + * control with the <OpenLayers.Control.TouchNavigation> constructor. + * + * If you’re only targeting touch enabled devices with your mapping application, + * you can create a map with only a TouchNavigation control. The + * <OpenLayers.Control.Navigation> control is mobile ready by default, but + * you can generate a smaller build of the library by only including this + * touch navigation control if you aren't concerned about mouse interaction. + * + * Inherits: + * - <OpenLayers.Control> + */ +OpenLayers.Control.TouchNavigation = OpenLayers.Class(OpenLayers.Control, { + + /** + * Property: dragPan + * {<OpenLayers.Control.DragPan>} + */ + dragPan: null, + + /** + * APIProperty: dragPanOptions + * {Object} Options passed to the DragPan control. + */ + dragPanOptions: null, + + /** + * Property: pinchZoom + * {<OpenLayers.Control.PinchZoom>} + */ + pinchZoom: null, + + /** + * APIProperty: pinchZoomOptions + * {Object} Options passed to the PinchZoom control. + */ + pinchZoomOptions: null, + + /** + * APIProperty: clickHandlerOptions + * {Object} Options passed to the Click handler. + */ + clickHandlerOptions: null, + + /** + * APIProperty: documentDrag + * {Boolean} Allow panning of the map by dragging outside map viewport. + * Default is false. + */ + documentDrag: false, + + /** + * APIProperty: autoActivate + * {Boolean} Activate the control when it is added to a map. Default is + * true. + */ + autoActivate: true, + + /** + * Constructor: OpenLayers.Control.TouchNavigation + * Create a new navigation control + * + * Parameters: + * options - {Object} An optional object whose properties will be set on + * the control + */ + initialize: function(options) { + this.handlers = {}; + OpenLayers.Control.prototype.initialize.apply(this, arguments); + }, + + /** + * Method: destroy + * The destroy method is used to perform any clean up before the control + * is dereferenced. Typically this is where event listeners are removed + * to prevent memory leaks. + */ + destroy: function() { + this.deactivate(); + if(this.dragPan) { + this.dragPan.destroy(); + } + this.dragPan = null; + if (this.pinchZoom) { + this.pinchZoom.destroy(); + delete this.pinchZoom; + } + OpenLayers.Control.prototype.destroy.apply(this,arguments); + }, + + /** + * Method: activate + */ + activate: function() { + if(OpenLayers.Control.prototype.activate.apply(this,arguments)) { + this.dragPan.activate(); + this.handlers.click.activate(); + this.pinchZoom.activate(); + return true; + } + return false; + }, + + /** + * Method: deactivate + */ + deactivate: function() { + if(OpenLayers.Control.prototype.deactivate.apply(this,arguments)) { + this.dragPan.deactivate(); + this.handlers.click.deactivate(); + this.pinchZoom.deactivate(); + return true; + } + return false; + }, + + /** + * Method: draw + */ + draw: function() { + var clickCallbacks = { + click: this.defaultClick, + dblclick: this.defaultDblClick + }; + var clickOptions = OpenLayers.Util.extend({ + "double": true, + stopDouble: true, + pixelTolerance: 2 + }, this.clickHandlerOptions); + this.handlers.click = new OpenLayers.Handler.Click( + this, clickCallbacks, clickOptions + ); + this.dragPan = new OpenLayers.Control.DragPan( + OpenLayers.Util.extend({ + map: this.map, + documentDrag: this.documentDrag + }, this.dragPanOptions) + ); + this.dragPan.draw(); + this.pinchZoom = new OpenLayers.Control.PinchZoom( + OpenLayers.Util.extend({map: this.map}, this.pinchZoomOptions) + ); + }, + + /** + * Method: defaultClick + * + * Parameters: + * evt - {Event} + */ + defaultClick: function (evt) { + if(evt.lastTouches && evt.lastTouches.length == 2) { + this.map.zoomOut(); + } + }, + + /** + * Method: defaultDblClick + * + * Parameters: + * evt - {Event} + */ + defaultDblClick: function (evt) { + this.map.zoomTo(this.map.zoom + 1, evt.xy); + }, + + CLASS_NAME: "OpenLayers.Control.TouchNavigation" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/TransformFeature.js b/misc/openlayers/lib/OpenLayers/Control/TransformFeature.js new file mode 100644 index 0000000..8c21456 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/TransformFeature.js @@ -0,0 +1,624 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Control/DragFeature.js + * @requires OpenLayers/Feature/Vector.js + * @requires OpenLayers/Geometry/LineString.js + * @requires OpenLayers/Geometry/Point.js + */ + +/** + * Class: OpenLayers.Control.TransformFeature + * Control to transform features with a standard transformation box. + * + * Inherits From: + * - <OpenLayers.Control> + */ +OpenLayers.Control.TransformFeature = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: events + * {<OpenLayers.Events>} Events instance for listeners and triggering + * control specific events. + * + * Register a listener for a particular event with the following syntax: + * (code) + * control.events.register(type, obj, listener); + * (end) + * + * Supported event types (in addition to those from <OpenLayers.Control.events>): + * beforesetfeature - Triggered before a feature is set for + * tranformation. The feature will not be set if a listener returns + * false. Listeners receive a *feature* property, with the feature + * that will be set for transformation. Listeners are allowed to + * set the control's *scale*, *ratio* and *rotation* properties, + * which will set the initial scale, ratio and rotation of the + * feature, like the <setFeature> method's initialParams argument. + * setfeature - Triggered when a feature is set for tranformation. + * Listeners receive a *feature* property, with the feature that + * is now set for transformation. + * beforetransform - Triggered while dragging, before a feature is + * transformed. The feature will not be transformed if a listener + * returns false (but the box still will). Listeners receive one or + * more of *center*, *scale*, *ratio* and *rotation*. The *center* + * property is an <OpenLayers.Geometry.Point> object with the new + * center of the transformed feature, the others are Floats with the + * scale, ratio or rotation change since the last transformation. + * transform - Triggered while dragging, when a feature is transformed. + * Listeners receive an event object with one or more of *center*, + * scale*, *ratio* and *rotation*. The *center* property is an + * <OpenLayers.Geometry.Point> object with the new center of the + * transformed feature, the others are Floats with the scale, ratio + * or rotation change of the feature since the last transformation. + * transformcomplete - Triggered after dragging. Listeners receive + * an event object with the transformed *feature*. + */ + + /** + * APIProperty: geometryTypes + * {Array(String)} To restrict transformation to a limited set of geometry + * types, send a list of strings corresponding to the geometry class + * names. + */ + geometryTypes: null, + + /** + * Property: layer + * {<OpenLayers.Layer.Vector>} + */ + layer: null, + + /** + * APIProperty: preserveAspectRatio + * {Boolean} set to true to not change the feature's aspect ratio. + */ + preserveAspectRatio: false, + + /** + * APIProperty: rotate + * {Boolean} set to false if rotation should be disabled. Default is true. + * To be passed with the constructor or set when the control is not + * active. + */ + rotate: true, + + /** + * APIProperty: feature + * {<OpenLayers.Feature.Vector>} Feature currently available for + * transformation. Read-only, use <setFeature> to set it manually. + */ + feature: null, + + /** + * APIProperty: renderIntent + * {String|Object} Render intent for the transformation box and + * handles. A symbolizer object can also be provided here. + */ + renderIntent: "temporary", + + /** + * APIProperty: rotationHandleSymbolizer + * {Object|String} Optional. A custom symbolizer for the rotation handles. + * A render intent can also be provided here. Defaults to + * (code) + * { + * stroke: false, + * pointRadius: 10, + * fillOpacity: 0, + * cursor: "pointer" + * } + * (end) + */ + rotationHandleSymbolizer: null, + + /** + * APIProperty: box + * {<OpenLayers.Feature.Vector>} The transformation box rectangle. + * Read-only. + */ + box: null, + + /** + * APIProperty: center + * {<OpenLayers.Geometry.Point>} The center of the feature bounds. + * Read-only. + */ + center: null, + + /** + * APIProperty: scale + * {Float} The scale of the feature, relative to the scale the time the + * feature was set. Read-only, except for *beforesetfeature* + * listeners. + */ + scale: 1, + + /** + * APIProperty: ratio + * {Float} The ratio of the feature relative to the ratio the time the + * feature was set. Read-only, except for *beforesetfeature* + * listeners. + */ + ratio: 1, + + /** + * Property: rotation + * {Integer} the current rotation angle of the box. Read-only, except for + * *beforesetfeature* listeners. + */ + rotation: 0, + + /** + * APIProperty: handles + * {Array(<OpenLayers.Feature.Vector>)} The 8 handles currently available + * for scaling/resizing. Numbered counterclockwise, starting from the + * southwest corner. Read-only. + */ + handles: null, + + /** + * APIProperty: rotationHandles + * {Array(<OpenLayers.Feature.Vector>)} The 4 rotation handles currently + * available for rotating. Numbered counterclockwise, starting from + * the southwest corner. Read-only. + */ + rotationHandles: null, + + /** + * Property: dragControl + * {<OpenLayers.Control.DragFeature>} + */ + dragControl: null, + + /** + * APIProperty: irregular + * {Boolean} Make scaling/resizing work irregularly. If true then + * dragging a handle causes the feature to resize in the direction + * of movement. If false then the feature resizes symetrically + * about it's center. + */ + irregular: false, + + /** + * Constructor: OpenLayers.Control.TransformFeature + * Create a new transform feature control. + * + * Parameters: + * layer - {<OpenLayers.Layer.Vector>} Layer that contains features that + * will be transformed. + * options - {Object} Optional object whose properties will be set on the + * control. + */ + initialize: function(layer, options) { + OpenLayers.Control.prototype.initialize.apply(this, [options]); + + this.layer = layer; + + if(!this.rotationHandleSymbolizer) { + this.rotationHandleSymbolizer = { + stroke: false, + pointRadius: 10, + fillOpacity: 0, + cursor: "pointer" + }; + } + + this.createBox(); + this.createControl(); + }, + + /** + * APIMethod: activate + * Activates the control. + */ + activate: function() { + var activated = false; + if(OpenLayers.Control.prototype.activate.apply(this, arguments)) { + this.dragControl.activate(); + this.layer.addFeatures([this.box]); + this.rotate && this.layer.addFeatures(this.rotationHandles); + this.layer.addFeatures(this.handles); + activated = true; + } + return activated; + }, + + /** + * APIMethod: deactivate + * Deactivates the control. + */ + deactivate: function() { + var deactivated = false; + if(OpenLayers.Control.prototype.deactivate.apply(this, arguments)) { + this.layer.removeFeatures(this.handles); + this.rotate && this.layer.removeFeatures(this.rotationHandles); + this.layer.removeFeatures([this.box]); + this.dragControl.deactivate(); + deactivated = true; + } + return deactivated; + }, + + /** + * Method: setMap + * + * Parameters: + * map - {<OpenLayers.Map>} + */ + setMap: function(map) { + this.dragControl.setMap(map); + OpenLayers.Control.prototype.setMap.apply(this, arguments); + }, + + /** + * APIMethod: setFeature + * Place the transformation box on a feature and start transforming it. + * If the control is not active, it will be activated. + * + * Parameters: + * feature - {<OpenLayers.Feature.Vector>} + * initialParams - {Object} Initial values for rotation, scale or ratio. + * Setting a rotation value here will cause the transformation box to + * start rotated. Setting a scale or ratio will not affect the + * transormation box, but applications may use this to keep track of + * scale and ratio of a feature across multiple transforms. + */ + setFeature: function(feature, initialParams) { + initialParams = OpenLayers.Util.applyDefaults(initialParams, { + rotation: 0, + scale: 1, + ratio: 1 + }); + + var oldRotation = this.rotation; + var oldCenter = this.center; + OpenLayers.Util.extend(this, initialParams); + + var cont = this.events.triggerEvent("beforesetfeature", + {feature: feature} + ); + if (cont === false) { + return; + } + + this.feature = feature; + this.activate(); + + this._setfeature = true; + + var featureBounds = this.feature.geometry.getBounds(); + this.box.move(featureBounds.getCenterLonLat()); + this.box.geometry.rotate(-oldRotation, oldCenter); + this._angle = 0; + + var ll; + if(this.rotation) { + var geom = feature.geometry.clone(); + geom.rotate(-this.rotation, this.center); + var box = new OpenLayers.Feature.Vector( + geom.getBounds().toGeometry()); + box.geometry.rotate(this.rotation, this.center); + this.box.geometry.rotate(this.rotation, this.center); + this.box.move(box.geometry.getBounds().getCenterLonLat()); + var llGeom = box.geometry.components[0].components[0]; + ll = llGeom.getBounds().getCenterLonLat(); + } else { + ll = new OpenLayers.LonLat(featureBounds.left, featureBounds.bottom); + } + this.handles[0].move(ll); + + delete this._setfeature; + + this.events.triggerEvent("setfeature", {feature: feature}); + }, + + /** + * APIMethod: unsetFeature + * Remove the transformation box off any feature. + * If the control is active, it will be deactivated first. + */ + unsetFeature: function() { + if (this.active) { + this.deactivate(); + } else { + this.feature = null; + this.rotation = 0; + this.scale = 1; + this.ratio = 1; + } + }, + + /** + * Method: createBox + * Creates the box with all handles and transformation handles. + */ + createBox: function() { + var control = this; + + this.center = new OpenLayers.Geometry.Point(0, 0); + this.box = new OpenLayers.Feature.Vector( + new OpenLayers.Geometry.LineString([ + new OpenLayers.Geometry.Point(-1, -1), + new OpenLayers.Geometry.Point(0, -1), + new OpenLayers.Geometry.Point(1, -1), + new OpenLayers.Geometry.Point(1, 0), + new OpenLayers.Geometry.Point(1, 1), + new OpenLayers.Geometry.Point(0, 1), + new OpenLayers.Geometry.Point(-1, 1), + new OpenLayers.Geometry.Point(-1, 0), + new OpenLayers.Geometry.Point(-1, -1) + ]), null, + typeof this.renderIntent == "string" ? null : this.renderIntent + ); + + // Override for box move - make sure that the center gets updated + this.box.geometry.move = function(x, y) { + control._moving = true; + OpenLayers.Geometry.LineString.prototype.move.apply(this, arguments); + control.center.move(x, y); + delete control._moving; + }; + + // Overrides for vertex move, resize and rotate - make sure that + // handle and rotationHandle geometries are also moved, resized and + // rotated. + var vertexMoveFn = function(x, y) { + OpenLayers.Geometry.Point.prototype.move.apply(this, arguments); + this._rotationHandle && this._rotationHandle.geometry.move(x, y); + this._handle.geometry.move(x, y); + }; + var vertexResizeFn = function(scale, center, ratio) { + OpenLayers.Geometry.Point.prototype.resize.apply(this, arguments); + this._rotationHandle && this._rotationHandle.geometry.resize( + scale, center, ratio); + this._handle.geometry.resize(scale, center, ratio); + }; + var vertexRotateFn = function(angle, center) { + OpenLayers.Geometry.Point.prototype.rotate.apply(this, arguments); + this._rotationHandle && this._rotationHandle.geometry.rotate( + angle, center); + this._handle.geometry.rotate(angle, center); + }; + + // Override for handle move - make sure that the box and other handles + // are updated, and finally transform the feature. + var handleMoveFn = function(x, y) { + var oldX = this.x, oldY = this.y; + OpenLayers.Geometry.Point.prototype.move.call(this, x, y); + if(control._moving) { + return; + } + var evt = control.dragControl.handlers.drag.evt; + var preserveAspectRatio = !control._setfeature && + control.preserveAspectRatio; + var reshape = !preserveAspectRatio && !(evt && evt.shiftKey); + var oldGeom = new OpenLayers.Geometry.Point(oldX, oldY); + var centerGeometry = control.center; + this.rotate(-control.rotation, centerGeometry); + oldGeom.rotate(-control.rotation, centerGeometry); + var dx1 = this.x - centerGeometry.x; + var dy1 = this.y - centerGeometry.y; + var dx0 = dx1 - (this.x - oldGeom.x); + var dy0 = dy1 - (this.y - oldGeom.y); + if (control.irregular && !control._setfeature) { + dx1 -= (this.x - oldGeom.x) / 2; + dy1 -= (this.y - oldGeom.y) / 2; + } + this.x = oldX; + this.y = oldY; + var scale, ratio = 1; + if (reshape) { + scale = Math.abs(dy0) < 0.00001 ? 1 : dy1 / dy0; + ratio = (Math.abs(dx0) < 0.00001 ? 1 : (dx1 / dx0)) / scale; + } else { + var l0 = Math.sqrt((dx0 * dx0) + (dy0 * dy0)); + var l1 = Math.sqrt((dx1 * dx1) + (dy1 * dy1)); + scale = l1 / l0; + } + + // rotate the box to 0 before resizing - saves us some + // calculations and is inexpensive because we don't drawFeature. + control._moving = true; + control.box.geometry.rotate(-control.rotation, centerGeometry); + delete control._moving; + + control.box.geometry.resize(scale, centerGeometry, ratio); + control.box.geometry.rotate(control.rotation, centerGeometry); + control.transformFeature({scale: scale, ratio: ratio}); + if (control.irregular && !control._setfeature) { + var newCenter = centerGeometry.clone(); + newCenter.x += Math.abs(oldX - centerGeometry.x) < 0.00001 ? 0 : (this.x - oldX); + newCenter.y += Math.abs(oldY - centerGeometry.y) < 0.00001 ? 0 : (this.y - oldY); + control.box.geometry.move(this.x - oldX, this.y - oldY); + control.transformFeature({center: newCenter}); + } + }; + + // Override for rotation handle move - make sure that the box and + // other handles are updated, and finally transform the feature. + var rotationHandleMoveFn = function(x, y){ + var oldX = this.x, oldY = this.y; + OpenLayers.Geometry.Point.prototype.move.call(this, x, y); + if(control._moving) { + return; + } + var evt = control.dragControl.handlers.drag.evt; + var constrain = (evt && evt.shiftKey) ? 45 : 1; + var centerGeometry = control.center; + var dx1 = this.x - centerGeometry.x; + var dy1 = this.y - centerGeometry.y; + var dx0 = dx1 - x; + var dy0 = dy1 - y; + this.x = oldX; + this.y = oldY; + var a0 = Math.atan2(dy0, dx0); + var a1 = Math.atan2(dy1, dx1); + var angle = a1 - a0; + angle *= 180 / Math.PI; + control._angle = (control._angle + angle) % 360; + var diff = control.rotation % constrain; + if(Math.abs(control._angle) >= constrain || diff !== 0) { + angle = Math.round(control._angle / constrain) * constrain - + diff; + control._angle = 0; + control.box.geometry.rotate(angle, centerGeometry); + control.transformFeature({rotation: angle}); + } + }; + + var handles = new Array(8); + var rotationHandles = new Array(4); + var geom, handle, rotationHandle; + var positions = ["sw", "s", "se", "e", "ne", "n", "nw", "w"]; + for(var i=0; i<8; ++i) { + geom = this.box.geometry.components[i]; + handle = new OpenLayers.Feature.Vector(geom.clone(), { + role: positions[i] + "-resize" + }, typeof this.renderIntent == "string" ? null : + this.renderIntent); + if(i % 2 == 0) { + rotationHandle = new OpenLayers.Feature.Vector(geom.clone(), { + role: positions[i] + "-rotate" + }, typeof this.rotationHandleSymbolizer == "string" ? + null : this.rotationHandleSymbolizer); + rotationHandle.geometry.move = rotationHandleMoveFn; + geom._rotationHandle = rotationHandle; + rotationHandles[i/2] = rotationHandle; + } + geom.move = vertexMoveFn; + geom.resize = vertexResizeFn; + geom.rotate = vertexRotateFn; + handle.geometry.move = handleMoveFn; + geom._handle = handle; + handles[i] = handle; + } + + this.rotationHandles = rotationHandles; + this.handles = handles; + }, + + /** + * Method: createControl + * Creates a DragFeature control for this control. + */ + createControl: function() { + var control = this; + this.dragControl = new OpenLayers.Control.DragFeature(this.layer, { + documentDrag: true, + // avoid moving the feature itself - move the box instead + moveFeature: function(pixel) { + if(this.feature === control.feature) { + this.feature = control.box; + } + OpenLayers.Control.DragFeature.prototype.moveFeature.apply(this, + arguments); + }, + // transform while dragging + onDrag: function(feature, pixel) { + if(feature === control.box) { + control.transformFeature({center: control.center}); + } + }, + // set a new feature + onStart: function(feature, pixel) { + var eligible = !control.geometryTypes || + OpenLayers.Util.indexOf(control.geometryTypes, + feature.geometry.CLASS_NAME) !== -1; + var i = OpenLayers.Util.indexOf(control.handles, feature); + i += OpenLayers.Util.indexOf(control.rotationHandles, + feature); + if(feature !== control.feature && feature !== control.box && + i == -2 && eligible) { + control.setFeature(feature); + } + }, + onComplete: function(feature, pixel) { + control.events.triggerEvent("transformcomplete", + {feature: control.feature}); + } + }); + }, + + /** + * Method: drawHandles + * Draws the handles to match the box. + */ + drawHandles: function() { + var layer = this.layer; + for(var i=0; i<8; ++i) { + if(this.rotate && i % 2 === 0) { + layer.drawFeature(this.rotationHandles[i/2], + this.rotationHandleSymbolizer); + } + layer.drawFeature(this.handles[i], this.renderIntent); + } + }, + + /** + * Method: transformFeature + * Transforms the feature. + * + * Parameters: + * mods - {Object} An object with optional scale, ratio, rotation and + * center properties. + */ + transformFeature: function(mods) { + if(!this._setfeature) { + this.scale *= (mods.scale || 1); + this.ratio *= (mods.ratio || 1); + var oldRotation = this.rotation; + this.rotation = (this.rotation + (mods.rotation || 0)) % 360; + + if(this.events.triggerEvent("beforetransform", mods) !== false) { + var feature = this.feature; + var geom = feature.geometry; + var center = this.center; + geom.rotate(-oldRotation, center); + if(mods.scale || mods.ratio) { + geom.resize(mods.scale, center, mods.ratio); + } else if(mods.center) { + feature.move(mods.center.getBounds().getCenterLonLat()); + } + geom.rotate(this.rotation, center); + this.layer.drawFeature(feature); + feature.toState(OpenLayers.State.UPDATE); + this.events.triggerEvent("transform", mods); + } + } + this.layer.drawFeature(this.box, this.renderIntent); + this.drawHandles(); + }, + + /** + * APIMethod: destroy + * Take care of things that are not handled in superclass. + */ + destroy: function() { + var geom; + for(var i=0; i<8; ++i) { + geom = this.box.geometry.components[i]; + geom._handle.destroy(); + geom._handle = null; + geom._rotationHandle && geom._rotationHandle.destroy(); + geom._rotationHandle = null; + } + this.center = null; + this.feature = null; + this.handles = null; + this.rotationHandleSymbolizer = null; + this.rotationHandles = null; + this.box.destroy(); + this.box = null; + this.layer = null; + this.dragControl.destroy(); + this.dragControl = null; + OpenLayers.Control.prototype.destroy.apply(this, arguments); + }, + + CLASS_NAME: "OpenLayers.Control.TransformFeature" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/UTFGrid.js b/misc/openlayers/lib/OpenLayers/Control/UTFGrid.js new file mode 100644 index 0000000..7993201 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/UTFGrid.js @@ -0,0 +1,240 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Handler/Hover.js + * @requires OpenLayers/Handler/Click.js + */ + +/** + * Class: OpenLayers.Control.UTFGrid + * + * This Control provides behavior associated with UTFGrid Layers. + * These 'hit grids' provide underlying feature attributes without + * calling the server (again). This control allows Mousemove, Hovering + * and Click events to trigger callbacks that use the attributes in + * whatever way you need. + * + * The most common example may be a UTFGrid layer containing feature + * attributes that are displayed in a div as you mouseover. + * + * Example Code: + * + * (start code) + * var world_utfgrid = new OpenLayers.Layer.UTFGrid( + * 'UTFGrid Layer', + * "http://tiles/world_utfgrid/${z}/${x}/${y}.json" + * ); + * map.addLayer(world_utfgrid); + * + * var control = new OpenLayers.Control.UTFGrid({ + * layers: [world_utfgrid], + * handlerMode: 'move', + * callback: function(infoLookup) { + * // do something with returned data + * + * } + * }) + * (end code) + * + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.UTFGrid = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: autoActivate + * {Boolean} Activate the control when it is added to a map. Default is + * true. + */ + autoActivate: true, + + /** + * APIProperty: Layers + * List of layers to consider. Must be Layer.UTFGrids + * `null` is the default indicating all UTFGrid Layers are queried. + * {Array} <OpenLayers.Layer.UTFGrid> + */ + layers: null, + + /* Property: defaultHandlerOptions + * The default opts passed to the handler constructors + */ + defaultHandlerOptions: { + 'delay': 300, + 'pixelTolerance': 4, + 'stopMove': false, + 'single': true, + 'double': false, + 'stopSingle': false, + 'stopDouble': false + }, + + /* APIProperty: handlerMode + * Defaults to 'click'. Can be 'hover' or 'move'. + */ + handlerMode: 'click', + + /** + * APIMethod: setHandler + * sets this.handlerMode and calls resetHandler() + * + * Parameters: + * hm - {String} Handler Mode string; 'click', 'hover' or 'move'. + */ + setHandler: function(hm) { + this.handlerMode = hm; + this.resetHandler(); + }, + + /** + * Method: resetHandler + * Deactivates the old hanlder and creates a new + * <OpenLayers.Handler> based on the mode specified in + * this.handlerMode + * + */ + resetHandler: function() { + if (this.handler) { + this.handler.deactivate(); + this.handler.destroy(); + this.handler = null; + } + + if (this.handlerMode == 'hover') { + // Handle this event on hover + this.handler = new OpenLayers.Handler.Hover( + this, + {'pause': this.handleEvent, 'move': this.reset}, + this.handlerOptions + ); + } else if (this.handlerMode == 'click') { + // Handle this event on click + this.handler = new OpenLayers.Handler.Click( + this, { + 'click': this.handleEvent + }, this.handlerOptions + ); + } else if (this.handlerMode == 'move') { + this.handler = new OpenLayers.Handler.Hover( + this, + // Handle this event while hovering OR moving + {'pause': this.handleEvent, 'move': this.handleEvent}, + this.handlerOptions + ); + } + if (this.handler) { + return true; + } else { + return false; + } + }, + + /** + * Constructor: <OpenLayers.Control.UTFGrid> + * + * Parameters: + * options - {Object} + */ + initialize: function(options) { + options = options || {}; + options.handlerOptions = options.handlerOptions || this.defaultHandlerOptions; + OpenLayers.Control.prototype.initialize.apply(this, [options]); + this.resetHandler(); + }, + + /** + * Method: handleEvent + * Internal method called when specified event is triggered. + * + * This method does several things: + * + * Gets the lonLat of the event. + * + * Loops through the appropriate hit grid layers and gathers the attributes. + * + * Passes the attributes to the callback + * + * Parameters: + * evt - {<OpenLayers.Event>} + */ + handleEvent: function(evt) { + if (evt == null) { + this.reset(); + return; + } + + var lonLat = this.map.getLonLatFromPixel(evt.xy); + if (!lonLat) { + return; + } + + var layers = this.findLayers(); + if (layers.length > 0) { + var infoLookup = {}; + var layer, idx; + for (var i=0, len=layers.length; i<len; i++) { + layer = layers[i]; + idx = OpenLayers.Util.indexOf(this.map.layers, layer); + infoLookup[idx] = layer.getFeatureInfo(lonLat); + } + this.callback(infoLookup, lonLat, evt.xy); + } + }, + + /** + * APIMethod: callback + * Function to be called when a mouse event corresponds with a location that + * includes data in one of the configured UTFGrid layers. + * + * Parameters: + * infoLookup - {Object} Keys of this object are layer indexes and can be + * used to resolve a layer in the map.layers array. The structure of + * the property values depend on the data included in the underlying + * UTFGrid and may be any valid JSON type. + */ + callback: function(infoLookup) { + // to be provided in the constructor + }, + + /** + * Method: reset + * Calls the callback with null. + */ + reset: function(evt) { + this.callback(null); + }, + + /** + * Method: findLayers + * Internal method to get the layers, independent of whether we are + * inspecting the map or using a client-provided array + * + * The default value of this.layers is null; this causes the + * findLayers method to return ALL UTFGrid layers encountered. + * + * Parameters: + * None + * + * Returns: + * {Array} Layers to handle on each event + */ + findLayers: function() { + var candidates = this.layers || this.map.layers; + var layers = []; + var layer; + for (var i=candidates.length-1; i>=0; --i) { + layer = candidates[i]; + if (layer instanceof OpenLayers.Layer.UTFGrid ) { + layers.push(layer); + } + } + return layers; + }, + + CLASS_NAME: "OpenLayers.Control.UTFGrid" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/WMSGetFeatureInfo.js b/misc/openlayers/lib/OpenLayers/Control/WMSGetFeatureInfo.js new file mode 100644 index 0000000..c9242f6 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/WMSGetFeatureInfo.js @@ -0,0 +1,532 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Handler/Click.js + * @requires OpenLayers/Handler/Hover.js + * @requires OpenLayers/Request.js + * @requires OpenLayers/Format/WMSGetFeatureInfo.js + */ + +/** + * Class: OpenLayers.Control.WMSGetFeatureInfo + * The WMSGetFeatureInfo control uses a WMS query to get information about a point on the map. The + * information may be in a display-friendly format such as HTML, or a machine-friendly format such + * as GML, depending on the server's capabilities and the client's configuration. This control + * handles click or hover events, attempts to parse the results using an OpenLayers.Format, and + * fires a 'getfeatureinfo' event with the click position, the raw body of the response, and an + * array of features if it successfully read the response. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.WMSGetFeatureInfo = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: hover + * {Boolean} Send GetFeatureInfo requests when mouse stops moving. + * Default is false. + */ + hover: false, + + /** + * APIProperty: drillDown + * {Boolean} Drill down over all WMS layers in the map. When + * using drillDown mode, hover is not possible, and an infoFormat that + * returns parseable features is required. Default is false. + */ + drillDown: false, + + /** + * APIProperty: maxFeatures + * {Integer} Maximum number of features to return from a WMS query. This + * sets the feature_count parameter on WMS GetFeatureInfo + * requests. + */ + maxFeatures: 10, + + /** + * APIProperty: clickCallback + * {String} The click callback to register in the + * {<OpenLayers.Handler.Click>} object created when the hover + * option is set to false. Default is "click". + */ + clickCallback: "click", + + /** + * APIProperty: output + * {String} Either "features" or "object". When triggering a getfeatureinfo + * request should we pass on an array of features or an object with with + * a "features" property and other properties (such as the url of the + * WMS). Default is "features". + */ + output: "features", + + /** + * APIProperty: layers + * {Array(<OpenLayers.Layer.WMS>)} The layers to query for feature info. + * If omitted, all map WMS layers with a url that matches this <url> or + * <layerUrls> will be considered. + */ + layers: null, + + /** + * APIProperty: queryVisible + * {Boolean} If true, filter out hidden layers when searching the map for + * layers to query. Default is false. + */ + queryVisible: false, + + /** + * APIProperty: url + * {String} The URL of the WMS service to use. If not provided, the url + * of the first eligible layer will be used. + */ + url: null, + + /** + * APIProperty: layerUrls + * {Array(String)} Optional list of urls for layers that should be queried. + * This can be used when the layer url differs from the url used for + * making GetFeatureInfo requests (in the case of a layer using cached + * tiles). + */ + layerUrls: null, + + /** + * APIProperty: infoFormat + * {String} The mimetype to request from the server. If you are using + * drillDown mode and have multiple servers that do not share a common + * infoFormat, you can override the control's infoFormat by providing an + * INFO_FORMAT parameter in your <OpenLayers.Layer.WMS> instance(s). + */ + infoFormat: 'text/html', + + /** + * APIProperty: vendorParams + * {Object} Additional parameters that will be added to the request, for + * WMS implementations that support them. This could e.g. look like + * (start code) + * { + * radius: 5 + * } + * (end) + */ + vendorParams: {}, + + /** + * APIProperty: format + * {<OpenLayers.Format>} A format for parsing GetFeatureInfo responses. + * Default is <OpenLayers.Format.WMSGetFeatureInfo>. + */ + format: null, + + /** + * APIProperty: formatOptions + * {Object} Optional properties to set on the format (if one is not provided + * in the <format> property. + */ + formatOptions: null, + + /** + * APIProperty: handlerOptions + * {Object} Additional options for the handlers used by this control, e.g. + * (start code) + * { + * "click": {delay: 100}, + * "hover": {delay: 300} + * } + * (end) + */ + + /** + * Property: handler + * {Object} Reference to the <OpenLayers.Handler> for this control + */ + handler: null, + + /** + * Property: hoverRequest + * {<OpenLayers.Request>} contains the currently running hover request + * (if any). + */ + hoverRequest: null, + + /** + * APIProperty: events + * {<OpenLayers.Events>} Events instance for listeners and triggering + * control specific events. + * + * Register a listener for a particular event with the following syntax: + * (code) + * control.events.register(type, obj, listener); + * (end) + * + * Supported event types (in addition to those from <OpenLayers.Control.events>): + * beforegetfeatureinfo - Triggered before the request is sent. + * The event object has an *xy* property with the position of the + * mouse click or hover event that triggers the request. + * nogetfeatureinfo - no queryable layers were found. + * getfeatureinfo - Triggered when a GetFeatureInfo response is received. + * The event object has a *text* property with the body of the + * response (String), a *features* property with an array of the + * parsed features, an *xy* property with the position of the mouse + * click or hover event that triggered the request, and a *request* + * property with the request itself. If drillDown is set to true and + * multiple requests were issued to collect feature info from all + * layers, *text* and *request* will only contain the response body + * and request object of the last request. + */ + + /** + * Constructor: <OpenLayers.Control.WMSGetFeatureInfo> + * + * Parameters: + * options - {Object} + */ + initialize: function(options) { + options = options || {}; + options.handlerOptions = options.handlerOptions || {}; + + OpenLayers.Control.prototype.initialize.apply(this, [options]); + + if(!this.format) { + this.format = new OpenLayers.Format.WMSGetFeatureInfo( + options.formatOptions + ); + } + + if(this.drillDown === true) { + this.hover = false; + } + + if(this.hover) { + this.handler = new OpenLayers.Handler.Hover( + this, { + 'move': this.cancelHover, + 'pause': this.getInfoForHover + }, + OpenLayers.Util.extend(this.handlerOptions.hover || {}, { + 'delay': 250 + })); + } else { + var callbacks = {}; + callbacks[this.clickCallback] = this.getInfoForClick; + this.handler = new OpenLayers.Handler.Click( + this, callbacks, this.handlerOptions.click || {}); + } + }, + + /** + * Method: getInfoForClick + * Called on click + * + * Parameters: + * evt - {<OpenLayers.Event>} + */ + getInfoForClick: function(evt) { + this.events.triggerEvent("beforegetfeatureinfo", {xy: evt.xy}); + // Set the cursor to "wait" to tell the user we're working on their + // click. + OpenLayers.Element.addClass(this.map.viewPortDiv, "olCursorWait"); + this.request(evt.xy, {}); + }, + + /** + * Method: getInfoForHover + * Pause callback for the hover handler + * + * Parameters: + * evt - {Object} + */ + getInfoForHover: function(evt) { + this.events.triggerEvent("beforegetfeatureinfo", {xy: evt.xy}); + this.request(evt.xy, {hover: true}); + }, + + /** + * Method: cancelHover + * Cancel callback for the hover handler + */ + cancelHover: function() { + if (this.hoverRequest) { + this.hoverRequest.abort(); + this.hoverRequest = null; + } + }, + + /** + * Method: findLayers + * Internal method to get the layers, independent of whether we are + * inspecting the map or using a client-provided array + */ + findLayers: function() { + + var candidates = this.layers || this.map.layers; + var layers = []; + var layer, url; + for(var i = candidates.length - 1; i >= 0; --i) { + layer = candidates[i]; + if(layer instanceof OpenLayers.Layer.WMS && + (!this.queryVisible || layer.getVisibility())) { + url = OpenLayers.Util.isArray(layer.url) ? layer.url[0] : layer.url; + // if the control was not configured with a url, set it + // to the first layer url + if(this.drillDown === false && !this.url) { + this.url = url; + } + if(this.drillDown === true || this.urlMatches(url)) { + layers.push(layer); + } + } + } + return layers; + }, + + /** + * Method: urlMatches + * Test to see if the provided url matches either the control <url> or one + * of the <layerUrls>. + * + * Parameters: + * url - {String} The url to test. + * + * Returns: + * {Boolean} The provided url matches the control <url> or one of the + * <layerUrls>. + */ + urlMatches: function(url) { + var matches = OpenLayers.Util.isEquivalentUrl(this.url, url); + if(!matches && this.layerUrls) { + for(var i=0, len=this.layerUrls.length; i<len; ++i) { + if(OpenLayers.Util.isEquivalentUrl(this.layerUrls[i], url)) { + matches = true; + break; + } + } + } + return matches; + }, + + /** + * Method: buildWMSOptions + * Build an object with the relevant WMS options for the GetFeatureInfo request + * + * Parameters: + * url - {String} The url to be used for sending the request + * layers - {Array(<OpenLayers.Layer.WMS)} An array of layers + * clickPosition - {<OpenLayers.Pixel>} The position on the map where the mouse + * event occurred. + * format - {String} The format from the corresponding GetMap request + */ + buildWMSOptions: function(url, layers, clickPosition, format) { + var layerNames = [], styleNames = []; + for (var i = 0, len = layers.length; i < len; i++) { + if (layers[i].params.LAYERS != null) { + layerNames = layerNames.concat(layers[i].params.LAYERS); + styleNames = styleNames.concat(this.getStyleNames(layers[i])); + } + } + var firstLayer = layers[0]; + // use the firstLayer's projection if it matches the map projection - + // this assumes that all layers will be available in this projection + var projection = this.map.getProjection(); + var layerProj = firstLayer.projection; + if (layerProj && layerProj.equals(this.map.getProjectionObject())) { + projection = layerProj.getCode(); + } + var params = OpenLayers.Util.extend({ + service: "WMS", + version: firstLayer.params.VERSION, + request: "GetFeatureInfo", + exceptions: firstLayer.params.EXCEPTIONS, + bbox: this.map.getExtent().toBBOX(null, + firstLayer.reverseAxisOrder()), + feature_count: this.maxFeatures, + height: this.map.getSize().h, + width: this.map.getSize().w, + format: format, + info_format: firstLayer.params.INFO_FORMAT || this.infoFormat + }, (parseFloat(firstLayer.params.VERSION) >= 1.3) ? + { + crs: projection, + i: parseInt(clickPosition.x), + j: parseInt(clickPosition.y) + } : + { + srs: projection, + x: parseInt(clickPosition.x), + y: parseInt(clickPosition.y) + } + ); + if (layerNames.length != 0) { + params = OpenLayers.Util.extend({ + layers: layerNames, + query_layers: layerNames, + styles: styleNames + }, params); + } + OpenLayers.Util.applyDefaults(params, this.vendorParams); + return { + url: url, + params: OpenLayers.Util.upperCaseObject(params), + callback: function(request) { + this.handleResponse(clickPosition, request, url); + }, + scope: this + }; + }, + + /** + * Method: getStyleNames + * Gets the STYLES parameter for the layer. Make sure the STYLES parameter + * matches the LAYERS parameter + * + * Parameters: + * layer - {<OpenLayers.Layer.WMS>} + * + * Returns: + * {Array(String)} The STYLES parameter + */ + getStyleNames: function(layer) { + // in the event of a WMS layer bundling multiple layers but not + // specifying styles,we need the same number of commas to specify + // the default style for each of the layers. We can't just leave it + // blank as we may be including other layers that do specify styles. + var styleNames; + if (layer.params.STYLES) { + styleNames = layer.params.STYLES; + } else { + if (OpenLayers.Util.isArray(layer.params.LAYERS)) { + styleNames = new Array(layer.params.LAYERS.length); + } else { // Assume it's a String + styleNames = layer.params.LAYERS.replace(/[^,]/g, ""); + } + } + return styleNames; + }, + + /** + * Method: request + * Sends a GetFeatureInfo request to the WMS + * + * Parameters: + * clickPosition - {<OpenLayers.Pixel>} The position on the map where the + * mouse event occurred. + * options - {Object} additional options for this method. + * + * Valid options: + * - *hover* {Boolean} true if we do the request for the hover handler + */ + request: function(clickPosition, options) { + var layers = this.findLayers(); + if(layers.length == 0) { + this.events.triggerEvent("nogetfeatureinfo"); + // Reset the cursor. + OpenLayers.Element.removeClass(this.map.viewPortDiv, "olCursorWait"); + return; + } + + options = options || {}; + if(this.drillDown === false) { + var wmsOptions = this.buildWMSOptions(this.url, layers, + clickPosition, layers[0].params.FORMAT); + var request = OpenLayers.Request.GET(wmsOptions); + + if (options.hover === true) { + this.hoverRequest = request; + } + } else { + this._requestCount = 0; + this._numRequests = 0; + this.features = []; + // group according to service url to combine requests + var services = {}, url; + for(var i=0, len=layers.length; i<len; i++) { + var layer = layers[i]; + var service, found = false; + url = OpenLayers.Util.isArray(layer.url) ? layer.url[0] : layer.url; + if(url in services) { + services[url].push(layer); + } else { + this._numRequests++; + services[url] = [layer]; + } + } + var layers; + for (var url in services) { + layers = services[url]; + var wmsOptions = this.buildWMSOptions(url, layers, + clickPosition, layers[0].params.FORMAT); + OpenLayers.Request.GET(wmsOptions); + } + } + }, + + /** + * Method: triggerGetFeatureInfo + * Trigger the getfeatureinfo event when all is done + * + * Parameters: + * request - {XMLHttpRequest} The request object + * xy - {<OpenLayers.Pixel>} The position on the map where the + * mouse event occurred. + * features - {Array(<OpenLayers.Feature.Vector>)} or + * {Array({Object}) when output is "object". The object has a url and a + * features property which contains an array of features. + */ + triggerGetFeatureInfo: function(request, xy, features) { + this.events.triggerEvent("getfeatureinfo", { + text: request.responseText, + features: features, + request: request, + xy: xy + }); + + // Reset the cursor. + OpenLayers.Element.removeClass(this.map.viewPortDiv, "olCursorWait"); + }, + + /** + * Method: handleResponse + * Handler for the GetFeatureInfo response. + * + * Parameters: + * xy - {<OpenLayers.Pixel>} The position on the map where the + * mouse event occurred. + * request - {XMLHttpRequest} The request object. + * url - {String} The url which was used for this request. + */ + handleResponse: function(xy, request, url) { + + var doc = request.responseXML; + if(!doc || !doc.documentElement) { + doc = request.responseText; + } + var features = this.format.read(doc); + if (this.drillDown === false) { + this.triggerGetFeatureInfo(request, xy, features); + } else { + this._requestCount++; + if (this.output === "object") { + this._features = (this._features || []).concat( + {url: url, features: features} + ); + } else { + this._features = (this._features || []).concat(features); + } + if (this._requestCount === this._numRequests) { + this.triggerGetFeatureInfo(request, xy, this._features.concat()); + delete this._features; + delete this._requestCount; + delete this._numRequests; + } + } + }, + + CLASS_NAME: "OpenLayers.Control.WMSGetFeatureInfo" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/WMTSGetFeatureInfo.js b/misc/openlayers/lib/OpenLayers/Control/WMTSGetFeatureInfo.js new file mode 100644 index 0000000..c26f8f3 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/WMTSGetFeatureInfo.js @@ -0,0 +1,400 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Handler/Click.js + * @requires OpenLayers/Handler/Hover.js + * @requires OpenLayers/Request.js + * @requires OpenLayers/Format/WMSGetFeatureInfo.js + */ + +/** + * Class: OpenLayers.Control.WMTSGetFeatureInfo + * The WMTSGetFeatureInfo control uses a WMTS query to get information about a + * point on the map. The information may be in a display-friendly format + * such as HTML, or a machine-friendly format such as GML, depending on the + * server's capabilities and the client's configuration. This control + * handles click or hover events, attempts to parse the results using an + * OpenLayers.Format, and fires a 'getfeatureinfo' event for each layer + * queried. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.WMTSGetFeatureInfo = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: hover + * {Boolean} Send GetFeatureInfo requests when mouse stops moving. + * Default is false. + */ + hover: false, + + /** + * Property: requestEncoding + * {String} One of "KVP" or "REST". Only KVP encoding is supported at this + * time. + */ + requestEncoding: "KVP", + + /** + * APIProperty: drillDown + * {Boolean} Drill down over all WMTS layers in the map. When + * using drillDown mode, hover is not possible. A getfeatureinfo event + * will be fired for each layer queried. + */ + drillDown: false, + + /** + * APIProperty: maxFeatures + * {Integer} Maximum number of features to return from a WMTS query. This + * sets the feature_count parameter on WMTS GetFeatureInfo + * requests. + */ + maxFeatures: 10, + + /** APIProperty: clickCallback + * {String} The click callback to register in the + * {<OpenLayers.Handler.Click>} object created when the hover + * option is set to false. Default is "click". + */ + clickCallback: "click", + + /** + * Property: layers + * {Array(<OpenLayers.Layer.WMTS>)} The layers to query for feature info. + * If omitted, all map WMTS layers will be considered. + */ + layers: null, + + /** + * APIProperty: queryVisible + * {Boolean} Filter out hidden layers when searching the map for layers to + * query. Default is true. + */ + queryVisible: true, + + /** + * Property: infoFormat + * {String} The mimetype to request from the server + */ + infoFormat: 'text/html', + + /** + * Property: vendorParams + * {Object} Additional parameters that will be added to the request, for + * WMTS implementations that support them. This could e.g. look like + * (start code) + * { + * radius: 5 + * } + * (end) + */ + vendorParams: {}, + + /** + * Property: format + * {<OpenLayers.Format>} A format for parsing GetFeatureInfo responses. + * Default is <OpenLayers.Format.WMSGetFeatureInfo>. + */ + format: null, + + /** + * Property: formatOptions + * {Object} Optional properties to set on the format (if one is not provided + * in the <format> property. + */ + formatOptions: null, + + /** + * APIProperty: handlerOptions + * {Object} Additional options for the handlers used by this control, e.g. + * (start code) + * { + * "click": {delay: 100}, + * "hover": {delay: 300} + * } + * (end) + */ + + /** + * Property: handler + * {Object} Reference to the <OpenLayers.Handler> for this control + */ + handler: null, + + /** + * Property: hoverRequest + * {<OpenLayers.Request>} contains the currently running hover request + * (if any). + */ + hoverRequest: null, + + /** + * APIProperty: events + * {<OpenLayers.Events>} Events instance for listeners and triggering + * control specific events. + * + * Register a listener for a particular event with the following syntax: + * (code) + * control.events.register(type, obj, listener); + * (end) + * + * Supported event types (in addition to those from <OpenLayers.Control.events>): + * beforegetfeatureinfo - Triggered before each request is sent. + * The event object has an *xy* property with the position of the + * mouse click or hover event that triggers the request and a *layer* + * property referencing the layer about to be queried. If a listener + * returns false, the request will not be issued. + * getfeatureinfo - Triggered when a GetFeatureInfo response is received. + * The event object has a *text* property with the body of the + * response (String), a *features* property with an array of the + * parsed features, an *xy* property with the position of the mouse + * click or hover event that triggered the request, a *layer* property + * referencing the layer queried and a *request* property with the + * request itself. If drillDown is set to true, one event will be fired + * for each layer queried. + * exception - Triggered when a GetFeatureInfo request fails (with a + * status other than 200) or whenparsing fails. Listeners will receive + * an event with *request*, *xy*, and *layer* properties. In the case + * of a parsing error, the event will also contain an *error* property. + */ + + /** + * Property: pending + * {Number} The number of pending requests. + */ + pending: 0, + + /** + * Constructor: <OpenLayers.Control.WMTSGetFeatureInfo> + * + * Parameters: + * options - {Object} + */ + initialize: function(options) { + options = options || {}; + options.handlerOptions = options.handlerOptions || {}; + + OpenLayers.Control.prototype.initialize.apply(this, [options]); + + if (!this.format) { + this.format = new OpenLayers.Format.WMSGetFeatureInfo( + options.formatOptions + ); + } + + if (this.drillDown === true) { + this.hover = false; + } + + if (this.hover) { + this.handler = new OpenLayers.Handler.Hover( + this, { + move: this.cancelHover, + pause: this.getInfoForHover + }, + OpenLayers.Util.extend( + this.handlerOptions.hover || {}, {delay: 250} + ) + ); + } else { + var callbacks = {}; + callbacks[this.clickCallback] = this.getInfoForClick; + this.handler = new OpenLayers.Handler.Click( + this, callbacks, this.handlerOptions.click || {} + ); + } + }, + + /** + * Method: getInfoForClick + * Called on click + * + * Parameters: + * evt - {<OpenLayers.Event>} + */ + getInfoForClick: function(evt) { + this.request(evt.xy, {}); + }, + + /** + * Method: getInfoForHover + * Pause callback for the hover handler + * + * Parameters: + * evt - {Object} + */ + getInfoForHover: function(evt) { + this.request(evt.xy, {hover: true}); + }, + + /** + * Method: cancelHover + * Cancel callback for the hover handler + */ + cancelHover: function() { + if (this.hoverRequest) { + --this.pending; + if (this.pending <= 0) { + OpenLayers.Element.removeClass(this.map.viewPortDiv, "olCursorWait"); + this.pending = 0; + } + this.hoverRequest.abort(); + this.hoverRequest = null; + } + }, + + /** + * Method: findLayers + * Internal method to get the layers, independent of whether we are + * inspecting the map or using a client-provided array + */ + findLayers: function() { + var candidates = this.layers || this.map.layers; + var layers = []; + var layer; + for (var i=candidates.length-1; i>=0; --i) { + layer = candidates[i]; + if (layer instanceof OpenLayers.Layer.WMTS && + layer.requestEncoding === this.requestEncoding && + (!this.queryVisible || layer.getVisibility())) { + layers.push(layer); + if (!this.drillDown || this.hover) { + break; + } + } + } + return layers; + }, + + /** + * Method: buildRequestOptions + * Build an object with the relevant options for the GetFeatureInfo request. + * + * Parameters: + * layer - {<OpenLayers.Layer.WMTS>} A WMTS layer. + * xy - {<OpenLayers.Pixel>} The position on the map where the + * mouse event occurred. + */ + buildRequestOptions: function(layer, xy) { + var loc = this.map.getLonLatFromPixel(xy); + var getTileUrl = layer.getURL( + new OpenLayers.Bounds(loc.lon, loc.lat, loc.lon, loc.lat) + ); + var params = OpenLayers.Util.getParameters(getTileUrl); + var tileInfo = layer.getTileInfo(loc); + OpenLayers.Util.extend(params, { + service: "WMTS", + version: layer.version, + request: "GetFeatureInfo", + infoFormat: this.infoFormat, + i: tileInfo.i, + j: tileInfo.j + }); + OpenLayers.Util.applyDefaults(params, this.vendorParams); + return { + url: OpenLayers.Util.isArray(layer.url) ? layer.url[0] : layer.url, + params: OpenLayers.Util.upperCaseObject(params), + callback: function(request) { + this.handleResponse(xy, request, layer); + }, + scope: this + }; + }, + + /** + * Method: request + * Sends a GetFeatureInfo request to the WMTS + * + * Parameters: + * xy - {<OpenLayers.Pixel>} The position on the map where the mouse event + * occurred. + * options - {Object} additional options for this method. + * + * Valid options: + * - *hover* {Boolean} true if we do the request for the hover handler + */ + request: function(xy, options) { + options = options || {}; + var layers = this.findLayers(); + if (layers.length > 0) { + var issue, layer; + for (var i=0, len=layers.length; i<len; i++) { + layer = layers[i]; + issue = this.events.triggerEvent("beforegetfeatureinfo", { + xy: xy, + layer: layer + }); + if (issue !== false) { + ++this.pending; + var requestOptions = this.buildRequestOptions(layer, xy); + var request = OpenLayers.Request.GET(requestOptions); + if (options.hover === true) { + this.hoverRequest = request; + } + } + } + if (this.pending > 0) { + OpenLayers.Element.addClass(this.map.viewPortDiv, "olCursorWait"); + } + } + }, + + /** + * Method: handleResponse + * Handler for the GetFeatureInfo response. + * + * Parameters: + * xy - {<OpenLayers.Pixel>} The position on the map where the mouse event + * occurred. + * request - {XMLHttpRequest} The request object. + * layer - {<OpenLayers.Layer.WMTS>} The queried layer. + */ + handleResponse: function(xy, request, layer) { + --this.pending; + if (this.pending <= 0) { + OpenLayers.Element.removeClass(this.map.viewPortDiv, "olCursorWait"); + this.pending = 0; + } + if (request.status && (request.status < 200 || request.status >= 300)) { + this.events.triggerEvent("exception", { + xy: xy, + request: request, + layer: layer + }); + } else { + var doc = request.responseXML; + if (!doc || !doc.documentElement) { + doc = request.responseText; + } + var features, except; + try { + features = this.format.read(doc); + } catch (error) { + except = true; + this.events.triggerEvent("exception", { + xy: xy, + request: request, + error: error, + layer: layer + }); + } + if (!except) { + this.events.triggerEvent("getfeatureinfo", { + text: request.responseText, + features: features, + request: request, + xy: xy, + layer: layer + }); + } + } + }, + + CLASS_NAME: "OpenLayers.Control.WMTSGetFeatureInfo" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/Zoom.js b/misc/openlayers/lib/OpenLayers/Control/Zoom.js new file mode 100644 index 0000000..70140f4 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/Zoom.js @@ -0,0 +1,138 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Events/buttonclick.js + */ + +/** + * Class: OpenLayers.Control.Zoom + * The Zoom control is a pair of +/- links for zooming in and out. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.Zoom = OpenLayers.Class(OpenLayers.Control, { + + /** + * APIProperty: zoomInText + * {String} + * Text for zoom-in link. Default is "+". + */ + zoomInText: "+", + + /** + * APIProperty: zoomInId + * {String} + * Instead of having the control create a zoom in link, you can provide + * the identifier for an anchor element already added to the document. + * By default, an element with id "olZoomInLink" will be searched for + * and used if it exists. + */ + zoomInId: "olZoomInLink", + + /** + * APIProperty: zoomOutText + * {String} + * Text for zoom-out link. Default is "\u2212". + */ + zoomOutText: "\u2212", + + /** + * APIProperty: zoomOutId + * {String} + * Instead of having the control create a zoom out link, you can provide + * the identifier for an anchor element already added to the document. + * By default, an element with id "olZoomOutLink" will be searched for + * and used if it exists. + */ + zoomOutId: "olZoomOutLink", + + /** + * Method: draw + * + * Returns: + * {DOMElement} A reference to the DOMElement containing the zoom links. + */ + draw: function() { + var div = OpenLayers.Control.prototype.draw.apply(this), + links = this.getOrCreateLinks(div), + zoomIn = links.zoomIn, + zoomOut = links.zoomOut, + eventsInstance = this.map.events; + + if (zoomOut.parentNode !== div) { + eventsInstance = this.events; + eventsInstance.attachToElement(zoomOut.parentNode); + } + eventsInstance.register("buttonclick", this, this.onZoomClick); + + this.zoomInLink = zoomIn; + this.zoomOutLink = zoomOut; + return div; + }, + + /** + * Method: getOrCreateLinks + * + * Parameters: + * el - {DOMElement} + * + * Return: + * {Object} Object with zoomIn and zoomOut properties referencing links. + */ + getOrCreateLinks: function(el) { + var zoomIn = document.getElementById(this.zoomInId), + zoomOut = document.getElementById(this.zoomOutId); + if (!zoomIn) { + zoomIn = document.createElement("a"); + zoomIn.href = "#zoomIn"; + zoomIn.appendChild(document.createTextNode(this.zoomInText)); + zoomIn.className = "olControlZoomIn"; + el.appendChild(zoomIn); + } + OpenLayers.Element.addClass(zoomIn, "olButton"); + if (!zoomOut) { + zoomOut = document.createElement("a"); + zoomOut.href = "#zoomOut"; + zoomOut.appendChild(document.createTextNode(this.zoomOutText)); + zoomOut.className = "olControlZoomOut"; + el.appendChild(zoomOut); + } + OpenLayers.Element.addClass(zoomOut, "olButton"); + return { + zoomIn: zoomIn, zoomOut: zoomOut + }; + }, + + /** + * Method: onZoomClick + * Called when zoomin/out link is clicked. + */ + onZoomClick: function(evt) { + var button = evt.buttonElement; + if (button === this.zoomInLink) { + this.map.zoomIn(); + } else if (button === this.zoomOutLink) { + this.map.zoomOut(); + } + }, + + /** + * Method: destroy + * Clean up. + */ + destroy: function() { + if (this.map) { + this.map.events.unregister("buttonclick", this, this.onZoomClick); + } + delete this.zoomInLink; + delete this.zoomOutLink; + OpenLayers.Control.prototype.destroy.apply(this); + }, + + CLASS_NAME: "OpenLayers.Control.Zoom" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/ZoomBox.js b/misc/openlayers/lib/OpenLayers/Control/ZoomBox.js new file mode 100644 index 0000000..9d4b2da --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/ZoomBox.js @@ -0,0 +1,129 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control.js + * @requires OpenLayers/Handler/Box.js + */ + +/** + * Class: OpenLayers.Control.ZoomBox + * The ZoomBox control enables zooming directly to a given extent, by drawing + * a box on the map. The box is drawn by holding down shift, whilst dragging + * the mouse. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.ZoomBox = OpenLayers.Class(OpenLayers.Control, { + /** + * Property: type + * {OpenLayers.Control.TYPE} + */ + type: OpenLayers.Control.TYPE_TOOL, + + /** + * Property: out + * {Boolean} Should the control be used for zooming out? + */ + out: false, + + /** + * APIProperty: keyMask + * {Integer} Zoom only occurs if the keyMask matches the combination of + * keys down. Use bitwise operators and one or more of the + * <OpenLayers.Handler> constants to construct a keyMask. Leave null if + * not used mask. Default is null. + */ + keyMask: null, + + /** + * APIProperty: alwaysZoom + * {Boolean} Always zoom in/out when box drawn, even if the zoom level does + * not change. + */ + alwaysZoom: false, + + /** + * APIProperty: zoomOnClick + * {Boolean} Should we zoom when no box was dragged, i.e. the user only + * clicked? Default is true. + */ + zoomOnClick: true, + + /** + * Method: draw + */ + draw: function() { + this.handler = new OpenLayers.Handler.Box( this, + {done: this.zoomBox}, {keyMask: this.keyMask} ); + }, + + /** + * Method: zoomBox + * + * Parameters: + * position - {<OpenLayers.Bounds>} or {<OpenLayers.Pixel>} + */ + zoomBox: function (position) { + if (position instanceof OpenLayers.Bounds) { + var bounds, + targetCenterPx = position.getCenterPixel(); + if (!this.out) { + var minXY = this.map.getLonLatFromPixel({ + x: position.left, + y: position.bottom + }); + var maxXY = this.map.getLonLatFromPixel({ + x: position.right, + y: position.top + }); + bounds = new OpenLayers.Bounds(minXY.lon, minXY.lat, + maxXY.lon, maxXY.lat); + } else { + var pixWidth = position.right - position.left; + var pixHeight = position.bottom - position.top; + var zoomFactor = Math.min((this.map.size.h / pixHeight), + (this.map.size.w / pixWidth)); + var extent = this.map.getExtent(); + var center = this.map.getLonLatFromPixel(targetCenterPx); + var xmin = center.lon - (extent.getWidth()/2)*zoomFactor; + var xmax = center.lon + (extent.getWidth()/2)*zoomFactor; + var ymin = center.lat - (extent.getHeight()/2)*zoomFactor; + var ymax = center.lat + (extent.getHeight()/2)*zoomFactor; + bounds = new OpenLayers.Bounds(xmin, ymin, xmax, ymax); + } + // always zoom in/out + var lastZoom = this.map.getZoom(), + size = this.map.getSize(), + centerPx = {x: size.w / 2, y: size.h / 2}, + zoom = this.map.getZoomForExtent(bounds), + oldRes = this.map.getResolution(), + newRes = this.map.getResolutionForZoom(zoom); + if (oldRes == newRes) { + this.map.setCenter(this.map.getLonLatFromPixel(targetCenterPx)); + } else { + var zoomOriginPx = { + x: (oldRes * targetCenterPx.x - newRes * centerPx.x) / + (oldRes - newRes), + y: (oldRes * targetCenterPx.y - newRes * centerPx.y) / + (oldRes - newRes) + }; + this.map.zoomTo(zoom, zoomOriginPx); + } + if (lastZoom == this.map.getZoom() && this.alwaysZoom == true){ + this.map.zoomTo(lastZoom + (this.out ? -1 : 1)); + } + } else if (this.zoomOnClick) { // it's a pixel + if (!this.out) { + this.map.zoomTo(this.map.getZoom() + 1, position); + } else { + this.map.zoomTo(this.map.getZoom() - 1, position); + } + } + }, + + CLASS_NAME: "OpenLayers.Control.ZoomBox" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/ZoomIn.js b/misc/openlayers/lib/OpenLayers/Control/ZoomIn.js new file mode 100644 index 0000000..8da1e1c --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/ZoomIn.js @@ -0,0 +1,29 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control/Button.js + */ + +/** + * Class: OpenLayers.Control.ZoomIn + * The ZoomIn control is a button to increase the zoom level of a map. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.ZoomIn = OpenLayers.Class(OpenLayers.Control.Button, { + + /** + * Method: trigger + */ + trigger: function(){ + if (this.map) { + this.map.zoomIn(); + } + }, + + CLASS_NAME: "OpenLayers.Control.ZoomIn" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/ZoomOut.js b/misc/openlayers/lib/OpenLayers/Control/ZoomOut.js new file mode 100644 index 0000000..72a657b --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/ZoomOut.js @@ -0,0 +1,29 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control/Button.js + */ + +/** + * Class: OpenLayers.Control.ZoomOut + * The ZoomOut control is a button to decrease the zoom level of a map. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.ZoomOut = OpenLayers.Class(OpenLayers.Control.Button, { + + /** + * Method: trigger + */ + trigger: function(){ + if (this.map) { + this.map.zoomOut(); + } + }, + + CLASS_NAME: "OpenLayers.Control.ZoomOut" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/ZoomPanel.js b/misc/openlayers/lib/OpenLayers/Control/ZoomPanel.js new file mode 100644 index 0000000..147f6cb --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/ZoomPanel.js @@ -0,0 +1,54 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control/Panel.js + * @requires OpenLayers/Control/ZoomIn.js + * @requires OpenLayers/Control/ZoomOut.js + * @requires OpenLayers/Control/ZoomToMaxExtent.js + */ + +/** + * Class: OpenLayers.Control.ZoomPanel + * The ZoomPanel control is a compact collecton of 3 zoom controls: a + * <OpenLayers.Control.ZoomIn>, a <OpenLayers.Control.ZoomToMaxExtent>, and a + * <OpenLayers.Control.ZoomOut>. By default it is drawn in the upper left + * corner of the map. + * + * Note: + * If you wish to use this class with the default images and you want + * it to look nice in ie6, you should add the following, conditionally + * added css stylesheet to your HTML file: + * + * (code) + * <!--[if lte IE 6]> + * <link rel="stylesheet" href="../theme/default/ie6-style.css" type="text/css" /> + * <![endif]--> + * (end) + * + * Inherits from: + * - <OpenLayers.Control.Panel> + */ +OpenLayers.Control.ZoomPanel = OpenLayers.Class(OpenLayers.Control.Panel, { + + /** + * Constructor: OpenLayers.Control.ZoomPanel + * Add the three zooming controls. + * + * Parameters: + * options - {Object} An optional object whose properties will be used + * to extend the control. + */ + initialize: function(options) { + OpenLayers.Control.Panel.prototype.initialize.apply(this, [options]); + this.addControls([ + new OpenLayers.Control.ZoomIn(), + new OpenLayers.Control.ZoomToMaxExtent(), + new OpenLayers.Control.ZoomOut() + ]); + }, + + CLASS_NAME: "OpenLayers.Control.ZoomPanel" +}); diff --git a/misc/openlayers/lib/OpenLayers/Control/ZoomToMaxExtent.js b/misc/openlayers/lib/OpenLayers/Control/ZoomToMaxExtent.js new file mode 100644 index 0000000..bc2e754 --- /dev/null +++ b/misc/openlayers/lib/OpenLayers/Control/ZoomToMaxExtent.js @@ -0,0 +1,35 @@ +/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for + * full list of contributors). Published under the 2-clause BSD license. + * See license.txt in the OpenLayers distribution or repository for the + * full text of the license. */ + +/** + * @requires OpenLayers/Control/Button.js + */ + +/** + * Class: OpenLayers.Control.ZoomToMaxExtent + * The ZoomToMaxExtent control is a button that zooms out to the maximum + * extent of the map. It is designed to be used with a + * <OpenLayers.Control.Panel>. + * + * Inherits from: + * - <OpenLayers.Control> + */ +OpenLayers.Control.ZoomToMaxExtent = OpenLayers.Class(OpenLayers.Control.Button, { + + /** + * Method: trigger + * + * Called whenever this control is being rendered inside of a panel and a + * click occurs on this controls element. Actually zooms to the maximum + * extent of this controls map. + */ + trigger: function() { + if (this.map) { + this.map.zoomToMaxExtent(); + } + }, + + CLASS_NAME: "OpenLayers.Control.ZoomToMaxExtent" +}); |