/BAEM/Contour-Plugin/forms/frm_setup.cs
C# | 191 lines | 150 code | 23 blank | 18 comment | 8 complexity | 2056935c9124a92a59173ef7c3759217 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-3.0
- /* ***** BEGIN LICENSE BLOCK *****
- * 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,
- * and/or educational purposes.
- *
- * 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,
- * 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
- * its use would not infringe on privately owned rights.
- *
- * This code was created by James Rineer on 9-16-2010 while working at RTI International (www.rti.org),
- * Environment, Health, and Economics Division.
- *
- * Contributor(s):
- *
- * ***** END LICENSE BLOCK ***** */
-
- using System;
- using System.Windows.Forms;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Text;
- using System.Windows.Forms;
- using System.Xml;
- using DotSpatial.Controls;
- using DotSpatial.Data;
- using DotSpatial.Topology;
- using gov.epa.BASINS.utils;
- using gov.epa.BASINS.tools;
- using System.Reflection;
- using IronPython.Runtime;
- using System.Numerics;
- using NLog;
-
- namespace gov.epa.BASINS.plugins.contour.forms
- {
- public partial class frm_setup : Form
- {
- private static readonly Logger Logger = NLog.LogManager.GetCurrentClassLogger();
-
- private IMapPluginArgs _mapArgs = null;
-
- public frm_setup(IMapPluginArgs mapArgs)
- {
- _mapArgs = mapArgs;
-
- InitializeComponent();
-
- txtCodeFragement.Text = @"
- # This example asks python to generate x random points and then to return
- # contours of this data. The algorithm called can be found in matplotlib and
- # originates from NCAR. Below is a snippet of the python code which
- # generates the random points. The contours will be added to the map.
- # It should be fairly straight forward to extend this exmaple
- # to read from point shape files or other sources of point data.
-
- x = npr.normal(0,size=npts) # create some normally distributed dependent data in x.
- y = npr.normal(0,size=npts) # ... do the same for y.
- zorig = x**2 + y**2 # z is a function of the form z = f(x, y).
- noise = npr.normal(0,scale=6.0, size=npts) # add a good amount of noise
- z = zorig + noise # z = f(x, y) = x**2 + y**2";
- }
-
- private void btnGo_Click(object sender, EventArgs e)
- {
- List<int> Contours = new List<int>();
- int startContour = System.Convert.ToInt32(txtStartContour.Text);
- int endContour = System.Convert.ToInt32(txtEndContour.Text);
- int interval = System.Convert.ToInt32(txtInterval.Text);
- for (int x = startContour; x <= endContour; x = x + interval)
- {
- Contours.Add(x);
- }
-
- Dictionary<string, object> aDictionary = new Dictionary<string, object>();
- //def create_random_contour_dict2(npts,contours):
- aDictionary.Add("npts", System.Convert.ToInt32(txtNumpts.Text));
- aDictionary.Add("contours", Contours);
-
- string path = Assembly.GetExecutingAssembly().Location;
- string rootDir = Directory.GetParent(path).FullName;
-
- CommTYPES ContourPythonCommType=GetCommType(ConfigurationManager.AppSettings["ContourPythonCommType"]);
- string ContourSecurityKey=ConfigurationManager.AppSettings["ContourSecurityKey"];
- string ContourIronPythonProxyCode=Path.Combine(rootDir, ConfigurationManager.AppSettings["ContourIronPythonProxyCode"]);
- string ContourPathToIronPythonLib=Path.Combine(rootDir, ConfigurationManager.AppSettings["ContourPathToIronPythonLib"]);
- string ContourPathToCPythonCode=Path.Combine(rootDir, ConfigurationManager.AppSettings["ContourPathToCPythonCode"]);
- string ContourExecutable=Path.Combine(rootDir, ConfigurationManager.AppSettings["ContourExecutable"]);
- string ContourPythonDebugLevel=ConfigurationManager.AppSettings["ContourPythonDebugLevel"];
-
- //RTI_CPythonProxy _fe = new RTI_CPythonProxy("create_random_contour_dict2", aDictionary, ContourPythonCommType, ContourSecurityKey, ContourIronPythonProxyCode, ContourPathToIronPythonLib, ContourPathToCPythonCode, ContourExecutable, ContourPythonDebugLevel);
- RTI_CPythonProxy _fe = new RTI_CPythonProxy("create_random_contour_dict2", aDictionary, ContourSecurityKey, ContourIronPythonProxyCode, ContourPathToIronPythonLib, ContourPathToCPythonCode, ContourExecutable, ContourPythonDebugLevel);
- frm_Progress frmProg = new frm_Progress();
- frmProg.fe = _fe;
- frmProg.RunType = ContourPythonCommType;
- if (frmProg.ShowDialog(this) == DialogResult.OK)
- {
- _fe = frmProg.fe;
- }
- else
- {
- _fe = null;
- }
- frmProg.Dispose();
-
- if (_fe != null)
- {
- List<object> localResult = (List<object>)_fe.resultObject;
- //StringBuilder sb = new StringBuilder();
- IFeatureSet ifs = CreateFeatureSet();
- int contourCount = 0;
- foreach (IronPython.Runtime.List acontour in localResult)
- {
- contourCount++;
- //int polygonCount = 0;
- foreach (IronPython.Runtime.List apolygon in acontour)
- {
- IFeature aLine = CreateLine(apolygon);
- AddLineToFeatureSet(ifs, aLine, (double)Contours[contourCount - 1]);
- }
- }
- AddFeatureSetToMap(ifs);
- }
- }
-
- private void AddFeatureSetToMap(IFeatureSet ifs){
- ifs.Name = "Contours";
- SaveFileDialog sfd = new SaveFileDialog();
- sfd.Filter = "Shape File (*.shp)|*.shp";
- sfd.Title = "Save a Shape File";
- sfd.ShowDialog();
-
-
- if (sfd.FileName !="")
- {
- ifs.SaveAs(sfd.FileName, true);
- }
-
- _mapArgs.Map.Layers.Add(ifs);
- }
-
- private IFeatureSet CreateFeatureSet(){
- IFeatureSet ifs = AddFields(null);
- return ifs;
- }
-
- private IFeature CreateLine(IronPython.Runtime.List apolygon)
- {
- List<Coordinate> coordinates = new List<Coordinate>();
- foreach (IronPython.Runtime.List coordinate in apolygon)
- {
- Coordinate aMWCoord = new Coordinate((double)coordinate[0], (double)coordinate[1]);
- coordinates.Add(aMWCoord);
- }
- IFeature lineFeat = new Feature(FeatureType.Line,coordinates);
- return lineFeat;
- }
-
- private void AddLineToFeatureSet(IFeatureSet ifs, IFeature lineFeat, double fieldVal)
- {
- IFeature curFeat=ifs.AddFeature(lineFeat);
- curFeat.DataRow["Contour"] = fieldVal;
- }
-
- private IFeatureSet AddFields(IFeatureSet ifs)
- {
- if (ifs == null)
- {
- ifs = new FeatureSet();
- }
- ifs.DataTable.Columns.Add("Contour", System.Type.GetType("System.Double"));
- return ifs;
- }
-
- private CommTYPES GetCommType(string theType)
- {
- CommTYPES ct = (CommTYPES)Enum.Parse(typeof(CommTYPES), theType, true);
- return ct;
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- frm_exportProject export = new frm_exportProject(_mapArgs);
- export.ShowDialog();
- export.Close();
- export.Dispose();
- }
- }
-
- }