PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/GPX.FireMap.Workflow/GPX.FireMap.Workflow/ConvertMGRSReference.cs

https://bitbucket.org/shope/dfu
C# | 227 lines | 139 code | 59 blank | 29 comment | 8 complexity | c87f311a3eac1aafeb80845ad306dbd0 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Activities;
  6. using System.ComponentModel;
  7. using GPX.Firemap.Workflow.Activities;
  8. using Geocortex.Logging;
  9. using Geocortex.Workflow.Activities;
  10. using ESRI.ArcGIS.Client.Tasks;
  11. using ESRI.ArcGIS.Client;
  12. using System.Net;
  13. using System.IO;
  14. using ESRI.ArcGIS.Client.Geometry;
  15. using Newtonsoft.Json.Linq;
  16. using ESRI.ArcGIS.Client.Symbols;
  17. namespace GPX.Firemap.Workflow.Activities.Server
  18. {
  19. /// <summary>
  20. /// Todo - document
  21. /// </summary>
  22. [Description("Converts an MGRS reference to a coordinate")]
  23. [WorkflowDesigner(
  24. DisplayName = "Convert MGRS Reference",
  25. ToolboxCategory = "Firemap Server Activities")]
  26. public sealed class ConvertMGRSReference : CodeActivity
  27. {
  28. [CategoryAttribute("In Arguments")]
  29. [Description(@"MGRS Reference")]
  30. [DefaultValue(null)]
  31. [DisplayName("MGRS Reference")]
  32. public InArgument<string> MGRSReference { get; set; }
  33. [CategoryAttribute("In Arguments")]
  34. [Description(@"Spatial Reference Identifier")]
  35. [DefaultValue(null)]
  36. [DisplayName("Spatial Reference Identifier")]
  37. public InArgument<int> Wkid { get; set; }
  38. [CategoryAttribute("In Arguments")]
  39. [Description(@"Convert MGRS Service URL")]
  40. [DefaultValue(null)]
  41. [DisplayName("Convert MGRS Service URL")]
  42. public InArgument<string> MGRSServiceUrl { get; set; }
  43. [CategoryAttribute("Out Arguments")]
  44. [Description(@"Geometries")]
  45. [DefaultValue(null)]
  46. [DisplayName("Geometries")]
  47. public OutArgument<FeatureSet> Geometries { get; set; }
  48. /// <summary>
  49. /// When implemented in a derived class, performs the execution of the activity.
  50. /// </summary>
  51. /// <param name="context">The execution context under which the activity executes.</param>
  52. protected override void Execute(CodeActivityContext context)
  53. {
  54. try
  55. {
  56. // Convert to a Geocortex geometry
  57. Logger.SystemInfo(this.DisplayName + " beginning activity execute ");
  58. // Obtain the runtime values
  59. string inputCoordinate = context.GetValue(this.MGRSReference);
  60. if (string.IsNullOrEmpty(inputCoordinate))
  61. Logger.SystemError("The MGRS reference is not defined");
  62. string serviceUrl = context.GetValue(this.MGRSServiceUrl);
  63. if (string.IsNullOrEmpty(serviceUrl))
  64. Logger.SystemError("The MGRS service url is not defined");
  65. int inputSrid = context.GetValue(this.Wkid);
  66. if (inputSrid==0)
  67. Logger.SystemError("The WKID is not defined");
  68. JObject serverResponse;
  69. AGSServerSupport.CallArcGISServer(AGSServerSupport.CreateMGRSConvertRequest(serviceUrl, inputCoordinate, inputSrid.ToString()), out serverResponse);
  70. FeatureSet fs = new FeatureSet();
  71. fs.GeometryType = ESRI.ArcGIS.Client.Tasks.GeometryType.Point;
  72. if (serverResponse != null)
  73. {
  74. //List<MapPoint> points = new List<MapPoint>();
  75. string wkid = (string)serverResponse["wkid"];
  76. SpatialReference sr = new SpatialReference(Convert.ToInt32(wkid));
  77. //SH - DFU-40
  78. //JArray coordinates = (JArray)serverResponse["ConvertedCoordinates"];
  79. JArray coordinates = (JArray)serverResponse["convertedReferences"];
  80. foreach (var coordinate in coordinates)
  81. {
  82. string reference = (string)coordinate["reference"];
  83. float x = (float)coordinate["point"]["x"];
  84. float y = (float)coordinate["point"]["y"];
  85. // float x = (float)coordinate["x"];
  86. // float y = (float)coordinate["y"];
  87. MapPoint pnt = new MapPoint(x, y);
  88. pnt.SpatialReference = sr;
  89. Graphic grp = CreateGraphic(reference, pnt);
  90. if (grp != null)
  91. fs.Features.Add(grp);
  92. //points.Add(pnt);
  93. }
  94. // fs = CreateFeatureSet(points);
  95. }
  96. context.SetValue(Geometries, fs);
  97. Logger.SystemInfo(this.DisplayName + " ending activity execute ");
  98. }
  99. catch (System.Exception e)
  100. {
  101. Logger.SystemError("ERROR occurred in " + this.DisplayName + e.Message);
  102. }
  103. }
  104. //SH - DFU-40
  105. /// <summary>
  106. /// Creates the graphic.
  107. /// </summary>
  108. /// <param name="reference">The reference.</param>
  109. /// <param name="point">The point.</param>
  110. /// <returns></returns>
  111. private Graphic CreateGraphic(string reference, MapPoint point)
  112. {
  113. Graphic grp = null;
  114. try
  115. {
  116. SimpleMarkerSymbol symbol = new SimpleMarkerSymbol();
  117. symbol.Style = SimpleMarkerSymbol.SimpleMarkerStyle.Circle;
  118. symbol.Size = 20;
  119. grp = new Graphic();
  120. grp.Symbol = symbol;
  121. grp.Geometry = point;
  122. grp.Attributes.Add("reference", reference);
  123. return grp;
  124. }
  125. catch (System.Exception e)
  126. {
  127. Logger.SystemError("ERROR occurred in " + this.DisplayName + e.Message);
  128. return grp;
  129. }
  130. finally
  131. {
  132. }
  133. }
  134. /// <summary>
  135. /// Creates a client feature set from an array of points.
  136. /// </summary>
  137. /// <param name="points">The points.</param>
  138. /// <returns></returns>
  139. /// <remarks>Adds a graphic to each point for display</remarks>
  140. private FeatureSet CreateFeatureSet(List<MapPoint> points)
  141. {
  142. FeatureSet fs = new FeatureSet();
  143. try
  144. {
  145. fs.GeometryType = ESRI.ArcGIS.Client.Tasks.GeometryType.Point;
  146. SimpleMarkerSymbol symbol = new SimpleMarkerSymbol();
  147. symbol.Style = SimpleMarkerSymbol.SimpleMarkerStyle.Circle;
  148. symbol.Size = 20;
  149. for (int i = 0; i < points.Count; i++)
  150. {
  151. Graphic grp = new Graphic();
  152. grp.Symbol = symbol;
  153. grp.Geometry = points[i];
  154. fs.Features.Add(grp);
  155. }
  156. return fs;
  157. }
  158. catch (System.Exception e)
  159. {
  160. Logger.SystemError("ERROR occurred in " + this.DisplayName + e.Message);
  161. return fs;
  162. }
  163. finally
  164. {
  165. }
  166. }
  167. }
  168. }