Select to view content in your preferred language

How do I retrieve layer type?

1108
8
Jump to solution
05-03-2013 01:44 PM
JennB
by
Regular Contributor
Hi everyone,

I'm trying to adjust my Print Widget so that raster layers do not show up in the legend. My thinking was that I could test for a layer type and not add the item to the legend if it is a raster.

Something like:
For each item in the legend     If layer is a raster:         do nothing;     Else         Add it to the legend


I know there's a LayerInfo thing out there but I'm not really sure how to use it. Incidentally, I'm new to C#/Silverlight/ArcGIS for Server so this simple task is a bit challenging for me. I did notice there was a GetType() method(?) that I might be able to use but I'm not sure if how if that is indeed the answer. The foreach legend item code already exists and such -- I'm looking to wrap some conditions around the legendItems.Add bit.

Alternatively, if I'm going about this in a difficult/wrong way, feel free to point that out!

Thanks in advance!

[ATTACH=CONFIG]24024[/ATTACH]

edit: found the following resources, but not sure how to use them if they are helpful...

http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.FeatureSer... --> the "Type" property, perhaps?
0 Kudos
1 Solution

Accepted Solutions
MahenderRajendran
Deactivated User
Hi Jennifer,

You can loop through layer items in the legend and remove if the layer type is of raster.

There is a layertype property in LayerItemViewModel which can be used to check the layer type.

In Xaml include the refreshed event (replace the map, layerIDS):

 
            <esri:Legend Map="{Binding ElementName=MyMap}"                            LayerIDs="Points of Interest, United States"                          LayerItemsMode="Tree"                           ShowOnlyVisibleLayers="False"                          Refreshed="Legend_Refreshed">


