Script returns value on test, but not in popup

675
4
Jump to solution
05-28-2020 02:40 AM
by Anonymous User
Not applicable

Hi

I created a script to convert UTM32N coordinates to WGS84 Coordinates and put them in a Google Street View parameter to get a dynamic link to Google Street View for every property.

The script runs just fine and even returns a nice string that seems very correct when i'm testing the script. Once I save the script and deploy to use in a popup the script doesn't return anything. Is there something I'm doing wrong?

var PointGeometry = Centroid(Geometry($feature));

var ArcadeX = PointGeometry.x;
var ArcadeY = PointGeometry.y;
var ArcadeSr = PointGeometry.spatialReference.wkid;
var Latitude, Longitude;

var DatumEqRad = [6378137.0,
                    6378137.0,
                    6378137.0,
                    6378135.0,
                    6378160.0,
                    6378245.0,
                    6378206.4,
                    6378388.0,
                    6378388.0,
                    6378249.1,
                    6378206.4,
                    6377563.4,
                    6377397.2,
                    6377276.3]; 
var DatumFlat = [298.2572236,
                   298.2572236,
                   298.2572215,
                   298.2597208,
                   298.2497323,
                   298.2997381,
                   294.9786982,
                   296.9993621,
                   296.9993621,
                   293.4660167,
                   294.9786982,
                   299.3247788,
                   299.1527052,
                   300.8021499]; 

var Item = 0;             
var a    = DatumEqRad[Item];    
var f    = 1 / DatumFlat[Item]; 
var drad = PI / 180;      

// Mor constants, extracted from the function:
var k0   = 0.9996;                    
var b    = a*(1 - f);               
var e    = Sqrt(1 - (b/a)*(b/a)); 
var e0   = e / Sqrt(1 - e*e);    
var esq  = (1 - (b/a) * (b/a));       
var e0sq = e*e / (1 - e*e);         

function utmToLatLon(x, y, utmz, north) {

  // First some validation:
  if (x < 160000 || x > 840000) {
    alert("Outside permissible range of easting values.");
    return;
  } 
  if (y < 0){
    alert("Negative values are not allowed for northing.");
    return;
  }
  if (y > 10000000) {
    alert("Northing may not exceed 10,000,000.");
    return;
  }

  // Now the actual calculation:
  var zcm = 3 + 6*(utmz-1) - 180;
  var e1  = (1 - Sqrt(1 - e*e)) / (1 + Sqrt(1 - e*e));
  var M0  = 0;  // in case origin other than zero lat - not needed for standard UTM

  var M;  // arc length along standard meridian
  if (north) {
    M = M0 + y/k0;
  } else {  // southern hemisphere
    M = M0 + (y-10000000) / k;
  }
  var mu = M / (a * (1 - esq*(1/4 + esq*(3/64 + 5*esq/256))));
  var phi1 = mu + e1*(3/2 - 27*e1*e1/32)*Sin(2*mu) + e1*e1*(21/16 -55*e1*e1/32)*Sin(4*mu);  // footprint Latitude
  phi1 = phi1 + e1*e1*e1*(Sin(6*mu)*151/96 + e1*Sin(8*mu)*1097/512);
  var C1 = e0sq * Pow(Cos(phi1), 2);
  var T1 = Pow(Tan(phi1), 2);
  var N1 = a / Sqrt(1 - Pow(e * Sin(phi1), 2));
  var R1 = N1 * (1 - e*e) / (1 - Pow(e * Sin(phi1), 2));
  var D  = (x - 500000) / (N1*k0);
  var phi = (D*D) * (1/2 - D*D * (5 + 3*T1 + 10*C1 - 4*C1*C1 - 9*e0sq) / 24);
  phi = phi + Pow(D, 6) * (61 + 90*T1 + 298*C1 + 45*T1*T1 -252*e0sq - 3*C1*C1) / 720;
  phi = phi1 - (N1 * Tan(phi1) / R1) * phi;

  // Output Latitude:
  var outLat = Floor(1000000*phi / drad) / 1000000;

  var lng = D * (1 + D*D * ((-1 -2*T1 -C1)/6 + D*D * (5 - 2*C1 + 28*T1 - 3*C1*C1 + 8*e0sq + 24*T1*T1) / 120)) / Cos(phi1);
  var lngd = zcm + lng/drad;

  // Output Longitude:
  var outLon = Floor(1000000*lngd)/1000000;

    Latitude = outLat;
    Longitude = outLon;

  return [outLat, outLon];
}

if (ArcadeSr == 4326) {
  Console("4326 Spatial Reference - No Conversion Necessary");
  Latitude = ArcadeY;
  Longitude = ArcadeX;
} else if (ArcadeSr == 25832) {
  Console("25832 Spatial Reference - Conversion Necessary");
  utmToLatLon(ArcadeX, ArcadeY, 32, true);
} else {
  Console(ArcadeSr + " Spatial Reference is not supported - currently works with Web Maps where the basemap is in WGS84 (4326) or UTM32N (25832)");
}

var url = "https://www.google.com/maps/@?api=1&map_action=pano&viewpoint=" + text(Latitude) + "," + text(Longitude) + "&heading=-45&pitch=38&fov=80";
return url;
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

I figured out the issue, it seems as though Arcade Script in Portal is more limited than in ArcGIS Pro, so I had to simplify the code and calculations. Here is the final result:

var PointGeometry = Centroid(Geometry($feature));

var ArcadeX = PointGeometry.x;
var ArcadeY = PointGeometry.y;
var ArcadeSr = PointGeometry.spatialReference.wkid;
var Latitude, Longitude;

function utmToLatLon(x, y, utmz) {
    var a = 6378137.0;
    var f    = 1 / 298.2572236;
    var drad = PI / 180;
    
    var k0   = 0.9996;
    var b    = a*(1 - f);
    var e    = Sqrt(1 - (b/a)*(b/a));
    var e0   = e / Sqrt(1 - e*e);
    var esq  = (1 - (b/a) * (b/a));
    var e0sq = e*e / (1 - e*e);

  // First some validation:
  if (x < 160000 || x > 840000) {
    // Outside permissible range of easting values.
    return "";
  }
  if (y < 0){
    // Negative values are not allowed for northing.
    return "";
  }
  if (y > 10000000) {
    // Northing may not exceed 10,000,000.
    return "";
  }

  // Now the actual calculation:
  var zcm = 3 + 6*(utmz-1) - 180;
  var e1  = (1 - Sqrt(1 - e*e)) / (1 + Sqrt(1 - e*e));
  var M0  = 0;
  var M = M0 + y / k0;
 
  var mu = M / (a * (1 - esq*(1/4 + esq*(3/64 + 5*esq/256))));
 
  var phi1 = mu + e1*(3/2 - 27*e1*e1/32)*Sin(2*mu) + e1*e1*(21/16 -55*e1*e1/32)*Sin(4*mu);  // footprint Latitude
  phi1 = phi1 + e1*e1*e1*(Sin(6*mu)*151/96 + e1*Sin(8*mu)*1097/512);
 
  var C1 = e0sq * Pow(Cos(phi1), 2);
  var T1 = Pow(Tan(phi1), 2);
  var N1 = a / Sqrt(1 - Pow(e * Sin(phi1), 2));
  var R1 = N1 * (1 - e*e) / (1 - Pow(e * Sin(phi1), 2));
  var D = (x - 500000) / (N1*k0);
 
  var phi = (D*D) * (1/2 - D*D * (5 + 3*T1 + 10*C1 - 4*C1*C1 - 9*e0sq) / 24);
  phi = phi + Pow(D, 6) * (61 + 90*T1 + 298*C1 + 45*T1*T1 -252*e0sq - 3*C1*C1) / 720;
  phi = phi1 - (N1 * Tan(phi1) / R1) * phi;

  // Output Latitude:
  Latitude = Floor(1000000*phi / drad) / 1000000;

  var lng = D * (1 + D*D * ((-1 -2*T1 -C1)/6 + D*D * (5 - 2*C1 + 28*T1 - 3*C1*C1 + 8*e0sq + 24*T1*T1) / 120)) / Cos(phi1);
  var lngd = zcm + lng/drad;

  // Output Longitude:
  Longitude = Floor(1000000*lngd)/1000000;
}

if (ArcadeSr == 4326) {
  Latitude = ArcadeY;
  Longitude = ArcadeX;
  return "https://www.google.com/maps/@?api=1&map_action=pano&viewpoint=" + text(Latitude) + "," + text(Longitude) + "&heading=-45&pitch=38&fov=80";
} else if (ArcadeSr == 25832) {
  utmToLatLon(ArcadeX, ArcadeY, 32);
  return "https://www.google.com/maps/@?api=1&map_action=pano&viewpoint=" + text(Latitude) + "," + text(Longitude) + "&heading=-45&pitch=38&fov=80";
} else {
  return "";
}

View solution in original post

4 Replies
by Anonymous User
Not applicable

By the way, the script works perfectly in ArcGIS Pro but not in ArcGIS Online or Portal.

0 Kudos
by Anonymous User
Not applicable

I figured out the issue, it seems as though Arcade Script in Portal is more limited than in ArcGIS Pro, so I had to simplify the code and calculations. Here is the final result:

var PointGeometry = Centroid(Geometry($feature));

var ArcadeX = PointGeometry.x;
var ArcadeY = PointGeometry.y;
var ArcadeSr = PointGeometry.spatialReference.wkid;
var Latitude, Longitude;

function utmToLatLon(x, y, utmz) {
    var a = 6378137.0;
    var f    = 1 / 298.2572236;
    var drad = PI / 180;
    
    var k0   = 0.9996;
    var b    = a*(1 - f);
    var e    = Sqrt(1 - (b/a)*(b/a));
    var e0   = e / Sqrt(1 - e*e);
    var esq  = (1 - (b/a) * (b/a));
    var e0sq = e*e / (1 - e*e);

  // First some validation:
  if (x < 160000 || x > 840000) {
    // Outside permissible range of easting values.
    return "";
  }
  if (y < 0){
    // Negative values are not allowed for northing.
    return "";
  }
  if (y > 10000000) {
    // Northing may not exceed 10,000,000.
    return "";
  }

  // Now the actual calculation:
  var zcm = 3 + 6*(utmz-1) - 180;
  var e1  = (1 - Sqrt(1 - e*e)) / (1 + Sqrt(1 - e*e));
  var M0  = 0;
  var M = M0 + y / k0;
 
  var mu = M / (a * (1 - esq*(1/4 + esq*(3/64 + 5*esq/256))));
 
  var phi1 = mu + e1*(3/2 - 27*e1*e1/32)*Sin(2*mu) + e1*e1*(21/16 -55*e1*e1/32)*Sin(4*mu);  // footprint Latitude
  phi1 = phi1 + e1*e1*e1*(Sin(6*mu)*151/96 + e1*Sin(8*mu)*1097/512);
 
  var C1 = e0sq * Pow(Cos(phi1), 2);
  var T1 = Pow(Tan(phi1), 2);
  var N1 = a / Sqrt(1 - Pow(e * Sin(phi1), 2));
  var R1 = N1 * (1 - e*e) / (1 - Pow(e * Sin(phi1), 2));
  var D = (x - 500000) / (N1*k0);
 
  var phi = (D*D) * (1/2 - D*D * (5 + 3*T1 + 10*C1 - 4*C1*C1 - 9*e0sq) / 24);
  phi = phi + Pow(D, 6) * (61 + 90*T1 + 298*C1 + 45*T1*T1 -252*e0sq - 3*C1*C1) / 720;
  phi = phi1 - (N1 * Tan(phi1) / R1) * phi;

  // Output Latitude:
  Latitude = Floor(1000000*phi / drad) / 1000000;

  var lng = D * (1 + D*D * ((-1 -2*T1 -C1)/6 + D*D * (5 - 2*C1 + 28*T1 - 3*C1*C1 + 8*e0sq + 24*T1*T1) / 120)) / Cos(phi1);
  var lngd = zcm + lng/drad;

  // Output Longitude:
  Longitude = Floor(1000000*lngd)/1000000;
}

if (ArcadeSr == 4326) {
  Latitude = ArcadeY;
  Longitude = ArcadeX;
  return "https://www.google.com/maps/@?api=1&map_action=pano&viewpoint=" + text(Latitude) + "," + text(Longitude) + "&heading=-45&pitch=38&fov=80";
} else if (ArcadeSr == 25832) {
  utmToLatLon(ArcadeX, ArcadeY, 32);
  return "https://www.google.com/maps/@?api=1&map_action=pano&viewpoint=" + text(Latitude) + "," + text(Longitude) + "&heading=-45&pitch=38&fov=80";
} else {
  return "";
}

markkellermannBWB
New Contributor II

That is what I need! And it works fine, also with EPSG:25833

Thanks....

by Anonymous User
Not applicable

Nice, you're welcome 🙂

0 Kudos