VEImagery Service Pixel to LatLong calculations

 

The VEImagery service gives us the ability to render static versions of Virtual Earth Maps. As handy as this is it would also be handy to be able to georeference the image that is returned. The article found here:

http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!488.entry

explains how to convert from LatLong to Pixel so that custom icons could be added to the map, however the conversion from Pixel to LatLong was not needed at that time. Building on top of the code that was provided in that article the following method can be used to calculate a LatLong value based on the pixel coordinate of a static image. The following formula can be used to perform this calculation.

        /// <summary>
        /// Convert Pixel coordinates to LatLong coordinates
        /// Based on code found here: http://msdn.microsoft.com/en-us/library/cc161076.aspx
        /// </summary>
        /// <param name="offset"></param>
        /// <returns></returns>
        public LatLong PixelToLatLong(Point offset)
        {
            int x = TopLeftCorner.X + offset.X;
            int y = TopLeftCorner.Y + offset.Y;

            double Longitude = (((double)x * 360) / (256 * Math.Pow(2, view.zoom))) – 180;

            double efactor = Math.Exp((0.5 – (double)y / 256 / Math.Pow(2, view.zoom)) * 4 * Math.PI);

            double Latitude = Math.Asin((efactor – 1) / (efactor + 1)) * 180 / Math.PI;

            LatLong latlong = new LatLong();
            latlong.Latitude = Latitude;
            latlong.Longitude = Longitude;

            return latlong;
        }

 

The complete source code can be downloaded here:

http://cid-e7dba9a4bfd458c5.skydrive.live.com/self.aspx/VE%20Sample%20code/CustomIconVEService.zip

Here is a screen shot of this application in action.

image

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s