Click here to Skip to main content
15,897,519 members
Articles / Multimedia / GDI+

Writing GIS and Mapping Software for .NET

,
Rate me:
Please Sign up or sign in to vote.
4.89/5 (183 votes)
19 Aug 2008CPOL11 min read 254.9K   8.9K   259  
In part three of the series, the authors of the "GIS.NET" mapping component for .NET explain how to write a geographic mapping engine which can display geographic coordinates. Source code is provided which can pan and zoom a sample geographic object (the state of Nebraska), in C# and VB.NET.
''' <summary>
''' Represents the Equidistant Cylindrical map projection.
''' </summary>
''' <remarks>Map projections are used to flatten 3D coordinates into a 2D form which can be displayed
''' easily on a computer screen.  This class provides two methods, Project and Deproject, which convert
''' convert geographic coordinates to projected coordinates, and vice versa.</remarks>
Public Class MapProjection

    ' Many map projections work with coordinates in the form of radians instead of degrees.
    ' These constants will help us convert back and forth.
    Private Const DEGREEStoRADIANS As Double = Math.PI / 180
    Private Const RADIANStoDEGREES As Double = 180 / Math.PI

    ' These values represent the equatorial radius of the WGS84 ellipsoid in meters.
    ' resulting in projected coordinates which are also in meters
    Private Const WGS84SEMIMAJOR As Double = 6378137
    Private Const ONEOVERWGS84SEMIMAJOR As Double = 1 / WGS84SEMIMAJOR

    Public Sub New()
    End Sub

    ''' <summary>
    ''' Converts a geographic coordinate into a projected coordinate.
    ''' </summary>
    ''' <param name="geographicCoordinate">A <strong>PointF</strong> object, representing a geographic coordinate.</param>
    ''' <returns>A projected coordinate in the form of a <strong>PointF</strong> object.</returns>
    Public Function Project(ByVal geographicCoordinate As PointF) As PointF

        ' First, convert the geographic coordinate to radians
        Dim radianX As Double = geographicCoordinate.X * DEGREEStoRADIANS
        Dim radianY As Double = geographicCoordinate.Y * DEGREEStoRADIANS

        ' Make a new Point object
        Dim result As New PointF()

        ' Calculate the projected X coordinate
        result.X = Convert.ToSingle(radianX * Math.Cos(0) * WGS84SEMIMAJOR)

        ' Calculate the projected Y coordinate
        result.Y = Convert.ToSingle(radianY * WGS84SEMIMAJOR)

        ' Return the result
        Return result
    End Function

    ''' <summary>
    ''' Converts a projected coordinate to a geographic coordinate.
    ''' </summary>
    ''' <param name="projectedCoordinate">A <strong>PointF</strong> object, representing a projected coordinate.</param>
    ''' <returns>A geographic coordinate in the form of a <strong>PointF</strong> object.</returns>
    Public Function Deproject(ByVal projectedCoordinate As PointF) As PointF

        ' Make a new point to store the result
        Dim result As New PointF()

        ' Calculate the geographic X coordinate (longitude)
        result.X = Convert.ToSingle(projectedCoordinate.X / Math.Cos(0) * RADIANStoDEGREES * ONEOVERWGS84SEMIMAJOR)

        ' Calculate the geographic Y coordinate (latitude)
        result.Y = Convert.ToSingle(projectedCoordinate.Y * RADIANStoDEGREES * ONEOVERWGS84SEMIMAJOR)

        Return result
    End Function
End Class

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Black Knight Financial Services
United States United States
Hi there! From 2004 to 2009 I ran a company called "GeoFrameworks," publishing two components called GPS.NET and GIS.NET which helped developers quickly write location-based services. Now, I've released the source code for GPS.NET to CodePlex for you to use as you see fit.

GPS.NET 2.0 on CodePlex
GPS.NET 3.0 on CodePlex

... I've also released the source code of a library called the "GeoFramework," a collection of commonly used classes such as Latitude, Longitude, Distance, Speed, and Position:

GeoFramework 1.0 on CodePlex
GeoFramework 2.0 on CodePlex

I'm now taking a break from programming, but I really appreciate the positive feedback from readers!

Written By
Architect GeoFrameworks
United States United States
Phil has a highly technical mind as well as having adepts in both art and music. His twenty-five years of computer experience brings knowledge to the table for any enterprise endeavor. But his skills in problem solving provide practical solutions to a myriad of problems, both technical and administrative. He also has a propensity for math and science that suit him well for tasks associated with geospacial information systems.

As a .Net architect and developer, Phil has six years under his belt in C# development, on top of twelve years of Visual Basic, starting with version four. He has an extensive background in business enterprise development, which includes SQL server database design and development. Now Phil prefers to exercise his talents in fields of science and mathematics.

Comments and Discussions