522 lines
12 KiB
JavaScript
522 lines
12 KiB
JavaScript
import GisApp from './gis.core';
|
|
import Draw from 'ol/interaction/Draw.js';
|
|
import Modify from 'ol/interaction/Modify.js';
|
|
import Collection from 'ol/Collection.js';
|
|
import Snap from 'ol/interaction/Snap.js';
|
|
import { click, always } from 'ol/events/condition';
|
|
import UndoRedo from 'ol-ext/interaction/UndoRedo.js';
|
|
import Transform from 'ol-ext/interaction/Transform.js';
|
|
import { gAlert } from './util';
|
|
import VectorSource from 'ol/source/Vector.js';
|
|
import VectorLayer from 'ol/layer/Vector';
|
|
|
|
const GisOlEditLayer = function (options) {
|
|
this.initialize(options);
|
|
};
|
|
|
|
GisOlEditLayer.prototype = {
|
|
defaults: {},
|
|
editFlag: false,
|
|
draw: null,
|
|
modify: null,
|
|
snap: null,
|
|
undoredo: null,
|
|
transform: null,
|
|
drawType: {
|
|
point: 'Point',
|
|
line: 'MultiLineString',
|
|
poligon: 'MultiPolygon',
|
|
},
|
|
selectedLayerId: null, //셀렉트 박스에서 선택된 레이어 ID
|
|
targetElement: null,
|
|
editFeatureInfo: {},
|
|
addFeatureNo: 1,
|
|
initialize: function (options) {
|
|
Object.assign(this.defaults, options);
|
|
},
|
|
/**
|
|
* [공통] Interaction 추가
|
|
*/
|
|
addInteration: function () {
|
|
this.setUndoInteraction();
|
|
},
|
|
/**
|
|
* [공통] Interaction 삭제
|
|
*/
|
|
removeInteraction: function () {
|
|
this.removeDraw();
|
|
this.removeModify();
|
|
this.removeSnap();
|
|
this.clearUndoRedoFeature();
|
|
},
|
|
/**
|
|
* [공통] Interaction Active 컨트롤
|
|
*/
|
|
setInteractionActive: function (active) {
|
|
this.setDrawActive(active);
|
|
this.setModifyActive(active);
|
|
this.setActiveUndo(active);
|
|
},
|
|
/**
|
|
* [그리기] 기능 추가
|
|
*/
|
|
setDraw: function (lyrId) {
|
|
if (!lyrId) return '레이어를 선택해주십시오.';
|
|
const core = GisApp.Module.core;
|
|
const layer = core.getLayerById(lyrId);
|
|
const layerCode = GisApp.layerCode[lyrId];
|
|
if (!layer) return '트리에서 레이어 체크 후 추가 가능합니다.';
|
|
const lyrTy = layerCode.gobjTy;
|
|
|
|
//초기화
|
|
this.removeModify();
|
|
this.removeTransform();
|
|
if (GisApp.Module.select) {
|
|
GisApp.Module.select.setActive(false);
|
|
}
|
|
|
|
const originalSource = layer.getSource();
|
|
const drawSource = new VectorSource({
|
|
features: originalSource.getFeatures(), // 기존 feature들 복사
|
|
});
|
|
|
|
// 새로운 임시 레이어 추가 (draw 전용)
|
|
const drawLayer = new VectorLayer({
|
|
source: drawSource,
|
|
minZoom: 1, // 이 레이어만 minZoom을 1로 설정
|
|
style: layer.getStyle(),
|
|
});
|
|
core.map.addLayer(drawLayer);
|
|
|
|
//그리기 기능 추가
|
|
const me = this;
|
|
|
|
if (me.draw) {
|
|
core.map.removeInteraction(me.draw);
|
|
}
|
|
const drawType = this.drawType[lyrTy];
|
|
this.draw = new Draw({
|
|
type: drawType,
|
|
source: drawSource,
|
|
condition: function (evt) {
|
|
return true;
|
|
},
|
|
});
|
|
core.map.addInteraction(this.draw);
|
|
|
|
//그리기 기능 이벤트
|
|
this.draw.on('drawstart', function (event) {});
|
|
|
|
const drawFeatureCallback = function (event) {
|
|
me.setActiveUndo(true);
|
|
const drawFeature = event.feature;
|
|
drawFeature.setId(lyrId + '_feature_' + me.addFeatureNo++);
|
|
|
|
const properties = {};
|
|
properties['lyr_id'] = lyrId;
|
|
drawFeature.setProperties(properties);
|
|
|
|
me.setAddFeatureUndo(layer, drawFeature);
|
|
me.setActiveUndo(false);
|
|
};
|
|
|
|
this.draw.on('drawend', function (event) {
|
|
drawFeatureCallback(event);
|
|
});
|
|
|
|
/*drawSource.listeners_.addfeature = null;
|
|
let addfeatureCallback = function(event){
|
|
}
|
|
drawSource.on('addfeature', addfeatureCallback);*/
|
|
},
|
|
/**
|
|
* [그리기] 기능 제거
|
|
*/
|
|
removeDraw: function () {
|
|
let core = GisApp.Module.core;
|
|
core.map.removeInteraction(this.draw);
|
|
if (GisApp.Module.select) {
|
|
GisApp.Module.select.setActive(true);
|
|
}
|
|
},
|
|
/**
|
|
* [그리기] 기능 활성화
|
|
*/
|
|
setDrawActive: function (active) {
|
|
if (this.draw) {
|
|
this.draw.setActive(active);
|
|
if (!active) {
|
|
/* $('#selectEditLayer').css('display', 'none');
|
|
$('.btn_edit_add').removeClass('active'); */
|
|
}
|
|
}
|
|
},
|
|
/**
|
|
* [히스토리] UndoInteraction 추가
|
|
*/
|
|
setUndoInteraction: function () {
|
|
let core = GisApp.Module.core;
|
|
if (!this.undoredo) {
|
|
this.undoredo = new UndoRedo();
|
|
core.map.addInteraction(this.undoredo);
|
|
}
|
|
},
|
|
/**
|
|
* [히스토리] UndoInteraction 제거
|
|
*/
|
|
removeUndoInteraction: function () {
|
|
let core = GisApp.Module.core;
|
|
if (this.undoredo) {
|
|
core.map.removeInteraction(this.undoredo);
|
|
this.undoredo = null;
|
|
}
|
|
},
|
|
/**
|
|
* [히스토리] UndoInteraction 활성화
|
|
*/
|
|
setActiveUndo: function (active) {
|
|
if (this.undoredo) {
|
|
this.undoredo.setActive(active);
|
|
}
|
|
},
|
|
/**
|
|
* [히스토리] 피처 추가
|
|
*/
|
|
setAddFeatureUndo: function (layer, addFeature) {
|
|
if (this.undoredo) {
|
|
let obj = {};
|
|
obj.target = layer.getSource();
|
|
obj.feature = addFeature;
|
|
obj.type = 'addfeature';
|
|
obj.feature.set('uploadType', 'addFeature');
|
|
this.undoredo._onAddRemove(obj);
|
|
}
|
|
},
|
|
/**
|
|
* [히스토리] 피처 삭제
|
|
*/
|
|
setRemoveFeatureUndo: function (layer, removeFeature) {
|
|
if (this.undoredo) {
|
|
let obj = {};
|
|
obj.target = layer.getSource();
|
|
obj.feature = removeFeature;
|
|
obj.type = 'removefeature';
|
|
if (obj.feature.get('uploadType') !== 'addFeature') {
|
|
obj.feature.set('uploadType', 'removeFeature');
|
|
}
|
|
this.undoredo._onAddRemove(obj);
|
|
}
|
|
},
|
|
/**
|
|
* [히스토리] 이전
|
|
*/
|
|
undo: function () {
|
|
if (this.undoredo) {
|
|
if (this.undoredo.hasUndo() === 0) {
|
|
gAlert('INFO', 'NO_PREV_HISTORY');
|
|
return;
|
|
}
|
|
this.setActiveUndo(true);
|
|
this.undoredo.undo();
|
|
this.setActiveUndo(false);
|
|
}
|
|
},
|
|
/**
|
|
* [히스토리] 다음
|
|
*/
|
|
redo: function () {
|
|
if (this.undoredo) {
|
|
if (this.undoredo.hasRedo() === 0) {
|
|
gAlert('INFO', 'NO_NEXT_HISTORY');
|
|
return;
|
|
}
|
|
this.setActiveUndo(true);
|
|
this.undoredo.redo();
|
|
this.setActiveUndo(false);
|
|
}
|
|
},
|
|
/**
|
|
* [히스토리] 초기화
|
|
*/
|
|
clearUndoRedo: function () {
|
|
if (this.undoredo) {
|
|
const undoStack = this.undoredo._undoStack.array_;
|
|
for (let i in undoStack) {
|
|
const undo = undoStack[i];
|
|
if (undo.feature) {
|
|
undo.feature.set('uploadType', null);
|
|
}
|
|
}
|
|
this.undoredo.clear();
|
|
}
|
|
},
|
|
/**
|
|
* [히스토리] 피처 초기화
|
|
*/
|
|
clearUndoRedoFeature: function () {
|
|
if (this.undoredo) {
|
|
this.setActiveUndo(true);
|
|
const undoStack = [...this.undoredo._undoStack.array_];
|
|
for (let i in undoStack) {
|
|
const undo = undoStack[i];
|
|
if (undo.feature) {
|
|
this.undoredo.undo();
|
|
}
|
|
}
|
|
this.setActiveUndo(false);
|
|
this.undoredo.clear();
|
|
}
|
|
},
|
|
/**
|
|
* [삭제] 피처 삭제
|
|
*/
|
|
deleteFeature: function () {
|
|
//초기화
|
|
this.removeDraw();
|
|
this.removeModify();
|
|
this.removeTransform();
|
|
|
|
//삭제 기능 실행
|
|
let me = this;
|
|
let core = GisApp.Module.core;
|
|
let select = GisApp.Module.select;
|
|
select.getSelectedFeatures().forEach(function (selectedFeature) {
|
|
let selectedFeatureId = selectedFeature.getId();
|
|
let layers = core.map.getLayers();
|
|
for (let i in layers.array_) {
|
|
let layer = layers.array_[i];
|
|
if (layer.get('mdfcnYn') === 'Y' && select.getVisible(layer)) {
|
|
layer
|
|
.getSource()
|
|
.getFeatures()
|
|
.forEach(function (feature) {
|
|
let featureId = feature.getId();
|
|
if (selectedFeatureId === featureId) {
|
|
me.setActiveUndo(true);
|
|
feature.setProperties({ lyr_id: layer.get('id') });
|
|
layer.getSource().removeFeature(feature);
|
|
me.setRemoveFeatureUndo(layer, feature);
|
|
me.setActiveUndo(false);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
});
|
|
},
|
|
/**
|
|
* [수정] 피처 수정 기능 셋팅
|
|
*/
|
|
setModify: function () {
|
|
//초기화
|
|
this.removeDraw();
|
|
this.removeTransform();
|
|
|
|
//수정기능 추가
|
|
let me = this;
|
|
let core = GisApp.Module.core;
|
|
let select = GisApp.Module.select;
|
|
let selectedFeatures = select.getSelectedFeatures();
|
|
|
|
this.modify = new Modify({
|
|
features: selectedFeatures,
|
|
});
|
|
core.map.addInteraction(this.modify);
|
|
|
|
select.selectInteraction.on('change:active', function () {
|
|
selectedFeatures.forEach(function (each) {
|
|
selectedFeatures.remove(each);
|
|
});
|
|
});
|
|
|
|
this.modify.on('modifystart', function (event) {});
|
|
|
|
this.modify.on('modifyend', function (event) {
|
|
let features = event.features.getArray();
|
|
for (let i = 0; i < features.length; i++) {
|
|
let feature = features[i];
|
|
let layer = select.selectInteraction.getLayer(feature);
|
|
feature.setProperties({ lyr_id: layer.get('id') });
|
|
}
|
|
});
|
|
},
|
|
/**
|
|
* [수정] 피처 수정 기능 제거
|
|
*/
|
|
removeModify: function () {
|
|
if (this.modify) {
|
|
let core = GisApp.Module.core;
|
|
core.map.removeInteraction(this.modify);
|
|
this.modify = null;
|
|
}
|
|
},
|
|
/**
|
|
* [수정] 기능 활성화
|
|
*/
|
|
setModifyActive: function (active) {
|
|
if (this.modify) {
|
|
this.modify.setActive(active);
|
|
}
|
|
},
|
|
/**
|
|
* [크기조절] 피처 크기조절 기능 셋팅
|
|
*/
|
|
setTransform: function () {
|
|
//초기화
|
|
this.removeDraw();
|
|
this.removeModify();
|
|
|
|
let me = this;
|
|
let core = GisApp.Module.core;
|
|
let select = GisApp.Module.select;
|
|
|
|
this.transform = new Transform({
|
|
enableRotatedTransform: false,
|
|
addCondition: click,
|
|
filter: function (f, l) {
|
|
return f.getGeometry().getType() !== 'Point';
|
|
},
|
|
// layers: [vector],
|
|
hitTolerance: select.defaults.hitTolerance,
|
|
translateFeature: true,
|
|
scale: true,
|
|
rotate: true,
|
|
keepAspectRatio: always,
|
|
translate: true,
|
|
stretch: true,
|
|
});
|
|
this.transform.on(['rotateend', 'translateend', 'scaleend'], function (event) {
|
|
let features = event.features.getArray();
|
|
for (let i = 0; i < features.length; i++) {
|
|
let feature = features[i];
|
|
let layer = feature.getLayer();
|
|
feature.setProperties({ lyr_id: layer.get('id') });
|
|
}
|
|
});
|
|
|
|
core.map.addInteraction(this.transform);
|
|
},
|
|
/**
|
|
* [크기조절] 피처 크기조절 기능 제거
|
|
*/
|
|
removeTransform: function () {
|
|
if (this.transform) {
|
|
let core = GisApp.Module.core;
|
|
core.map.removeInteraction(this.transform);
|
|
this.transform = null;
|
|
}
|
|
},
|
|
/**
|
|
* [크기조절] 피처 크기조절 기능 활성화
|
|
*/
|
|
setTransformActive: function (active) {
|
|
if (this.transform) {
|
|
this.transform.setActive(active);
|
|
}
|
|
},
|
|
/**
|
|
* [스냅] 기능 추가
|
|
*/
|
|
setSnap: function (active) {
|
|
//초기화
|
|
this.removeSnap();
|
|
|
|
//스냅 기능 추가
|
|
let core = GisApp.Module.core;
|
|
let select = GisApp.Module.select;
|
|
core.map.removeInteraction(this.snap);
|
|
let features = [];
|
|
let layers = core.map.getLayers();
|
|
for (let i in layers.array_) {
|
|
let layer = layers.array_[i];
|
|
if (select.getVisible(layer) && layer.get('type') !== 'base') {
|
|
if (layer.getSource().getFeatures().length > 0) {
|
|
features = features.concat(layer.getSource().getFeatures());
|
|
}
|
|
}
|
|
}
|
|
let snapCollection = new Collection(features);
|
|
this.snap = new Snap({
|
|
features: snapCollection,
|
|
pixelTolerance: 30,
|
|
});
|
|
core.map.addInteraction(this.snap);
|
|
},
|
|
/**
|
|
* [스냅] 기능 제거
|
|
*/
|
|
removeSnap: function () {
|
|
if (this.snap) {
|
|
let core = GisApp.Module.core;
|
|
core.map.removeInteraction(this.snap);
|
|
this.snap = null;
|
|
}
|
|
},
|
|
/**
|
|
* [스냅] 스냅에 피처 추가
|
|
*/
|
|
addSnap: function (layers) {
|
|
let me = this;
|
|
if (me.snap) {
|
|
for (let i in layers) {
|
|
let source = layers[i].getSource();
|
|
source.listeners_.addfeature = null;
|
|
source.on('addfeature', function (event) {
|
|
me.snap.addFeature(event.feature);
|
|
});
|
|
}
|
|
}
|
|
},
|
|
/**
|
|
* [스냅] 기능 활성화
|
|
*/
|
|
setSnapActive: function (active) {
|
|
if (this.snap) {
|
|
this.snap.setActive(active);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* [저장] 피처 수정 정보 셋팅
|
|
*/
|
|
setEditFeatureInfo: function (flag, feature) {
|
|
const featureId = feature.getId();
|
|
const properties = feature.getProperties();
|
|
|
|
//신규 추가 후 삭제시에 editFeatureInfo에서 제거
|
|
if (properties.uploadType === 'addFeature' && flag === 'delete') {
|
|
delete this.editFeatureInfo[featureId];
|
|
return;
|
|
}
|
|
|
|
//좌표값 WKT포멧으로 변경
|
|
const pGeom = GisApp.Module.core.convertFeatureGeomToWKT(feature);
|
|
const param = {
|
|
flag,
|
|
...properties,
|
|
geom: pGeom,
|
|
};
|
|
this.editFeatureInfo[featureId] = param;
|
|
},
|
|
getEditFeatureInfo: function () {
|
|
const undoStack = this.undoredo._undoStack.array_;
|
|
this.editFeatureInfo = {};
|
|
for (let i in undoStack) {
|
|
const undo = undoStack[i];
|
|
if (undo.feature) {
|
|
const feature = undo.feature;
|
|
const properties = feature.getProperties();
|
|
if (properties.uploadType === 'addFeature') {
|
|
this.setEditFeatureInfo('insert', feature);
|
|
} else if (properties.uploadType === 'removeFeature') {
|
|
this.setEditFeatureInfo('delete', feature);
|
|
} else if (undo.type === 'changegeometry') {
|
|
this.setEditFeatureInfo('update', feature);
|
|
}
|
|
}
|
|
}
|
|
return this.editFeatureInfo;
|
|
},
|
|
};
|
|
|
|
export default GisOlEditLayer;
|