PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/BAEM/Contour-Plugin/forms/frm_setup.cs

http://aor.codeplex.com
C# | 191 lines | 150 code | 23 blank | 18 comment | 8 complexity | 2056935c9124a92a59173ef7c3759217 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-3.0
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * This software product has been produced with public funds and as such is considered public domain software. It may be freely used for non-commercial, scientific,
  3. * and/or educational purposes.
  4. *
  5. * Neither the U.S. Government nor any of their employees, makes any warranty, express or implied, including the warranties of merchantability and fitness for a particular purpose,
  6. * or assumes any legal liability or responsibility for the accuracy, completeness, or usefulness of any code, information, apparatus, product, or process disclosed, or represents that
  7. * its use would not infringe on privately owned rights.
  8. *
  9. * This code was created by James Rineer on 9-16-2010 while working at RTI International (www.rti.org),
  10. * Environment, Health, and Economics Division.
  11. *
  12. * Contributor(s):
  13. *
  14. * ***** END LICENSE BLOCK ***** */
  15. using System;
  16. using System.Windows.Forms;
  17. using System.Collections.Generic;
  18. using System.Configuration;
  19. using System.Data;
  20. using System.Drawing;
  21. using System.IO;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. using System.Xml;
  25. using DotSpatial.Controls;
  26. using DotSpatial.Data;
  27. using DotSpatial.Topology;
  28. using gov.epa.BASINS.utils;
  29. using gov.epa.BASINS.tools;
  30. using System.Reflection;
  31. using IronPython.Runtime;
  32. using System.Numerics;
  33. using NLog;
  34. namespace gov.epa.BASINS.plugins.contour.forms
  35. {
  36. public partial class frm_setup : Form
  37. {
  38. private static readonly Logger Logger = NLog.LogManager.GetCurrentClassLogger();
  39. private IMapPluginArgs _mapArgs = null;
  40. public frm_setup(IMapPluginArgs mapArgs)
  41. {
  42. _mapArgs = mapArgs;
  43. InitializeComponent();
  44. txtCodeFragement.Text = @"
  45. # This example asks python to generate x random points and then to return
  46. # contours of this data. The algorithm called can be found in matplotlib and
  47. # originates from NCAR. Below is a snippet of the python code which
  48. # generates the random points. The contours will be added to the map.
  49. # It should be fairly straight forward to extend this exmaple
  50. # to read from point shape files or other sources of point data.
  51. x = npr.normal(0,size=npts) # create some normally distributed dependent data in x.
  52. y = npr.normal(0,size=npts) # ... do the same for y.
  53. zorig = x**2 + y**2 # z is a function of the form z = f(x, y).
  54. noise = npr.normal(0,scale=6.0, size=npts) # add a good amount of noise
  55. z = zorig + noise # z = f(x, y) = x**2 + y**2";
  56. }
  57. private void btnGo_Click(object sender, EventArgs e)
  58. {
  59. List<int> Contours = new List<int>();
  60. int startContour = System.Convert.ToInt32(txtStartContour.Text);
  61. int endContour = System.Convert.ToInt32(txtEndContour.Text);
  62. int interval = System.Convert.ToInt32(txtInterval.Text);
  63. for (int x = startContour; x <= endContour; x = x + interval)
  64. {
  65. Contours.Add(x);
  66. }
  67. Dictionary<string, object> aDictionary = new Dictionary<string, object>();
  68. //def create_random_contour_dict2(npts,contours):
  69. aDictionary.Add("npts", System.Convert.ToInt32(txtNumpts.Text));
  70. aDictionary.Add("contours", Contours);
  71. string path = Assembly.GetExecutingAssembly().Location;
  72. string rootDir = Directory.GetParent(path).FullName;
  73. CommTYPES ContourPythonCommType=GetCommType(ConfigurationManager.AppSettings["ContourPythonCommType"]);
  74. string ContourSecurityKey=ConfigurationManager.AppSettings["ContourSecurityKey"];
  75. string ContourIronPythonProxyCode=Path.Combine(rootDir, ConfigurationManager.AppSettings["ContourIronPythonProxyCode"]);
  76. string ContourPathToIronPythonLib=Path.Combine(rootDir, ConfigurationManager.AppSettings["ContourPathToIronPythonLib"]);
  77. string ContourPathToCPythonCode=Path.Combine(rootDir, ConfigurationManager.AppSettings["ContourPathToCPythonCode"]);
  78. string ContourExecutable=Path.Combine(rootDir, ConfigurationManager.AppSettings["ContourExecutable"]);
  79. string ContourPythonDebugLevel=ConfigurationManager.AppSettings["ContourPythonDebugLevel"];
  80. //RTI_CPythonProxy _fe = new RTI_CPythonProxy("create_random_contour_dict2", aDictionary, ContourPythonCommType, ContourSecurityKey, ContourIronPythonProxyCode, ContourPathToIronPythonLib, ContourPathToCPythonCode, ContourExecutable, ContourPythonDebugLevel);
  81. RTI_CPythonProxy _fe = new RTI_CPythonProxy("create_random_contour_dict2", aDictionary, ContourSecurityKey, ContourIronPythonProxyCode, ContourPathToIronPythonLib, ContourPathToCPythonCode, ContourExecutable, ContourPythonDebugLevel);
  82. frm_Progress frmProg = new frm_Progress();
  83. frmProg.fe = _fe;
  84. frmProg.RunType = ContourPythonCommType;
  85. if (frmProg.ShowDialog(this) == DialogResult.OK)
  86. {
  87. _fe = frmProg.fe;
  88. }
  89. else
  90. {
  91. _fe = null;
  92. }
  93. frmProg.Dispose();
  94. if (_fe != null)
  95. {
  96. List<object> localResult = (List<object>)_fe.resultObject;
  97. //StringBuilder sb = new StringBuilder();
  98. IFeatureSet ifs = CreateFeatureSet();
  99. int contourCount = 0;
  100. foreach (IronPython.Runtime.List acontour in localResult)
  101. {
  102. contourCount++;
  103. //int polygonCount = 0;
  104. foreach (IronPython.Runtime.List apolygon in acontour)
  105. {
  106. IFeature aLine = CreateLine(apolygon);
  107. AddLineToFeatureSet(ifs, aLine, (double)Contours[contourCount - 1]);
  108. }
  109. }
  110. AddFeatureSetToMap(ifs);
  111. }
  112. }
  113. private void AddFeatureSetToMap(IFeatureSet ifs){
  114. ifs.Name = "Contours";
  115. SaveFileDialog sfd = new SaveFileDialog();
  116. sfd.Filter = "Shape File (*.shp)|*.shp";
  117. sfd.Title = "Save a Shape File";
  118. sfd.ShowDialog();
  119. if (sfd.FileName !="")
  120. {
  121. ifs.SaveAs(sfd.FileName, true);
  122. }
  123. _mapArgs.Map.Layers.Add(ifs);
  124. }
  125. private IFeatureSet CreateFeatureSet(){
  126. IFeatureSet ifs = AddFields(null);
  127. return ifs;
  128. }
  129. private IFeature CreateLine(IronPython.Runtime.List apolygon)
  130. {
  131. List<Coordinate> coordinates = new List<Coordinate>();
  132. foreach (IronPython.Runtime.List coordinate in apolygon)
  133. {
  134. Coordinate aMWCoord = new Coordinate((double)coordinate[0], (double)coordinate[1]);
  135. coordinates.Add(aMWCoord);
  136. }
  137. IFeature lineFeat = new Feature(FeatureType.Line,coordinates);
  138. return lineFeat;
  139. }
  140. private void AddLineToFeatureSet(IFeatureSet ifs, IFeature lineFeat, double fieldVal)
  141. {
  142. IFeature curFeat=ifs.AddFeature(lineFeat);
  143. curFeat.DataRow["Contour"] = fieldVal;
  144. }
  145. private IFeatureSet AddFields(IFeatureSet ifs)
  146. {
  147. if (ifs == null)
  148. {
  149. ifs = new FeatureSet();
  150. }
  151. ifs.DataTable.Columns.Add("Contour", System.Type.GetType("System.Double"));
  152. return ifs;
  153. }
  154. private CommTYPES GetCommType(string theType)
  155. {
  156. CommTYPES ct = (CommTYPES)Enum.Parse(typeof(CommTYPES), theType, true);
  157. return ct;
  158. }
  159. private void button1_Click(object sender, EventArgs e)
  160. {
  161. frm_exportProject export = new frm_exportProject(_mapArgs);
  162. export.ShowDialog();
  163. export.Close();
  164. export.Dispose();
  165. }
  166. }
  167. }