From 3fb96b4a72b2f3f8d087dbe20d6ef465d161f6f4 Mon Sep 17 00:00:00 2001 From: jpfiset Date: Mon, 20 Mar 2017 22:45:44 -0400 Subject: [PATCH] nunaliit2-js: On map canvas, set minimum sizes for polygons and lines. Smaller geometries are turned into points. Issue #637 --- .../src/main/js/nunaliit2/n2.couchModule.js | 8 ++ .../main/js/nunaliit2/n2.mapAndControls.js | 19 +++ .../main/js/nunaliit2/n2.olFeatureStrategy.js | 117 ++++++++++++++++++ .../src/main/js/nunaliit2/n2.utilities.js | 1 + 4 files changed, 145 insertions(+) diff --git a/nunaliit2-js/src/main/js/nunaliit2/n2.couchModule.js b/nunaliit2-js/src/main/js/nunaliit2/n2.couchModule.js index 2055aa223..56c591c03 100644 --- a/nunaliit2-js/src/main/js/nunaliit2/n2.couchModule.js +++ b/nunaliit2-js/src/main/js/nunaliit2/n2.couchModule.js @@ -1179,6 +1179,14 @@ var ModuleDisplay = $n2.Class({ if( layerInfo.clustering ){ layerDefinition.clustering = layerInfo.clustering; }; + + if( typeof layerInfo.minimumLinePixelSize === 'number' ){ + layerDefinition.minimumLinePixelSize = layerInfo.minimumLinePixelSize; + }; + + if( typeof layerInfo.minimumPolygonPixelSize === 'number' ){ + layerDefinition.minimumPolygonPixelSize = layerInfo.minimumPolygonPixelSize; + }; // Add layer to map mapOptions.overlays.push( layerDefinition ); diff --git a/nunaliit2-js/src/main/js/nunaliit2/n2.mapAndControls.js b/nunaliit2-js/src/main/js/nunaliit2/n2.mapAndControls.js index 21011cf98..cdecd7f74 100644 --- a/nunaliit2-js/src/main/js/nunaliit2/n2.mapAndControls.js +++ b/nunaliit2-js/src/main/js/nunaliit2/n2.mapAndControls.js @@ -2289,8 +2289,27 @@ var MapAndControls = $n2.Class({ }; layerInfo.featureStrategy = new OpenLayers.Strategy.NunaliitFeatureStrategy(); layerOptions.strategies.push( layerInfo.featureStrategy ); + if( typeof layerDefinition.minimumLinePixelSize === 'number' + || typeof layerDefinition.minimumPolygonPixelSize === 'number' ){ + var minimumSizeOptions = {}; + if( typeof layerDefinition.minimumLinePixelSize === 'number' ){ + minimumSizeOptions.minimumLinePixelSize = layerDefinition.minimumLinePixelSize; + }; + if( typeof layerDefinition.minimumPolygonPixelSize === 'number' ){ + minimumSizeOptions.minimumPolygonPixelSize = layerDefinition.minimumPolygonPixelSize; + }; + layerInfo.featureStrategy.setMinimumSize(minimumSizeOptions); + }; if( layerInfo.clustering ) { var clusterOptions = {}; + + if( typeof layerDefinition.minimumLinePixelSize === 'number' ){ + clusterOptions.minimumLinePixelSize = layerDefinition.minimumLinePixelSize; + }; + if( typeof layerDefinition.minimumPolygonPixelSize === 'number' ){ + clusterOptions.minimumPolygonPixelSize = layerDefinition.minimumPolygonPixelSize; + }; + for(var cProp in layerInfo.clustering){ var cValue = layerInfo.clustering[cProp]; if( 'distance' === cProp diff --git a/nunaliit2-js/src/main/js/nunaliit2/n2.olFeatureStrategy.js b/nunaliit2-js/src/main/js/nunaliit2/n2.olFeatureStrategy.js index 7182eab52..e9542cdb2 100644 --- a/nunaliit2-js/src/main/js/nunaliit2/n2.olFeatureStrategy.js +++ b/nunaliit2-js/src/main/js/nunaliit2/n2.olFeatureStrategy.js @@ -105,6 +105,108 @@ var Filtering = $n2.Class({ } }); +//+++++++++++++++++++++++++++++++++++++++++++++++ +var MinimumSizeStrategy = $n2.Class({ + + minimumLinePixelSize: 20, + + minimumPolygonPixelSize: 20, + + initialize: function(opts_){ + var opts = $n2.extend({ + minimumLinePixelSize: 20 + ,minimumPolygonPixelSize: 20 + },opts_); + + this.minimumLinePixelSize = 20; + if( typeof opts.minimumLinePixelSize === 'number' ){ + this.minimumLinePixelSize = opts.minimumLinePixelSize; + }; + + this.minimumPolygonPixelSize = 20; + if( typeof opts.minimumPolygonPixelSize === 'number' ){ + this.minimumPolygonPixelSize = opts.minimumPolygonPixelSize; + }; + }, + + processFeatures: function(features, resolution){ + var clusters = []; + var featuresToAdd = []; + var feature, clustered, cluster; + for(var i=0; i= 0 ){ + var bounds = feature.geometry.getBounds(); + var pixels = this._boundsToPixels(bounds, resolution); + if( pixels < this.minimumLinePixelSize ) { + eligible = true; + }; + } else if( feature.geometry.CLASS_NAME.indexOf('Polygon') >= 0 ){ + var bounds = feature.geometry.getBounds(); + var pixels = this._boundsToPixels(bounds, resolution); + if( pixels < this.minimumLinePixelSize ) { + eligible = true; + }; + }; + + return eligible; + }, + + _shouldRestore: function(feature, resolution) { + // By default, leave as is + var eligible = false; + + var geometry = feature.n2MinSizeGeom; + if( geometry ){ + if( geometry.CLASS_NAME.indexOf('Line') >= 0 ){ + var bounds = geometry.getBounds(); + var pixels = this._boundsToPixels(bounds, resolution); + if( pixels >= this.minimumPolygonPixelSize ) { + eligible = true; + }; + } else if( geometry.CLASS_NAME.indexOf('Polygon') >= 0 ){ + var bounds = geometry.getBounds(); + var pixels = this._boundsToPixels(bounds, resolution); + if( pixels >= this.minimumPolygonPixelSize ) { + eligible = true; + }; + }; + }; + + return eligible; + }, + + _boundsToPixels: function(bounds, resolution){ + var xLen = (bounds.right - bounds.left) / resolution; + var yLen = (bounds.top - bounds.bottom) / resolution; + if( xLen > yLen ){ + return xLen; + }; + return yLen; + } +}); + //+++++++++++++++++++++++++++++++++++++++++++++++ var Clustering = $n2.Class({ /** @@ -395,6 +497,12 @@ OpenLayers.Strategy.NunaliitFeatureStrategy = OpenLayers.Class(OpenLayers.Strate */ editFiltering: null, + /** + * Property: minimumSize + * {Object} Component that converts lines and polygons to points if too small + */ + minimumSize: null, + /** * Property: clustering * {Object} Component that performs clustering @@ -433,6 +541,10 @@ OpenLayers.Strategy.NunaliitFeatureStrategy = OpenLayers.Class(OpenLayers.Strate this.sorting = new Sorting(); }, + setMinimumSize: function(opts_){ + this.minimumSize = new MinimumSizeStrategy(opts_); + }, + setClustering: function(opts_){ this.clustering = new Clustering(opts_); }, @@ -562,6 +674,11 @@ OpenLayers.Strategy.NunaliitFeatureStrategy = OpenLayers.Class(OpenLayers.Strate features = this.filtering.performFiltering(features, filteredOutFeatures); }; + if( this.minimumSize ){ + var resolution = this.layer.map.getResolution(); + features = this.minimumSize.processFeatures(features, resolution); + }; + if( this.clustering ){ var resolution = this.layer.map.getResolution(); this.clustering.setResolution(resolution); diff --git a/nunaliit2-js/src/main/js/nunaliit2/n2.utilities.js b/nunaliit2-js/src/main/js/nunaliit2/n2.utilities.js index 72af9133e..ed2f7ee65 100644 --- a/nunaliit2-js/src/main/js/nunaliit2/n2.utilities.js +++ b/nunaliit2-js/src/main/js/nunaliit2/n2.utilities.js @@ -166,6 +166,7 @@ var SelectDocumentOnModuleIntroduction = $n2.Class({ // { // "utilityType": "selectDocumentOnFilterChange" // ,"sourceModelId": "filterDocsByLayer" +// ,"performSelectedEvent": false // ,"selectionMap": { // "123": "public" // ,"456": "approved"