Skip to content

Commit 5facf28

Browse files
committed
修改了重命名图层的方法,更加友好;新增启动后加载图层若有失败,会显示失败详情
1 parent eed3b85 commit 5facf28

12 files changed

+134
-106
lines changed

MapBoard.Core/Mapping/Model/MapLayerCollection.cs

Lines changed: 9 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public MapLayerInfo Selected
5252
}
5353
}
5454

55+
public ItemsOperationErrorCollection LoadErrors { get; private set; }
56+
5557
public static async Task<MapLayerCollection> GetInstanceAsync(ELayerCollection esriLayers)
5658
{
5759
string path = Path.Combine(Parameters.DataPath, LayersFileName);
@@ -74,6 +76,12 @@ await Task.Run(() =>
7476
{
7577
Debug.WriteLine($"图层{layer.Name}加载失败:{ex.Message}");
7678
instance.LayerList.Remove(layer);
79+
80+
if (instance.LoadErrors == null)
81+
{
82+
instance.LoadErrors = new ItemsOperationErrorCollection();
83+
}
84+
instance.LoadErrors.Add(new ItemsOperationError(layer.Name, ex));
7785
}
7886
}
7987

@@ -158,7 +166,7 @@ private async Task AddLayerAsync(MapLayerInfo layer, int index)
158166
{
159167
if (!layer.HasTable)
160168
{
161-
await layer.SetTableAsync(new ShapefileFeatureTable(layer.GetFilePath()));
169+
await layer.LoadAsync();
162170
}
163171
FeatureLayer fl = layer.Layer;
164172
if (index == -1)
@@ -169,8 +177,6 @@ private async Task AddLayerAsync(MapLayerInfo layer, int index)
169177
{
170178
EsriLayers.Insert(index, fl);
171179
}
172-
await Task.Run(layer.ApplyStyle);
173-
await layer.LayerCompleteAsync();
174180
}
175181
catch (Exception ex)
176182
{
@@ -188,58 +194,5 @@ private async Task AddLayerAsync(MapLayerInfo layer, int index)
188194
throw;
189195
}
190196
}
191-
192-
public async Task LoadLayersAsync()
193-
{
194-
if (!Directory.Exists(Parameters.DataPath))
195-
{
196-
Directory.CreateDirectory(Parameters.DataPath);
197-
return;
198-
}
199-
200-
foreach (var layer in LayerList.Cast<MapLayerInfo>().ToList())
201-
{
202-
if (File.Exists(Path.Combine(Parameters.DataPath, layer.Name + ".shp")))
203-
{
204-
await LoadLayerAsync(layer);
205-
}
206-
else
207-
{
208-
Remove(layer);
209-
}
210-
}
211-
return;
212-
//下面的啥东西?
213-
HashSet<string> files = Directory.EnumerateFiles(Parameters.DataPath)
214-
.Where(p => Path.GetExtension(p) == ".shp")
215-
.Select(p =>
216-
{
217-
int index = p.LastIndexOf('.');
218-
if (index == -1)
219-
{
220-
return p;
221-
}
222-
return p.Remove(index, p.Length - index).RemoveStart(Parameters.DataPath + "\\");
223-
}).ToHashSet();
224-
225-
foreach (var name in files)
226-
{
227-
if (!LayerList.Any(p => p.Name == name))
228-
{
229-
MapLayerInfo style = new MapLayerInfo();
230-
style.Name = name;
231-
await LoadLayerAsync(style);
232-
}
233-
}
234-
}
235-
236-
public async Task LoadLayerAsync(MapLayerInfo layer)
237-
{
238-
ShapefileFeatureTable featureTable = new ShapefileFeatureTable(Parameters.DataPath + "\\" + layer.Name + ".shp");
239-
await featureTable.LoadAsync();
240-
if (featureTable.LoadStatus == LoadStatus.Loaded)
241-
{
242-
}
243-
}
244197
}
245198
}

MapBoard.Core/Mapping/Model/MapLayerInfo.cs

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Esri.ArcGISRuntime.Geometry;
44
using Esri.ArcGISRuntime.Mapping;
55
using FzLib.Extension;
6+
using MapBoard.IO;
67
using MapBoard.Model;
78
using MapBoard.Util;
89
using Newtonsoft.Json;
@@ -12,6 +13,7 @@
1213
using System.Collections.ObjectModel;
1314
using System.ComponentModel;
1415
using System.Diagnostics;
16+
using System.IO;
1517
using System.Linq;
1618
using System.Threading.Tasks;
1719

@@ -42,13 +44,22 @@ public MapLayerInfo()
4244
{
4345
}
4446

45-
public MapLayerInfo(LayerInfo layer)
47+
public MapLayerInfo(string name)
4648
{
47-
new MapperConfiguration(cfg =>
48-
{
49-
cfg.CreateMap<LayerInfo, MapLayerInfo>();
50-
}).CreateMapper().Map(layer, this);
49+
Name = name;
50+
}
5151

52+
public static MapLayerInfo CreateTemplate()
53+
{
54+
return new MapLayerInfo();
55+
}
56+
57+
public MapLayerInfo(LayerInfo layer)
58+
{
59+
new MapperConfiguration(cfg =>
60+
{
61+
cfg.CreateMap<LayerInfo, MapLayerInfo>();
62+
}).CreateMapper().Map(layer, this);
5263
}
5364

5465
public override object Clone()
@@ -64,18 +75,65 @@ public override object Clone()
6475
return layer;
6576
}
6677

78+
/// <summary>
79+
/// 修改图层名,并同步修改物理文件的名称
80+
/// </summary>
81+
/// <param name="newName"></param>
82+
/// <param name="layers">Esri图层集合</param>
83+
/// <returns></returns>
84+
public async Task ChangeNameAsync(string newName, Esri.ArcGISRuntime.Mapping.LayerCollection layers)
85+
{
86+
Debug.Assert(!string.IsNullOrEmpty(newName));
87+
if (newName == Name)
88+
{
89+
return;
90+
}
91+
if (newName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0
92+
|| newName.Length > 240 || newName.Length < 1)
93+
{
94+
throw new IOException("新文件名不合法");
95+
}
96+
//检查文件存在
97+
foreach (var file in Shapefile.GetExistShapefiles(Parameters.DataPath, layer.Name))
98+
{
99+
if (File.Exists(Path.Combine(Parameters.DataPath, newName + Path.GetExtension(file))))
100+
{
101+
throw new IOException("该名称的文件已存在");
102+
}
103+
}
104+
//检查图层是否在集合中
105+
if (!layers.Contains(Layer))
106+
{
107+
throw new ArgumentException("本图层不在给定的图层集合中");
108+
}
109+
int index = layers.IndexOf(Layer);
110+
111+
table.Close();
112+
//重命名
113+
foreach (var file in Shapefile.GetExistShapefiles(Parameters.DataPath, layer.Name))
114+
{
115+
File.Move(file, Path.Combine(Parameters.DataPath, newName + Path.GetExtension(file)));
116+
}
117+
Name = newName;
118+
await LoadAsync();
119+
layers[index] = Layer;
120+
}
121+
67122
private FeatureLayer layer;
68123

69124
[JsonIgnore]
70125
public FeatureLayer Layer => layer;
71126

72127
private ShapefileFeatureTable table;
73128

74-
public async Task SetTableAsync(ShapefileFeatureTable table)
129+
public async Task LoadAsync()
75130
{
76-
this.table = table;
131+
table = new ShapefileFeatureTable(this.GetFilePath());
77132
await table.LoadAsync();
78133
layer = new FeatureLayer(table);
134+
135+
await Task.Run(this.ApplyStyle);
136+
await this.LayerCompleteAsync();
79137
}
80138

