Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

Drawing Component to build GIS and CAM applications

Rate me:
Please Sign up or sign in to vote.
4.72/5 (14 votes)
21 Nov 20055 min read 107.7K   8K   123  
An object oriented drawing component for building GIS and CAM applications with multiple views of geometrical model, multiple resolution handling, layers, optimized graphics and more.
using System;
using GeomModel;
using System.Drawing;

namespace TerraService
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class TerraServiceImage : CanvasItem
	{
		/*public TerraServiceImage()
		{
			//
			// TODO: Add constructor logic here
			//			
		}
		*/

		string m_server_name;
		float x=0;
		float y=0;
		float width=0;
		float height=0;
		bool is_selected = false;

		GDalGIS.SpatialReference m_spatial_src_ref;			
		GDalGIS.SpatialReference m_spatial_dst_ref;
		//GDalGIS.CoordinateTransform m_coord_transform;

		public override bool createFromRectangle(RectangleF r)
		{
			x = r.X;
			y = r.Y;
			//width = r.Width;
			//height = r.Height;

			return true;
		}

		public TerraServiceImage(CanvasLayer l)
		{
			layer = l;
			layer.add(this);
			
			width = 0;
			height = 0;
		}

		public TerraServiceImage(string server_name,CanvasLayer l)
		{
			m_server_name = server_name;
			layer = l;
			layer.add(this);
			x = 0;
			y = 0;
			width = 200;
			height = 200;

			m_spatial_src_ref = new GDalGIS.SpatialReference();
			m_spatial_src_ref.SetWellKnownGeogCS("WGS84");
			
			m_spatial_dst_ref = new GDalGIS.SpatialReference();
			m_spatial_dst_ref.SetProjCS("UTM Proj");
			m_spatial_dst_ref.SetWellKnownGeogCS("WGS84");
			

			//m_coord_transform = new GDalGIS.CoordinateTransform(m_spatial_src_ref,m_spatial_dst_ref);

			/*
			//m_geo_image.Open(fname);
			
			x = m_geo_image.X();
			y = m_geo_image.Y();
			width = m_geo_image.Width();
			height = m_geo_image.Height();
			*/
		}

		public override bool isSelected()
		{
			return is_selected;
		}

		public override void select(bool m)
		{
			is_selected = m;
		}

		public override bool isCloseToPoint(PointF pnt,float dist)
		{
			RectangleF rect1 = new RectangleF(x,y,width,height);
			if(rect1.Contains(pnt))
				return true;

			return false;
		}


		public override RectangleF boundingBox()
		{
			return new RectangleF(x,y,width,height);
		}

		public override float X()
		{
			return x;
		}

		public override float Y()
		{
			return y;
		}

		public override void move(PointF p)
		{
			x = p.X;
			y = p.Y;
		}

		public override void moveBy(float xs,float ys)
		{
			x+=xs;
			y+=ys;
		}

		private static void copyStreamToFile(System.IO.Stream stream, string destination)
		{
			using (System.IO.BufferedStream bs = new System.IO.BufferedStream(stream))
			{
				using (System.IO.FileStream os = System.IO.File.OpenWrite(destination))
				{
					byte[] buffer = new byte[2 * 4096];
					int nBytes;
					while ((nBytes = bs.Read(buffer, 0, buffer.Length)) > 0)
					{
						os.Write(buffer, 0, nBytes);
					}
				}
			}
		}

		public override void draw(Graphics graph,System.Drawing.Drawing2D.Matrix trans)
		{
			if(width == 0 &&
				height == 0)
				return;

			if(!layer.Visiable)
				return;
		
			System.Drawing.RectangleF clip_rect = graph.ClipBounds;

			PointF [] bbox_points = new PointF[4];
			bbox_points[0] = new PointF(clip_rect.Left,clip_rect.Top);
			bbox_points[1] = new PointF(clip_rect.Right,clip_rect.Top);
			bbox_points[2] = new PointF(clip_rect.Right,clip_rect.Bottom);
			bbox_points[3] = new PointF(clip_rect.Left,clip_rect.Bottom);

			System.Drawing.Drawing2D.Matrix c2r = trans.Clone();
			c2r.Invert();
			c2r.TransformPoints(bbox_points);
			
			int zone1 = (int)((180.0+bbox_points[0].X)/6+1);			
			int zone2 = (int)((180.0+bbox_points[1].X)/6+1);
			
			Pen pen = new Pen(Color.BlueViolet,1);

			if(zone1 != zone2)
			{
				// draw all UTM zones and returns:
				for(int z=3; z<20; z++)
				{
					PointF [] ps = new PointF[2];
					ps[0] = new PointF(z*6-180,-180);
					ps[1] = new PointF(z*6-180,180);
					trans.TransformPoints(ps);
					graph.DrawLine(pen,ps[0],ps[1]);
				}
				return;
			}

			pen.Dispose();

			m_spatial_dst_ref.SetUTM(zone1,1);
						
			// convert WGS84 to UTM:
			
			GDalGIS.CoordinateTransform geo2utm_coord_transform = new GDalGIS.CoordinateTransform(m_spatial_src_ref,m_spatial_dst_ref);
	
			/*
			double [] xl = new double[4];
			double [] yl = new double[4];
			double [] zl = new double[4];
			for(int i=0; i<4; i++)
			{
				xl[i] = bbox_points[i].X;
				yl[i] = bbox_points[i].Y;
				zl[0] = 0;
			}
			coord_transform.transform(xl,yl,zl);
			
			for(int i=0; i<4; i++)
			{
				bbox_points[i].X = (float)xl[i];
				bbox_points[i].Y = (float)yl[i];
			
			}
*/
			geo2utm_coord_transform.transform(bbox_points);

			System.UriBuilder uri = new System.UriBuilder(m_server_name);
			RectangleF bbox = Geom.BoundingBox(bbox_points);
			double sx = (double)bbox.Width/clip_rect.Width;
			double sy = (double)bbox.Height/clip_rect.Height;
			
			GDalGIS.CoordinateTransform utm2geo_coord_transform = new GDalGIS.CoordinateTransform(m_spatial_dst_ref,m_spatial_src_ref);
			PointF [] geo_bbox_points = new PointF[3];
			geo_bbox_points[0].X = bbox.Left;
			geo_bbox_points[0].Y = bbox.Bottom;
			
			geo_bbox_points[1].X = bbox.Right;
			geo_bbox_points[1].Y = bbox.Bottom;

			//geo_bbox_points[2].X = bbox.Right;
			//geo_bbox_points[2].Y = bbox.Bottom;

			geo_bbox_points[2].X = bbox.Left;
			geo_bbox_points[2].Y = bbox.Top;

			utm2geo_coord_transform.transform(geo_bbox_points);
			
			// transform from WGS84 to client coordinates:
			trans.TransformPoints(geo_bbox_points);

			if( sx<1 || sy<1)
			{
				clip_rect.Width = bbox.Width;
				clip_rect.Height = bbox.Height;
			}

			uri.Query = "version=1.1.1&request=GetMap&Layers=DOQ&Styles=&"+
				"SRS=EPSG:269"+zone1+
				"&BBOX="+(int)bbox.Left+","+(int)bbox.Top+","+(int)bbox.Right+","+(int)bbox.Bottom+
				"&width="+(int)clip_rect.Width+"&height="+(int)clip_rect.Height+
				"&format=image/jpeg&Exceptions=se_xml";

			try
			{
				int bid = WorkSpace.WsProgressBar.Instance.Create("getting map");
				System.Net.WebRequest wr = System.Net.WebRequest.Create(uri.Uri);			
				System.Net.WebResponse response = wr.GetResponse();
				WorkSpace.WsProgressBar.Instance.Delete(bid);
				Image img = Image.FromStream(response.GetResponseStream());
					

				trans.TransformPoints(bbox_points);

				/*if( sx<1 || sy<1)
				{
					Rectangle src_r = new Rectangle(0,0,(int)(clip_rect.Width),(int)(clip_rect.Height));
					Rectangle dst_r = new Rectangle((int)clip_rect.X,(int)clip_rect.Y,
									(int)(clip_rect.Width/sx),(int)(clip_rect.Height/sy));
					graph.DrawImage(img,dst_r,src_r,GraphicsUnit.Pixel);			
				}
				else*/
				{
					//graph.DrawImageUnscaled(img,(int)clip_rect.X,(int)clip_rect.Y);
					graph.DrawImage(img,geo_bbox_points);
				}
			}
			catch( Exception e )
			{
				System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 8);
				System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(Color.Red);
		
				graph.DrawString("Failed to get a map: "+e.Message,
					drawFont,drawBrush,clip_rect.X+10,clip_rect.Y+10);
				
				drawFont.Dispose();
				drawBrush.Dispose();
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Canada Canada
Baranovsky Eduard has been a software developer for more then 10 years. He has an experence in image processing, computer graphics and distributed systems design.

Comments and Discussions