Select to view content in your preferred language

Convert map extent to UTM

2469
11
07-27-2018 09:58 AM
rajujee
Emerging Contributor

I  have a widget that calculate map extent or lat/long.

I need to convert these values to UTM (Mercator). Could you please help. I have included my code below.

 

 

define([

    'dojo/_base/declare',

    'dojo/_base/lang',

    'jimu/BaseWidget',

    'jimu/WidgetManager'

  ],

  function (

    declare,

    lang,

    BaseWidget,

    WidgetManager) {

      var clazz = declare([BaseWidget], {

 

          name: 'UrlButton',

          baseClass: 'widget-urlbutton',

          isOpening: false,

 

          onOpen: function () {

              if (!this.isOpening) {

                  this.isOpening = true;

                  //Get lat,long,scale start

                  var mapCeneter = this.map.extent.getCenter();

                  var lat = mapCeneter.getLatitude();

                  var lon = mapCeneter.getLongitude();

 

                  var minxx = this.map.extent.xmin;

                  var minyy = this.map.extent.ymin;

                  var maxxx = this.map.extent.xmax;

                  var maxyy = this.map.extent.ymax;

 

Do I have to use toUtm(point, conversionMode, addSpaces)

https://developers.arcgis.com/javascript/latest/api-reference/esri-geometry-coordinateFormatter.html...

0 Kudos
11 Replies
KenBuja
MVP Esteemed Contributor

It looks like you're developing 2-D widget...is that true? If so, you can't use toUTM since that's a 4.x object and 2-D widgets use 3.x.

RobertScheitlin__GISP
MVP Emeritus

I would just take the geometry and use the GeometryService to project it to the UTM zone you are interested in.

rajujee
Emerging Contributor

Thanks for prompt response. Could you share code or steps please.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Raju,

   The documentation has some code for that:

https://developers.arcgis.com/javascript/3/jsapi/geometryservice-amd.html#project 

require([
  "esri/tasks/ProjectParameters", ... 
], function(ProjectParameters, ... ) {
  var params = new ProjectParameters();
  params.geometries = [point];
  params.outSR = outSR;
  params.transformation = transformation;
  gsvc.project(params);
  ...
});
0 Kudos
rajujee
Emerging Contributor

I didn't imagine It would be tough to convert lat,long into bbox. I am trying to use this function but getting stuck on gsvc. Could you please guide me what I is wrong in my code.

 var params = new ProjectParameters();
                  params.geometries = [lat, lon];
                  params.outSR = 4326; // is it source transformation?
                  params.transformation = 4326; // is it destination transformation?

                  console.log(params)
                  var gsvc; // what kind of variable is required?
                  gsvc.project(params); // how I can get bbox from gsvc varaible?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Raju,


  The geometry service geometries property is expecting an array of geometries. You have giving it an array of Lat, Lon coordinates. Create a point class from your coordinates and then add that point class to the geometries array.

0 Kudos
rajujee
Emerging Contributor

Thanks Rob for being helpful.  I am getting error  TypeError: Cannot read property 'spatialReference' of undefined

var outSR = new SpatialReference({ wkid: 4326 });

               
                  var mapPnt = new Point(lon, lat);
                  var params = new ProjectParameters();
                  params.geometries = mapPnt;
                  params.outSR = 4326;
                  params.transformation = 4326;

                 var geometryService = esri.tasks.GeometryService("https://utility.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
                  geometryService.project(params);

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Raju,

  You have not specified a SR when you created the Point.

var mapPnt = new Point(lon, lat, missing SpatialReference);

0 Kudos
rajujee
Emerging Contributor

I hope you have a nice weekend. I am still getting same error after adding spatial reference.

var outSR = new SpatialReference({ wkid: 4326 });

 

                
                  var mapPnt = new Point(lon, lat, outSR);
                  var params = new ProjectParameters();
                  params.geometries = mapPnt;
                  params.outSR = outSR; 
                  params.transformation = 4326;

 

                 var geometryService = esri.tasks.GeometryService("https://utility.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
                  geometryService.project(params);

Do I have to use daw-complete to re-project?

// geometry service
var toolbar = new Draw(this.map);
toolbar.on("draw-omplete", addToMap);

addtoMap: function (evt) {
var graphic = new Graphic(evt.geometry, symbol);
map.graphics.add(graphic);
console.log(dojo.toJson(geometry.toJson()));

0 Kudos