Skip to content

Commit

Permalink
Merge pull request #5023 from camptocamp/add_orthogonality_lines
Browse files Browse the repository at this point in the history
GSEPF-150: Add orthogonality to line drawing
  • Loading branch information
llienher committed Jul 25, 2019
2 parents 2d42608 + eabe6c7 commit 2cff32e
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 14 deletions.
4 changes: 4 additions & 0 deletions contribs/gmf/apps/desktop/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import ngeoProjEPSG21781 from 'ngeo/proj/EPSG21781.js';
import * as olBase from 'ol/index.js';
import Raven from 'raven-js/src/raven.js';
import RavenPluginsAngular from 'raven-js/plugins/angular.js';
import olSourceVector from 'ol/source/Vector.js';

if (!window.requestAnimationFrame) {
alert('Your browser is not supported, please update it or use another one. You will be redirected.\n\n'
Expand Down Expand Up @@ -123,6 +124,9 @@ exports.module = angular.module('Appdesktop', [
gmfControllersAbstractDesktopController.module.name,
]);

exports.module.value('ngeoSnappingTolerance', 20);
exports.module.value('ngeoSnappingSource', new olSourceVector());

exports.module.value('gmfContextualdatacontentTemplateUrl', 'gmf/contextualdata');
exports.module.run(/* @ngInject */ ($templateCache) => {
$templateCache.put('gmf/contextualdata', require('./contextualdata.html'));
Expand Down
1 change: 0 additions & 1 deletion contribs/gmf/src/controllers/AbstractDesktopController.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,5 +254,4 @@ exports.module.value('ngeoQueryOptions', {
exports.module.value('ngeoMeasurePrecision', 3);
exports.module.value('ngeoMeasureDecimals', 0);


export default exports;
65 changes: 57 additions & 8 deletions contribs/gmf/src/editing/Snapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import * as olBase from 'ol/index.js';
import * as olEvents from 'ol/events.js';
import olCollection from 'ol/Collection.js';
import olFormatWFS from 'ol/format/WFS.js';
import olInteractionSnap from 'ol/interaction/Snap.js';
import olInteractionSnap, {handleEvent as snapHandleEvent} from 'ol/interaction/Snap.js';
import {handleEvent as handlePointerEvent} from 'ol/interaction/Pointer.js';


/**
* The snapping service of GMF. Responsible of collecting the treeCtrls that
Expand All @@ -26,14 +28,15 @@ import olInteractionSnap from 'ol/interaction/Snap.js';
* @param {angular.$http} $http Angular $http service.
* @param {angular.$q} $q The Angular $q service.
* @param {!angular.Scope} $rootScope Angular rootScope.
* @param {!angular.$injector} $injector Angular injector.
* @param {angular.$timeout} $timeout Angular timeout service.
* @param {gmf.theme.Themes} gmfThemes The gmf Themes service.
* @param {gmf.layertree.TreeManager} gmfTreeManager The gmf TreeManager service.
* @ngInject
* @ngdoc service
* @ngname gmfSnapping
*/
const exports = function($http, $q, $rootScope, $timeout, gmfThemes,
const exports = function($http, $q, $rootScope, $injector, $timeout, gmfThemes,
gmfTreeManager) {

// === Injected services ===
Expand Down Expand Up @@ -62,6 +65,12 @@ const exports = function($http, $q, $rootScope, $timeout, gmfThemes,
*/
this.timeout_ = $timeout;

/**
* @type {!angular.$injector}
* @private
*/
this.injector_ = $injector;

/**
* @type {gmf.theme.Themes}
* @private
Expand Down Expand Up @@ -113,13 +122,39 @@ const exports = function($http, $q, $rootScope, $timeout, gmfThemes,
*/
this.ogcServers_ = null;

/**
* @type {ol.source.Vector | undefined}
* @private
*/
this.ngeoSnappingSource_ = this.injector_.get('ngeoSnappingSource') || undefined;

};


class CustomSnap extends olInteractionSnap {
constructor(options) {
super(options);
this.modifierPressed = false;
document.body.addEventListener('keydown', (evt) => {
this.modifierPressed = evt.keyCode === 17; // Ctrl key
});
document.body.addEventListener('keyup', () => {
this.modifierPressed = false;
});
this.handleEvent = (evt) => { // horrible hack
if (!this.modifierPressed) {
return snapHandleEvent.call(this, evt);
}
return handlePointerEvent.call(this, evt);
};
}
}


/**
* In order for a `ol.interaction.Snap` to work properly, it has to be added
* to the map after any draw interactions or other kinds of interactions that
* ineracts with features on the map.
* interacts with features on the map.
*
* This method can be called to make sure the Snap interactions are on top.
*
Expand All @@ -131,7 +166,7 @@ exports.prototype.ensureSnapInteractionsOnTop = function() {

let item;
for (const uid in this.cache_) {
item = this.cache_[+uid];
item = this.cache_[uid];
if (item.active) {
googAsserts.assert(item.interaction);
map.removeInteraction(item.interaction);
Expand Down Expand Up @@ -258,11 +293,11 @@ exports.prototype.registerTreeCtrl_ = function(treeCtrl) {
*/
exports.prototype.unregisterAllTreeCtrl_ = function() {
for (const uid in this.cache_) {
const item = this.cache_[+uid];
const item = this.cache_[uid];
if (item) {
item.stateWatcherUnregister();
this.deactivateItem_(item);
delete this.cache_[+uid];
delete this.cache_[uid];
}
}
};
Expand Down Expand Up @@ -384,7 +419,7 @@ exports.prototype.activateItem_ = function(item) {
const map = this.map_;
googAsserts.assert(map);

const interaction = new olInteractionSnap({
const interaction = new CustomSnap({
edge: item.snappingConfig.edge,
features: item.features,
pixelTolerance: item.snappingConfig.tolerance,
Expand Down Expand Up @@ -431,6 +466,7 @@ exports.prototype.deactivateItem_ = function(item) {
}

item.active = false;
this.refreshSnappingSource_();
};


Expand All @@ -441,7 +477,7 @@ exports.prototype.loadAllItems_ = function() {
this.mapViewChangePromise_ = null;
let item;
for (const uid in this.cache_) {
item = this.cache_[+uid];
item = this.cache_[uid];
if (item.active) {
this.loadItemFeatures_(item);
}
Expand Down Expand Up @@ -514,6 +550,7 @@ exports.prototype.loadItemFeatures_ = function(item) {
const readFeatures = new olFormatWFS().readFeatures(response.data);
if (readFeatures) {
item.features.extend(readFeatures);
this.refreshSnappingSource_();
}
});

Expand All @@ -535,6 +572,18 @@ exports.prototype.handleMapMoveEnd_ = function() {
);
};

/**
* @private
*/
exports.prototype.refreshSnappingSource_ = function() {
this.ngeoSnappingSource_.clear();
for (const uid in this.cache_) {
const item = this.cache_[uid];
if (item.active) {
this.ngeoSnappingSource_.addFeatures(item.features.getArray());
}
}
};

/**
* @typedef {Object<number, gmf.editing.Snapping.CacheItem>}
Expand Down
1 change: 0 additions & 1 deletion examples/drawfeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ exports.module = angular.module('app', [
ngeoMiscToolActivateMgr.module.name,
]);


/**
* @param {!angular.Scope} $scope Angular scope.
* @param {ol.Collection.<ol.Feature>} ngeoFeatures Collection of features.
Expand Down
13 changes: 11 additions & 2 deletions src/editing/createfeatureComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ exports.directive('ngeoCreatefeature', exports.directive_);
* @param {!angularGettext.Catalog} gettextCatalog Gettext catalog.
* @param {!angular.$compile} $compile Angular compile service.
* @param {!angular.$filter} $filter Angular filter
* @param {!angular.$injector} $injector Angular injector service.
* @param {!angular.Scope} $scope Scope.
* @param {!angular.$timeout} $timeout Angular timeout service.
* @param {!ngeo.misc.EventHelper} ngeoEventHelper Ngeo event helper service
Expand All @@ -89,7 +90,7 @@ exports.directive('ngeoCreatefeature', exports.directive_);
* @ngdoc controller
* @ngname ngeoCreatefeatureController
*/
exports.Controller_ = function(gettextCatalog, $compile, $filter, $scope,
exports.Controller_ = function(gettextCatalog, $compile, $filter, $injector, $scope,
$timeout, ngeoEventHelper) {

/**
Expand Down Expand Up @@ -152,6 +153,12 @@ exports.Controller_ = function(gettextCatalog, $compile, $filter, $scope,
*/
this.ngeoEventHelper_ = ngeoEventHelper;

/**
* @type {!angular.$injector}
* @private
*/
this.injector_ = $injector;

/**
* The draw or measure interaction responsible of drawing the vector feature.
* The actual type depends on the geometry type.
Expand Down Expand Up @@ -201,7 +208,9 @@ exports.Controller_.prototype.$onInit = function() {
{
style: new olStyleStyle(),
startMsg: this.compile_(`<div translate>${helpMsg}</div>`)(this.scope_)[0],
continueMsg: this.compile_(`<div translate>${contMsg}</div>`)(this.scope_)[0]
continueMsg: this.compile_(`<div translate>${contMsg}</div>`)(this.scope_)[0],
tolerance: this.injector_.has('ngeoSnappingTolerance') ? this.injector_.get('ngeoSnappingTolerance') : undefined,
source: this.injector_.has('ngeoSnappingSource') ? this.injector_.get('ngeoSnappingSource') : undefined,
}
);
} else if (this.geomType === ngeoGeometryType.POLYGON ||
Expand Down

0 comments on commit 2cff32e

Please sign in to comment.