-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMapFile.cs
More file actions
266 lines (222 loc) · 9.54 KB
/
MapFile.cs
File metadata and controls
266 lines (222 loc) · 9.54 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
///http://spatialnews.geocomm.com/features/mif_format/
///
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.IO;
using System.Text;
using System.Collections;
using System.Linq;
//using GeoAPI.Geometries;
namespace MapInfo.IO
{
/// <summary>
/// Откройте файл .map, и инициализировать структуры, чтобы быть готовыми, чтобы прочитать объекты из него.
/// Поскольку .map и .id файлы не являются обязательными,
/// вы можете установить bNoErrorMsg = TRUE, чтобы отключить сообщение об ошибке и получите обратный значение 1,
/// если файл не может быть открыт.
/// В этом случае, только методы MoveToObjId () и GetCurObjType () могут быть использованы.
/// Они будут вести себя так, как будто файл .id содержал только пустые ссылки,
/// так что все объект будет выглядеть, как нет у них геометрии.
/// </summary>
class MapFile
{
//public int m_nMinTABVersion = 300;
//private Collection<mapFileRecord> _records = new Collection<mapFileRecord>();
public TABMAPHeaderBlock header;
public TABMAPIndexBlock index;
public TABMAPObjectBlock objects;
public void Open(string fileName)
{
if (string.IsNullOrEmpty(fileName))
throw new ArgumentNullException("fileName");
// читаем геометрию из файла .map
string mapFile = fileName.ToLower().Replace(".tab", ".map");
//int[] offsets;
// .map-файл необходим по спецификации, но прочесть shape-файл можно и без него.
if (File.Exists(mapFile))
//offsets = ReadIndex(mapFile);
//else
// offsets = new int[] { };
using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
try
{
while (stream.Position < stream.Length)
{
try
{
if (stream.Position == 0)
{
header = new TABMAPHeaderBlock(TABRawBlock.GetBlock(stream));
}
else if (stream.Position == TABRawBlock.Size &&
header.m_nMAPVersionNumber == TABMAPHeaderBlock.HDR_VERSION_NUMBER)
{
header.Add(TABRawBlock.GetBlock(stream));
}
else
{
byte[] blk = TABRawBlock.GetBlock(stream);
switch (TABRawBlock.GetBlockClass(blk))
{
case SupportedBlockTypes.TABMAP_INDEX_BLOCK:
if (index == null)
index = new TABMAPIndexBlock(blk);
else
index.Add(blk);
break;
case SupportedBlockTypes.TABMAP_OBJECT_BLOCK:
if (objects == null)
objects = new TABMAPObjectBlock(blk);
else
objects.Add(blk);
break;
default:
break;
}
}
}
catch (IOException)
{
break;
}
}
}
catch
{
stream.Flush();
stream.Close();
}
}
//string dbaseFile = fileName.ToLower().Replace(".shp", ".dbf");
////dbaseFile = dbaseFile.Replace(".SHP", ".DBF");
////!!!
//this.ReadAttributes(dbaseFile);
}
/// <summary>
/// Returns the appropriate class to convert a shaperecord to an MapAround geometry given the type of shape.
/// </summary>
/// <param name="type">The shape file type.</param>
/// <returns>An instance of the appropriate handler to convert the shape record to a Geometry</returns>
//internal static ShapeHandler GetShapeHandler(ShapeType type)
//{
// switch (type)
// {
// case ShapeType.Point:
// //case ShapeGeometryType.PointM:
// //case ShapeGeometryType.PointZ:
// //case ShapeGeometryType.PointZM:
// return new MapPointShapeHandler();
// //case ShapeType.Polygon:
// // //case ShapeGeometryType.PolygonM:
// // //case ShapeGeometryType.PolygonZ:
// // //case ShapeGeometryType.PolygonZM:
// // return new PolygonHandler();
// //case ShapeType.Polyline: //.LineString:
// // //case ShapeGeometryType.LineStringM:
// // //case ShapeGeometryType.LineStringZ:
// // //case ShapeGeometryType.LineStringZM:
// // return new MultiLineHandler();
// //case ShapeType.Multipoint:
// // //case ShapeGeometryType.MultiPointM:
// // //case ShapeGeometryType.MultiPointZ:
// // //case ShapeGeometryType.MultiPointZM:
// // return new MultiPointHandler();
// default:
// string msg = String.Format(System.Globalization.CultureInfo.InvariantCulture, "ShapeType {0} is not supported.", (int)type);
// throw new InvalidDataException(msg);
// }
//}
}
/// <summary>
/// Represents a record of shape file.
/// </summary>
public class MapFileRecord
{
#region Private fields
public byte ShapeType;
public byte Symbol;
public TABMAPIndexEntry MBR;
private int _contentLength;
private Collection<int> _parts = new Collection<int>();
private Collection<TABMAPVertex> _points = new Collection<TABMAPVertex>();
private DataRow _attributes;
public int CoordBlockPtr;
public int CoordDataSize;
public TABMAPVertex LabelLocation;
#endregion
#region Constructor
/// <summary>
/// Initializes a new instance of the MapAround.IO.ShapeFileRecord
/// </summary>
public MapFileRecord()
{
}
#endregion Constructor
#region Properties
/// <summary>
/// Gets or sets the length (in bytes) of this record.
/// </summary>
public int ContentLength
{
get { return _contentLength; }
set { _contentLength = value; }
}
/// <summary>
/// Gets a number of parts of the geometry.
/// </summary>
public int NumberOfParts
{
get { return _parts.Count; }
}
/// <summary>
/// Gets a number of points of the geometry.
/// </summary>
public int NumberOfPoints
{
get { return _points.Count; }
}
/// <summary>
/// Gets a collection containing the indices of
/// coordinate sequences corresponding parts of
/// geometry.
/// </summary>
public Collection<int> Parts
{
get { return _parts; }
}
/// <summary>
/// Gets a collection of coordinates of
/// the geometry.
/// </summary>
public Collection<TABMAPVertex> Points
{
get { return _points; }
}
/// <summary>
/// Gets or sets an attributes row associated
/// with this record.
/// </summary>
public DataRow Attributes
{
get { return _attributes; }
set { _attributes = value; }
}
#endregion Properties
#region Public methods
/// <summary>
/// Returns a System.String that represents the current MapAround.IO.ShapeFileRecord.
/// </summary>
/// <returns>A System.String that represents the current MapAround.IO.ShapeFilerecord</returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
//sb.AppendFormat("ShapeFileRecord: RecordNumber={0}, ContentLength={1}, ShapeType={2}",
// this._contentLength, this._shapeType); //this._recordNumber,
return sb.ToString();
}
#endregion Public methods
}
}