3

I have a .NET application that typically uses the standard Google Maps JavaScript API V2. Within this application I am using a GTileLayerOverlay to add my own custom dynamic tile layer.

  function addJourneyLineOverlay(assetId, startDate, finishDate) {

        var path = "http://<%= Request.Url.Host %>";
        if ("<%= Request.Url.Port %>" != 80) {
            path += ":<%= Request.Url.Port %>";
        }
        path += "/Private/polyLineTileLayer/Tile.aspx";

        var myCopyright = new GCopyrightCollection("(c) ");
        myCopyright.addCopyright(new GCopyright('Demo', new GLatLngBounds(new GLatLng(-20, 130), new GLatLng(-30, 155)), 0, '@2012 IMT Pty Ltd'));

        var journeyPathLayer = new GTileLayer(myCopyright);
        journeyPathLayer.getTileUrl = function (tile, zoom) {
            return path + '?zoom=' + zoom +
              '&x=' + tile.x +
                  '&y=' + tile.y +
                      '&assetId=' + assetId +
                          '&startDate=' + startDate +
                              '&finishDate=' + finishDate;
        };

        journeyPathLayer.isPng = function () {
            return true;
        };
        journeyPathLayer.getOpacity = function () {
            return 1.0;
        };

        journeyPathOverlay = new GTileLayerOverlay(journeyPathLayer);
        map.addOverlay(journeyPathOverlay);
    }

This overlay requests it tiles from an aspx page that gets some points from a database, determines if the points are in the tile and then returns an image with the points on it. I developed it based on the same pattern as Gheat.net. And I am also using the Gmap.Net library for its Mercator projection, for when I need to work out which points should be in which tiles.

This works really well when I am using the standard Google maps API. However, recently I have to integrate my application with our clients Google Earth Enterprise server. This means that I am now getting my map imagery and API from our clients server and not from Google proper. This has caused a problem with my overlay.

My overlay is not longer position where it should be relative to the map location. They are appearing a bit lower that where they should be.

Using firebug I examined the request for my tile layer and found out that the tile X and Y where different than what I was expecting.

At zoom level 12, using Google maps a particular LatLng location is in tile X-3392, Y-2312. But in Google Earth the same location at the same zoom level is in tile X-3392, Y-2305. that's a 7 tile difference in the Y axis.

What can account for this difference? Does Google Earth use a difference projection, if so is there a convenient .net library for it?

3 Answers 3

6

It is not correct to refer to WGS84 as a 'projection'. Rather it is a datum that is slightly different from a perfect sphere. WGS84 can still be projected onto a Mercator map (as in Google Maps) or a sphere (as in Google Earth). You are simply going between Mercator and spherical. Use the following functions to convert between the two.

(in PHP, should be pretty simple to express in a different language)

function latlng2xy($lat,$lng) {
    $x = $lng;
    $y = rad2deg(asinh(tan(deg2rad($lat))));
    return Array($x,$y);
}

function xy2latlng($x,$y) {
    $lat = rad2deg(atan(sinh(deg2rad($y))));
    $lng = $x;
    return Array($lat,$lng);
}
3

Google Earth uses the WGS84 projection whereas Google Maps uses a close variant of the Mercator projection.

If the earth were perfectly spherical, the projection would be the same as the Mercator. Google Maps uses the formulæ for the spherical Mercator, but the coordinates of features on Google Maps are the GPS coordinates based on the WGS 84 datum.

For the libraries, you could try DotSpatial. From their site:

DotSpatial is a geographic information system library written for .NET 4. It allows developers to incorporate spatial data, analysis and mapping functionality >into their applications or to contribute GIS extensions to the community.

It has the ability to reproject on the fly if I am not mistaken.

1
  • 1
    because Google Earth was written by geographers and Google Maps bu computer scientists/mathematicians
    – Ian Turton
    Mar 8, 2012 at 8:47
3

As R.K. said, Google Earth uses the WGS84 datum and geographic latlong projection which has the code EPSG:4326 whereas Google Maps uses EPSG:3857 (alias EPSG:900913).

GDAL/OGR is a good open source library to convert coordinates and there seems to be a .NET interface too: http://trac.osgeo.org/gdal/wiki/GdalOgrInCsharp

But I don't think that you can use it to calculate tiles coordinates (X, Y at a given zoom level) directly, you would probably have to calculate the tiles coordinates from the projection coordinates yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.