In code behind check the layer type in legend items:

 
private void Legend_Refreshed(object sender, Legend.RefreshedEventArgs e)         {                         // If a map layer has sublayers, iterate through them.             if (e.LayerItem.LayerItems != null)             {                 // Iterate through all the sublayer items.                 foreach (LayerItemViewModel layerItemVM in e.LayerItem.LayerItems)                 {                      // Remove the layer other than Feature layer.                     if (layerItemVM.LayerType != "Feature Layer")                       e.LayerItem.LayerItems.Remove(layerItemVM);                 }                       }


Hope this helps.

Cheers,
Mahender

View solution in original post

0 Kudos
8 Replies
JennB
by
Regular Contributor
Any thoughts? 🙂

I'm still a bit stuck on this...
0 Kudos
MahenderRajendran
Deactivated User
Hi Jennifer,

You can loop through layer items in the legend and remove if the layer type is of raster.

There is a layertype property in LayerItemViewModel which can be used to check the layer type.

In Xaml include the refreshed event (replace the map, layerIDS):

 
            <esri:Legend Map="{Binding ElementName=MyMap}"                            LayerIDs="Points of Interest, United States"                          LayerItemsMode="Tree"                           ShowOnlyVisibleLayers="False"                          Refreshed="Legend_Refreshed">


In code behind check the layer type in legend items:

 
private void Legend_Refreshed(object sender, Legend.RefreshedEventArgs e)         {                         // If a map layer has sublayers, iterate through them.             if (e.LayerItem.LayerItems != null)             {                 // Iterate through all the sublayer items.                 foreach (LayerItemViewModel layerItemVM in e.LayerItem.LayerItems)                 {                      // Remove the layer other than Feature layer.                     if (layerItemVM.LayerType != "Feature Layer")                       e.LayerItem.LayerItems.Remove(layerItemVM);                 }                       }


Hope this helps.

Cheers,
Mahender
0 Kudos
JennB
by
Regular Contributor

In Xaml include the refreshed event (replace the map, layerIDS):

 
            <esri:Legend Map="{Binding ElementName=MyMap}"  
                         LayerIDs="Points of Interest, United States"
                         LayerItemsMode="Tree" 
                         ShowOnlyVisibleLayers="False"
                         Refreshed="Legend_Refreshed">


Mahender,

First of all, thank you so much for taking the time to respond. It's greatly appreciated! I like the direction you're pointing me in, but likely due to me being a novice, the code from you I've quoted has confused me a bit.

Is it required that I specify LayerIDs? Since this widget is being designed to work on a product for many different clients, I'd like it to be as dynamic and flexible as possible (I'm not building this map from the ground-up. It already exists and I'm just fine-tuning it, such as removing rasters from the print widget legend).

Regards,
Jenn

edit:
Alas, when I tried to implement the solution...
"Error: Unhandled Error in Silverlight Application
Code: 2531   
Category: ParserError      
Message: Failed to assign to property 'ESRI.ArcGIS.Client.Toolkit.Legend.Refreshed'."

Guess I have to figure this out! 😃

edit 2:
I suppose I'll have a hard time getting your solution to work. The way the legend is defined in the XAML is... unusual?

<StackPanel x:Name="LegendPanel" Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Top">
     <StackPanel.Resources>
           <DataTemplate x:Key="SymbolTreeNode">
                  <StackPanel Orientation="Horizontal" Margin="0,-2,0,-2">
                         <Image Source="{Binding ImageSource}" Stretch="None" Width="36" Margin="0,0,0,0"></Image>
                         <TextBlock Text="{Binding Label}" Margin="4,0,0,0" VerticalAlignment="Center"></TextBlock>
                  </StackPanel>
            </DataTemplate>
       </StackPanel.Resources>
 </StackPanel>
0 Kudos
MahenderRajendran
Deactivated User
Jenn,

What kind of viewer you are currently working on?

Is it a custom made web application using ArcGIS Silverlight API? If so which library are you using for printing?

OR

Is it a ArcGIS viewer for Silverlight?



Cheers,
Mahender
0 Kudos
JennB
by
Regular Contributor
Mahender,

It's the Silverlight Viewer. I found a "work around" that my supervisor has accepted. All our raster imagery have similar labels, so I'm just excluding them on the basis of "if legendItem.Label.Contains("")."

Thank you so much. It really means a lot to me that you took time to help me =).

Cheers,
Jenn
0 Kudos
MonikaHonkova1
Deactivated User
Hi Jenn,

would you mind sharing how you declare LegendItem in your code?

Thanks a lot
0 Kudos
JennB
by
Regular Contributor
Hi there,

Sorry for the delay - I was on vacation. I actually work with a different company now so I don't have access to exactly how I did it. I suspect I was using the LayerLegendInfo or LegendItemInfo class (or something else similar to do with Legend or Layers...) and reading one of their values into my legendItem variable... 

http://resources.arcgis.com/en/help/silverlight-api/concepts/0166/pdf/ESRI.ArcGIS.Client.pdf
That's a link to the Client Namespace where you'll find those classes and their methods/properties.

http://resources.arcgis.com/en/help/silverlight-api/concepts/0166/pdf/ArcGISSilverlightOMD_Overview....
That's a link to an overview :).

http://resources.arcgis.com/en/help/silverlight-api/samples/start.htm
These are examples. I don't doubt I got some of my ideas from there, probably including what I was looking for here (check out the Toolkit Controls > Legend with Templates)

I'm really sorry I can't help you out more. I have a pitiful memory :(.
0 Kudos
JennB
by
Regular Contributor
Hah! I found something that I suspected was still saved in my drop box. This was roughly the code for my purposes:

foreach (LegendItemInfo legItem in (List<LegendItemInfo>)treeNode.ItemsSource)
{
if (legItem.ImageSource != null)
   itemLabel = (((List<LegendItemInfo>)treeNode.ItemSource.Count -- 1) ?
   layerName : legItem.Label;

   if (itemLabel.Contains(removed content)
   { legendItem.Add(new LegendItemInfo() { ImageSource = legItem.ImageSource, Label = itemLabel });
}
}
}


What that tells me is that legendItem was declared elsewhere as a LegendItemInfo or something similar (note its difference from legItem). I can't be certain but that's the best clue I've got :(. Sorry for the coding format. Retyping from a PDF ;o. Good luck!
0 Kudos