81139
public bool HasTable => table != null;
@@ -115,7 +173,7 @@ public SymbolInfo GetDefaultSymbol()
115173
GeometryType.Multipoint => SymbolInfo.DefaultPointSymbol,
116174
GeometryType.Polyline => SymbolInfo.DefaultLineSymbol,
117175
GeometryType.Polygon => SymbolInfo.DefaultPolygonSymbol,
118-
_ => throw new ArgumentOutOfRangeException(),
176+
_ => throw new InvalidEnumArgumentException(),
119177
};
120178
}
121179

MapBoard.Core/Util/LayerUtility.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ public async static Task<MapLayerInfo> CreateLayerAsync(GeometryType type,
7272
fields = new List<FieldInfo>();
7373
}
7474
await Shapefile.CreateShapefileAsync(type, name, null, fields);
75-
MapLayerInfo layer = template == null ? new MapLayerInfo() : template.Clone() as MapLayerInfo;
75+
MapLayerInfo layer = template == null ? new MapLayerInfo(name) : template.Clone() as MapLayerInfo;
7676
layer.Fields = fields.ToArray();
77-
layer.Name = name;
7877
await layers.AddAsync(layer);
7978
layers.Selected = layer;
8079
return layer;
@@ -132,7 +131,7 @@ public static async Task LayerCompleteAsync(this MapLayerInfo layer)
132131

133132
public async static Task BufferAsync(this MapLayerInfo layer, MapLayerCollection layers, double meters)
134133
{
135-
var template = new MapLayerInfo();
134+
var template = MapLayerInfo.CreateTemplate();
136135
foreach (var symbol in layer.Symbols)
137136
{
138137
template.Symbols.Add(symbol.Key, new SymbolInfo()

MapBoard.Model/LayerInfo.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ namespace MapBoard.Model
1313
[DebuggerDisplay("{Name}")]
1414
public class LayerInfo : INotifyPropertyChanged, ICloneable
1515
{
16-
public string Name { get; set; }
16+
private string name;
17+
18+
public string Name
19+
{
20+
get => name;
21+
set => this.SetValueAndNotify(ref name, value, nameof(Name));
22+
}
1723

1824
public Dictionary<string, SymbolInfo> Symbols { get; set; } = new Dictionary<string, SymbolInfo>();
1925

MapBoard.UI/UI/Bar/SelectionBar.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ async Task CopyAttributeAsync()
434434
default:
435435
break;
436436
}
437-
await ItemsOperaionErrorsDialog.TryShowErrorsAsync(errors);
437+
await ItemsOperaionErrorsDialog.TryShowErrorsAsync("部分属性复制失败", errors);
438438
}
439439
}
440440
}

