-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseShapeRecord.cs
310 lines (232 loc) · 8.14 KB
/
BaseShapeRecord.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using DbfDataReader;
using Microsoft.SqlServer.Types;
using System.Data.SqlTypes;
// useful as previous but not specifcally what we're looking for here.
// apparently a whole geometric model with all coordinates and metadata can be defined in wkt
// this doesnt have what we're looking for otherwise however.
// http://docs.opengeospatial.org/is/18-010r7/18-010r7.html#1
namespace ShapeUtilities
{
public class BaseShapeRecord
{
public BaseShapeRecord(BinaryReader reader)
{
}
/// <summary>
/// Primarily the purpose for this is to populate a bulkcopy ready datatable.
/// Returns the dacfx server type.
/// </summary>
public SqlGeography GetMSSQLInstance()
{
return
SqlGeography.STGeomFromText(new SqlChars(new SqlString(this.GetWKT())), 4326);
}
/// <summary>
/// Gets the bounding box of a shape implemeting IHasPoints
/// </summary>
/// <returns>Null if shape does not a have a points collection otherwise returns a boundingbox enclosing the points</returns>
public BoundingBox GetExtent()
{
if (this is IHasPoints)
{
var obj = (IHasPoints)this;
var xes = obj.Points.Select(o => o.X).ToList();
var yes = obj.Points.Select(o => o.Y).ToList();
return new BoundingBox() { X1 = xes.Min(), X2 = xes.Max(), Y1 = yes.Min(), Y2 = yes.Max() };
}
else return null;
}
public string GetWKT()
{
StringBuilder sb = new StringBuilder();
#region Implemented
if (this is PolygonShape)
{
var obj = (PolygonShape)this;
sb.Append("POLYGON(");
for (int parts = 0; parts < obj.NumParts; parts++)
{
sb.Append("(");
int index = obj.Parts[parts];
int lastindex = 0;
if (obj.NumParts==1)
{
lastindex = obj.NumPoints - 1;
}
else if (parts < obj.NumParts-1)
{
lastindex = obj.Parts[parts + 1] - 1;
}
else
{
lastindex = obj.NumPoints - 1;
}
for (int pointindex = index; pointindex < lastindex + 1; pointindex++)
{
sb.Append(obj.Points[pointindex].X.ToString() + " " +
obj.Points[pointindex].Y.ToString() +
(pointindex==lastindex?" ": ","));
}
sb.Append(")");
if (parts < obj.NumParts - 1)
{
sb.Append(", ");
}
}
sb.Append(")");
return sb.ToString();
}
// as per the esri documentation on shapefiles, the multipart line would just translate into mulkiple linestrings
// however in this implementation
if (this is PolyLineShape)
{
var obj = (PolyLineShape)this;
if (obj.NumParts > 1)
{
sb.Append("MULTILINESTRING(");
}
else
{
// OPENING AND CLOSING PARENTHESIS WILL OCCUR IN THE GENERIC CODE INCLUDED BELOW.
sb.Append("LINESTRING");
}
for (int parts = 0; parts < obj.NumParts; parts++)
{
sb.Append("(");
int index = obj.Parts[parts];
int lastindex = 0;
if (obj.NumParts == 1)
{
lastindex = obj.NumPoints - 1;
}
else if (parts < obj.NumParts - 1)
{
lastindex = obj.Parts[parts + 1] - 1;
}
else
{
lastindex = obj.NumPoints - 1;
}
for (int pointindex = index; pointindex < lastindex + 1; pointindex++)
{
sb.Append(obj.Points[pointindex].X.ToString() + " " +
obj.Points[pointindex].Y.ToString() +
(pointindex == lastindex ? " " : ","));
}
sb.Append(")");
if (parts < obj.NumParts - 1)
{
sb.Append(", ");
}
}
// FOR MULTILINESTRING
if (obj.NumParts > 1)
{
sb.Append(")");
}
return sb.ToString();
}
#endregion
#region NotImplemented
if (this is MultiPatchShape)
{
var obj = (MultiPatchShape)this;
sb.Append("MULTIPATCH(");
}
if (this is MultiPointMShape)
{
var obj = (MultiPointMShape)this;
sb.Append("MULTIPOINT(");
}
if (this is MultiPointShape)
{
var obj = (MultiPointShape)this;
sb.Append("MULTIPOINT(");
}
if (this is MultiPointZShape)
{
var obj = (MultiPointZShape)this;
sb.Append("MULTIPOINT(");
}
if (this is NullShape)
{
return String.Empty;
}
if (this is PointMShape)
{
var obj = (PointMShape)this;
sb.Append("POINT(");
}
if (this is PointShape)
{
var obj = (PointShape)this;
sb.Append("POINT(");
}
if (this is PointZShape)
{
var obj = (PointZShape)this;
sb.Append("POINT(");
}
if (this is PolygonMShape)
{
var obj = (PolygonMShape)this;
sb.Append("POLYGON(");
}
if (this is PolygonZShape)
{
var obj = (PolygonZShape)this;
sb.Append("POLYGON(");
}
if (this is PolyLineMShape)
{
var obj = (PolyLineMShape)this;
sb.Append("POLYLINE(");
}
if (this is PolyLineZShape)
{
var obj = (PolyLineZShape)this;
sb.Append("POLYLINE(");
}
#endregion
sb.Append(")");
return string.Empty;
}
public static BaseShapeRecord ReadRecord(BinaryReader reader)
{
return new NullShape(reader);
}
public List<ShpPoint> ReadPoints(BinaryReader br, int numberpoints)
{
List<ShpPoint> retval = new List<ShpPoint>();
for (int x = 0; x < numberpoints; x++)
{
retval.Add(new ShpPoint(br));
}
return retval;
}
public List<double> ReadDoubles(BinaryReader br, int num)
{
List<double> retval = new List<double>();
for (int x=0; x< num;x++)
{
retval.Add(br.ReadDouble());
}
return retval;
}
public List<int> ReadParts(BinaryReader br, int numparts)
{
List<int> retval = new List<int>();
for (int x = 0; x < numparts; x++)
{
retval.Add(br.ReadInt32());
}
return retval;
}
}
}