Repository navigation
Expand file tree
/
Copy pathvectorLayerDemo.js
More file actions
279 lines (260 loc) · 10.9 KB
/
Copy pathvectorLayerDemo.js
File metadata and controls
279 lines (260 loc) · 10.9 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Demo of OpenLayers vector layer
//
// OpenLayers map consisting of:
// * oBaseLayer - OL base layer, consisting of a few vanilla WMS layers
// * oHighlightLayer - OL vector layer
//
// The vector layer is styled:
// * 3px blue border ("stroke"), complety opaque
// * red "fill", 50% opaque
//
// The vector layer is also labeled with the town name in 10px black font.
// See the code for additional details on the styling of the labels for
// the vector features.
// Note: See https://openlayers.org/en/latest/examples/vector-labels.html
// for a demo of labeling OpenLayers vector features
//
// This demo also supports an 'on-pointermove' (i.e., 'on-hover') event handler
// that displays a "popup" (an OL "overlay") when the mouse hovers over
// any feature in the vector layer.
//
// Make WFS request for features to be added to vector layer
//
// Upon successful completion of WFS request, add the features
// returned by the WFS request to the vector layer
//
// B. Krepp, attending metaphysician
// 11 May 2020, 5 June 2020
var szServerRoot = location.protocol + '//' + location.hostname + '/maploc/';
var szWMSserverRoot = szServerRoot + '/wms';
var szWFSserverRoot = szServerRoot + '/wfs';
// Set up OpenLayers Map Projection (MA State Plane NAD83, meters)
// FYI - This can be done more simply via the inclusion of proj4.js,
// and the relevant projection file.
// Leaving this code as-is, though, since this demo was cooked
// up very quickly from pieces of existing code.
var projection = new ol.proj.Projection({
code: 'EPSG:26986',
extent: [33861.26,777514.31,330846.09,959747.44],
units: 'm'
});
ol.proj.addProjection(projection);
var MaStatePlane = '+proj=lcc +lat_1=42.68333333333333 +lat_2=41.71666666666667 +lat_0=41 +lon_0=-71.5 +x_0=200000 +y_0=750000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs';
ol.proj.addCoordinateTransforms(
'EPSG:4326',
projection,
function(coordinate){
var WGS_to_MAState = proj4(MaStatePlane).forward(coordinate);
return WGS_to_MAState;
},
function(coordinate){
var MAState_to_WGS = proj4(MaStatePlane).inverse(coordinate);
return MAState_to_WGS;
}
);
// Initial center point and zoom level for OL map
var mapCenter = [232908.27147578463, 902215.0940791398];
var mapZoom = 3.0;
// Define the base layer for the OpenLayers map
//
// It consists of: ocean background, NE states, MA towns, MPO boundary
// By default, the 'transparent' property is set to false, to indicate
// that this is the map's BASE layer
var oBaseLayer = new ol.layer.Tile({
source: new ol.source.TileWMS({
url : szWMSserverRoot,
params : {
'LAYERS': [ 'postgis:ctps_oceanmask_poly_small',
'postgis:mgis_nemask_poly',
'postgis:mgis_townssurvey_polym',
'postgis:ctps_ma_wo_model_area' ],
'STYLES': [ 'oceanmask_poly',
'ne_states',
'towns_blank',
'non_boston_mpo_gray_mask' ]
}
})
});
// The following declarations are to support the vector layer:
//
// Define OpenLayers vector layer - it will overlay the base layer
//
var oHighlightLayer = new ol.layer.Vector({source: new ol.source.Vector({ wrapX: false }) });
// Max map resolution at which to label vector features.
var maxResolutionForLabelingVectorFeatures = 1200;
// Our function to return text to label vector features
//
// Unabashedly borrowed from https://openlayers.org/en/latest/examples/vector-labels.html,
// and subsequently morphed for our purposes.
//
var getText = function(feature, resolution) {
var maxResolution = maxResolutionForLabelingVectorFeatures;
var text = feature.get('town');
if (resolution > maxResolution) {
text = '';
}
return text;
};
// Our createTextStyle function for labeling the vector layer
//
// Unabashedly borrowed from https://openlayers.org/en/latest/examples/vector-labels.html,
// and subsequently morphed for our purposes.
//
var createTextStyle = function(feature, resolution) {
var align = 'center';
var baseline = 'middle';
var size = '10px';
var height = 1;
var offsetX = 0;
var offsetY = 10; // Displace by 10px "south", so the label from the WMS layer is also visible.
var weight = 'normal';
var placement = 'point';
var maxAngle = 45;
var overflow = 'true';
var rotation = 0;
var font = weight + ' ' + size + '/' + height + ' ' + 'Arial';
var fillColor = 'black'; // Color of label TEXT itself
var outlineColor = 'white'; // Color of label OUTLINE
var outlineWidth = 0;
return new ol.style.Text({
textAlign: align,
textBaseline: baseline,
font: font,
text: getText(feature, resolution),
fill: new ol.style.Fill({color: fillColor}),
stroke: new ol.style.Stroke({color: outlineColor, width: outlineWidth}),
offsetX: offsetX,
offsetY: offsetY,
placement: placement,
maxAngle: maxAngle,
overflow: overflow,
rotation: rotation
});
};
// Define a function to style the polygon vector layer, and set the vector layer's style to use it
function myVectorPolygonStyleFunction(feature, resolution) {
return new ol.style.Style({ stroke: new ol.style.Stroke({ color: 'rgba(0,0,255,1.0)', width: 3.0}),
fill: new ol.style.Fill({ color: 'rgba(255,0,0,0.5)' }),
text: createTextStyle(feature, resolution)
});
}
oHighlightLayer.setStyle(myVectorPolygonStyleFunction);
// The following is to support the 'popup':
//
// Elements that make up the popup:
var container = document.getElementById('popup');
var content = document.getElementById('popup-content');
var closer = document.getElementById('popup-closer');
// Create an overlay to anchor the popup to the map:
var overlay = new ol.Overlay({
element: container,
autoPan: true,
autoPanAnimation: {
duration: 250
}
});
// Add an on-click handler to hide the popup
// (This is no longer necessary)
closer.onclick = function() {
overlay.setPosition(undefined);
closer.blur();
return false;
};
// Define OpenLayers map object
// Notes:
// 1. the vector layer is listed after the base layer in the "layers" array,
// so that it will be rendered on top of the base layer
// 2. the "overlay" is to support the on-hover popup for the vector layer
//
var map = new ol.Map({ target: 'map',
layers: [ oBaseLayer,
oHighlightLayer
],
overlays: [overlay],
view: new ol.View({ projection: projection,
center : mapCenter,
zoom :mapZoom
})
});
// On 'pointermove' event handler for OL map object.
// This function displays the "popup" when the mouse hovers over the vector layer.
//
// With thanks to:
// https://gis.stackexchange.com/questions/202473/openlayers3-add-click-hover-action-on-ol-layer-vector
// https://stackoverflow.com/questions/55743504/how-to-implement-feature-popups-in-openlayers-5-on-mouse-hover-and-click
map.on('pointermove', function(evt) {
var feature = map.forEachFeatureAtPixel(evt.pixel,
// Restrict the listener to the oHighlightLayer
function(feature, layer) {
var retval;
if (layer == oHighlightLayer) {
retval = feature;
} else {
retval = null;
}
return retval;
});
if (feature) {
// Here: code to display/hide a popup, or whatever
var _DEBUG_HOOK = 0;
var props = feature.getProperties();
var s = 'You hovered over ' + props['town'] + '.';
console.log(s);
var coordinate = evt.coordinate;
content.innerHTML = '<p>' + s + '</p>';
content.hidden = false;
container.hidden = false;
overlay.setPosition(coordinate);
} else {
content.innerHTML = '';
content.hidden = true;
container.hidden = true;
}
}); // end of OL map's on-pointermove handler
// Issue WFS request for all towns in MassGIS TOWNSSURVEY_POLYM with
// a town_id attribute < 10
//
var cqlFilter = "(town_id < 10)";
var szUrl = szWFSserverRoot + '?';
szUrl += '&service=wfs';
szUrl += '&version=1.0.0';
szUrl += '&request=getfeature';
szUrl += '&typename=postgis:townssurvey_polym';
szUrl += '&outputformat=json';
szUrl += '&cql_filter=' + cqlFilter;
$.ajax({ url : szUrl,
type : 'GET',
dataType : 'json',
success : function (data, textStatus, jqXHR) {
var reader, aFeatures = [], props = {}, point, coords, view, size;
reader = new ol.format.GeoJSON();
aFeatures = reader.readFeatures(jqXHR.responseText);
if (aFeatures.length === 0) {
alert('WFS request returned no features.');
return;
}
// Upon success of WFS request:
// 1. Clear anything that might previously be in the vector layer
// 2. Add features returned by the WFS request to
// the vector layer's "source"
// 3. Set the vector layers's "source" to the data accumulated
// in step (2)
var i;
var vSource = oHighlightLayer.getSource();
//(1) Clear anything that might previously be in the vector layer
vSource.clear();
// (2) Add features returned by the WFS request to
// the vector layer's "source"
for (i = 0; i < aFeatures.length; i++) {
vSource.addFeature(aFeatures[i]);
}
// (3) Set the vector layers's "source" to the data accumulated
// in step (2)
oHighlightLayer.setSource(vSource);
},
error : function (qXHR, textStatus, errorThrown ) {
alert('WFS request failed.\n' +
'Status: ' + textStatus + '\n' +
'Error: ' + errorThrown);
}
});