Adding a raster layer from a map service results in null layer

1820
17
04-17-2019 06:26 AM
Vidar
by
Occasional Contributor II

Hi,

NOTE: I have updated this question from struggling to add a specific layer from a map service (as I have managed to work this out) to having a problem with adding raster layers (don't work) vs adding vector layers (which do work!).

I have a map service which has many layers of type "Raster Layer".  It is stored on Portal. I can locate the exact layer I want (it's a single layer) - but when I add it to the map - the result never shows in the map.  I just have an empty map.

However - if you change the code ever so slightly below to work against a Map Service with just vector/feature based layers - the layer loads ABSOLUTELY FINE!

So why is this not working for raster layers ?? This is beyond frustrating!!

Anyone have any ideas?

public static async void AddLayerToView()
 {
    Uri portalUri = new Uri("https://fooserver.foocompany.local/portal");

    var portal = ArcGISPortalManager.Current.GetPortal(portalUri);
    var owner = portal.GetSignOnUsername();
    var portalInfo = await portal.GetPortalInfoAsync();

    // Get all web maps and map services - include user, organization
    // and "foo" in the title
    var query2 = PortalQueryParameters.CreateForItemsOfTypes(new List<PortalItemType>() {
    PortalItemType.WebMap, PortalItemType.MapService, PortalItemType.Layer}, owner, "", "Title:Foo");
    query2.OrganizationId = portalInfo.OrganizationId;

   //retrieve in batches of up to a 100 each time
    query2.Limit = 100;

   //Loop until done
    var portalItems = new List<PortalItem>();
    while (query2 != null)
    {
       //run the search
       PortalQueryResultSet<PortalItem> results = await portal.SearchForContentAsync(query2);
       portalItems.AddRange(results.Results);
       query2 = results.NextQueryParameters;
    }

    RasterLayer rasterLayer = null;
    //Layer vectorLayer = null;  //UNCOMMENT FOR VECTOR

    //process results
    foreach (var pi in portalItems)
    {
       string itemID = pi.ID;
       Item currentItem = ItemFactory.Instance.Create(itemID, ItemFactory.ItemType.PortalItem);

       await QueuedTask.Run(() =>
       {
          var subItems = currentItem.GetItems();

          foreach (var item in subItems)
          {
             if (item.Name == "MY_LAYER_NAME_TO_MATCH_ON")
             {
                if (LayerFactory.Instance.CanCreateLayerFrom(item))
                {
                   rasterLayer = (RasterLayer)LayerFactory.Instance.CreateLayer(item, MapView.Active.Map);
                   //vectorLayer = LayerFactory.Instance.CreateLayer(item, MapView.Active.Map);
//UNCOMMENT FOR VECTOR
                }
            }
         }
    });
}




PS - Is there any way to make the above look more like code layout.
Tags (3)
0 Kudos
17 Replies
RobertBorchert
Frequent Contributor III

Your best option is to create a new service that only has the layers you want

0 Kudos
Vidar
by
Occasional Contributor II

Impossible as I have over 40,000 layers

0 Kudos
RobertBorchert
Frequent Contributor III

using Pro or ArcMap add the layers you want from the database and create a new feature service and publish it.

0 Kudos
Vidar
by
Occasional Contributor II

I don't need a feature service - as no editing is required. I only need to query for a specific layer.  I can't make thousands of map services for each individual layer be it raster or vector it would be crazy.

0 Kudos
RobertBorchert
Frequent Contributor III

Cannot you simply connect to the database and add the specific raster you want

0 Kudos
Vidar
by
Occasional Contributor II

I want the data to be stored centrally (on a hosted server) not via a Geodatabase or ArcSDE, that's why I thought ArcGIS Portal should work.

0 Kudos
RobertBorchert
Frequent Contributor III

The data is stored somewhere.  Most commonly an SDE database.  We have our Portal set up with datastores and redundancy etc.  The Portal still needs to find the data somewhere.  We have it in an SDE Oracle database  on our company system. 

The map service was created by someone with all 40000 raster files being referenced.  Somewhere there is an  MXD or Pro Project that was used to create the service.  

In any event you should still be able to access the rasters wherever the database is that is storing them and add them.

Unless that is the raster dataset does not belong to your organization and you are adding them from a web service.  Such as in my location we can add raster air photography, which itself contains many thousands of rasters.  In this case you do not have the option of loading specific rasters without the proper permissions from the owner.

0 Kudos
Vidar
by
Occasional Contributor II

Yes, I created the services with ArcMap and there is a File Geodatabase backing the Map Services - but I don't want to access the FGDB - as it takes too long to connect and open it up.

0 Kudos
UmaHarano
Esri Regular Contributor

Hi

You can add an entire feature service or just one layer to your map using the CreateLayer method on the LayerFactory. To add just one layer, you use the layer ID. Like this:

string url = @"http://sampleserver6.arcgisonline.com/arcgis/rest/services/NapervilleShelters/FeatureServer/0";  //FeatureLayer off a map service or feature service

Uri uri = new Uri(url);
await QueuedTask.Run(() => LayerFactory.Instance.CreateLayer(uri, MapView.Active.Map));

Here is a code snippet for this:

Create and add a layer to the active map

To access data via databases - you can use connection properties. Here are some code snippets for this:

Opening an Enterprise Geodatabase using connection properties

Opening an Enterprise Geodatabase using sde file path

Getting Database Connection Properties from a Connection File

Thanks

Uma

0 Kudos