Skip to content
Permalink
Browse files

Dont use unsafe instanceOf to check object types

fixes #240
  • Loading branch information...
raitisbe committed Jul 23, 2019
1 parent 43e30de commit 0b21685611c9e677339492196b0bc13c5ce1ae60
@@ -951,7 +951,7 @@ angular.module('hs.draw', ['hs.map', 'hs.core', 'hs.utils', 'hs.geolocation'])
function fillDrawableLayersList() {
$scope.drawable_layers = [];
angular.forEach(map.getLayers(), function (layer) {
if (layer instanceof VectorLayer && layer.getVisible() && (angular.isUndefined(layer.get('show_in_manager')) || layer.get('show_in_manager') == true) && (angular.isDefined(layer.get('title')) && layer.get('title') != '')) {
if (utils.instOf(layer, VectorLayer) && layer.getVisible() && (angular.isUndefined(layer.get('show_in_manager')) || layer.get('show_in_manager') == true) && (angular.isDefined(layer.get('title')) && layer.get('title') != '')) {
$scope.drawable_layers.push(layer);
if (layer == $scope.layer_to_select) {
$scope.selected_layer = layer.get('title');
@@ -130,7 +130,7 @@ export default ['$rootScope', 'hs.map.service', 'hs.layermanager.service', 'Core
$rootScope.$on('layermanager.layer_added', function (e, layer) {
me.prepLayerFilter(layer);

if (layer.layer instanceof VectorLayer) {
if (utils.instOf(layer.layer, VectorLayer)) {
var source = layer.layer.getSource();
console.log(source.getState());
var listenerKey = source.on('change', function (e) {
@@ -207,20 +207,20 @@ var me = {
},

processOlLayer(lyr) {
if (lyr instanceof Group) {
if (utils.instOf(lyr, Group)) {
angular.forEach(lyr.layers, function (sub_lyr) {
me.processOlLayer(sub_lyr);
})
} else {
lyr.setVisible(me.hs_map.isLayerVisible(lyr, me.hs_map.visible_layers) || lyr.getVisible());
lyr.manuallyAdded = false;
if (lyr.getSource() instanceof ImageWMS)
if (utils.instOf(lyr.getSource(), ImageWMS))
me.hs_map.proxifyLayerLoader(lyr, false);
if (lyr.getSource() instanceof TileWMS)
if (utils.instOf(lyr.getSource(), TileWMS))
me.hs_map.proxifyLayerLoader(lyr, true);
var cesium_layer = me.convertOlToCesiumProvider(lyr);
if (angular.isDefined(cesium_layer)) {
if (cesium_layer instanceof Cesium.ImageryLayer) {
if (utils.instOf(cesium_layer, Cesium.ImageryLayer)) {
me.linkOlLayerToCesiumLayer(lyr, cesium_layer);
me.viewer.imageryLayers.add(cesium_layer);
} else {
@@ -235,24 +235,24 @@ var me = {

convertOlToCesiumProvider(ol_lyr) {

if (ol_lyr.getSource() instanceof OSM) {
if (utils.instOf(ol_lyr.getSource(), OSM)) {
return new Cesium.ImageryLayer(Cesium.createOpenStreetMapImageryProvider(), {
show: ol_lyr.getVisible(),
minimumTerrainLevel: ol_lyr.minimumTerrainLevel || 15
});
} else if (ol_lyr.getSource() instanceof TileWMS)
} else if (utils.instOf(ol_lyr.getSource(), TileWMS))
return me.createTileProvider(ol_lyr);
else if (ol_lyr.getSource() instanceof ImageWMS)
else if (utils.instOf(ol_lyr.getSource(), ImageWMS))
return me.createSingleImageProvider(ol_lyr);
else if (ol_lyr.getSource() instanceof Vector)
else if (utils.instOf(ol_lyr.getSource(), Vector))
return me.createVectorDataSource(ol_lyr);
else {
if (console) console.error('Unsupported layer type for layer: ', ol_lyr, 'in Cesium converter');
}
},

createVectorDataSource(ol_lyr) {
if (ol_lyr.getSource().getFormat() instanceof KML) {
if (angular.isDefined(ol_lyr.getSource().getFormat()) && utils.instOf(ol_lyr.getSource().getFormat(), KML)) {
return Cesium.KmlDataSource.load(ol_lyr.getSource().getUrl(), {
camera: viewer.scene.camera,
canvas: viewer.scene.canvas,
@@ -1,9 +1,3 @@
import permalink from 'permalink';
import ol from 'ol';
import HsCsCamera from 'hs_cesium_camera';
import HsCsLayers from 'hs_cesium_layers';
import HsCsTime from 'hs_cesium_time';
import {transformExtent } from 'ol/proj';
import hscesiumService from './hscesium.service';

/**
@@ -1,3 +1,9 @@
import HsCsCamera from 'hs_cesium_camera';
import HsCsLayers from 'hs_cesium_layers';
import HsCsTime from 'hs_cesium_time';
import {transformExtent } from 'ol/proj';
import 'permalink.module';

export default ['config', '$rootScope', 'hs.utils.service', 'hs.map.service', 'hs.layermanager.service', 'Core', function (config, $rootScope, utils, hs_map, layer_manager_service, Core) {
var viewer;
var BING_KEY = angular.isDefined(config.cesiumBingKey) ? config.cesiumBingKey : 'Ak5NFHBx3tuU85MOX4Lo-d2JP0W8amS1IHVveZm4TIY9fmINbSycLR8rVX9yZG82';
@@ -134,7 +134,7 @@ export default ['$rootScope', 'hs.map.service', 'Core', 'hs.utils.service', 'con
var metadata = {};
var value = layer.get('dimensions').time.values;
if (angular.isArray(value)) value = value[0];
if (typeof value === 'string' || value instanceof String) {
if (typeof value === 'string' || utils.instOf(value, String)) {
value = value.replace(/\s*/g, "");

if (value.search("/") > -1) {
@@ -140,7 +140,7 @@ export default {
$scope.isLayerType = function (layer, type) {
switch (type) {
case 'wms':
return isWms(layer);
return LayMan.isWms(layer);
case 'point':
return layer.getSource().hasPoint;
case 'line':
@@ -152,10 +152,6 @@ export default {
}
}

function isWms(layer) {
return (layer.getSource() instanceof TileWMS || layer.getSource() instanceof ImageWMS);
}

$scope.setProp = function (layer, property, value) {
layer.set(property, value);
}
@@ -180,8 +176,8 @@ export default {
}
if (source.hasPoint) {
var image = style.getImage();
if (image instanceof Circle) wrapper.style.pointType = 'Circle';
else if (image instanceof RegularShape) {
if (utils.instOf(image, Circle)) wrapper.style.pointType = 'Circle';
else if (utils.instOf(image, RegularShape)) {
wrapper.style.pointPoints = image.getPoints();
wrapper.style.rotation = image.getRotation();
if (angular.isUndefined(image.getRadius2())) wrapper.style.pointType = 'Polygon';
@@ -190,7 +186,7 @@ export default {
wrapper.style.radius2 = image.getRadius2();
}
}
if (image instanceof Circle || image instanceof RegularShape) {
if (utils.instOf(image, Circle) || utils.instOf(image, RegularShape)) {
wrapper.style.radius = image.getRadius();
wrapper.style.pointFill = image.getFill().getColor();
wrapper.style.pointStroke = image.getStroke().getColor();
@@ -554,24 +550,9 @@ export default {
* @function isLayerInResolutionInterval
* @memberOf hs.layermanager.controller
* @param {Ol.layer} lyr Selected layer
* @description Test if layer (WMS) resolution is within map interval
* @description Test if layer (WMS) resolution is within map resolution interval
*/
$scope.isLayerInResolutionInterval = function (lyr) {
var src = lyr.getSource();
if (src instanceof ImageWMS || src instanceof TileWMS) {
var view = OlMap.map.getView();
var resolution = view.getResolution();
var units = map.getView().getProjection().getUnits();
var dpi = 25.4 / 0.28;
var mpu = METERS_PER_UNIT[units];
var cur_res = resolution * mpu * 39.37 * dpi;
return (lyr.getMinResolution() >= cur_res || cur_res >= lyr.getMaxResolution());
} else {
var cur_res = OlMap.map.getView().getResolution();
return lyr.getMinResolution() >= cur_res && cur_res <= lyr.getMaxResolution();

}
}
$scope.isLayerInResolutionInterval = LayMan.isLayerInResolutionInterval;

/**
* @function isLayerWithDimensions
@@ -132,8 +132,8 @@ export default ['$rootScope', 'hs.map.service', 'Core', 'hs.utils.service', 'con
};

function checkLayerHealth(layer){
var src = layer.getSource();
if (['ImageWMS', 'TileWMS'].indexOf(utils.typeName(src)) > -1) {
if (me.isWms(layer)) {
var src = layer.getSource();
if(angular.isUndefined(src.getParams().LAYERS)){
console.warn('Layer', layer, 'is missing LAYERS parameter');
}
@@ -497,7 +497,7 @@ export default ['$rootScope', 'hs.map.service', 'Core', 'hs.utils.service', 'con
source.loadTotal = 0;
source.loadError = 0;
source.loaded = true;
if (layer instanceof ImageLayer) {
if (utils.instOf(layer, ImageLayer)) {
source.on('imageloadstart', function (event) {
source.loaded = false;
source.loadCounter += 1;
@@ -516,7 +516,7 @@ export default ['$rootScope', 'hs.map.service', 'Core', 'hs.utils.service', 'con
$rootScope.$broadcast('layermanager.layer_loaded', layer);
if (!$rootScope.$$phase) $rootScope.$digest();
});
} else if (layer instanceof Tile) {
} else if (utils.instOf(layer, Tile)) {
source.on('tileloadstart', function (event) {
source.loadCounter += 1;
source.loadTotal += 1;
@@ -553,15 +553,19 @@ export default ['$rootScope', 'hs.map.service', 'Core', 'hs.utils.service', 'con
}
}

me.isWms = function(layer){
return (utils.instOf(layer.getSource(), TileWMS) || utils.instOf(layer.getSource(), ImageWMS));
}

/**
* @function isLayerInResolutionInterval
* @memberOf hs.layermanager.service
* @param {Ol.layer} lyr Selected layer
* @description Test if layer (WMS) resolution is within map interval
* @description Test if layer (WMS) resolution is within map resolution interval
*/
me.isLayerInResolutionInterval = function (lyr) {
var src = lyr.getSource();
if (src instanceof ImageWMS || src instanceof TileWMS) {
if (me.isWms(lyr)) {
var view = OlMap.map.getView();
var resolution = view.getResolution();
var units = map.getView().getProjection().getUnits();
@@ -26,9 +26,9 @@ export default ['hs.utils.service', function (utils) {
*/
getLegendUrl: function (source, layer_name) {
var source_url = "";
if (source instanceof TileWMS) {
if (utils.instOf(source, TileWMS)) {
source_url = source.getUrls()[0]
} else if (source instanceof ImageWMS) {
} else if (utils.instOf(source, ImageWMS)) {
source_url = source.getUrl()
} else {
return ""
@@ -52,7 +52,7 @@ export default ['hs.utils.service', function (utils) {
* @param {string} layer_name Name of layer for which legend is requested
*/
getLayerLegendDescriptor: function (layer) {
if (layer.getSource() instanceof TileWMS || layer.getSource() instanceof ImageWMS) {
if (utils.instOf(layer.getSource(), TileWMS) || utils.instOf(layer.getSource(), ImageWMS)) {
var subLayerLegends = layer.getSource().getParams().LAYERS.split(",");
for (var i = 0; i < subLayerLegends.length; i++) {
subLayerLegends[i] = me.getLegendUrl(layer.getSource(), subLayerLegends[i]);
@@ -1,15 +1,15 @@
import '../permalink/permalink.module';
import {DoubleClickZoom, KeyboardPan, KeyboardZoom, MouseWheelZoom, PinchRotate, PinchZoom, DragPan, DragRotate, DragZoom} from 'ol/interaction';
import { DoubleClickZoom, KeyboardPan, KeyboardZoom, MouseWheelZoom, PinchRotate, PinchZoom, DragPan, DragRotate, DragZoom } from 'ol/interaction';
import Kinetic from 'ol/Kinetic';
import Map from 'ol/Map';
import View from 'ol/View';
import {MousePosition, defaults as controlDefaults} from 'ol/control';
import {createStringXY} from 'ol/coordinate';
import { MousePosition, defaults as controlDefaults } from 'ol/control';
import { createStringXY } from 'ol/coordinate';
import { TileWMS, WMTS } from 'ol/source';
import {ImageWMS, ImageArcGISRest} from 'ol/source';
import { ImageWMS, ImageArcGISRest } from 'ol/source';
import Feature from 'ol/Feature';
import {Group} from 'ol/layer';
import {Vector} from 'ol/source';
import { Group } from 'ol/layer';
import { Vector } from 'ol/source';

/**
* This is a workaround.
@@ -23,7 +23,7 @@ Feature.prototype.getLayer = function (map) {
layer_, layersToLookFor = [];
var check = function (layer) {
var source = layer.getSource();
if (source instanceof Vector) {
if (utils.instOf(source, Vector)) {
var features = source.getFeatures();
if (features.length > 0) {
layersToLookFor.push({
@@ -34,7 +34,7 @@ Feature.prototype.getLayer = function (map) {
}
};
map.getLayers().forEach(function (layer) {
if (layer instanceof Group) {
if (utils.instOf(layer, Group)) {
layer.getLayers().forEach(check);
} else {
check(layer);
@@ -235,11 +235,11 @@ export default ['config', '$rootScope', 'hs.utils.service', '$timeout', function
if (!me.layerDuplicate(lyr)) {
lyr.setVisible(me.isLayerVisible(lyr, me.visible_layers));
lyr.manuallyAdded = false;
if (lyr.getSource() instanceof ImageWMS)
if (utils.instOf(lyr.getSource(), ImageWMS))
me.proxifyLayerLoader(lyr, false);
if (lyr.getSource() instanceof TileWMS)
if (utils.instOf(lyr.getSource(), TileWMS))
me.proxifyLayerLoader(lyr, true);
if (lyr.getSource() instanceof Vector)
if (utils.instOf(lyr.getSource(), Vector))
me.getVectorType(lyr);
me.map.addLayer(lyr);
}
@@ -252,11 +252,11 @@ export default ['config', '$rootScope', 'hs.utils.service', '$timeout', function
if (!me.layerDuplicate(lyr)) {
lyr.setVisible(me.isLayerVisible(lyr, me.visible_layers));
lyr.manuallyAdded = false;
if (lyr.getSource() instanceof ImageWMS)
if (utils.instOf(lyr.getSource(), ImageWMS))
me.proxifyLayerLoader(lyr, false);
if (lyr.getSource() instanceof TileWMS)
if (utils.instOf(lyr.getSource(), TileWMS))
me.proxifyLayerLoader(lyr, true);
if (lyr.getSource() instanceof Vector)
if (utils.instOf(lyr.getSource(), Vector))
me.getVectorType(lyr);
me.map.addLayer(lyr);
}
@@ -6,8 +6,8 @@ import { Draw } from 'ol/interaction';
import { getArea, getDistance } from 'ol/sphere';
import { transform, transformExtent } from 'ol/proj';

export default ['$rootScope', 'hs.map.service',
function ($rootScope, OlMap) {
export default ['$rootScope', 'hs.map.service', 'hs.utils.service',
function ($rootScope, OlMap, utils) {
var me = this;

var map;
@@ -119,9 +119,9 @@ export default ['$rootScope', 'hs.map.service',

for (var i = 0; i < me.sketch.length; i++) {
var geom = me.sketch[i].getGeometry();
if (geom instanceof Polygon) {
if (utils.instOf(geom, Polygon)) {
output = addMultiple(formatArea(geom), output);
} else if (geom instanceof LineString) {
} else if (utils.instOf(geom, LineString)) {
output = addMultiple(formatLength(geom), output);
}
}
@@ -1,6 +1,6 @@
import {TileWMS, WMTS} from 'ol/source';
import {ImageWMS, ImageArcGISRest} from 'ol/source';
import {Tile, Image as ImageLayer} from 'ol/layer';
import { TileWMS, WMTS } from 'ol/source';
import { ImageWMS, ImageArcGISRest } from 'ol/source';
import { Tile, Image as ImageLayer } from 'ol/layer';

export default ['$rootScope', '$http', '$sce', 'hs.query.baseService', 'hs.map.service', 'hs.utils.service', 'Core',
function ($rootScope, $http, $sce, Base, OlMap, utils, Core) {
@@ -176,11 +176,11 @@ export default ['$rootScope', '$http', '$sce', 'hs.query.baseService', 'hs.map.s

function isLayerWmsQueryable(layer) {
if (!layer.getVisible()) return false;
if (layer instanceof Tile &&
layer.getSource() instanceof TileWMS &&
if (utils.instOf(layer, Tile) &&
utils.instOf(layer.getSource(), TileWMS) &&
layer.getSource().getParams().INFO_FORMAT) return true;
if (layer instanceof ImageLayer &&
layer.getSource() instanceof ImageWMS &&
if (utils.instOf(layer, ImageLayer) &&
utils.instOf(layer.getSource(), ImageWMS) &&
layer.getSource().getParams().INFO_FORMAT) return true;
return false;
}

0 comments on commit 0b21685

Please sign in to comment.
You can’t perform that action at this time.