I was trying to obtain a gpx file with some coordinates by drawing on an Openlayers map with an ArcGIS baseMap.

When I draw the polyline and create the gpx, if I open it on Google Earth, what I see is not what I drawed before, the line is totally different from the original and not positioned where I drawed it. I know it's a projection problem, I've tried trasforming the geometry object from Mercator to Geographic, also getting directly the geographic coordinates from the map coordinates, but nothing. I tried to set "spatialReference" to 4362 and then to 3857, but nothing changes.

I'm going to use that .gpx on a gps device (the next week I'll go to the Svalbard islands and I need some gps tracks to go around Longyearbyen by snowmobile, there there aren't any sign of life out the town, so I must be prepared to it), when I'll be there I'll adjust the output right for the device they will rent to me, but now I need to save on the .gpx file almost the right coordinates.

I'm getting from the map those coordinates: lat: 61.22582068741976 lon: 4.684820015391338 when I'm expecting instead something around 78. lat and 15. lon.

This is some of the code I use to create the map (I'm not pasting the code I know it's not responsible of my problems):

    var initialExtent = new esri.geometry.Extent({"xmin":505615.5801124362,"ymin":8678955.717187276,"xmax":525935.6207525175,"ymax":8689168.654279819,"spatialReference":{"wkid":32633,"latestWkid":32633}});

    map = map = new esri.Map("map", {extent: initialExtent, logo : false});
    basemapURL = "http://geodata.npolar.no/ArcGIS/rest/services/inspire1/NP_TopoSvalbard_U33_CHL/MapServer/";
    map.addLayer(new esri.layers.ArcGISTiledMapServiceLayer(basemapURL));

Here I'm using wkid 32633 that is the default for that map, tried to change with known ones, but nothing happened.

And now the code I use to get the coordinates:

        dojo.connect(tb, "onDrawEnd", getCoords);
        function getCoords(geo) {
            var r=confirm("Salvare tracciato?");
            if (r==true) {
                geo = esri.geometry.webMercatorToGeographic(geo);   
                for ( var path = 0; path < geo.paths.length; path ++ ) {                   
                    for ( var pt = 0; pt < geo.paths[path].length; pt++ ) {
                        tra += '<wpt lat="'+geo.paths[path][pt][1]+'" lon="'+geo.paths[path][pt][0]+'"></wpt>';

"tra" is a variable that stores all the code I'll insert into the gpx file with an other function. The "webMercatorToGeographic" function transform the map coordinates to geographic ones.

  • If your map is in wkid:32633, it's not in web mercator. You can't use webMercatorToGeographic function to get geographic coordinates. And what exactly are you using? OpenLayers, or ESRI's JavascriptAPI? Your code seems to suggest the later. – Devdatta Tengshe Feb 5 '14 at 6:02
  • Ya, I'm using ESRI's js API. – Alberto Feb 5 '14 at 11:22
  • @DevdattaTengshe, do you know a way to convert the coordinates in wkid:32633 to "real" coordinates? – Alberto Feb 7 '14 at 0:46

The issues is that your map is in a projection with wkid:32633 , i.e. UTM 33 N.This isn't the webMercator projections.

That's why if you use the esri.geometry.webMercatorToGeographic() method, you are bound to get wrong results.

You need to properly project the coordinates to Geographic Lat-long. There is nothing out of the box for this kind of coordinate transformation on the client side. You have two options:

  • Write some custom code for doing this coordinate transformation. This is not as difficult as it sounds.
  • Use the proj4js library in your application. To do this, follow these steps:

    1. Download the latest library from https://github.com/proj4js/proj4js/releases
    2. In your HTML file use <script type="text/javascript" src="dist/proj4.js"></script>
    3. You'll need to define the projections before you can use it. You can do this by using the following lines of code: var UTM33N= "+proj=utm +zone=33 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"; var GCS84="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs";

    4. When you need to transform, you can use the following line: var transformed=proj4(UTM33N,GCS84,[x,y]); where x & y are the coordinates in your given projection. As output you'll get an array with two elements, the longitude, & the Latitude

If you want to do this on the server side, you could always use the Project Operation on the Geometry service of ArcGIS Server. A sample is available here: Project a point

  • Really, REALLY thank you! It works fine with proj4js. Thanks! – Alberto Feb 7 '14 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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