using System;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Windows.Forms;
using ThinkGeo.MapSuite.Core;
namespace GeodeticToUTM
{
public partial class TestForm : Form
{
private MapEngine mapEngine = new MapEngine();
private MapEngine mapEngine2 = new MapEngine();
private Bitmap bitmap = null;
private Bitmap bitmap2 = null;
public TestForm()
{
InitializeComponent();
}
private void TestForm_Load(object sender, EventArgs e)
{
// Sets the map in Decimal Degrees (Geodetic)
mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-125.77,50.32,-67.71,23.23), Map.Width, Map.Height);
mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);
//Background layer of the world
ShapeFileFeatureLayer CountriesLayer = new ShapeFileFeatureLayer(@"..\..\Data\countries02.shp");
CountriesLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
CountriesLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
//Layer of the state of Nuevo Leon (Mexico)
ShapeFileFeatureLayer stateLayer = new ShapeFileFeatureLayer(@"..\..\Data\MEX-18.shp");
stateLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.Transparent, GeoColor.StandardColors.Red,2);
stateLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
ShapeFileFeatureLayer stateLabelLayer = new ShapeFileFeatureLayer(@"..\..\Data\MEX-18.shp");
stateLabelLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.CreateSimpleTextStyle("ADM1", "Arial", 10, DrawingFontStyles.Bold,
GeoColor.StandardColors.Black, GeoColor.StandardColors.White, 2);
stateLabelLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
mapEngine.StaticLayers.Add("CountriesLayer", CountriesLayer);
mapEngine.StaticLayers.Add("StateLayer", stateLayer);
mapEngine.StaticLayers.Add("StateLabelLayer", stateLabelLayer);
ScaleBarAdornmentLayer scaleBar = new ScaleBarAdornmentLayer();
scaleBar.UnitFamily = UnitSystem.Metric;
scaleBar.Location = AdornmentLocation.LowerLeft;
mapEngine.AdornmentLayers.Add(scaleBar);
DrawImage();
//Sets the map in meters (UTM zone 13)
mapEngine2.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(814020,3128509,1218181,2546125), Map.Width, Map.Height);
mapEngine2.BackgroundFillBrush = new GeoSolidBrush(GeoColor.StandardColors.White);
//Tiff image of the area of Nuevo Leon in UTM
GeoTiffRasterLayer geoTiffRasterLayer = new GeoTiffRasterLayer(@"..\..\Data\MEX-18_UTM13.tif");
geoTiffRasterLayer.UpperThreshold = double.MaxValue;
geoTiffRasterLayer.LowerThreshold = 0;
geoTiffRasterLayer.IsGrayscale = false;
mapEngine2.StaticLayers.Add(geoTiffRasterLayer);
//Projection class to go from Geodetic projection to UTM zone 13.
Proj4Projection GeoToUTM_Projection = new Proj4Projection();
GeoToUTM_Projection.InternalProjectionParametersString = Proj4Projection.GetEsriParametersString(4326); //WGS84 (Geodetic)
GeoToUTM_Projection.ExternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(26913); //UTM
//Layer of the state of Nuevo Leon projected to UTM on the fly to match the tiff.
ShapeFileFeatureLayer stateLayerUTM = new ShapeFileFeatureLayer(@"..\..\Data\MEX-18.shp");
stateLayerUTM.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.Transparent, GeoColor.StandardColors.Red, 2);
stateLayerUTM.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
stateLayerUTM.FeatureSource.Projection = GeoToUTM_Projection;
mapEngine2.StaticLayers.Add("StateLayerUTM", stateLayerUTM);
ScaleBarAdornmentLayer scaleBar2 = new ScaleBarAdornmentLayer();
scaleBar2.UnitFamily = UnitSystem.Metric;
scaleBar2.Location = AdornmentLocation.LowerLeft;
mapEngine2.AdornmentLayers.Add(scaleBar2);
DrawImage2();
}
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.DrawAdornmentLayers(bitmap, GeographyUnit.DecimalDegree);
mapEngine.CloseAllLayers();
Map.Image = bitmap;
}
private void DrawImage2()
{
if (bitmap2 != null) { bitmap2.Dispose(); }
bitmap2 = new Bitmap(Map2.Width, Map2.Height);
mapEngine2.OpenAllLayers();
mapEngine2.DrawStaticLayers(bitmap2, GeographyUnit.Meter);
mapEngine2.DrawDynamicLayers(bitmap2, GeographyUnit.Meter);
mapEngine2.DrawAdornmentLayers(bitmap2, GeographyUnit.Meter);
mapEngine2.CloseAllLayers();
Map2.Image = bitmap2;
}
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);
}
private void Map2_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(mapEngine2.CurrentExtent, new ScreenPointF(e.X, e.Y), Map2.Width, Map2.Height);
//Displays world coordinates.
statusStrip1.Items["toolStripStatusLabelWorld"].Text = "(world) X:" + Math.Round(pointShape.X, 2) + " Y:" + Math.Round(pointShape.Y, 2);
}
}
}