「DotSpacial」を使って図形のコピー及び削除を行う場合のメモです。
図形の編集は「DotSpatial.Plugins.ShapeEditor」などのプラグインを参考にし、
事前にボタンを用意しておきます。
図形のコピー処理
1.図形のコピー
「コピー」ボタンクリック時に、選択図形を保持しておきます。
1 2 3 4 5 6 7 8 9 |
private void CopyClick(object sender, EventArgs e) { var selectedFeature = this.GetSelectedFeature(); if (selectedFeature != null) { this.copyFeature = selectedFeature; this.btnPaste.Enabled = true; } } |
2.選択図形の貼り付け
「貼り付け」ボタンクリック時に、選択図形をレイヤーに追加します。
レイヤーの追加には、FeatureSet.CopySubset()メソッドを使って選択図形をコピーします。
このメソッドを使用しなければコピーができませんでした。
また、コピーした後は、同じ位置だと結果が分かりにくいので、
AffineTransformationクラスを使って、XY座標ともに1mずつ相対移動さています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
private void PasteClick(object sender, EventArgs e) { if (this.copyFeature != null) { var activeLayer = this.GetActiveLayer(); if (activeLayer != null) { var indices = new List() { this.copyFeature.Fid }; var newFeature = activeLayer.DataSet.CopySubset(indices).Features[0]; var trans = AffineTransformation.TranslationInstance(1, 1); newFeature.Geometry = trans.Transform(newFeature.Geometry); this.App.Map.MapFrame.Initialize(); this.copyFeature = null; this.btnCopy.Enabled = false; this.btnPaste.Enabled = false; this.btnDel.Enabled = false; } } } |
処理イメージは以下の通り。
- コピー前
- コピー後
図形の削除処理
1.図形の削除
「削除」ボタンクリック時に、選択図形を削除します。
レイヤから削除するには、FeatureSet.RemoveShapeAt()メソッドを使います。
引数のindexは、Featuer.Fidを使用します。
このFidは、DotSpatialで、shapeファイル読み込み時に、属性に振られるIDで、
様々な処理で利用されます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
private void PasteClick(object sender, EventArgs e) { if (this.copyFeature != null) { var activeLayer = this.GetActiveLayer(); if (activeLayer != null) { var indices = new List() { this.copyFeature.Fid }; var fset = activeLayer.DataSet.CopySubset(indices); activeLayer.DataSet.Features.Add(fset.Features[0]); this.App.Map.MapFrame.Initialize(); this.copyFeature = null; this.btnCopy.Enabled = false; this.btnPaste.Enabled = false; this.btnDel.Enabled = false; } } } |
処理イメージは以下の通り。
- 削除前
- 削除後