Applying on-the-fly transformation in SharpMap

Source: Internet
Author: User

I have got Ed 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 wowould recommend applying transformations once-and-for-all to increase performance (you cocould use OGR for this), it is easy to setup in sharpmap. below are some examples on how to accomplish this.

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

/// <summary>/// Creates a UTM projection for the northern
/// hemisphere based on the WGS84 datum/// </summary>/// <param name="utmZone">Utm Zone</param>/// <returns>Projection</returns>private 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<ProjectionParameter> parameters = new List<ProjectionParameter>(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:

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)

/// <summary>/// Create coordinatesystem based on a Well-Known text/// </summary>/// <param name="wkt"></param>/// <returns></returns>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:

((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:

  • 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 ):

//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!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.