现在的位置: 首页 > 综合 > 正文

SharpGIS博客翻译2006年7月:在SharpMap中使用即时坐标变换

2013年12月10日 ⁄ 综合 ⁄ 共 5636字 ⁄ 字号 评论关闭

 Applying on-the-fly transformation in SharpMap [原文] [翻译] [作者:Clark]

Applying on-the-fly transformation in SharpMap

I have received a lot of questions on how to transform data from one coordinatesystem to another on the fly in SharpMap. Usually the problem is that they have data in different coordinatesystems and want to match them. Although I would recommend applying transformations once-and-for-all to increase performance (you could use OGR for this), it is easy to setup in SharpMap. Below are some examples on how to accomplish this.

我收到好多关于如果在SharpMap中飞行时进行坐标转换的问题。通常这类问题出现在他们想在不同的坐标系统中匹配数据。我想提醒一下如果你想提高这方面的执行效率最好使用OGR,在SharpMap非常容易设置。下面是如何实现的一个例子。

SharpMap gives you the full power to specify all the parameters in a projection. The following method demonstrates how to setup a UTM projection:

在SharpMap可以指定一个投影的所有参数,下面是设置UTM投影的方法:

/// /// Creates a UTM projection for the northern
/// hemisphere based on the WGS84 datum
/// /// Utm Zone/// Projectionprivate IProjectedCoordinateSystem CreateUtmProjection(int utmZone){ CoordinateSystemFactory cFac =
      new SharpMap.CoordinateSystems.CoordinateSystemFactory(); //Create geographic coordinate system based on the WGS84 datum IEllipsoid ellipsoid = cFac.CreateFlattenedSphere("WGS 84",
           6378137, 298.257223563, LinearUnit.Metre); IHorizontalDatum datum = cFac.CreateHorizontalDatum("WGS_1984",
                     DatumType.HD_Geocentric, ellipsoid, null); IGeographicCoordinateSystem gcs = cFac.CreateGeographicCoordinateSystem(
                     "WGS 84", AngularUnit.Degrees, datum,
                     PrimeMeridian.Greenwich,
                     new AxisInfo("Lon", AxisOrientationEnum.East),              new AxisInfo("Lat", AxisOrientationEnum.North)); //Create UTM projection List parameters = new List(5); parameters.Add(new ProjectionParameter("latitude_of_origin", 0)); parameters.Add(new ProjectionParameter("central_meridian", -183+6*utmZone)); parameters.Add(new ProjectionParameter("scale_factor", 0.9996)); parameters.Add(new ProjectionParameter("false_easting", 500000)); parameters.Add(new ProjectionParameter("false_northing", 0.0)); IProjection projection = cFac.CreateProjection(
"Transverse Mercator", "Transverse_Mercator", parameters); return cFac.CreateProjectedCoordinateSystem(
         "WGS 84 / UTM zone "+utmZone.ToString() +"N", gcs,
projection, LinearUnit.Metre,
new AxisInfo("East", AxisOrientationEnum.East),
new AxisInfo("North", AxisOrientationEnum.North));}

If you have a well-known text-representation, you can also create a projection from this. A WKT for an UTM projection might look like this:

下面是UTM投影的WKT(well-known text)表现形式:

PROJCS["WGS 84 / UTM zone 32N",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",9],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","32632"]]

SharpMap comes with WKT parsers for parsing a WKT to a coordinate system (note: the current v0.9RC1 has a few bug in its WKT parser, but if you get problems parsing the WKT, use the current source from the repository, where these issues have been resolved)

SharpMap里有一个WKT解析器,用来解析WKT格式的坐标系统(注意:在最新的v0.9RC1里的WKT解析器有一个BUG,如果你在使用时碰到问题可以使用下面的代码)

/// /// Create coordinatesystem based on a Well-Known text/// /// /// private ICoordinateSystem CreateCoordinateSystemFromWKT(string wkt){    CoordinateSystemFactory cFac = new CoordinateSystemFactory();    return cFac.CreateFromWkt(strProj);}

If your data is based on shapefile data and they have a .prj file defining the coordinatesystem, you can simply retrieve the CS from the shapefile instead:

如果你是ShapFiles格式的数据,而且用一个.prj来定义坐标系统,那么可以通过下面这种方式获取为坐标系统:

((myMap.Layers[0] as VectorLayer).DataSource as ShapeFile).CoordinateSystem

The next step is to create a transformation between two coordinate systems. SharpMap currently supports transforming between a geographic coordinate system and one of the following projections:

下一步是在两个坐标之间创建一个变换。SharpMap现在支持以下这些投影之间的变换:

  • Mercator 1-standard parallel (Mercator_1SP)
  • Mercator 1-standard parallels (Mercator_2SP)
  • Transverse mercator (Transverse_Mercator)
  • Lambert Conic Conformal 2-standard parallel (Lambert Conic Conformal (2SP))
  • Albers

Unfortunately datum-shifts and transformations between two projections are still down the pipeline, but the above will be sufficient in most cases. (for those interested full transformation between all supported projections as well as datum-shifts are almost done...)

不幸的是在两个投影里只能使用同一个椭球体参数,不过这也足以满足大部分的应用了。(在所有支持相互转换的坐标系统中变换也快实现完了......)

The following shows how to create a transformation and apply it to a vectorlayer (only vector- and label-layers supports on-the-fly transformations):

下面是一个在矢量层上创建坐标变换的例子(只有矢量层支持on-the-fly变换):

//Create zone UTM 32N projectionIProjectedCoordinateSystem utmProj = CreateUtmProjection(32);//Create geographic coordinate system (lets just reuse the CS from the projection)IGeographicCoordinateSystem geoCS = utmProj.GeographicCoordinateSystem;//Create transformationCoordinateTransformationFactory ctFac = new CoordinateTransformationFactory();ICoordinateTransformation transform = 
   ctFac.CreateFromCoordinateSystems(source, target);//Apply transformation to a vectorlayer(myMap.Layers[0] as VectorLayer).CoordinateTransformation = transform;

Happy transforming!

抱歉!评论已关闭.