-
Notifications
You must be signed in to change notification settings - Fork 2
/
DSUtility.cs
947 lines (812 loc) · 43.9 KB
/
DSUtility.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
//-----------------------------------------------------------------------
// <copyright file="DSUtility.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.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
using ESRI.ArcGIS;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Location;
using ESRI.ArcGIS.Server;
using ESRI.ArcGIS.SOESupport;
/// <summary>
/// class Dynamic Segmentation Utility
/// </summary>
[ComVisible(true)]
[Guid("dc829495-de6f-4c81-acb0-3e5ef225f654")]
[ClassInterface(ClassInterfaceType.None)]
[ServerObjectExtension("MapServer",
AllCapabilities = "Point Location,Line Location,Identify Route,Identify Route Ex",
DefaultCapabilities = "Point Location,Line Location,Identify Route",
Description = "Dynamic Segmentation Utility",
DisplayName = "Dynamic Segmentation Utility",
Properties = "",
HasManagerPropertiesConfigurationPane = false,
SupportsREST = true,
SupportsSOAP = false)]
[SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = "Warning StyleCop - Code ESRI - pSOH")]
[SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "SA1306:FieldNamesMustBeginWithLowerCaseLetter", Justification = "Warning FxCop - Code ESRI - Capabilities")]
public class DSUtility : IServerObjectExtension, IObjectConstruct, IRESTRequestHandler
{
/// <summary>
/// name of soe
/// </summary>
private string soeName;
/// <summary>
/// Helper ServerObject
/// </summary>
private IServerObjectHelper serverObjectHelper;
/// <summary>
/// request handler
/// </summary>
private IRESTRequestHandler reqHandler;
/// <summary>
/// List of RouteLayerInfo
/// </summary>
private List<RouteLayerInfo> routeLayerInfos;
/// <summary>
/// version arcgis server
/// </summary>
private string agsVersion = "Unknown";
/// <summary>
/// Initializes a new instance of the <see cref="DSUtility"/> class
/// </summary>
public DSUtility()
{
this.soeName = this.GetType().Name;
this.reqHandler = new SoeRestImpl(this.soeName, this.CreateRestSchema()) as IRESTRequestHandler;
}
/// <summary>
/// input name operation of measure unit
/// </summary>
private enum MeasureUnit
{
/// <summary>
/// route Measure Unit
/// </summary>
routeMeasureUnit,
/// <summary>
/// route Location Measure Unit
/// </summary>
routeLocationMeasureUnit
}
#region IServerObjectExtension Members
/// <summary>
/// init event of soe
/// </summary>
/// <param name="pSOH">Helper ServerObject</param>
public void Init(IServerObjectHelper pSOH)
{
this.serverObjectHelper = pSOH;
this.agsVersion = RuntimeManager.ActiveRuntime.Version;
}
/// <summary>
/// shutdown event of soe
/// </summary>
public void Shutdown()
{
}
#endregion
#region IObjectConstruct Members
/// <summary>
/// costruct event of soe
/// </summary>
/// <param name="props">properties of soe</param>
public void Construct(IPropertySet props)
{
this.routeLayerInfos = new List<RouteLayerInfo>();
this.GetRouteLayerInfos();
}
#endregion
#region IRESTRequestHandler Members
/// <summary>
/// get schema of soe
/// </summary>
/// <returns>schema of soe</returns>
public string GetSchema()
{
return this.reqHandler.GetSchema();
}
/// <summary>
/// Handler of request rest
/// </summary>
/// <param name="capabilities">capabilities of soe</param>
/// <param name="resourceName">name of resource</param>
/// <param name="operationName">name of operation</param>
/// <param name="operationInput">input of operation</param>
/// <param name="outputFormat">format of output</param>
/// <param name="requestProperties">list of request properties</param>
/// <param name="responseProperties">list of response properties </param>
/// <returns>response in byte</returns>
public byte[] HandleRESTRequest(string capabilities, string resourceName, string operationName, string operationInput, string outputFormat, string requestProperties, out string responseProperties)
{
return this.reqHandler.HandleRESTRequest(capabilities, resourceName, operationName, operationInput, outputFormat, requestProperties, out responseProperties);
}
#endregion
/// <summary>
/// Get from input of operation the routeIDFieldName
/// </summary>
/// <param name="operationInput">input of operation</param>
/// <param name="routeIDFieldNameDefault">value of default if routeIDFieldName is missing</param>
/// <returns>value of RouteIDFieldName</returns>
private static string GetRouteIDFieldName(JsonObject operationInput, string routeIDFieldNameDefault)
{
string routeIDFieldNameValue;
bool found = operationInput.TryGetString("routeIDFieldName", out routeIDFieldNameValue);
if (!found || string.IsNullOrEmpty(routeIDFieldNameValue))
{
if (routeIDFieldNameDefault == null)
{
throw new DynamicSegmentationException("routeIDFieldName");
}
else
{
routeIDFieldNameValue = routeIDFieldNameDefault;
}
}
return routeIDFieldNameValue;
}
/// <summary>
/// Get from input of operation the routeIDFieldName
/// </summary>
/// <param name="operationInput">input of operation</param>
/// <returns>value of RouteIDFieldName</returns>
private static string GetRouteIDFieldName(JsonObject operationInput)
{
return DSUtility.GetRouteIDFieldName(operationInput, null);
}
/// <summary>
/// Get from input of operation the routeMeasureUnit
/// </summary>
/// <param name="operationInput">input of operation</param>
/// <param name="inputName">name of operation input measure unit</param>
/// <returns>value of routeMeasureUnit</returns>
private static esriUnits GetMeasureUnit(JsonObject operationInput, MeasureUnit inputName)
{
string routeMeasureUnitValue;
esriUnits routeMeasureUnit = esriUnits.esriUnknownUnits;
bool found = operationInput.TryGetString(Enum.GetName(typeof(MeasureUnit), inputName), out routeMeasureUnitValue);
if (found && !string.IsNullOrEmpty(routeMeasureUnitValue))
{
if (Enum.IsDefined(typeof(esriUnits), routeMeasureUnitValue))
{
routeMeasureUnit = (esriUnits)Enum.Parse(typeof(esriUnits), routeMeasureUnitValue, true);
}
}
return routeMeasureUnit;
}
/// <summary>
/// Get from input of operation the SegmentExtension
/// </summary>
/// <param name="operationInput">input of operation</param>
/// <returns>value of SegmentExtension</returns>
private static esriSegmentExtension GetSegmentExtension(JsonObject operationInput)
{
string segmentExtensionValue;
esriSegmentExtension segmentExtension = esriSegmentExtension.esriNoExtension;
bool found = operationInput.TryGetString("segmentExtension", out segmentExtensionValue);
if (found && !string.IsNullOrEmpty(segmentExtensionValue))
{
if (Enum.IsDefined(typeof(esriSegmentExtension), segmentExtensionValue))
{
segmentExtension = (esriSegmentExtension)Enum.Parse(typeof(esriSegmentExtension), segmentExtensionValue, true);
}
}
return segmentExtension;
}
/// <summary>
/// get RouteLocator
/// </summary>
/// <param name="featureClass">object feature class</param>
/// <param name="routeIDFieldNameValue">value of routeIDFieldName</param>
/// <param name="routeMeasureUnit">unit of route measure</param>
/// <returns>object IRouteLocator2</returns>
private static IRouteLocator2 GetRouteLocator(IFeatureClass featureClass, string routeIDFieldNameValue, esriUnits routeMeasureUnit)
{
IDataset dataset = featureClass as IDataset;
IName nameDataset = dataset.FullName;
IRouteLocatorName routeMeasureLocatorName = new RouteMeasureLocatorNameClass();
routeMeasureLocatorName.RouteFeatureClassName = nameDataset;
routeMeasureLocatorName.RouteIDFieldName = routeIDFieldNameValue;
routeMeasureLocatorName.RouteMeasureUnit = routeMeasureUnit;
routeMeasureLocatorName.RouteWhereClause = string.Empty;
nameDataset = (IName)routeMeasureLocatorName;
return nameDataset.Open() as IRouteLocator2;
}
/// <summary>
/// get RouteLayerInfo from Id
/// </summary>
/// <param name="routeLayerID">value of routeLayerID</param>
/// <returns>object RouteLayerInfo</returns>
private RouteLayerInfo GetRouteLayerInfo(int routeLayerID)
{
if (routeLayerID < 0)
{
throw new ArgumentOutOfRangeException("routeLayerID");
}
IMapServer3 serverObject = this.GetMapServer();
IMapLayerInfos mapLayerInfos = serverObject.GetServerInfo(serverObject.DefaultMapName).MapLayerInfos;
long count = mapLayerInfos.Count;
for (int i = 0; i < count; i++)
{
IMapLayerInfo mapLayerInfo = mapLayerInfos.get_Element(i);
if (mapLayerInfo.ID == routeLayerID)
{
return new RouteLayerInfo(mapLayerInfo);
}
}
throw new ArgumentOutOfRangeException("routeLayerID");
}
/// <summary>
/// Feature Class from id of layer
/// </summary>
/// <param name="routelayerID">id route layer</param>
/// <returns>feature class</returns>
private IFeatureClass GetRouteFeatureClass(int routelayerID)
{
IMapServer3 mapServer = this.GetMapServer();
IMapServerDataAccess dataAccess = (IMapServerDataAccess)mapServer;
return (IFeatureClass)dataAccess.GetDataSource(mapServer.DefaultMapName, routelayerID);
}
/// <summary>
/// From service fills list of layer with M
/// </summary>
private void GetRouteLayerInfos()
{
IMapServer3 serverObject = this.GetMapServer();
IMapLayerInfos mapLayerInfos = serverObject.GetServerInfo(serverObject.DefaultMapName).MapLayerInfos;
this.routeLayerInfos = new List<RouteLayerInfo>();
for (int i = 0; i < mapLayerInfos.Count; i++)
{
IMapLayerInfo mapLayerInfo = mapLayerInfos.get_Element(i);
if (mapLayerInfo.IsFeatureLayer)
{
IFields fields = mapLayerInfo.Fields;
for (int j = 0; j < fields.FieldCount; j++)
{
IField field = fields.get_Field(j);
if (field.Type == esriFieldType.esriFieldTypeGeometry)
{
IGeometryDef geometryDef = field.GeometryDef;
if (geometryDef.HasM)
{
this.routeLayerInfos.Add(new RouteLayerInfo(mapLayerInfo));
}
break;
}
}
}
}
}
/// <summary>
/// Get object MapServer of ServerObject
/// </summary>
/// <returns>object MapServer</returns>
private IMapServer3 GetMapServer()
{
IMapServer3 mapServer = this.serverObjectHelper.ServerObject as IMapServer3;
if (mapServer == null)
{
throw new DynamicSegmentationException("Unable to access the map server.");
}
return mapServer;
}
/// <summary>
/// create schema of soe
/// </summary>
/// <returns>resource of soe</returns>
private RestResource CreateRestSchema()
{
RestResource rootRes = new RestResource(this.soeName, false, this.RootResHandler);
RestResource infoResource = new RestResource("Info", false, this.InfoResHandler);
rootRes.resources.Add(infoResource);
RestResource helpResource = new RestResource("Help", false, this.HelpResHandler);
rootRes.resources.Add(helpResource);
RestResource item = new RestResource("RouteLayers", true, new ResourceHandler(this.RouteLayer));
RestOperation pointLocation = new RestOperation("PointLocation", new string[] { "routeIDFieldName", "routeID", "measure", "lateralOffset", "routeMeasureUnit", "routeLocationMeasureUnit" }, new string[] { "json" }, this.PointLocationOperHandler, "Point Location");
RestOperation lineLocation = new RestOperation("LineLocation", new string[] { "routeIDFieldName", "routeID", "fromMeasure", "toMeasure", "lateralOffset", "routeMeasureUnit", "routeLocationMeasureUnit" }, new string[] { "json" }, this.LineLocationOperHandler, "Line Location");
RestOperation identifyRoute = new RestOperation("IdentifyRoute", new string[] { "location", "tolerance", "routeMeasureUnit", "routeIDFieldName" }, new string[] { "json" }, this.IdentifyRouteOperHandler, "Identify Route");
RestOperation identifyRouteEx = new RestOperation("IdentifyRouteEx", new string[] { "location", "tolerance", "routeID", "routeMeasureUnit", "routeIDFieldName", "segmentExtension" }, new string[] { "json" }, this.IdentifyRouteExOperHandler, "Identify Route Ex");
item.operations.Add(pointLocation);
item.operations.Add(lineLocation);
item.operations.Add(identifyRoute);
item.operations.Add(identifyRouteEx);
rootRes.resources.Add(item);
return rootRes;
}
/// <summary>
/// handler of resource root
/// </summary>
/// <param name="boundVariables">list of variables bound</param>
/// <param name="outputFormat">format of output</param>
/// <param name="requestProperties">list of request properties</param>
/// <param name="responseProperties">list of response properties </param>
/// <returns>root resource in format output in byte</returns>
private byte[] RootResHandler(NameValueCollection boundVariables, string outputFormat, string requestProperties, out string responseProperties)
{
responseProperties = "{\"Content-Type\" : \"application/json\"}";
JsonObject[] objectArray = System.Array.ConvertAll(this.routeLayerInfos.ToArray(), i => i.ToJsonObject());
JsonObject jsonObject = new JsonObject();
jsonObject.AddString("Description", "Dynamic Segmentation Utility SOE Rest");
jsonObject.AddArray("routeLayers", objectArray);
return jsonObject.JsonByte();
}
/// <summary>
/// Returns JSON representation of Info resource. This resource is not a collection.
/// </summary>
/// <param name="boundVariables">list of variables bound</param>
/// <param name="outputFormat">format of output</param>
/// <param name="requestProperties">list of request properties</param>
/// <param name="responseProperties">list of response properties </param>
/// <returns>String JSON representation of Info resource.</returns>
private byte[] InfoResHandler(NameValueCollection boundVariables, string outputFormat, string requestProperties, out string responseProperties)
{
responseProperties = "{\"Content-Type\" : \"application/json\"}";
JsonObject result = new JsonObject();
AddInPackageAttribute addInPackage = (AddInPackageAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AddInPackageAttribute), false)[0];
result.AddString("agsVersion", addInPackage.TargetVersion);
result.AddString("soeVersion", addInPackage.Version);
result.AddString("author", addInPackage.Author);
result.AddString("company", addInPackage.Company);
return result.JsonByte();
}
/// <summary>
/// Returns JSON representation of Help resource. This resource is not a collection.
/// </summary>
/// <param name="boundVariables">list of variables bound</param>
/// <param name="outputFormat">format of output</param>
/// <param name="requestProperties">list of request properties</param>
/// <param name="responseProperties">list of response properties </param>
/// <returns>String JSON representation of Help resource.</returns>
private byte[] HelpResHandler(NameValueCollection boundVariables, string outputFormat, string requestProperties, out string responseProperties)
{
responseProperties = "{\"Content-Type\" : \"application/json\"}";
JsonObject result = new JsonObject();
JsonObject soeResources = new JsonObject();
soeResources.AddString("RouteLayers", "A list of polylineM layers in the map.");
result.AddJsonObject("Resources", soeResources);
JsonObject getIdentifyRouteInputs = new JsonObject();
getIdentifyRouteInputs.AddString("location", "(geometry) Point");
getIdentifyRouteInputs.AddString("tolerance", "(number) optional but if you don't set, soe set 0 so you could no have results");
getIdentifyRouteInputs.AddString("routeMeasureUnit", "(enum) optional default is esriUnknownUnits");
getIdentifyRouteInputs.AddString("routeIDFieldName", "(string)");
JsonObject getIdentifyRouteOutput = new JsonObject();
JsonObject getIdentifyRouteOutputLoc = new JsonObject();
getIdentifyRouteOutputLoc.AddString("routeID", "(string, double, int) depends fieldtype routeIDFieldName");
getIdentifyRouteOutputLoc.AddString("measure", "(number)");
getIdentifyRouteOutput.AddArray("location (array)", new JsonObject[] { getIdentifyRouteOutputLoc });
JsonObject getIdentifyRouteParams = new JsonObject();
getIdentifyRouteParams.AddString("Info", "Identify Route operation. To learn more about formatting the input geometries, input geometry, please visit the 'Geometry Objects' section of the ArcGIS Server REST documentation.");
getIdentifyRouteParams.AddJsonObject("Inputs", getIdentifyRouteInputs);
getIdentifyRouteParams.AddJsonObject("Outputs", getIdentifyRouteOutput);
JsonObject getPointLocationInputs = new JsonObject();
getPointLocationInputs.AddString("routeIDFieldName", "(string)");
getPointLocationInputs.AddString("routeID", "(string, double, int) depends fieldtype routeIDFieldName");
getPointLocationInputs.AddString("measure", "(number)");
getPointLocationInputs.AddString("lateralOffset", "(number) optional default = 0");
getPointLocationInputs.AddString("routeMeasureUnit", "(enum) optional default = esriUnknownUnits");
getPointLocationInputs.AddString("routeLocationMeasureUnit", "(enum) optional default = esriUnknownUnits");
JsonObject getPointLocationOutput = new JsonObject();
getPointLocationOutput.AddString("geometries", "(geometry) point or multipoint");
JsonObject getPointLocationParams = new JsonObject();
getPointLocationParams.AddString("Info", "Point Location operation.");
getPointLocationParams.AddJsonObject("Inputs", getPointLocationInputs);
getPointLocationParams.AddJsonObject("Outputs", getPointLocationOutput);
JsonObject getLineLocationInputs = new JsonObject();
getLineLocationInputs.AddString("routeIDFieldName", "(string)");
getLineLocationInputs.AddString("routeID", "(string, double, int) depends fieldtype routeIDFieldName");
getLineLocationInputs.AddString("fromMeasure", "(number) optional if you set toMeasure");
getLineLocationInputs.AddString("toMeasure", "(number) optional if you set fromMeasure");
getLineLocationInputs.AddString("lateralOffset", "(number) optional default = 0");
getLineLocationInputs.AddString("routeMeasureUnit", "(enum) optional default = esriUnknownUnits");
getLineLocationInputs.AddString("routeLocationMeasureUnit", "(enum) optional default = esriUnknownUnits");
JsonObject getLineLocationOutput = new JsonObject();
getLineLocationOutput.AddString("geometries", "(geometry) polyline");
JsonObject getLineLocationParams = new JsonObject();
getLineLocationParams.AddString("Info", "Line Location operation.");
getLineLocationParams.AddJsonObject("Inputs", getLineLocationInputs);
getLineLocationParams.AddJsonObject("Outputs", getLineLocationOutput);
JsonObject getIdentifyRouteExInputs = new JsonObject();
getIdentifyRouteExInputs.AddString("location", "(geometry) Point");
getIdentifyRouteExInputs.AddString("routeID", "(string, double, int) depends fieldtype routeIDFieldName");
getIdentifyRouteExInputs.AddString("tolerance", "(number) optional but if you don't set, soe set 0 so you could no have results");
getIdentifyRouteExInputs.AddString("routeMeasureUnit", "(enum) optional default = esriUnknownUnits");
getIdentifyRouteExInputs.AddString("routeIDFieldName", "(string)");
getIdentifyRouteExInputs.AddString("segmentExtension", "(enum) optional default = esriNoExtension");
JsonObject getIdentifyRouteExOutput = new JsonObject();
JsonObject getIdentifyRouteExOutputLoc = new JsonObject();
getIdentifyRouteExOutputLoc.AddString("routeID", "(string, double, int) depends fieldtype routeIDFieldName");
getIdentifyRouteExOutputLoc.AddString("measure", "(number)");
getIdentifyRouteExOutputLoc.AddString("location", "(geometry) point");
getIdentifyRouteExOutput.AddArray("location (array)", new JsonObject[] { getIdentifyRouteExOutputLoc });
JsonObject getIdentifyRouteExParams = new JsonObject();
getIdentifyRouteExParams.AddString("Info", "Identify Route Ex operation. To learn more about formatting the input geometries, input geometry, please visit the 'Geometry Objects' section of the ArcGIS Server REST documentation.");
getIdentifyRouteExParams.AddJsonObject("Inputs", getIdentifyRouteExInputs);
getIdentifyRouteExParams.AddJsonObject("Outputs", getIdentifyRouteExOutput);
JsonObject soeOperations = new JsonObject();
soeOperations.AddJsonObject("IdentifyRoute", getIdentifyRouteParams);
soeOperations.AddJsonObject("PointLocation", getPointLocationParams);
soeOperations.AddJsonObject("LineLocation", getLineLocationParams);
soeOperations.AddJsonObject("IdentifyRouteEx", getIdentifyRouteExParams);
result.AddJsonObject("Operations", soeOperations);
return result.JsonByte();
}
/// <summary>
/// resource RouteLayer
/// </summary>
/// <param name="boundVariables">list of variables bound</param>
/// <param name="outputFormat">format of output</param>
/// <param name="requestProperties">list of request properties</param>
/// <param name="responseProperties">list of response properties </param>
/// <returns>resource in byte</returns>
private byte[] RouteLayer(NameValueCollection boundVariables, string outputFormat, string requestProperties, out string responseProperties)
{
responseProperties = "{\"Content-Type\" : \"application/json\"}";
if (boundVariables["RouteLayersID"] == null)
{
JsonObject[] objectArray = System.Array.ConvertAll(this.routeLayerInfos.ToArray(), i => i.ToJsonObject());
JsonObject jsonObject = new JsonObject();
jsonObject.AddArray("routeLayers", objectArray);
return jsonObject.JsonByte();
}
else
{
int layerID = Convert.ToInt32(boundVariables["RouteLayersID"], CultureInfo.InvariantCulture);
return this.GetRouteLayerInfo(layerID).ToJsonObject().JsonByte();
}
}
/// <summary>
/// Handler operation Point Location
/// </summary>
/// <param name="boundVariables">list of variables bound</param>
/// <param name="operationInput">input of operation</param>
/// <param name="outputFormat">format of output</param>
/// <param name="requestProperties">list of request properties</param>
/// <param name="responseProperties">list of response properties </param>
/// <returns>response in byte</returns>
private byte[] PointLocationOperHandler(NameValueCollection boundVariables, JsonObject operationInput, string outputFormat, string requestProperties, out string responseProperties)
{
responseProperties = "{\"Content-Type\" : \"application/json\"}";
int routeLayerID = Convert.ToInt32(boundVariables["RouteLayersID"], CultureInfo.InvariantCulture);
esriUnits routeMeasureUnit = DSUtility.GetMeasureUnit(operationInput, MeasureUnit.routeMeasureUnit);
esriUnits routeLocationMeasureUnit = DSUtility.GetMeasureUnit(operationInput, MeasureUnit.routeLocationMeasureUnit);
double? measure;
bool found = operationInput.TryGetAsDouble("measure", out measure);
if (!found || !measure.HasValue)
{
throw new DynamicSegmentationException("measure not valid");
}
double? lateralOffset;
found = operationInput.TryGetAsDouble("lateralOffset", out lateralOffset);
if (!found || !lateralOffset.HasValue)
{
lateralOffset = 0;
}
IFeatureClass featureClass = this.GetRouteFeatureClass(routeLayerID);
string routeIDFieldNameValue = DSUtility.GetRouteIDFieldName(operationInput);
IFields fields = featureClass.Fields;
int indexField = fields.FindField(routeIDFieldNameValue);
if (indexField == -1)
{
throw new DynamicSegmentationException(string.Format(CultureInfo.InvariantCulture, "routeIDFieldName {0} not found!", routeIDFieldNameValue));
}
object routeID;
found = operationInput.TryGetObject("routeID", out routeID);
if (!found)
{
throw new DynamicSegmentationException("routeID not valid");
}
IField field = fields.get_Field(indexField);
this.CheckRouteID(routeID, field);
IRouteLocator2 routeLocator = DSUtility.GetRouteLocator(featureClass, routeIDFieldNameValue, routeMeasureUnit);
IRouteLocation routeLocation = new RouteMeasurePointLocationClass();
routeLocation.RouteID = routeID;
routeLocation.MeasureUnit = routeLocationMeasureUnit;
routeLocation.LateralOffset = lateralOffset.Value;
IRouteMeasurePointLocation routeMeasurePointLocation = (IRouteMeasurePointLocation)routeLocation;
routeMeasurePointLocation.Measure = measure.Value;
IGeometry geometry;
esriLocatingError locatingError;
routeLocator.Locate(routeLocation, out geometry, out locatingError);
int errorId = (int)locatingError;
JsonObject result = null;
if (errorId != 0)
{
string errorDescription = string.Format(CultureInfo.InvariantCulture, "{0} ({1})", Enum.GetName(typeof(esriLocatingError), locatingError), errorId);
IObjectJson jsonObjectError = new ObjectError(errorDescription);
result = jsonObjectError.ToJsonObject();
}
if ((geometry != null) && (!geometry.IsEmpty))
{
if (result == null)
{
result = new JsonObject();
}
result.AddJsonObject("geometries", Conversion.ToJsonObject(geometry));
}
return result.JsonByte();
}
/// <summary>
/// Handler operation Line Location
/// </summary>
/// <param name="boundVariables">list of variables bound</param>
/// <param name="operationInput">input of operation</param>
/// <param name="outputFormat">format of output</param>
/// <param name="requestProperties">list of request properties</param>
/// <param name="responseProperties">list of response properties </param>
/// <returns>response in byte</returns>
private byte[] LineLocationOperHandler(NameValueCollection boundVariables, JsonObject operationInput, string outputFormat, string requestProperties, out string responseProperties)
{
responseProperties = "{\"Content-Type\" : \"application/json\"}";
int routeLayerID = Convert.ToInt32(boundVariables["RouteLayersID"], CultureInfo.InvariantCulture);
esriUnits routeMeasureUnit = DSUtility.GetMeasureUnit(operationInput, MeasureUnit.routeMeasureUnit);
esriUnits routeLocationMeasureUnit = DSUtility.GetMeasureUnit(operationInput, MeasureUnit.routeLocationMeasureUnit);
double? fromMeasure;
operationInput.TryGetAsDouble("fromMeasure", out fromMeasure);
double? toMeasure;
operationInput.TryGetAsDouble("toMeasure", out toMeasure);
if (!fromMeasure.HasValue && !toMeasure.HasValue)
{
throw new DynamicSegmentationException("Set at least fromMeasure or toMeasure!");
}
double? lateralOffset;
bool found = operationInput.TryGetAsDouble("lateralOffset", out lateralOffset);
if (!found || !lateralOffset.HasValue)
{
lateralOffset = 0;
}
IFeatureClass featureClass = this.GetRouteFeatureClass(routeLayerID);
string routeIDFieldNameValue = DSUtility.GetRouteIDFieldName(operationInput);
IFields fields = featureClass.Fields;
int indexField = fields.FindField(routeIDFieldNameValue);
if (indexField == -1)
{
throw new DynamicSegmentationException(string.Format(CultureInfo.InvariantCulture, "routeIDFieldName {0} not found!", routeIDFieldNameValue));
}
object routeID;
found = operationInput.TryGetObject("routeID", out routeID);
if (!found)
{
throw new DynamicSegmentationException("routeID not valid");
}
IField field = fields.get_Field(indexField);
this.CheckRouteID(routeID, field);
IRouteLocator2 routeLocator = DSUtility.GetRouteLocator(featureClass, routeIDFieldNameValue, routeMeasureUnit);
IRouteLocation routeLocation = new RouteMeasureLineLocationClass();
routeLocation.RouteID = routeID;
routeLocation.MeasureUnit = routeLocationMeasureUnit;
routeLocation.LateralOffset = lateralOffset.Value;
IRouteMeasureLineLocation routeMeasureLineLocation = (IRouteMeasureLineLocation)routeLocation;
if (fromMeasure.HasValue)
{
routeMeasureLineLocation.FromMeasure = fromMeasure.Value;
}
if (toMeasure.HasValue)
{
routeMeasureLineLocation.ToMeasure = toMeasure.Value;
}
IGeometry geometry;
esriLocatingError locatingError;
routeLocator.Locate(routeLocation, out geometry, out locatingError);
int errorId = (int)locatingError;
JsonObject result = null;
if (errorId != 0)
{
string errorDescription = string.Format(CultureInfo.InvariantCulture, "{0} ({1})", Enum.GetName(typeof(esriLocatingError), locatingError), errorId);
IObjectJson jsonObjectError = new ObjectError(errorDescription);
result = jsonObjectError.ToJsonObject();
}
if ((geometry != null) && (!geometry.IsEmpty))
{
if (result == null)
{
result = new JsonObject();
}
result.AddJsonObject("geometries", Conversion.ToJsonObject(geometry));
}
return result.JsonByte();
}
/// <summary>
/// Handler operation Identify Route
/// </summary>
/// <param name="boundVariables">list of variables bound</param>
/// <param name="operationInput">input of operation</param>
/// <param name="outputFormat">format of output</param>
/// <param name="requestProperties">list of request properties</param>
/// <param name="responseProperties">list of response properties </param>
/// <returns>response in byte</returns>
private byte[] IdentifyRouteOperHandler(NameValueCollection boundVariables, JsonObject operationInput, string outputFormat, string requestProperties, out string responseProperties)
{
responseProperties = "{\"Content-Type\" : \"application/json\"}";
string methodName = MethodBase.GetCurrentMethod().Name;
int routeLayerID = Convert.ToInt32(boundVariables["RouteLayersID"], CultureInfo.InvariantCulture);
esriUnits routeMeasureUnit = DSUtility.GetMeasureUnit(operationInput, MeasureUnit.routeMeasureUnit);
IFeatureClass featureClass = this.GetRouteFeatureClass(routeLayerID);
string routeIDFieldNameValue = DSUtility.GetRouteIDFieldName(operationInput, featureClass.OIDFieldName);
IRouteLocator2 routeLocator = DSUtility.GetRouteLocator(featureClass, routeIDFieldNameValue, routeMeasureUnit);
double? tolerance;
bool found = operationInput.TryGetAsDouble("tolerance", out tolerance);
if (!found || !tolerance.HasValue)
{
tolerance = 0.0;
}
JsonObject jsonLocation;
if (!operationInput.TryGetJsonObject("location", out jsonLocation))
{
throw new ArgumentException("Invalid location", methodName);
}
IPoint location = Conversion.ToGeometry(jsonLocation, esriGeometryType.esriGeometryPoint) as IPoint;
if (location == null)
{
throw new ArgumentException("Invalid location", methodName);
}
IEnvelope envelope = location.Envelope;
envelope.Expand(tolerance.Value, tolerance.Value, false);
IRouteMeasurePointLocation routeMeasurePointLocation = new RouteMeasurePointLocationClass();
IRouteLocation routeLocation;
IFeature feature;
JsonObject result = new JsonObject();
List<JsonObject> measures = new List<JsonObject>();
IEnumRouteIdentifyResult enumResult = routeLocator.Identify(envelope, string.Empty);
for (int i = 1; i <= enumResult.Count; i++)
{
enumResult.Next(out routeLocation, out feature);
routeMeasurePointLocation = (IRouteMeasurePointLocation)routeLocation;
JsonObject measure = new JsonObject();
measure.AddString("routeID", routeLocation.RouteID.ToString());
measure.AddDouble("measure", routeMeasurePointLocation.Measure);
measures.Add(measure);
}
result.AddArray("location", measures.ToArray());
return result.JsonByte();
}
/// <summary>
/// Handler operation Identify Route Ex
/// </summary>
/// <param name="boundVariables">list of variables bound</param>
/// <param name="operationInput">input of operation</param>
/// <param name="outputFormat">format of output</param>
/// <param name="requestProperties">list of request properties</param>
/// <param name="responseProperties">list of response properties </param>
/// <returns>response in byte</returns>
private byte[] IdentifyRouteExOperHandler(NameValueCollection boundVariables, JsonObject operationInput, string outputFormat, string requestProperties, out string responseProperties)
{
responseProperties = "{\"Content-Type\" : \"application/json\"}";
string methodName = MethodBase.GetCurrentMethod().Name;
int routeLayerID = Convert.ToInt32(boundVariables["RouteLayersID"], CultureInfo.InvariantCulture);
esriUnits routeMeasureUnit = DSUtility.GetMeasureUnit(operationInput, MeasureUnit.routeMeasureUnit);
esriSegmentExtension segmentExtension = DSUtility.GetSegmentExtension(operationInput);
IFeatureClass featureClass = this.GetRouteFeatureClass(routeLayerID);
JsonObject jsonLocation;
if (!operationInput.TryGetJsonObject("location", out jsonLocation))
{
throw new ArgumentException("Invalid location", methodName);
}
IPoint location = Conversion.ToGeometry(jsonLocation, esriGeometryType.esriGeometryPoint) as IPoint;
if (location == null)
{
throw new ArgumentException("Invalid location", methodName);
}
string routeIDFieldNameValue = DSUtility.GetRouteIDFieldName(operationInput);
IFields fields = featureClass.Fields;
int indexField = fields.FindField(routeIDFieldNameValue);
if (indexField == -1)
{
throw new DynamicSegmentationException(string.Format(CultureInfo.InvariantCulture, "routeIDFieldName {0} not found!", routeIDFieldNameValue));
}
object routeID;
bool found = operationInput.TryGetObject("routeID", out routeID);
if (!found)
{
throw new DynamicSegmentationException("routeID not valid");
}
double? tolerance;
found = operationInput.TryGetAsDouble("tolerance", out tolerance);
if (!found || !tolerance.HasValue)
{
tolerance = 0.0;
}
IField field = fields.get_Field(indexField);
this.CheckRouteID(routeID, field);
IRouteLocator2 routeLocator = DSUtility.GetRouteLocator(featureClass, routeIDFieldNameValue, routeMeasureUnit);
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.SubFields = routeLocator.RouteIDFieldName;
queryFilter.AddField(routeLocator.RouteFeatureClass.ShapeFieldName);
string where = string.Format("{0} = {1}{2}{1}", routeLocator.RouteIDFieldNameDelimited, routeLocator.RouteIDIsString ? "'" : string.Empty, routeID);
queryFilter.WhereClause = where;
IPoint locationNearest = null;
IFeatureCursor featureCursor = null;
try
{
featureCursor = routeLocator.RouteFeatureClass.Search(queryFilter, true);
IFeature featureRoute = featureCursor.NextFeature();
if (featureRoute == null)
{
throw new DynamicSegmentationException(string.Format(CultureInfo.InvariantCulture, "Feature with value {0} not found!", routeID));
}
IProximityOperator proximityOperator = featureRoute.ShapeCopy as IProximityOperator;
locationNearest = proximityOperator.ReturnNearestPoint(location, segmentExtension);
}
catch
{
throw;
}
finally
{
if (featureCursor != null)
{
Marshal.ReleaseComObject(featureCursor);
}
}
IEnvelope envelope = locationNearest.Envelope;
envelope.Expand(tolerance.Value, tolerance.Value, false);
IRouteMeasurePointLocation routeMeasurePointLocation = new RouteMeasurePointLocationClass();
IRouteLocation routeLocation;
IFeature feature;
JsonObject result = new JsonObject();
List<JsonObject> measures = new List<JsonObject>();
IEnumRouteIdentifyResult enumResult = routeLocator.Identify(envelope, where);
for (int i = 1; i <= enumResult.Count; i++)
{
enumResult.Next(out routeLocation, out feature);
routeMeasurePointLocation = (IRouteMeasurePointLocation)routeLocation;
JsonObject measure = new JsonObject();
measure.AddString("routeID", routeLocation.RouteID.ToString());
measure.AddDouble("measure", routeMeasurePointLocation.Measure);
measure.AddJsonObject("location", Conversion.ToJsonObject(locationNearest, true));
measures.Add(measure);
}
result.AddArray("location", measures.ToArray());
return result.JsonByte();
}
/// <summary>
/// check if value of RouteID is string, double or string and check definition of its field
/// </summary>
/// <param name="routeID">value routeID</param>
/// <param name="field">field of routeID</param>
private void CheckRouteID(object routeID, IField field)
{
if ((field.Type == esriFieldType.esriFieldTypeInteger) || (field.Type == esriFieldType.esriFieldTypeOID))
{
try
{
int.Parse(routeID.ToString(), CultureInfo.InvariantCulture);
}
catch
{
throw new FormatException("routeID must be integer!");
}
}
else if (field.Type == esriFieldType.esriFieldTypeDouble)
{
try
{
double.Parse(routeID.ToString(), CultureInfo.InvariantCulture);
}
catch
{
throw new FormatException("routeID must be double!");
}
}
else if (field.Type == esriFieldType.esriFieldTypeString)
{
}
else
{
throw new DynamicSegmentationException(string.Format(CultureInfo.InvariantCulture, "Field Type routeIDFieldName must be string, double or integer!", field.Name));
}
}
}
}