-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTABFileDataProvider.cs
More file actions
387 lines (330 loc) · 15.7 KB
/
TABFileDataProvider.cs
File metadata and controls
387 lines (330 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
using System;
using System.Collections.Generic;
using MapAround.IO;
using MapAround.Geometry;
using MapAround.Mapping;
using MapAround.Indexing;
using MapAround.Caching;
using MapInfo.IO;
namespace MapAround.DataProviders
{
/// <summary>
/// Экземпляр класса обеспечивает доступ к данным, хранящимся в MapInfo формате TAB-файла.
/// </summary>
public class TABFileSpatialDataProvider : SpatialDataProviderBase
{
private string _fileName = string.Empty;
private bool _processAttributes = false;
private System.Text.Encoding _attributesEncoding = System.Text.Encoding.ASCII;
private IFeatureCollectionCacheAccessor _cacheAccessor = null;
/// <summary>
/// Кэш объектов средства доступа.
/// <para>
/// Если объект кэша средства доступа был назначен, первый запрос данных считывает весь файл
/// и помещает данные в кэш. Последующие запросы привести к попыткам найти нужные данные в кэш-памяти,
/// и только если есть никто не читает файл снова.
/// </para>
/// <remarks>
/// Слои использовать значение свойства псевдоним в качестве ключа доступа.
/// Убедитесь, что значение этого свойства не является нулевым, а не пустая строка.
/// </remarks>
/// </summary>
public IFeatureCollectionCacheAccessor CacheAccessor
{
get { return _cacheAccessor; }
set { _cacheAccessor = value; }
}
#region Свойства
/// <summary>
/// Получает или задает кодировку атрибутов.
/// </summary>
public System.Text.Encoding AttributesEncoding
{
get { return _attributesEncoding; }
set { _attributesEncoding = value; }
}
/// <summary>
/// Получает или задает значение, указывающее, будет ли обработаны атрибуты.
/// </summary>
public bool ProcessAttributes
{
get { return _processAttributes; }
set { _processAttributes = value; }
}
/// <summary>
/// Получает или задает имя файла.
/// </summary>
public string FileName
{
get { return _fileName; }
set { _fileName = value; }
}
#endregion
/// <summary>
/// Вызывает, когда новая функция вызвана.
/// </summary>
public event EventHandler<FeatureOperationEventArgs> FeatureFetched;
/// <summary>
/// Создает и сохраняет индекс
/// </summary>
/// <param name="featureType"></param>
/// <param name="b"></param>
/// <param name="settings"></param>
/// <param name="features"></param>
private void buildAndSaveIndex(MapAround.Mapping.FeatureType featureType,
BoundingRectangle b,
IndexSettings settings,
IEnumerable<Feature> features)
{
throw new NotImplementedException();
// ISpatialIndex index = null;
// if (b.IsEmpty())
// b = new BoundingRectangle(0, 0, 0, 0);
// if (settings.IndexType == "QuadTree")
// index = new QuadTree(b);
// if (index == null)
// index = new KDTree(b);
// index.MaxDepth = settings.MaxDepth;
// index.BoxSquareThreshold = settings.BoxSquareThreshold;
// index.MinObjectCount = settings.MinFeatureCount;
// index.Build(features);
// _cacheAccessor.SaveFeaturesIndex(index, featureType);
}
private int internalQueryFeatures(IFeatureReceiver fr, BoundingRectangle bounds, bool checkBounds)
{
if (_cacheAccessor != null && !string.IsNullOrEmpty(fr.Alias))
{
_cacheAccessor.Key = fr.Alias;
if (_cacheAccessor.ExistsInCache)
return FillFromCache(_cacheAccessor, fr, bounds, _processAttributes);
else
// Если объект не найден в кэше, вы должны удалить все объекты из файла, чтобы поместить их в кэш
checkBounds = false;
}
MapFile shapeFile = new MapFile();
//shapeFile.AttributesEncoding = _attributesEncoding;
shapeFile.Open(_fileName); //, checkBounds ? bounds : null
// if (ProcessAttributes)
// {
// fr.FeatureAttributeNames.Clear();
// foreach (string s in shapeFile.AttributeNames)
// fr.FeatureAttributeNames.Add(s);
// }
int result = 0;
string layerHashStr = fr.GetHashCode().ToString();
// List<Feature> points = new List<Feature>();
// List<Feature> multiPoints = new List<Feature>();
// List<Feature> polylines = new List<Feature>();
// List<Feature> polygons = new List<Feature>();
foreach (MapFileRecord record in shapeFile.objects.fetures)
{
// if (!checkBounds ||
// (record.MaxX >= bounds.MinX && record.MaxY >= bounds.MinY &&
// record.MinX <= bounds.MaxX && record.MinY <= bounds.MaxY))
// {
//Feature newFeature = null;
IGeometry geometry = geometryFromShapeRecord(record);
// if (geometry != null)
// {
// newFeature = new Feature(geometry);
// newFeature.UniqKey = layerHashStr + record.RecordNumber.ToString();
// if (ProcessAttributes && record.Attributes != null)
// newFeature.Attributes = record.Attributes.ItemArray;
// if (processFeature(newFeature, fr, points, multiPoints, polylines, polygons))
// result++;
// }
// }
}
// // If the objects are not extracted from the cache may be added to the cache.
// // This should be done only if the retrieval of all objects (checkBounds == false)
// if (_cacheAccessor != null && !string.IsNullOrEmpty(fr.Alias) &&
// checkBounds == false)
// {
// addFeaturesToCache(fr, points, multiPoints, polylines, polygons);
// }
return result;
}
private IGeometry geometryFromShapeRecord(MapFileRecord record)
{
throw new NotImplementedException();
}
//private IGeometry geometryFromMapRecord(MapFileRecord record)
//{
// switch (record.ShapeType)
// {
// // point
// case 1:
// return new PointD(record.Points[0].X, record.Points[0].Y);
// // polyline
// case 3:
// Polyline polyline = new Polyline();
// for (int i = 0; i < record.Parts.Count; i++)
// {
// LinePath path = new LinePath();
// int j;
// for (j = record.Parts[i]; j < (i == record.Parts.Count - 1 ? record.Points.Count : record.Parts[i + 1]); j++)
// path.Vertices.Add(PlanimetryEnvironment.NewCoordinate(record.Points[j].X, record.Points[j].Y));
// polyline.Paths.Add(path);
// }
// return polyline;
// // ground
// case 5:
// Polygon p = new Polygon();
// for (int i = 0; i < record.Parts.Count; i++)
// {
// Contour contour = new Contour();
// int j;
// for (j = record.Parts[i]; j < (i == record.Parts.Count - 1 ? record.Points.Count : record.Parts[i + 1]); j++)
// contour.Vertices.Add(PlanimetryEnvironment.NewCoordinate(record.Points[j].X, record.Points[j].Y));
// contour.Vertices.RemoveAt(contour.Vertices.Count - 1);
// p.Contours.Add(contour);
// }
// if (p.CoordinateCount > 0)
// return p;
// else
// return null;
// // set of points
// case 8:
// MultiPoint mp = new MultiPoint();
// for (int i = 0; i < record.Points.Count; i++)
// mp.Points.Add(PlanimetryEnvironment.NewCoordinate(record.Points[i].X, record.Points[i].Y));
// return mp;
// }
// return null;
//}
private bool processFeature(Feature feature, IFeatureReceiver fr, List<Feature> points,
List<Feature> multiPoints,
List<Feature> polylines,
List<Feature> polygons)
{
if (feature == null)
return false;
bool isAccepted = true;
if (FeatureFetched != null)
{
FeatureOperationEventArgs foea = new FeatureOperationEventArgs(feature);
FeatureFetched(this, foea);
isAccepted = foea.IsAccepted;
}
if (!isAccepted)
return false;
fr.AddFeature(feature);
switch (feature.FeatureType)
{
case FeatureType.Point: points.Add(feature); break;
case FeatureType.MultiPoint: multiPoints.Add(feature); break;
case FeatureType.Polyline: polylines.Add(feature); break;
case FeatureType.Polygon: polygons.Add(feature); break;
}
return true;
}
private void addFeaturesToCache(IFeatureReceiver fr,
List<Feature> points,
List<Feature> multiPoints,
List<Feature> polylines,
List<Feature> polygons)
{
_cacheAccessor.Key = fr.Alias;
if (!_cacheAccessor.ExistsInCache)
{
BoundingRectangle b = new BoundingRectangle();
List<Feature> pts = new List<Feature>();
foreach (Feature feature in points)
{
b.Join(feature.BoundingRectangle);
pts.Add(feature);
}
foreach (Feature feature in multiPoints)
{
b.Join(feature.BoundingRectangle);
pts.Add(feature);
}
//buildAndSaveIndex(MapAround.Mapping.FeatureType.Point, b, fr.DefaultPointsIndexSettings, pts);
b = new BoundingRectangle();
foreach (Feature feature in polylines)
b.Join(feature.BoundingRectangle);
//buildAndSaveIndex(MapAround.Mapping.FeatureType.Polyline, b, fr.DefaultPolylinesIndexSettings, polylines);
b = new BoundingRectangle();
foreach (Feature feature in polygons)
b.Join(feature.BoundingRectangle);
//buildAndSaveIndex(MapAround.Mapping.FeatureType.Polygon, b, fr.DefaultPolygonsIndexSettings, polygons);
if (_processAttributes)
_cacheAccessor.SaveAttributeNames(fr.FeatureAttributeNames);
}
}
/// <summary>
/// Adds features retrieved from the data source to the receiver.
/// </summary>
/// <param name="receiver">An object that receives features</param>
/// <param name="bounds">Rectangular region you want to fill with the objects</param>
/// <rereturns>A number of retrieved features</rereturns>
public override int QueryFeatures(IFeatureReceiver receiver, BoundingRectangle bounds)
{
throw new NotImplementedException("QueryFeatures");
//return internalQueryFeatures(receiver, bounds, true);
}
/// <summary>
/// Adds features retrieved from the data source to the receiver.
/// </summary>
/// <param name="receiver">An object that receives features</param>
/// <rereturns>A number of retrieved features</rereturns>
public override int QueryFeatures(IFeatureReceiver receiver)
{
//throw new NotImplementedException("QueryFeatures(IFeatureReceiver receiver)");
return internalQueryFeatures(receiver, new BoundingRectangle(), false);
}
}
/// <summary>
/// TAB-файл владелец поставщика данных.
/// </summary>
public class TABFileSpatialDataProviderHolder : SpatialDataProviderHolderBase
{
private static string[] _parameterNames = { "file_name", "process_attributes", "attributes_encoding" };
private Dictionary<string, string> _parameters = null;
/// <summary>
/// Устанавливает значения параметров.
/// </summary>
/// <param name="parameters">Значения параметров</param>
public override void SetParameters(Dictionary<string, string> parameters)
{
if (!parameters.ContainsKey("file_name"))
throw new ArgumentException("Missing parameter \"file_name\".");
_parameters = parameters;
}
/// <summary>
/// Получает список, содержащий имена параметров.
/// </summary>
/// <returns>Список, содержащий имена параметров</returns>
public override string[] GetParameterNames()
{
return _parameterNames;
}
private ISpatialDataProvider createProviderInstance()
{
ShapeFileSpatialDataProvider provider = new ShapeFileSpatialDataProvider();
if (_parameters == null)
throw new InvalidOperationException("Parameter values not set.");
provider.FileName = _parameters["file_name"];
if (_parameters.ContainsKey("process_attributes"))
provider.ProcessAttributes = _parameters["process_attributes"] == "1";
if (_parameters.ContainsKey("attributes_encoding"))
provider.AttributesEncoding = System.Text.Encoding.GetEncoding(_parameters["attributes_encoding"]);
return provider;
}
/// <summary>
/// Выполняет процедуру финализации для пространственного поставщика данных.
/// Эта реализация ничего не делает.
/// </summary>
/// <param name="provider">Экземпляр провайдера Пространственных данных</param>
public override void ReleaseProviderIfNeeded(ISpatialDataProvider provider)
{
}
/// <summary>
/// Инициализирует новый экземпляр
/// </summary>
public TABFileSpatialDataProviderHolder() : base("MapAround.DataProviders.TABFileSpatialDataProviderHolder")
{
GetProviderMethod = createProviderInstance;
}
}
}