Skip to content

Instantly share code, notes, and snippets.

@kflaw
Created October 28, 2014 22:26
Show Gist options
  • Save kflaw/dff392f399c302727723 to your computer and use it in GitHub Desktop.
Save kflaw/dff392f399c302727723 to your computer and use it in GitHub Desktop.
code custom widget for cmv trouble getting it to work
var imageInt = 0;
define([
"esri/layers/ImageServiceParameters",
"dojo/_base/declare", "dijit/_WidgetBase",
"dijit/_TemplatedMixin",
"dojo/on", "dojo/query", "dojo/dom-construct",
"dojo/dom-attr", "dojo/dom",
"esri/layers/MosaicRule", "dojo/domReady!"
], function (
ImageServiceParameters, declare, _WidgetBase, _TemplatedMixin, on, query,
domConstruct, domAttr, dom, MosaicRule
) {
return declare([_WidgetBase, _TemplatedMixin],{
//parser.parse();
var link = domConstruct.create("a", {
"class": "action",
"id": "rasterLock", //We also assign an ID "rasterLock" so we can change the link text to indicate something is happening
"innerHTML": "Add this raster", //text that appears in the popup for the link
"href": "javascript:void(0);"
}, query(".actionList", this.map.infoWindow.domNode)[0]);
on(link, "click", LockRasters); //When popup link is clicked Lock Rasters function runs to add images to the map.
function LockRasters(event){
//display a message so user knows something is happening
//The code to add rasters executes faster than this message can keep up and as a result it never shows up
dojo.setAttr(dojo.byId("rasterLock"), "innerHTML", "Locking Raster...");
//get the selected feature and it's popup content... we want to isolate the object id to pass
//the LockRaster Method.
var feature = this.map.infoWindow.getSelectedFeature();
var popupContent = feature.getContent();
//write contents to new page for printing
imageInt = imageInt + 1; //integer count of the number of rasters added to the map
var imageID = "imagery" + imageInt.toString(); //This will be the id of each image we add to the map. Each raster will have a unique id
var popStr = popupContent.split("="); //This extracts out the Object ID from the popup. Object ID in the popup appears as ObjectID: ###. This code splits out the numeric value.
var arrSlice = parseInt(popStr.slice(-1)[0]);//This line turns the numeric string into an integer
var arr = [arrSlice];//This line turns the integer object id into an array to be passed into the LockRasterID's Method
var imgParams = new ImageServiceParameters();//Image service display parameters
imgParams.noData = 0;
var mr = new MosaicRule(); //This line gets the Mosaic Rule Constructor
mr.method = MosaicRule.METHOD_LOCKRASTER; // This line gets the Lock Raster Method
mr.lockRasterIds = arr; //This line passes in the Object ID to lock inside an array from line 88
imgParams.mosaicRule = mr; //This line executes the LockRaster method on the selected raster in the popup.
var ImageService = "http://sv04esri:6080/arcgis/rest/services/Testing/IS_test/ImageServer"; //Image service to be added to the map.
imageServiceLayer = new ArcGISImageServiceLayer(ImageService, {
id: imageID, //ImageID set in line 85
imageServiceParameters: imgParams, // Lock Raster Method from line 94
opacity: 0.75 //Transparency is set to 25%
});
dojo.setAttr(dojo.byId("rasterLock"), "innerHTML", "Add map image"); //This line lets the popup know that the imgage processing is complets and resets the link back to its original state. Code is running so fast that users won't see any of this change.
theMap.addLayer(imageServiceLayer); // Adds the raster to the map.
}
function ClearRasters(){ //This function removes the last raster added to the map in reverse sequential order. Removes last raster added first.
if (imageInt == 0){ //if no rasters are added and someone clicks the clear rasters button this code allows it to gracefully not execute with an error
alert("There are currently no images loaded in the map.");
}
else if (this.map.getLayer != "layer0"){ //As long as there is an image on the map this section will run
var lastLayer = imageInt //This line gets the last image integer value assigned by the LockRasters() function
var removeLayer = "imagery" + lastLayer.toString() //last id added from the LockRasters function
var imageLayer = this.map.getLayer(removeLayer); //this line gets the raster we want to remove which is the last one added
this.map.removeLayer(imageLayer); //this actually removes the raster
imageInt = imageInt - 1 //this resets the integer id value back to the new last raster added.
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment