using System;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Windows.Forms;
using ThinkGeo.MapSuite.Core;
namespace MeasurementsWithDecimalDegrees
{
public partial class TestForm : Form
{
private MapEngine mapEngine = new MapEngine();
private Bitmap bitmap = null;
public TestForm()
{
InitializeComponent();
}
private void TestForm_Load(object sender, EventArgs e)
{
// Set the extent and the background color
mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-97.7591, 30.3126, -97.7317, 30.2964), Map.Width, Map.Height);
mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);
//Displays the World Map Kit as a background.
ThinkGeo.MapSuite.Core.WorldMapKitLayer worldMapKitLayer = new ThinkGeo.MapSuite.Core.WorldMapKitLayer();
mapEngine.StaticLayers.Add(worldMapKitLayer);
PolygonShape polygonShape = new PolygonShape();
polygonShape.OuterRing.Vertices.Add(new Vertex(-97.7406, 30.3057));
polygonShape.OuterRing.Vertices.Add(new Vertex(-97.7354, 30.3031));
polygonShape.OuterRing.Vertices.Add(new Vertex(-97.7384, 30.2979));
polygonShape.OuterRing.Vertices.Add(new Vertex(-97.7445, 30.3006));
polygonShape.OuterRing.Vertices.Add(new Vertex(-97.7406, 30.3057));
LineShape lineShape = new LineShape();
lineShape.Vertices.Add(new Vertex(-97.7507, 30.3093));
lineShape.Vertices.Add(new Vertex(-97.7524, 30.3065));
lineShape.Vertices.Add(new Vertex(-97.7458, 30.3033));
lineShape.Vertices.Add(new Vertex(-97.7467, 30.3017));
InMemoryFeatureLayer inMemoryFeatureLayer = new InMemoryFeatureLayer();
inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(80, GeoColor.StandardColors.Red), GeoColor.StandardColors.Black);
inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.CreateSimpleLineStyle(GeoColor.StandardColors.Red, 3, true);
inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
inMemoryFeatureLayer.InternalFeatures.Add("Area", new Feature(polygonShape));
inMemoryFeatureLayer.InternalFeatures.Add("Line", new Feature(lineShape));
mapEngine.DynamicLayers.Add("DynamicLayer", inMemoryFeatureLayer);
//Central Texas State Plane projection.
Proj4Projection StatePlaneProj4 = new Proj4Projection();
StatePlaneProj4.InternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326); //Decimal Degrees (WGS 84)
StatePlaneProj4.ExternalProjectionParametersString = Proj4Projection.GetEsriParametersString(102739);//State Plane Texas Central Zone unit in Feet
//UTM Zone 14 N projection.
Proj4Projection UTMProj4 = new Proj4Projection();
UTMProj4.InternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326); //Decimal Degrees (WGS 84)
UTMProj4.ExternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(26914); //UTM zone 14 N unit in meters
//Gets the area of the polygon based on the default spherical geometry used for decimal degrees.
double DefaultArea = Math.Round(polygonShape.GetArea(GeographyUnit.DecimalDegree, AreaUnit.SquareMeters));
lblDefaultArea.Text = System.Convert.ToString(DefaultArea);
//Gets the area based on planar geometry with state plane coordinates.
double StatePlaneArea = Math.Round(GetArea(polygonShape, StatePlaneProj4, GeographyUnit.Feet, AreaUnit.SquareMeters));
lblStatePlaneArea.Text = System.Convert.ToString(StatePlaneArea);
//Gets the area based on planar geometry with UTM coordinates.
double UTMArea = Math.Round(GetArea(polygonShape, UTMProj4, GeographyUnit.Meter, AreaUnit.SquareMeters));
lblUTMArea.Text = System.Convert.ToString(UTMArea);
//Gets the length of the line based on the default spherical geometry used for decimal degrees.
double DefaultDistance = Math.Round( lineShape.GetLength(GeographyUnit.DecimalDegree, DistanceUnit.Meter), 2);
lblDefaultDistance.Text = System.Convert.ToString(DefaultDistance);
//Gets the length of the line based on planar geometry with state plane coordinates.
double StatePlaneDistance = Math.Round(GetLength(lineShape, StatePlaneProj4, GeographyUnit.Feet, DistanceUnit.Meter), 2);
lblStatePlaneDistance.Text = System.Convert.ToString(StatePlaneDistance);
//Gets the length of the line based on planar geometry with UTM coordiantes.
double UTMDistance = Math.Round(GetLength(lineShape, UTMProj4, GeographyUnit.Meter, DistanceUnit.Meter), 2);
lblUTMDistance.Text = System.Convert.ToString(UTMDistance);
DrawImage();
}
//Gets the Area of a area shape in decimal degrees based on the projection used.
private double GetArea(AreaBaseShape areaBaseShape, Proj4Projection proj4, GeographyUnit projectionUnit, AreaUnit areaUnit)
{
double result = 0;
proj4.Open();
AreaBaseShape projAreaBaseShape = (AreaBaseShape)proj4.ConvertToExternalProjection(areaBaseShape);
result = projAreaBaseShape.GetArea(projectionUnit, areaUnit);
proj4.Close();
return result;
}
//Gets the Length of a line shape in decimal gegrees based on the projection used.
private double GetLength(LineBaseShape lineBaseShape, Proj4Projection proj4, GeographyUnit projectionUnit, DistanceUnit lengthUnit)
{
double result = 0;
proj4.Open();
LineBaseShape projLineBaseShape = (LineBaseShape)proj4.ConvertToExternalProjection(lineBaseShape);
result = projLineBaseShape.GetLength(projectionUnit, lengthUnit);
proj4.Close();
return result;
}
private void DrawImage()
{
if (bitmap != null) { bitmap.Dispose(); }
bitmap = new Bitmap(Map.Width, Map.Height);
mapEngine.OpenAllLayers();
mapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree);
mapEngine.DrawDynamicLayers(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.GetDrawingExtent(new RectangleShape(-180.0, 83.0, 180.0, -90.0), Map.Width, Map.Height);
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 btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void Map_MouseMove(object sender, MouseEventArgs e)
{
//Displays the X and Y in screen coordinates.
statusStrip1.Items["toolStripStatusLabelScreen"].Text = "X:" + e.X + " Y:" + e.Y;
//Gets the PointShape in world coordinates from screen coordinates.
PointShape pointShape = ExtentHelper.ToWorldCoordinate(mapEngine.CurrentExtent, new ScreenPointF(e.X, e.Y), Map.Width, Map.Height);
//Displays world coordinates.
statusStrip1.Items["toolStripStatusLabelWorld"].Text = "(world) X:" + Math.Round(pointShape.X, 4) + " Y:" + Math.Round(pointShape.Y, 4);
}
}
}