Retour à la documentation
/**
* @class spw.widgets.SpwSegmentationSearch
*/
define([
'dojo/_base/declare', 'dojo/_base/lang', 'dojo/_base/array',
'spw/api/SpwBaseTemplatedWidget', 'dojo/text!./templates/SpwSegmentationSearch.html',
'dojo/on', 'dojo/store/Memory', 'spw/api/MessageManager', 'esri/request',
'spw/api/GeometryConverter', 'spw/api/ProjectionManager', 'esri/graphic',
'esri/SpatialReference', 'dojo/dom-style',
'dijit/form/Form', 'dijit/form/FilteringSelect', 'dijit/form/Button', 'dijit/form/NumberSpinner'
], function(declare, lang, array, _Templated, template, on, Memory, MessageManager, request,
GeometryConverter, ProjectionManager, Graphic, SpatialReference, domStyle) {
return declare('spw.widgets.SpwSegmentationSearch', _Templated, /** @lends spw.widgets.SpwSegmentationSearch.prototype */ {
getSegmentsUrl: '//geoservices.wallonie.be/segmentation/rest/getListeSegments/1?d=1',
getSegmentUrl: '//geoservices.wallonie.be/segmentation/rest/getSegment/{road}/{bk1}/{bk2}/1?d=1',
getNearestSegmentUrl: '//geoservices.wallonie.be/segmentation/rest/getNearestSegment/{x}/{y}/1?d=1',
allowSelectionFromMap: true,
labelProperty: 'simplifiedCode',
apiSRID: 31370,
useCors: true,
closeOnZoom: true,
startBkLabel: 'Borne de début (km)',
endBkLabel: 'Borne de fin (km)',
apiLoaded: false,
templateString: template,
selectingFromMap: false,
selectHandler: null,
hasCloseButton: true,
widgetTitle: "Localiser une route",
// helpContent: "http://geoportail.wallonie.be/aideWalOnMap",
closable: true,
style: {
width: "400px"
},
postCreate: function () {
this.inherited(arguments);
if (this.allowSelectionFromMap) {
domStyle.set(this._selectFromMapContainerNode, 'display', '');
}
this.getSegments();
this.own(
on(this._formNode, 'submit', lang.hitch(this, this.goButtonClicked)),
on(this._formNode, 'reset', lang.hitch(this, this.onReset)),
on(this.roadSelectNode, 'change', lang.hitch(this, this.onRoadSelected)),
on(this.bk1SelectNode, 'change', lang.hitch(this, this.onBk1Changed)),
on(this.bk2SelectNode, 'change', lang.hitch(this, this.onBk2Changed)),
on(this._selectFromMapNode, 'click', lang.hitch(this, this.selectSegmentFromMap))
);
},
selectSegmentFromMap: function() {
if (this.selectingFromMap) {
this.deactivateSelectionFromMap();
}
else {
this.selectHandler = on(this.spwViewer.get('spwMap'), 'MapClicked', lang.hitch(this, function(x, y, srid) {
if (srid != this.apiSRID) {
var projected = ProjectionManager.getInstance().projectPoint(srid, this.apiSRID, x, y);
x = projected.x;
y = projected.y;
}
this.getNearestSegment(x, y);
}));
domStyle.set(this._selectFromMapLabelNode, 'display', '');
this.selectingFromMap = true;
}
},
deactivateSelectionFromMap: function() {
this.selectHandler && this.selectHandler.remove();
domStyle.set(this._selectFromMapLabelNode, 'display', 'none');
this.selectingFromMap = false;
},
getNearestSegment: function(x, y) {
request({
url: lang.replace(this.getNearestSegmentUrl, {
x: Math.round(x),
y: Math.round(y)
}),
preventCache: true,
handleAs: 'json',
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
}).then(lang.hitch(this, function(result) {
this.deactivateSelectionFromMap();
if (result.errorCode !== 0) {
console.error(result);
MessageManager.getInstance().notifyError(result.errorMsg);
return;
}
var segment = result.nearestSegment;
if (segment == null) {
console.error('segment null');
MessageManager.getInstance().notifyError('Impossible de récupérer le segment (erreur serveur)');
return;
}
this.roadSelectNode.set('value', segment.code);
}), lang.hitch(this, function(err) {
console.error(err);
MessageManager.getInstance().notifyError('Impossible de récupérer le segment (erreur serveur)');
}))
},
goButtonClicked: function() {
if (!this._formNode.validate()) {
return false;
}
var values = this._formNode.get('value');
var item = this.roadSelectNode.get('item');
if (values.bk1 == null || !isFinite(values.bk1)) {
values.bk1 = Math.floor(item.cumulee_start / 1000);
if (values.bk2 == null || !isFinite(values.bk2)) {
values.bk2 = Math.ceil(item.cumulee_end / 1000);
}
}
else if (values.bk2 == null || !isFinite(values.bk2)) {
values.bk2 = values.bk1;
}
request({
url: lang.replace(this.getSegmentUrl, values),
preventCache: true,
handleAs: 'json',
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
}, {
useProxy: this.useCors !== true
}).then(lang.hitch(this, function(result) {
if (result.errorCode !== 0) {
MessageManager.getInstance().notifyError('Impossible de récupérer le segment');
console.error(result.errorMsg);
return;
}
var segment = result.segment;
var geometry = GeometryConverter.wktToEsri(segment.geometry);
geometry.setSpatialReference(new SpatialReference({wkid: this.apiSRID}));
if (this.apiSRID !== this.spwViewer.spwMap.getSpatialReferenceSRID()) {
geometry = ProjectionManager.getInstance().transform(this.apiSRID, this.spwViewer.spwMap.getSpatialReferenceSRID(), geometry);
}
this.clearFeature();
this.currentFeature = new Graphic(geometry);
this.spwViewer.spwMap.highlightFeature(this.currentFeature);
this.spwViewer.spwMap.zoomToFeature(this.currentFeature);
this.closeOnZoom && this.closeWidget();
}), lang.hitch(this, function(err) {
console.error(err);
}));
return false;
},
clearFeature: function() {
if (this.currentFeature) {
this.spwViewer.spwMap.removeFeature(this.currentFeature);
this.currentFeature = null;
}
},
onReset: function() {
this.clearFeature();
this.bk1SelectNode.constraints.max = null;
this.bk2SelectNode.constraints.min = null;
this.deactivateSelectionFromMap();
},
onBk1Changed: function() {
var val = this.bk1SelectNode.get('value');
if (val == null || !isFinite(val)) {
delete this.bk2SelectNode.constraints.min;
}
else {
this.bk2SelectNode.constraints.min = val;
}
},
onBk2Changed: function() {
var val = this.bk2SelectNode.get('value');
if (val == null || !isFinite(val)) {
delete this.bk1SelectNode.constraints.max;
}
else {
this.bk1SelectNode.constraints.max = val;
}
},
onRoadSelected: function() {
var item = this.roadSelectNode.get('item');
if (item == null) {
return;
}
var start = Math.floor(item.cumulee_start / 1000);
var end = Math.ceil(item.cumulee_end / 1000);
this.bk1SelectNode.constraints.min = start;
if (isNaN(this.bk2SelectNode.get('value'))) {
this.bk1SelectNode.constraints.max = end;
}
if (isNaN(this.bk1SelectNode.get('value'))) {
this.bk2SelectNode.constraints.min = start;
}
this.bk2SelectNode.constraints.max = end;
this.bk1SelectNode.set('value', start);
this.bk2SelectNode.set('value', end);
},
getSegments: function() {
request({
url: this.getSegmentsUrl,
preventCache: true,
handleAs: 'json',
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
}).then(lang.hitch(this, this.onSegmentsSuccess), lang.hitch(this, function(err) {
MessageManager.getInstance().notifyError('Impossible de récupérer les segments');
console.error(err);
}));
},
onSegmentsSuccess: function(result) {
if (result.errorCode !== 0) {
MessageManager.getInstance().notifyError('Impossible de récupérer les segments');
console.error(result.errorMsg);
return;
}
var segments = result.segments;
this.roadSelectNode.set('store', new Memory({
data: segments,
idProperty: 'code'
}));
},
closeWidget: function() {
var p = this.getParent();
if (p && p.spwParentMenu && (p = p.spwParentMenu.getParent())) {
if (p.spwParentMenu && (p = p.spwParentMenu.getParent())) {
p._button.closeDropDown();
}
}
}
});
});