MapBoard.UI/UI/Dialog/ItemsOperaionErrorsDialog.xaml

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
88
xmlns:model="clr-namespace:MapBoard.Model;assembly=MapBoard.Model"
99
xmlns:ui="http://schemas.modernwpf.com/2019"
10-
Title="操作结果"
10+
xmlns:util="clr-namespace:MapBoard.UI.Util"
1111
DataContext="{Binding RelativeSource={RelativeSource Self}}"
1212
PrimaryButtonText="关闭" mc:Ignorable="d">
1313
<Grid
@@ -23,15 +23,32 @@
2323
<Run Text="{Binding Errors.Count, Mode=OneWay}" />
2424
<Run>项发生错误</Run>
2525
</TextBlock>
26-
<ListView
27-
Grid.Row="2" MaxHeight="160"
28-
ItemsSource="{Binding Errors}">
29-
<ListView.View>
30-
<GridView>
31-
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="描述" />
32-
<GridViewColumn DisplayMemberBinding="{Binding ErrorMessage}" Header="错误详情" />
33-
</GridView>
34-
</ListView.View>
35-
</ListView>
26+
<DataGrid
27+
Grid.Row="2" MaxHeight="240"
28+
util:SmoothScrollViewerHelper.SmoothScroll="True"
29+
AutoGenerateColumns="False"
30+
CanUserAddRows="False"
31+
CanUserDeleteRows="False"
32+
ItemsSource="{Binding Errors}"
33+
SelectionMode="Single"
34+
SelectionUnit="FullRow">
35+
<DataGrid.Columns>
36+
<DataGridTextColumn
37+
Binding="{Binding Name}"
38+
Header="描述" IsReadOnly="True" />
39+
<DataGridTextColumn
40+
Binding="{Binding ErrorMessage}"
41+
Header="错误详情" IsReadOnly="True" />
42+
</DataGrid.Columns>
43+
<DataGrid.RowDetailsTemplate>
44+
<DataTemplate>
45+
<TextBlock
46+
Margin="12,4"
47+
Text="{Binding Exception}"
48+
TextWrapping="Wrap"
49+
Visibility="{Binding Exception, Converter={StaticResource n2ic}}" />
50+
</DataTemplate>
51+
</DataGrid.RowDetailsTemplate>
52+
</DataGrid>
3653
</Grid>
3754
</commondialog:CommonDialog>

MapBoard.UI/UI/Dialog/ItemsOperaionErrorsDialog.xaml.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,22 @@ namespace MapBoard.UI.Dialog
2222
{
2323
public partial class ItemsOperaionErrorsDialog : CommonDialog
2424
{
25-
public ItemsOperaionErrorsDialog(ItemsOperationErrorCollection errors)
25+
public ItemsOperaionErrorsDialog(string title, ItemsOperationErrorCollection errors)
2626
{
27+
Title = title;
2728
Errors = errors;
2829
InitializeComponent();
2930
}
3031

3132
public ItemsOperationErrorCollection Errors { get; }
3233

33-
public async static Task TryShowErrorsAsync(ItemsOperationErrorCollection errors)
34+
public async static Task TryShowErrorsAsync(string title, ItemsOperationErrorCollection errors)
3435
{
3536
if (errors == null || errors.Count == 0)
3637
{
3738
return;
3839
}
39-
await new ItemsOperaionErrorsDialog(errors).ShowAsync();
40-
}
41-
42-
public async static Task TryShowErrorsAsync(Task<ItemsOperationErrorCollection> task)
43-
{
44-
var errors = await task;
45-
if (errors == null || errors.Count == 0)
46-
{
47-
return;
48-
}
49-
await new ItemsOperaionErrorsDialog(errors).ShowAsync();
40+
await new ItemsOperaionErrorsDialog(title, errors).ShowAsync();
5041
}
5142
}
5243
}

MapBoard.UI/UI/LayerListPanelHelper.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public LayerListPanelHelper(ListView list, MainWindow win, MainMapView mapView)
4343
public void ShowContextMenu()
4444
{
4545
ContextMenu menu = new ContextMenu();
46-
List<(string header, Func<MapLayerInfo, Task> action, bool visiable)?> menus = null;
4746
MapLayerInfo layer = MapView.Layers.Selected;
4847
MapLayerInfo[] layers = list.SelectedItems.Cast<MapLayerInfo>().ToArray();
4948
if (list.SelectedItems.Count == 1)
@@ -316,7 +315,7 @@ private async Task CopyAttributesAsync(MapLayerInfo layer)
316315
default:
317316
break;
318317
}
319-
await ItemsOperaionErrorsDialog.TryShowErrorsAsync(errors);
318+
await ItemsOperaionErrorsDialog.TryShowErrorsAsync("部分属性复制失败", errors);
320319
}
321320
}
322321

@@ -353,6 +352,5 @@ public void RightButtonClickToSelect(MouseEventArgs e)
353352
list.SelectedItem = layer;
354353
}
355354
}
356-
357355
}
358356
}

0 commit comments

Comments
 (0)