先看看图像坐标和位置坐标的转换
打开SharpMap.VS2010-->Utilities-->Transform.cs可以看到如下代码
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
///
<summary>
2 /// 图像坐标和位置坐标的转换
3 /// </summary>
4 public class Transform
5 {
6 /// <summary>
7 /// 位置坐标(WCS) to 转换到图像坐标。
8 /// 注意: 该方法不能代替SharpMap.Map.MapToWorld方法
9 /// </summary>
10 /// <param name="p"> x,y坐标 </param>
11 /// <param name="map"> 所在的地图 </param>
12 /// <returns> 图像上的坐标 </returns>
13 public static PointF WorldtoMap(Point p, Map map)
14 {
15 // if (map.MapTransform != null && !map.MapTransform.IsIdentity)
16 // map.MapTransform.TransformPoints(new System.Drawing.PointF[] { p });
17 PointF result = new System.Drawing.Point();
18 double Height = (map.Zoom * map.Size.Height) / map.Size.Width;
19 double left = map.Center.X - map.Zoom * 0.5 ;
20 double top = map.Center.Y + Height * 0.5 * map.PixelAspectRatio;
21 result.X = ( float ) ((p.X - left) / map.PixelWidth);
22 result.Y = ( float ) ((top - p.Y) / map.PixelHeight);
23 return result;
24 }
25
26 /// <summary>
27 /// 从图像坐标转化到位置坐标 (WCS).
28 /// 注意: 该方法不能代替SharpMap.Map.MapToWorld instead)
29 /// </summary>
30 /// <param name="p"> 图像中点的坐标 </param>
31 /// <param name="map"> 所在的地图 </param>
32 /// <returns> 返回位置坐标x,y </returns>
33 public static Point MapToWorld(PointF p, Map map)
34 {
35 // if (this.MapTransform != null && !this.MapTransform.IsIdentity)
36 // {
37 // System.Drawing.PointF[] p2 = new System.Drawing.PointF[] { p };
38 // this.MapTransform.TransformPoints(new System.Drawing.PointF[] { p });
39 // this.MapTransformInverted.TransformPoints(p2);
40 // return Utilities.Transform.MapToWorld(p2[0], this);
41 // }
42 // else
43 Point ul = new Point(map.Center.X - map.Zoom * . 5 , map.Center.Y + map.MapHeight * . 5 );
44 return new Point(ul.X + p.X * map.PixelWidth,
45 ul.Y - p.Y * map.PixelHeight);
46 // BoundingBox env = map.Envelope;
47 // return new Point(env.Min.X + p.X*map.PixelWidth,
48 // env.Max.Y - p.Y*map.PixelHeight);
49 }
50 }
2 /// 图像坐标和位置坐标的转换
3 /// </summary>
4 public class Transform
5 {
6 /// <summary>
7 /// 位置坐标(WCS) to 转换到图像坐标。
8 /// 注意: 该方法不能代替SharpMap.Map.MapToWorld方法
9 /// </summary>
10 /// <param name="p"> x,y坐标 </param>
11 /// <param name="map"> 所在的地图 </param>
12 /// <returns> 图像上的坐标 </returns>
13 public static PointF WorldtoMap(Point p, Map map)
14 {
15 // if (map.MapTransform != null && !map.MapTransform.IsIdentity)
16 // map.MapTransform.TransformPoints(new System.Drawing.PointF[] { p });
17 PointF result = new System.Drawing.Point();
18 double Height = (map.Zoom * map.Size.Height) / map.Size.Width;
19 double left = map.Center.X - map.Zoom * 0.5 ;
20 double top = map.Center.Y + Height * 0.5 * map.PixelAspectRatio;
21 result.X = ( float ) ((p.X - left) / map.PixelWidth);
22 result.Y = ( float ) ((top - p.Y) / map.PixelHeight);
23 return result;
24 }
25
26 /// <summary>
27 /// 从图像坐标转化到位置坐标 (WCS).
28 /// 注意: 该方法不能代替SharpMap.Map.MapToWorld instead)
29 /// </summary>
30 /// <param name="p"> 图像中点的坐标 </param>
31 /// <param name="map"> 所在的地图 </param>
32 /// <returns> 返回位置坐标x,y </returns>
33 public static Point MapToWorld(PointF p, Map map)
34 {
35 // if (this.MapTransform != null && !this.MapTransform.IsIdentity)
36 // {
37 // System.Drawing.PointF[] p2 = new System.Drawing.PointF[] { p };
38 // this.MapTransform.TransformPoints(new System.Drawing.PointF[] { p });
39 // this.MapTransformInverted.TransformPoints(p2);
40 // return Utilities.Transform.MapToWorld(p2[0], this);
41 // }
42 // else
43 Point ul = new Point(map.Center.X - map.Zoom * . 5 , map.Center.Y + map.MapHeight * . 5 );
44 return new Point(ul.X + p.X * map.PixelWidth,
45 ul.Y - p.Y * map.PixelHeight);
46 // BoundingBox env = map.Envelope;
47 // return new Point(env.Min.X + p.X*map.PixelWidth,
48 // env.Max.Y - p.Y*map.PixelHeight);
49 }
50 }
现在分析一下
WorldtoMap 方法是把位置的坐标转化为图像中的坐标。
MapToWorld方法是把图像中的坐标转化为位置坐标系中。