Skip to content

Commit

Permalink
nunaliit2-js: On map canvas, set minimum sizes for polygons and lines.
Browse files Browse the repository at this point in the history
Smaller geometries are turned into points.

Issue #637
  • Loading branch information
jpfiset committed Mar 21, 2017
1 parent 07c088b commit 3fb96b4
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
8 changes: 8 additions & 0 deletions nunaliit2-js/src/main/js/nunaliit2/n2.couchModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
19 changes: 19 additions & 0 deletions nunaliit2-js/src/main/js/nunaliit2/n2.mapAndControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
117 changes: 117 additions & 0 deletions nunaliit2-js/src/main/js/nunaliit2/n2.olFeatureStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<features.length; ++i) {
feature = features[i];
if( this._shouldTurnToPoint(feature, resolution) ){
var center = feature.geometry.getBounds().getCenterLonLat();
var pointGeom = new OpenLayers.Geometry.Point(center.lon, center.lat);

feature.n2MinSizeGeom = feature.geometry;
feature.geometry = pointGeom;

} else if( this._shouldRestore(feature, resolution) ){
var originalGeom = feature.n2MinSizeGeom;
feature.geometry = originalGeom;
delete feature.n2MinSizeGeom;
};
};

return features;
},

_shouldTurnToPoint: function(feature, resolution) {
// By default, leave as is
var eligible = false;

if( feature.geometry.CLASS_NAME.indexOf('Line') >= 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({
/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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_);
},
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions nunaliit2-js/src/main/js/nunaliit2/n2.utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ var SelectDocumentOnModuleIntroduction = $n2.Class({
// {
// "utilityType": "selectDocumentOnFilterChange"
// ,"sourceModelId": "filterDocsByLayer"
// ,"performSelectedEvent": false
// ,"selectionMap": {
// "123": "public"
// ,"456": "approved"
Expand Down

0 comments on commit 3fb96b4

Please sign in to comment.