-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGeometricNetwork.cs
506 lines (448 loc) · 18.5 KB
/
GeometricNetwork.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//-----------------------------------------------------------------------
// <copyright file="GeometricNetwork.cs" company="Studio A&T s.r.l.">
// Copyright (c) Studio A&T s.r.l. All rights reserved.
// </copyright>
// <author>Nicogis</author>
//-----------------------------------------------------------------------
namespace Studioat.ArcGis.Soe.Rest
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.NetworkAnalysis;
using ESRI.ArcGIS.SOESupport;
/// <summary>
/// Class for geometry network tracer
/// </summary>
[SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate", Justification = "-")]
internal abstract class GeometricNetwork : IGeometricNetwork
{
/// <summary>
/// Gets or sets Geometry Network
/// </summary>
protected ESRI.ArcGIS.Geodatabase.IGeometricNetwork geometricNetwork;
/// <summary>
/// Gets or sets flag Not Found
/// </summary>
protected List<IGeometry> flagNotFound;
/// <summary>
/// Gets or sets barrier Not Found
/// </summary>
protected List<IGeometry> barrierNotFound;
/// <summary>
/// list flag edge found
/// </summary>
protected IEdgeFlag[] edgeFlags;
/// <summary>
/// list flag junction found
/// </summary>
protected IJunctionFlag[] junctionFlags;
/// <summary>
/// list barrier edge found
/// </summary>
protected int[] edgeBarriers;
/// <summary>
/// list barrier junction found
/// </summary>
protected int[] junctionBarriers;
/// <summary>
/// Initializes a new instance of the GeometricNetwork class
/// </summary>
/// <param name="geometricNetwork">object geometricNetwork</param>
/// <param name="geometricNetworkParameters">object IGeometricNetworkParameters</param>
public GeometricNetwork(ESRI.ArcGIS.Geodatabase.IGeometricNetwork geometricNetwork, IGeometricNetworkParameters geometricNetworkParameters)
{
this.geometricNetwork = geometricNetwork;
this.EdgeFlags = geometricNetworkParameters.EdgeFlags;
this.JunctionFlags = geometricNetworkParameters.JunctionFlags;
this.EdgeBarriers = geometricNetworkParameters.EdgeBarriers;
this.JunctionBarriers = geometricNetworkParameters.JunctionBarriers;
this.MaxFeatures = geometricNetworkParameters.MaxFeatures;
this.Tolerance = geometricNetworkParameters.Tolerance;
this.FlowElements = geometricNetworkParameters.FlowElements;
this.OutFields = new string[] { "*" };
this.edgeFlags = new EdgeFlag[] { };
this.junctionFlags = new JunctionFlag[] { };
this.edgeBarriers = new int[] { };
this.junctionBarriers = new int[] { };
this.SetFlagsGeometricNetwork();
this.SetBarriersGeometricNetwork();
}
/// <summary>
/// Gets or sets Output fields
/// </summary>
public string[] OutFields
{
get;
set;
}
/// <summary>
/// Gets or sets Edge Flags
/// </summary>
public List<IPoint> EdgeFlags
{
get;
set;
}
/// <summary>
/// Gets or sets Junction Flags
/// </summary>
public List<IPoint> JunctionFlags
{
get;
set;
}
/// <summary>
/// Gets or sets Edge Barriers
/// </summary>
public List<IPoint> EdgeBarriers
{
get;
set;
}
/// <summary>
/// Gets or sets Junction Barriers
/// </summary>
public List<IPoint> JunctionBarriers
{
get;
set;
}
/// <summary>
/// Gets or sets max Features
/// </summary>
public int MaxFeatures
{
get;
set;
}
/// <summary>
/// Gets or sets max Features
/// </summary>
public double Tolerance
{
get;
set;
}
/// <summary>
/// Gets or sets Flow Elements
/// </summary>
public esriFlowElements FlowElements
{
get;
set;
}
/// <summary>
/// Gets or sets ShortestPathObjFn
/// </summary>
public esriShortestPathObjFn ShortestPathObjFn
{
get;
set;
}
/// <summary>
/// method solve
/// </summary>
/// <returns>result of solve</returns>
public abstract JsonObject Solve();
/// <summary>
/// return a featureSet from EIDs
/// </summary>
/// <param name="eids">object EIDs</param>
/// <param name="featureSets">array JsonObject featureSet</param>
protected void GetTrace(IEnumNetEID eids, out JsonObject[] featureSets)
{
IEIDHelper eidHelper = new EIDHelperClass();
eidHelper.GeometricNetwork = this.geometricNetwork;
eidHelper.ReturnGeometries = true;
eidHelper.ReturnFeatures = true;
Dictionary<int, IPair<List<int>, IFeatureClass>> dictionary = new Dictionary<int, IPair<List<int>, IFeatureClass>>();
IEnumEIDInfo enumEIDinfo = eidHelper.CreateEnumEIDInfo(eids);
enumEIDinfo.Reset();
IEIDInfo eidInfo = enumEIDinfo.Next();
while (eidInfo != null)
{
IFeatureClass featureClass = eidInfo.Feature.Class as IFeatureClass;
IFeature feature = eidInfo.Feature;
int featureClassID = featureClass.FeatureClassID;
if (!dictionary.ContainsKey(featureClassID))
{
IPair<List<int>, IFeatureClass> pair = new Pair<List<int>, IFeatureClass>(new List<int>(), featureClass);
dictionary.Add(featureClassID, pair);
}
dictionary[featureClassID].First.Add(feature.OID);
eidInfo = enumEIDinfo.Next();
}
List<JsonObject> jsonObjects = new List<JsonObject>();
foreach (int i in dictionary.Keys)
{
IPair<List<int>, IFeatureClass> pair = dictionary[i];
List<int> listOIDs = pair.First;
IFeatureClass featureClass = pair.Second;
IQueryFilter2 queryFilter = new QueryFilterClass();
if (this.OutFields[0] != "*")
{
List<string> listFields = new List<string>();
Array.ForEach(
this.OutFields,
s =>
{
if (featureClass.Fields.FindField(s) != -1)
{
listFields.Add(s);
}
});
if (listFields.Count == 0)
{
queryFilter.SubFields = featureClass.OIDFieldName;
}
else
{
listFields.Each((s, index) =>
{
if (index == 0)
{
queryFilter.SubFields = s;
}
else
{
queryFilter.AddField(s);
}
});
if (!Array.Exists(this.OutFields, s => s == featureClass.OIDFieldName))
{
queryFilter.AddField(featureClass.OIDFieldName);
}
if (!Array.Exists(this.OutFields, s => s == featureClass.ShapeFieldName))
{
queryFilter.AddField(featureClass.ShapeFieldName);
}
}
}
queryFilter.WhereClause = featureClass.OIDFieldName + " IN (" + string.Join(",", Array.ConvertAll<int, string>(listOIDs.ToArray(), s => s.ToString(CultureInfo.InvariantCulture))) + ")";
IRecordSet recordset = Helper.ConvertToRecordset(featureClass, queryFilter);
jsonObjects.Add(new JsonObject(Encoding.UTF8.GetString(Conversion.ToJson(recordset))));
}
featureSets = jsonObjects.ToArray();
}
/// <summary>
/// set edge and junction for results
/// </summary>
/// <param name="objectJson">object JsonObject</param>
/// <param name="edgeEIDs">object IEnumNetEID for edge</param>
/// <param name="junctionEIDs">object IEnumNetEID for junction</param>
protected void SetResults(ref JsonObject objectJson, IEnumNetEID edgeEIDs, IEnumNetEID junctionEIDs)
{
if ((this.FlowElements == esriFlowElements.esriFEEdges) || (this.FlowElements == esriFlowElements.esriFEJunctionsAndEdges))
{
if (edgeEIDs == null)
{
throw new GeometricNetworkException("No traced edges found");
}
else
{
if (edgeEIDs.Count > this.MaxFeatures)
{
throw new GeometricNetworkException(edgeEIDs.Count.ToString(CultureInfo.InvariantCulture) + " features were traced which exceeds the limit of " + this.MaxFeatures);
}
}
}
if ((this.FlowElements == esriFlowElements.esriFEJunctions) || (this.FlowElements == esriFlowElements.esriFEJunctionsAndEdges))
{
if (junctionEIDs == null)
{
throw new GeometricNetworkException("No traced junctions found");
}
else
{
if (junctionEIDs.Count > this.MaxFeatures)
{
throw new GeometricNetworkException(junctionEIDs.Count.ToString(CultureInfo.InvariantCulture) + " features were traced which exceeds the limit of " + this.MaxFeatures);
}
}
}
if (this.FlowElements == esriFlowElements.esriFEEdges || this.FlowElements == esriFlowElements.esriFEJunctionsAndEdges)
{
if (edgeEIDs.Count == 0)
{
objectJson.AddArray("edges", (new List<JsonObject>()).ToArray());
}
else
{
JsonObject[] featureSet;
this.GetTrace(edgeEIDs, out featureSet);
objectJson.AddArray("edges", featureSet);
}
}
if (this.FlowElements == esriFlowElements.esriFEJunctions || this.FlowElements == esriFlowElements.esriFEJunctionsAndEdges)
{
if (junctionEIDs.Count == 0)
{
objectJson.AddArray("junctions", (new List<JsonObject>()).ToArray());
}
else
{
JsonObject[] featureSet;
this.GetTrace(junctionEIDs, out featureSet);
objectJson.AddArray("junctions", featureSet);
}
}
}
/// <summary>
/// Set Barriers GeometricNetwork
/// </summary>
protected void SetBarriersGeometricNetwork()
{
this.barrierNotFound = new List<IGeometry>();
////barriers edge
if (this.EdgeBarriers.Count > 0)
{
List<int> eidEdgeBarriers = new List<int>();
foreach (IPoint point in this.EdgeBarriers)
{
int eid = this.GetEIDFromPoint(this.Tolerance, point, esriElementType.esriETEdge);
if (eid < 1)
{
this.barrierNotFound.Add(point);
continue;
}
eidEdgeBarriers.Add(eid);
}
this.edgeBarriers = eidEdgeBarriers.ToArray();
}
////barriers junction
if (this.JunctionBarriers.Count > 0)
{
List<int> eidJunctionBarriers = new List<int>();
foreach (IPoint point in this.JunctionBarriers)
{
int eid = this.GetEIDFromPoint(this.Tolerance, point, esriElementType.esriETJunction);
if (eid < 1)
{
this.barrierNotFound.Add(point);
continue;
}
eidJunctionBarriers.Add(eid);
}
this.junctionBarriers = eidJunctionBarriers.ToArray();
}
}
/// <summary>
/// set barrier for traceFlowSolver
/// </summary>
/// <param name="traceFlowSolver">object ITraceFlowSolverGEN</param>
protected void SetBarriers(ref ITraceFlowSolverGEN traceFlowSolver)
{
INetSolver netSolver = traceFlowSolver as INetSolver;
if (this.edgeBarriers.Length > 0)
{
INetElementBarriersGEN netElementBarriersGEN = new NetElementBarriersClass() as INetElementBarriersGEN;
netElementBarriersGEN.ElementType = esriElementType.esriETEdge;
netElementBarriersGEN.Network = this.geometricNetwork.Network;
netElementBarriersGEN.SetBarriersByEID(ref this.edgeBarriers);
INetElementBarriers netElementBarriers = netElementBarriersGEN as INetElementBarriers;
netSolver.set_ElementBarriers(esriElementType.esriETEdge, netElementBarriers);
}
if (this.junctionBarriers.Length > 0)
{
INetElementBarriersGEN netElementBarriersGEN = new NetElementBarriersClass() as INetElementBarriersGEN;
netElementBarriersGEN.ElementType = esriElementType.esriETJunction;
netElementBarriersGEN.Network = this.geometricNetwork.Network;
netElementBarriersGEN.SetBarriersByEID(ref this.junctionBarriers);
INetElementBarriers netElementBarriers = netElementBarriersGEN as INetElementBarriers;
netSolver.set_ElementBarriers(esriElementType.esriETJunction, netElementBarriers);
}
}
/// <summary>
/// set origin flag for traceFlowSolver
/// </summary>
/// <param name="traceFlowSolver">object ITraceFlowSolverGEN</param>
protected void SetFlagsOrigin(ref ITraceFlowSolverGEN traceFlowSolver)
{
if (this.edgeFlags.Length > 0)
{
traceFlowSolver.PutEdgeOrigins(ref this.edgeFlags);
}
if (this.junctionFlags.Length > 0)
{
traceFlowSolver.PutJunctionOrigins(ref this.junctionFlags);
}
}
/// <summary>
/// Set Flags Geometric Network
/// </summary>
protected void SetFlagsGeometricNetwork()
{
//// edge Flags
List<INetFlag> edgeFlagList = new List<INetFlag>();
this.flagNotFound = new List<IGeometry>();
if (this.EdgeFlags.Count > 0)
{
foreach (IPoint point in this.EdgeFlags)
{
int eid = this.GetEIDFromPoint(this.Tolerance, point, esriElementType.esriETEdge);
if (eid < 1)
{
this.flagNotFound.Add(point);
continue;
}
INetElements netElements = this.geometricNetwork.Network as INetElements;
int featureClassID, featureID, subID;
netElements.QueryIDs(eid, esriElementType.esriETEdge, out featureClassID, out featureID, out subID);
INetFlag netFlag = new EdgeFlagClass();
netFlag.UserClassID = featureClassID;
netFlag.UserID = featureID;
netFlag.UserSubID = subID;
edgeFlagList.Add(netFlag);
}
this.edgeFlags = new IEdgeFlag[edgeFlagList.Count];
edgeFlagList.Each((i, index) =>
{
this.edgeFlags[index] = i as IEdgeFlag;
});
}
//// junction Flags
List<INetFlag> junctionFlagList = new List<INetFlag>();
if (this.JunctionFlags.Count > 0)
{
foreach (IPoint point in this.JunctionFlags)
{
int eid = this.GetEIDFromPoint(this.Tolerance, point, esriElementType.esriETJunction);
if (eid < 1)
{
this.flagNotFound.Add(point);
continue;
}
INetElements netElements = this.geometricNetwork.Network as INetElements;
int featureClassID, featureID, subID;
netElements.QueryIDs(eid, esriElementType.esriETJunction, out featureClassID, out featureID, out subID);
INetFlag netFlag = new JunctionFlagClass();
netFlag.UserClassID = featureClassID;
netFlag.UserID = featureID;
netFlag.UserSubID = subID;
junctionFlagList.Add(netFlag);
}
this.junctionFlags = new IJunctionFlag[junctionFlagList.Count];
junctionFlagList.Each((i, index) =>
{
this.junctionFlags[index] = i as JunctionFlag;
});
}
}
/// <summary>
/// search the eid nearest from point
/// </summary>
/// <param name="searchTolerance">tolerance for search</param>
/// <param name="point">point input</param>
/// <param name="elementType">type of element</param>
/// <returns>return eid</returns>
private int GetEIDFromPoint(double searchTolerance, IPoint point, esriElementType elementType)
{
return Helper.GetEIDFromPoint(this.geometricNetwork, searchTolerance, point, elementType);
}
}
}