User Tools

Site Tools


source_code_serviceseditionsample_isolineswithkriging_cs_120914.zip

Source Code ServicesEditionSample IsolinesWithKriging CS 120914.zip

Program.cs

 using System;  
 using System.Windows.Forms;  
 
 namespace KrigingInterpolationTest  
 {  
     static class Program  
     {  
         /// <summary>  
         /// The main entry point for the application.  
         /// </summary>  
         [STAThread]  
         static void Main()  
         {  
             Application.EnableVisualStyles();  
             Application.SetCompatibleTextRenderingDefault(false);  
             Application.Run(new Sample());  
         }  
     }  
 }  
 

Sample.cs

 using System;  
 using System.Collections.Generic;  
 using System.Collections.ObjectModel;  
 using System.Drawing;  
 using System.IO;  
 using System.Windows.Forms;  
 using KrigingInterpolationTest;  
 using ThinkGeo.MapSuite.Core;  
 
 public partial class Sample : Form  
 {  
     private MapEngine mapEngine;  
     private Bitmap bitmap;  
 
     private string gridFilePath = @"..\..\Data\result.grid";  
     private string wellDepthPointDataFilePath = @"..\..\Data\GrayCountyIrrigationWellDepths.csv";  
     private Dictionary<PointShape, double> wellDepthPointData;  
 
     public Sample()  
     {  
         InitializeComponent();  
     }  
 
     private void DisplayASimpleMap_Load(object sender, EventArgs e)  
     {  
         bitmap = new Bitmap(map.Width, map.Height);  
         mapEngine = new MapEngine();  
 
         WorldMapKitLayer worldMapKitLayer = new WorldMapKitLayer();  
         mapEngine.StaticLayers.Add("WorldMapKitOverlay", worldMapKitLayer);  
 
         mapEngine.ShowLogo = true;  
         mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);  
 
         mapEngine.CurrentExtent = ExtentHelper.SnapToZoomLevel(new RectangleShape(-100.655827718567, 37.7441933095704, -100.613770681213, 37.7186586797486), GeographyUnit.DecimalDegree, map.Width, map.Height, new ZoomLevelSet());  
 
         this.wellDepthPointData = this.GetWellDepthPointDataFromCSV(this.wellDepthPointDataFilePath);  
 
         mapEngine.StaticLayers.Add("IsoLineLayer", GetGridIsoLineLayer());  
         mapEngine.StaticLayers.Add("GridCellsLayer", GetGridFeatureLayer());  
         mapEngine.StaticLayers.Add("WellsLayer", GetWellDepthPointLayer());  
 
         DrawImage();  
     }  
 
     private Dictionary<PointShape, double> GetWellDepthPointDataFromCSV(string csvFilePath)  
     {  
         StreamReader streamReader = null;  
         Dictionary<PointShape, double> wellDataPoints = new Dictionary<PointShape, double>();  
 
         try  
         {  
             streamReader = new StreamReader(csvFilePath);  
             string headline = streamReader.ReadLine();  
             while (!streamReader.EndOfStream)  
             {  
                 string line = streamReader.ReadLine();  
 
                 string[] parts = line.Split(',');  
                 wellDataPoints.Add(new PointShape(double.Parse(parts[0]), double.Parse(parts[1])), double.Parse(parts[2]));  
             }  
         }  
         finally  
         {  
             if (streamReader != null) { streamReader.Close(); }  
         }  
         return wellDataPoints;  
     }  
 
     private void CreateGridFile()  
     {  
         //Get the current extent since we use that to gerenate the grid.  Of course this is just for  
         //the demo and in the real world you can use any extent you like  
         RectangleShape currentDrawingExtent = ExtentHelper.GetDrawingExtent(mapEngine.CurrentExtent, map.Width, map.Height);  
 
         //Calculate the cell size based on how many rows and columns you specified  
         double cellSize = Math.Min(currentDrawingExtent.Width / double.Parse(txtGridIsoLineCellColumnCount.Text), currentDrawingExtent.Height / Int32.Parse(txtGridIsoLineCellRowCount.Text));  
 
         //Greate the grid definition based on the extent, cell size etc.  
         GridDefinition gridDefinition = new GridDefinition(currentDrawingExtent, cellSize, -9999, wellDepthPointData);  
 
         FileStream gridFileStream = null;  
         try  
         {  
             gridFileStream = new FileStream(gridFilePath, FileMode.Create, FileAccess.ReadWrite);  
 
             KrigingGridInterpolationModel interpolationModel;  
             int referencingPointCount = int.Parse(this.txtRefrencingPoint.Text);  
             if (this.rdoCircularKrigingModel.Checked)  
             {  
                 interpolationModel = new CircularKrigingGridInterpolationModel(wellDepthPointData, referencingPointCount);  
             }  
             else if (this.rdoExponentialKrigingModel.Checked)  
             {  
                 interpolationModel = new ExponentialKrigingGridInterpolationModel(wellDepthPointData, referencingPointCount);  
             }  
             else if (this.rdoGaussianKrigingModel.Checked)  
             {  
                 interpolationModel = new GaussianKrigingGridInterpolationModel(wellDepthPointData, referencingPointCount);  
             }  
             else if (this.rdoLinealKrigingModel.Checked)  
             {  
                 interpolationModel = new LinealKrigingGridInterpolationModel(wellDepthPointData, referencingPointCount);  
             }  
             else if (this.rdoSphericalKrigingModel.Checked)  
             {  
                 interpolationModel = new SphericalKrigingGridInterpolationModel(wellDepthPointData, referencingPointCount);  
             }  
             else  
             {  
                 interpolationModel = new LinealKrigingGridInterpolationModel(wellDepthPointData, referencingPointCount);  
             }  
 
             GridFeatureSource.GenerateGrid(gridDefinition, interpolationModel, gridFileStream);  
         }  
         finally  
         {  
             if (gridFileStream != null) { gridFileStream.Close(); }  
         }  
     }  
 
     private GridIsoLineLayer GetGridIsoLineLayer()  
     {  
         //Create a grid file based on the textboxes and the current extent  
         CreateGridFile();  
 
         //Load the grid we just created into the GridIsoLineLayer using the number of breaks defines  
         //on the screen  
         Collection<double> isoLineLevels = GridIsoLineLayer.GetIsoLineLevels(wellDepthPointData, Convert.ToInt32(txtGridIsoLineLevelCount.Text));  
         GridIsoLineLayer isoLineLayer = new GridIsoLineLayer(gridFilePath, isoLineLevels);  
         //Create a series of colors from blue to red that we will use for the breaks  
         Collection<GeoColor> colors = GeoColor.GetColorsInQualityFamily(GeoColor.StandardColors.Blue, GeoColor.StandardColors.Red, isoLineLevels.Count, ColorWheelDirection.Clockwise);  
 
         //Setup a class break style based on the isoline levels and the colors  
         ClassBreakStyle classBreakLineStyle = new ClassBreakStyle(isoLineLayer.DataValueColumnName);  
         LineStyle firstStyle = new LineStyle(new GeoPen(colors[0], 3));  
         classBreakLineStyle.ClassBreaks.Add(new ClassBreak(double.MinValue, firstStyle));  
         for (int i = 0; i < colors.Count - 1; i++)  
         {  
             LineStyle style = new LineStyle(new GeoPen(colors[i|+ 1], 3));  
             classBreakLineStyle.ClassBreaks.Add(new ClassBreak(isoLineLevels[i], style));  
         }  
         isoLineLayer.CustomStyles.Add(classBreakLineStyle);  
 
         //Create the text styles to label the lines  
         TextStyle textStyle = TextStyles.CreateSimpleTextStyle(isoLineLayer.DataValueColumnName, "Arial", 8, DrawingFontStyles.Bold, GeoColor.StandardColors.Black, 0, 0);  
         textStyle.OverlappingRule = LabelOverlappingRule.NoOverlapping;  
         textStyle.SplineType = SplineType.StandardSplining;  
         textStyle.DuplicateRule = LabelDuplicateRule.UnlimitedDuplicateLabels;  
         textStyle.TextLineSegmentRatio = 9999999;  
         textStyle.FittingLineInScreen = true;  
         textStyle.SuppressPartialLabels = true;  
         isoLineLayer.CustomStyles.Add(textStyle);  
 
         return isoLineLayer;  
     }  
 
     private GridFeatureLayer GetGridFeatureLayer()  
     {  
         GridIsoLineLayer gridIsoLineLayer = (GridIsoLineLayer)mapEngine.StaticLayers["IsoLineLayer"];  
 
         gridIsoLineLayer.Open();  
 
         //Get the line breaks  
         Collection<double> isoLineBreaks = GridIsoLineLayer.GetIsoLineLevels(wellDepthPointData, Convert.ToInt32(txtGridIsoLineLevelCount.Text));  
 
         //Load a new GridFeatureLayer based on the current grid file  
         GridFeatureLayer gridFeatureLayer = new GridFeatureLayer(gridFilePath);  
 
         //Create a series of colors from blue to red that we will use for the breaks  
         Collection<GeoColor> colors = GeoColor.GetColorsInQualityFamily(GeoColor.StandardColors.Blue, GeoColor.StandardColors.Red, isoLineBreaks.Count, ColorWheelDirection.Clockwise);  
 
         //Create a class break style  
         ClassBreakStyle classBreakLineStyle = new ClassBreakStyle(gridFeatureLayer.DataValueColumnName);  
 
         //Setup a class break style based on the isoline levels and the colors  
         AreaStyle firstStyle = new AreaStyle(new GeoPen(GeoColor.FromArgb(50, colors[0]), 1), new GeoSolidBrush(GeoColor.FromArgb(50, colors[0])));  
         classBreakLineStyle.ClassBreaks.Add(new ClassBreak(double.MinValue, firstStyle));  
         for (int i = 1; i < colors.Count - 1; i++)  
         {  
             AreaStyle style = new AreaStyle(new GeoPen(GeoColor.FromArgb(50, colors[i]), 1), new GeoSolidBrush(GeoColor.FromArgb(50, colors[i])));  
             classBreakLineStyle.ClassBreaks.Add(new ClassBreak(isoLineBreaks[i], style));  
         }  
 
         gridFeatureLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(classBreakLineStyle);  
         gridFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
         return gridFeatureLayer;  
     }  
 
     private InMemoryFeatureLayer GetWellDepthPointLayer()  
     {  
         //Create an in memory layer to hold the well data from the text file  
         InMemoryFeatureLayer inMemoryFeatureLayer = new InMemoryFeatureLayer();  
         inMemoryFeatureLayer.FeatureSource.Open();  
         //Make sure to specify the depth column  
         inMemoryFeatureLayer.Columns.Add(new FeatureSourceColumn("Depth", "String", 10));  
 
         //Loop through all the point data and add it to the in memory layer  
         foreach (KeyValuePair<PointShape, double> wellDepth in wellDepthPointData)  
         {  
             Feature feature = new Feature(wellDepth.Key);  
             feature.ColumnValues.Add("Depth", wellDepth.Value.ToString());  
             inMemoryFeatureLayer.InternalFeatures.Add(feature);  
         }  
         //Now that all of the data is added we can build an in memory index to make the lookups fast  
         inMemoryFeatureLayer.BuildIndex();  
 
         //Create the well point style  
         PointStyle pointStyle1 = PointStyles.CreateSimpleCircleStyle(GeoColor.StandardColors.White, 4, GeoColor.SimpleColors.Black, 2);  
         inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(pointStyle1);  
 
         //Create the text style with a halo  
         TextStyle textStyle = TextStyles.CreateSimpleTextStyle("Depth", "Arial", 10, DrawingFontStyles.Regular, GeoColor.SimpleColors.Black);  
         textStyle.HaloPen = new GeoPen(GeoColor.StandardColors.White, 3);  
         textStyle.PointPlacement = PointPlacement.UpperCenter;  
         textStyle.YOffsetInPixel = 5;  
 
         //Apply these styles at all levels and add then to the custom styles for the layer  
         inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
         inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(textStyle);  
 
         return inMemoryFeatureLayer;  
     }  
 
     private void DrawImage()  
     {  
         mapEngine.OpenAllLayers();  
         mapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree);  
         mapEngine.CloseAllLayers();  
 
         map.Image = bitmap;  
     }  
 
     private void ToolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)  
     {  
         switch (e.Button.Tag.ToString())  
         {  
             case "Zoom In":  
                 mapEngine.CurrentExtent.ScaleDown(50);  
                 break;  
             case "Zoom Out":  
                 mapEngine.CurrentExtent.ScaleUp(50);  
                 break;  
             case "Full Extent":  
                 mapEngine.CurrentExtent = ExtentHelper.SnapToZoomLevel(new RectangleShape(-100.655827718567, 37.7441933095704, -100.613770681213, 37.7186586797486), GeographyUnit.DecimalDegree, map.Width, map.Height, new ZoomLevelSet());  
                 break;  
             case "Pan Left":  
                 mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Left, 20);  
                 break;  
             case "Pan Right":  
                 mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Right, 20);  
                 break;  
             case "Pan Up":  
                 mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Up, 20);  
                 break;  
             case "Pan Down":  
                 mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Down, 20);  
                 break;  
             default:  
                 break;  
         }  
         DrawImage();  
     }  
 
     private void map_ClientSizeChanged(object sender, EventArgs e)  
     {  
         if (map.Width > bitmap.Width || map.Height > bitmap.Height)  
         {  
             bitmap = new Bitmap(map.Width, map.Height);  
             double scale = new ZoomLevelSet().GetZoomLevel(mapEngine.CurrentExtent, map.Width, GeographyUnit.DecimalDegree).Scale;  
             mapEngine.CurrentExtent = ExtentHelper.SnapToZoomLevel((RectangleShape)mapEngine.CurrentExtent.CloneDeep(), GeographyUnit.DecimalDegree, map.Width, map.Height, new ZoomLevelSet());  
             mapEngine.CurrentExtent = ExtentHelper.ZoomToScale(scale, mapEngine.CurrentExtent, GeographyUnit.DecimalDegree, map.Width, map.Height);  
             DrawImage();  
         }  
     }  
 
     private void btnGenerateGridFile_Click(object sender, EventArgs e)  
     {  
         //Close the previous isoLineLayer and get ready to replace it with a new one  
         mapEngine.StaticLayers["IsoLineLayer"].Close();  
 
         mapEngine.StaticLayers["IsoLineLayer"] = GetGridIsoLineLayer();  
         mapEngine.StaticLayers["GridCellsLayer"] = GetGridFeatureLayer();  
 
         DrawImage();  
     }  
 }  
 
source_code_serviceseditionsample_isolineswithkriging_cs_120914.zip.txt · Last modified: 2015/09/09 03:17 by admin