ThinkGeo Cloud
ThinkGeo UI Controls
ThinkGeo Open Source
Help and Support
External Resources
ThinkGeo Cloud
ThinkGeo UI Controls
ThinkGeo Open Source
Help and Support
External Resources
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace RasterLayerWithRectangleShape { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new TestForm()); } } }
using System; using System.Windows.Forms; using System.Collections.ObjectModel; using ThinkGeo.MapSuite.Core; using ThinkGeo.MapSuite.DesktopEdition; namespace RasterLayerWithRectangleShape { public partial class TestForm : Form { public TestForm() { InitializeComponent(); } private void TestForm_Load(object sender, EventArgs e) { winformsMap1.MapUnit = GeographyUnit.DecimalDegree; winformsMap1.CurrentExtent = new RectangleShape(-180,90,180,-90); winformsMap1.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.FromArgb(255, 198, 255, 255)); ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(@"..\..\Data\Countries02.shp"); worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.Transparent, GeoColor.StandardColors.DarkGreen, 1); worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; LayerOverlay layerOverlay = new LayerOverlay(); layerOverlay.Layers.Add(worldLayer); //Displays the RectangleShape used for the bounding box of the Image Layer in world coordinates. //-180: mininum longitude //90: maximum latitude //180: maximum longitude //-90: minimum latitude InMemoryFeatureLayer inMemoryFeatureLayer = new InMemoryFeatureLayer(); inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.Transparent, GeoColor.StandardColors.Red, 4); inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; RectangleShape rectangleShape = new RectangleShape(-180,90,180,-90); inMemoryFeatureLayer.InternalFeatures.Add(new Feature(rectangleShape)); LayerOverlay dynamicOverlay = new LayerOverlay(); dynamicOverlay.Layers.Add("ImageLayerBoundingBox",inMemoryFeatureLayer); winformsMap1.Overlays.Add("WorldOverlay",layerOverlay); winformsMap1.Overlays.Add("DynamicOverlay",dynamicOverlay); winformsMap1.Refresh(); } private void btnLoadRasterImage_Click(object sender, EventArgs e) { //Loads the ImageLayer using the RectangleShape. The Rectangleshape represent the bounding box of the image layer in world coordinates: //-180: mininum longitude //90: maximum latitude //180: maximum longitude //-90: minimum latitude //Gets the RectangleShape from the InMemoryFeatureLayer of the "DynamicOverlay" LayerOverlay. LayerOverlay dynamicOverlay = (LayerOverlay)winformsMap1.Overlays["DynamicOverlay"]; InMemoryFeatureLayer inMemoryFeatureLayer = (InMemoryFeatureLayer)dynamicOverlay.Layers["ImageLayerBoundingBox"]; //Note that we are getting a PolygonShape, not a RectangleShape because rectangle is a not a specification of WKT. PolygonShape polygonShape = (PolygonShape)inMemoryFeatureLayer.InternalFeatures[0].GetShape(); //Gets the Bounding box of the Polygonshape representing the extent of the image layer. RectangleShape imageLayerRectangleShape = polygonShape.GetBoundingBox(); //Loads the ImageLayer with its extent as a the RectangleShape. GdiPlusRasterLayer gdiPlusRasterLayer = new GdiPlusRasterLayer(@"..\..\Data\World.tif", imageLayerRectangleShape); //The alternative way to load the image is with the acompanying world file. You do that by just specifying the image layer path and the accompanying world file //will be used. For more info, see http://en.wikipedia.org/wiki/World_file //GdiPlusRasterLayer gdiPlusRasterLayer = new GdiPlusRasterLayer(@"..\..\Data\World.tif"); //The world file (.tfw) contains the world info as follow: //0.36000 pixel size in the x-direction in map units/pixel //0 rotation about y-axis (not used) //0 rotation about x-axis (not used) //-0.36000 pixel size in the y-direction in map units, almost always negative //-179.82000 x-coordinate of the center of the upper left pixel //89.8199999999999872 y-coordinate of the center of the upper left pixel gdiPlusRasterLayer.UpperThreshold = double.MaxValue; gdiPlusRasterLayer.LowerThreshold = 0; LayerOverlay ImageOverlay = new LayerOverlay(); ImageOverlay.Layers.Add("GdiPlusImageLayer", gdiPlusRasterLayer); winformsMap1.Overlays.Add(ImageOverlay); winformsMap1.Refresh(); } private void winformsMap1_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(winformsMap1.CurrentExtent, new ScreenPointF(e.X, e.Y), winformsMap1.Width, winformsMap1.Height); //Displays world coordinates. statusStrip1.Items["toolStripStatusLabelWorld"].Text = "(world) X:" + Math.Round(pointShape.X, 4) + " Y:" + Math.Round(pointShape.Y, 4); } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } } }