Select to view content in your preferred language

Get scale for a box in JS 4

357
1
Jump to solution
08-02-2023 03:14 PM
LefterisKoumis
Regular Contributor II

In JS API 3.x we could get easily the scale for a box drawn on the map after you define its 4 corners.

In the matrix for the migration from 3.x to 4.x, it states that there are no plans for the scaleUtils to be migrated.

So, what's the best option to get the scale in 4.x?

var scale = esri.geometry.scaleUtils.getScaleForBox(
  box,
  map.getSpatialReference()
); 

 

0 Kudos
1 Solution

Accepted Solutions
JoelBennett
MVP Regular Contributor

I haven't found any "getScaleForBox" function in the implementation of scaleUtils, however you can achieve the same result in 3.x via scaleUtils.getScale(map, extent, spatialReference), although the 2nd and 3rd parameters aren't documented.

As for 4.x, the implementation of the 3.x scaleUtils is fairly simple.  Migrating the relevant part to 4.x yields something like the following:

const screenDPI = 96;

function _getUnitValue(spatialReference) {
	if (spatialReference) {
		var wkid, wkt;

		if (typeof spatialReference == "object") {
			wkid = spatialReference.wkid;
			wkt = spatialReference.wkt;
		} else if (typeof spatialReference == "number")
			wkid = spatialReference;
		else if (typeof spatialReference == "string")
			wkt = spatialReference;

		if ((typeof wkid == "number") && (wkid >= 0))
			return WKIDUnitConversion.values[WKIDUnitConversion[wkid]];
		else if ((typeof wkt == "string") && (wkt.search(/^PROJCS/i) !== -1)) {
			var a = /UNIT\[([^\]]+)\]\]$/i.exec(wkt);

			if ((a) && (a[1]))
				return parseFloat(a[1].split(",")[1]);
		}
	}

	return 0;
}

function getScale(mapView, extent, spatialReference) {
	if ((mapView) && (mapView.width)) {
		var scaleExtent = extent || mapView.extent;

		if (scaleExtent) {
			var srUnitValue = _getUnitValue(spatialReference || scaleExtent.spatialReference);
			var unitValue = ((srUnitValue) ? srUnitValue : 6370997 * Math.PI / 180);

			return scaleExtent.width / mapView.width * unitValue * 39.37 * screenDPI;
		}
	}

	return 0;
}

 

As with 3.x, the 2nd and 3rd parameters are optional.  Note that "WKIDUnitConversion" is esri/geometry/support/WKIDUnitConversion, which you'll need to include.

View solution in original post

0 Kudos
1 Reply
JoelBennett
MVP Regular Contributor

I haven't found any "getScaleForBox" function in the implementation of scaleUtils, however you can achieve the same result in 3.x via scaleUtils.getScale(map, extent, spatialReference), although the 2nd and 3rd parameters aren't documented.

As for 4.x, the implementation of the 3.x scaleUtils is fairly simple.  Migrating the relevant part to 4.x yields something like the following:

const screenDPI = 96;

function _getUnitValue(spatialReference) {
	if (spatialReference) {
		var wkid, wkt;

		if (typeof spatialReference == "object") {
			wkid = spatialReference.wkid;
			wkt = spatialReference.wkt;
		} else if (typeof spatialReference == "number")
			wkid = spatialReference;
		else if (typeof spatialReference == "string")
			wkt = spatialReference;

		if ((typeof wkid == "number") && (wkid >= 0))
			return WKIDUnitConversion.values[WKIDUnitConversion[wkid]];
		else if ((typeof wkt == "string") && (wkt.search(/^PROJCS/i) !== -1)) {
			var a = /UNIT\[([^\]]+)\]\]$/i.exec(wkt);

			if ((a) && (a[1]))
				return parseFloat(a[1].split(",")[1]);
		}
	}

	return 0;
}

function getScale(mapView, extent, spatialReference) {
	if ((mapView) && (mapView.width)) {
		var scaleExtent = extent || mapView.extent;

		if (scaleExtent) {
			var srUnitValue = _getUnitValue(spatialReference || scaleExtent.spatialReference);
			var unitValue = ((srUnitValue) ? srUnitValue : 6370997 * Math.PI / 180);

			return scaleExtent.width / mapView.width * unitValue * 39.37 * screenDPI;
		}
	}

	return 0;
}

 

As with 3.x, the 2nd and 3rd parameters are optional.  Note that "WKIDUnitConversion" is esri/geometry/support/WKIDUnitConversion, which you'll need to include.

0 Kudos