How to get the spatialReference of a Basemap

2064
9
Jump to solution
11-16-2017 04:41 AM
NicoleSchmidt
New Contributor III

Is there a way to get access to the spatialReference of the basemap?  This seems like it should be simple.  I should be able to create the basemap from the portalId and check the spatial reference to predict whether the load will fail.  I have tried checking baselayers, I have tried calling .load on the basemap and .load on the baselayers then checking the spatialReference.   It is still null.

Will Esri eventually handle changing spatial references of a mapview when a user chooses a basemap that is in a different spatial reference from the currently loaded mapView?

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Nicole,

   I don't know how to do it without loading the portal item. Here is my code for loading the item and checking the spatialReference:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
  <title>Load a basic WebMap - 4.5</title>

  <style>
    html,
    body,
    #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
  </style>

  <link rel="stylesheet" href="https://js.arcgis.com/4.5/esri/css/main.css">

  <script src="https://js.arcgis.com/4.5/"></script>

  <script>
    require([
      "esri/views/MapView",
      "esri/WebMap",
      "esri/portal/PortalItem",
      "dojo/domReady!"
    ], function(
      MapView, WebMap, PortalItem
    ) {

      /************************************************************
       * Creates a new WebMap instance. A WebMap must reference
       * a PortalItem ID that represents a WebMap saved to
       * arcgis.com or an on-premise portal.
       *
       * To load a WebMap from an on-premise portal, set the portal
       * url with esriConfig.portalUrl.
       ************************************************************/
      var webmap = new WebMap({
        portalItem: { // autocasts as new PortalItem()
          id: "f2e9b762544945f390ca4ac3671cfa72"
        }
      });

      /************************************************************
       * Set the WebMap instance to the map property in a MapView.
       ************************************************************/
      var view = new MapView({
        map: webmap,
        container: "viewDiv"
      });
      
      view.then(function(){
        var item = new PortalItem({
          id: "6d4b8b5b8a174c20b55b0934941030f4"
        });
        item.load()
        item.fetchData("json").then(function(rslt){
          console.info(rslt.spatialReference.wkid);
        });
      });
    });
  </script>
</head>

<body>
  <div id="viewDiv"></div>
</body>

</html>

View solution in original post

9 Replies
RobertScheitlin__GISP
MVP Emeritus

Nicole,

   Based on your use of mapView can we assume you are asking about JS API 4.x?

0 Kudos
NicoleSchmidt
New Contributor III

Apologies, yes I am talking about JS API 4.x

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Nicole,

  Here si a sample that shows how to get the basemaps spatialReference:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
  <title>Load a basic WebMap - 4.5</title>

  <style>
    html,
    body,
    #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
  </style>

  <link rel="stylesheet" href="https://js.arcgis.com/4.5/esri/css/main.css">

  <script src="https://js.arcgis.com/4.5/"></script>

  <script>
    require([
      "esri/views/MapView",
      "esri/WebMap",
      "dojo/domReady!"
    ], function(
      MapView, WebMap
    ) {

      /************************************************************
       * Creates a new WebMap instance. A WebMap must reference
       * a PortalItem ID that represents a WebMap saved to
       * arcgis.com or an on-premise portal.
       *
       * To load a WebMap from an on-premise portal, set the portal
       * url with esriConfig.portalUrl.
       ************************************************************/
      var webmap = new WebMap({
        portalItem: { // autocasts as new PortalItem()
          id: "f2e9b762544945f390ca4ac3671cfa72"
        }
      });

      /************************************************************
       * Set the WebMap instance to the map property in a MapView.
       ************************************************************/
      var view = new MapView({
        map: webmap,
        container: "viewDiv"
      });
      view.then(function(){
        console.info(webmap.basemap.baseLayers.getItemAt(0).spatialReference.wkid);
      });
    });
  </script>
</head>

<body>
  <div id="viewDiv"></div>
</body>

</html>
0 Kudos
NicoleSchmidt
New Contributor III

Thank you, but I am trying to get the spatial reference without loading it into the map.  I am trying to accurately predict whether a "swap" of the base map is going to fail due to  "layerview:spatial-reference-incompatible".

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Nicole,

   What is the info you do know about the basemap that is being added? How are you adding the new basemap?

0 Kudos
NicoleSchmidt
New Contributor III

Robert,

This is the code I am using, I really just know the portalId of the basemap. 

var basemapNew = new Basemap({
  portalItem: {
  id: portalid
  }
});
myMapView.map.basemap = basemapNew; // "layerview:spatial-reference-incompatible" is logged to the console and the basemap property set fails if the new basemap is in a different SR

If I could trap that "layerview:spatial-reference-incompatible" state, I could handle the problem after setting the basemap, but I have not been able to find a way to do that. 

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Nicole,

   I don't know how to do it without loading the portal item. Here is my code for loading the item and checking the spatialReference:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
  <title>Load a basic WebMap - 4.5</title>

  <style>
    html,
    body,
    #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
  </style>

  <link rel="stylesheet" href="https://js.arcgis.com/4.5/esri/css/main.css">

  <script src="https://js.arcgis.com/4.5/"></script>

  <script>
    require([
      "esri/views/MapView",
      "esri/WebMap",
      "esri/portal/PortalItem",
      "dojo/domReady!"
    ], function(
      MapView, WebMap, PortalItem
    ) {

      /************************************************************
       * Creates a new WebMap instance. A WebMap must reference
       * a PortalItem ID that represents a WebMap saved to
       * arcgis.com or an on-premise portal.
       *
       * To load a WebMap from an on-premise portal, set the portal
       * url with esriConfig.portalUrl.
       ************************************************************/
      var webmap = new WebMap({
        portalItem: { // autocasts as new PortalItem()
          id: "f2e9b762544945f390ca4ac3671cfa72"
        }
      });

      /************************************************************
       * Set the WebMap instance to the map property in a MapView.
       ************************************************************/
      var view = new MapView({
        map: webmap,
        container: "viewDiv"
      });
      
      view.then(function(){
        var item = new PortalItem({
          id: "6d4b8b5b8a174c20b55b0934941030f4"
        });
        item.load()
        item.fetchData("json").then(function(rslt){
          console.info(rslt.spatialReference.wkid);
        });
      });
    });
  </script>
</head>

<body>
  <div id="viewDiv"></div>
</body>

</html>
NicoleSchmidt
New Contributor III

Thank you Robert, that is very helpful.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Don't forget to mark this question as answered by clicking on the "Mark Correct" link on the reply that answered your question.

0 Kudos