-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.js
executable file
·232 lines (197 loc) · 7.81 KB
/
map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Fish richness map javascript
// one global for persistent app variables
var app = {};
require(["esri/map", "esri/layers/FeatureLayer", "esri/tasks/ClassBreaksDefinition", "esri/tasks/AlgorithmicColorRamp", "esri/tasks/GenerateRendererParameters", "esri/tasks/GenerateRendererTask", "esri/symbols/SimpleLineSymbol", "esri/symbols/SimpleFillSymbol", "esri/dijit/PopupTemplate", "esri/dijit/Legend", "dojo/parser", "dojo/_base/array", "esri/Color", "dojo/on", "dojo/dom", "dojo/dom-construct", "dojo/number", "dojo/dom-style", "dojo/data/ItemFileReadStore", "dijit/form/FilteringSelect", "dijit/layout/BorderContainer", "dijit/layout/ContentPane", "dojo/domReady!"], function (
Map,
FeatureLayer,
ClassBreaksDefinition,
AlgorithmicColorRamp,
GenerateRendererParameters,
GenerateRendererTask,
SimpleLineSymbol,
SimpleFillSymbol,
PopupTemplate,
Legend,
parser,
arrayUtils,
Color,
on,
dom,
domConstruct,
number,
domStyle,
ItemFileReadStore,
FilteringSelect
) {
parser.parse();
// URL for the feature layer
app.Url = "//atlas.cws.ucdavis.edu/arcgis/rest/services/PISCESRichness/diversity/MapServer/0";
// default attribute to load
app.currentAttribute = "native_qc_richness";
// the map service uses the actual field name as the field alias
// set up an object to use as a lookup table to convert from terse field
// names to more user friendly field names
app.fields = {
"HU_12_NAME": "HUC12 Name",
"HUC_12": "HUC_12",
"native_qc_historic_richness": "Historical Richness",
"native_qc_richness": "Native Richness",
"nonnative_qc_richness": "Non-Native Richness",
"div_native_qc": "Jaccard: Native Fish",
"div_all_qc": "Jaccard: All Fish",
"historic_assemblage_Native_Fish": "Historical Assemblage",
"losses_Fish_list": "Species Lost",
"gains_Fish_list": "Species Gained"
};
//////// selectable fields ///////////
// get keys from the field list lookup table object
app.outFields = Object.keys(app.fields);
//array of fields to be selectable in dropdown menu - must be in app.outFields
app.selectable = ["native_qc_richness", "native_qc_historic_richness", "nonnative_qc_richness", "div_native_qc", "div_all_qc"];
// create a store and a filtering select for the layer's fields
var fieldNames = {
"identifier": "value",
"label": "name",
"items": []
};
arrayUtils.forEach(app.selectable, function (f) {
fieldNames.items.push({
"name": app.fields[f],
"value": f
});
});
var fieldStore = new ItemFileReadStore({
data: fieldNames
});
var fieldSelect = new FilteringSelect({
displayedValue: fieldNames.items[0].name,
value: fieldNames.items[0].value,
name: "fieldsFS",
required: false,
store: fieldStore,
searchAttr: "name",
style: {
"width": "200px",
"fontSize": "12pt",
"color": "#444"
}
}, domConstruct.create("div", null, dom.byId("fieldWrapper")));
fieldSelect.on("change", updateAttribute);
///////////////////////// helper functions ///////////////////////////
// apply the render on the richness layer, force redraw, create new legend
function applyRenderer(renderer) {
rich.setRenderer(renderer);
rich.redraw();
createLegend(app.map, rich);
}
// create a render for a field
function createRenderer(field) {
app.sfs = new SimpleFillSymbol(
SimpleFillSymbol.STYLE_SOLID,
new SimpleLineSymbol(
SimpleLineSymbol.STYLE_SOLID,
new Color([0, 0, 0, 0.5]),
0.5
),
null
);
var classDef = new ClassBreaksDefinition();
classDef.classificationField = app.currentAttribute;
classDef.classificationMethod = "quantile";
classDef.breakCount = 6;
classDef.baseSymbol = app.sfs;
var colorRamp = new AlgorithmicColorRamp();
colorRamp.fromColor = Color.fromHex("#ffe98e"); // colors for the renderer
colorRamp.toColor = Color.fromHex("#ff684c"); // colors for the renderer
colorRamp.algorithm = "hsv"; // options are: "cie-lab", "hsv", "lab-lch"
classDef.colorRamp = colorRamp;
var params = new GenerateRendererParameters();
params.classificationDefinition = classDef;
//params.where = app.layerDef; //app.layerDef not needed if fishless hucs have Nulls instead of zeros
var generateRenderer = new GenerateRendererTask(app.Url);
generateRenderer.execute(params, applyRenderer, errorHandler);
}
// things that get updated when a new metric is picked
function updateAttribute(ch) {
app.map.infoWindow.hide();
rich.setInfoTemplate(createPopup(ch));
app.currentAttribute = ch;
createRenderer(ch);
createLegend(app.map, rich);
}
function createPopup(cfield) {
// creates a popup
delete app.popupTemplate;
app.popupTemplate = new PopupTemplate({
title: "{HUC_12}: {HU_12_NAME}",
fieldInfos: [{
"fieldName": cfield,
"label": app.fields[cfield],
"visible": true,
//"format": { places: 0, digitSeparator: true } // controls sig figs visible in popup
}, {
"fieldName": "historic_assemblage_Native_Fish",
"label": "Historical Assemblage",
"visible": true
}, {
"fieldName": "losses_Fish_list",
"label": "Species Lost",
"visible": true
}, {
"fieldName": "gains_Fish_list",
"label": "Species Gained",
"visible": true
}],
showAttachments: true
});
return (app.popupTemplate)
}
function createLegend(map, fl) {
// destroy previous legend, if present
if (app.hasOwnProperty("legend")) {
app.legend.destroy();
domConstruct.destroy(dojo.byId("legendDiv"));
}
// create a new div for the legend
var legendDiv = domConstruct.create("div", {
id: "legendDiv"
}, dom.byId("legendWrapper"));
app.legend = new Legend({
map: map,
layerInfos: [{
layer: fl,
title: " "
}]
}, legendDiv);
app.legend.startup();
}
function errorHandler(err) {
console.log('Oops, error: ', err);
}
// map constructor
app.map = new Map("map", {
basemap: "oceans",
center: [-121.45, 38.0],
zoom: 7,
slider: false
});
// create a feature layer and wait for map to load so the map's extent is available
app.map.on("load", function () {
domStyle.set("loading", "visibility", "visible");
rich = new FeatureLayer(app.Url, {
infoTemplate: createPopup(app.currentAttribute),
mode: FeatureLayer.MODE_AUTO,
outFields: app.outFields,
opacity: 0.7
});
app.map.addLayer(rich);
createRenderer(app.currentAttribute);
});
// hide the loading icon when the counties layer finishes updating
app.map.on("update-end", function () {
domStyle.set("loading", "visibility", "hidden");
});
app.map.on("zoom-start", function () {
domStyle.set("loading", "visibility", "visible");
});
});