Select to view content in your preferred language

how to query map knowing map layer name (not id)

5623
16
11-15-2010 10:50 AM
ThaoNguyen
Emerging Contributor
HI,
I have a map layer name, but don't have map id.  How can I write the QueryTask that is needed to query the map?  How to get layer id if I know map layer name?  Please help!  Thank you so much!!

Below is my code:

QueryTask queryTask = new QueryTask(MapURL); //I don't know the layer ID, therefore, cannot do MapURL + "/<layerid>")
                queryTask.Failed += QueryTask_Failed;
                queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
              
                // Specify fields to return from query
                Query query = new ESRI.ArcGIS.Client.Tasks.Query();
                query.OutFields.Add("*");
                query.Where = "0=0";

                // Return geometry with result features
                query.ReturnGeometry = true;
                queryTask.ExecuteAsync(query);
16 Replies
JenniferNery
Esri Regular Contributor
Is there a reason why you don't want to set the ID of the layer? You can get the layer based on ID or index.
Map.Layers[LayerID] or Map.Layers[index]
0 Kudos
ThaoNguyen
Emerging Contributor
Is there a reason why you don't want to set the ID of the layer? You can get the layer based on ID or index.
Map.Layers[LayerID] or Map.Layers[index]


At this point, the map query is NOT executed yet; therefore, I don't have anything from the Map object, no layers, no features.  I only know layer name which is passed to me by another means, not after drawing the map. 
My queryTask URL should have something like http://myMachineName/ArcGIS/rest/services/MapServer/MyMap/LayerID
and I don't have layerID.  Is there another way to construct QueryTask?

Thanks!
0 Kudos
DominiqueBroux
Esri Frequent Contributor
One option is you to include an ArcGISDynamicMapService in your map. Then, the 'Layers' property of this ArcGISDynamicMapService is giving you an array with the layer IDs, names, ...

If you don't want to include such a layer in your map, you can request yourself the mapserver endpoint and deserialize the result. Example of mapserver url : http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Median_Age/MapServer?f=pjson
0 Kudos
ThaoNguyen
Emerging Contributor
One option is you to include an ArcGISDynamicMapService in your map. Then, the 'Layers' property of this ArcGISDynamicMapService is giving you an array with the layer IDs, names, ...

If you don't want to include such a layer in your map, you can request yourself the mapserver endpoint and deserialize the result. Example of mapserver url : http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Median_Age/MapServer?f=pjson


I do have ArcGISDynamicMapService in my map, but at this point, I have not run the query to get the map layers yet.  My code is run after the wpf map page is loaded (it is inside the xx_loaded method).  Map.Layers will be null.  Hope I'm clear now.
0 Kudos
JenniferNery
Esri Regular Contributor
Oh you don't mean x:Name?

If you mean name defined in the service, you can probably keep them in a dictionary (if you know that names will be unique), with URL as the value. You can add to the dictionary as each layer is initialized.

For the main layer, you can use MapName.
http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.ArcGISDyna...

For the sub layers, you can use LayerInfo.Name and append the sub layer ID to the main layer's URL.
http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.LayerInfo~...

For FeatureLayer, it's similar.
http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.FeatureSer...

These information are available after the layer has initialized.
0 Kudos
ThaoNguyen
Emerging Contributor
Oh you don't mean x:Name?

If you mean name defined in the service, you can probably keep them in a dictionary (if you know that names will be unique), with URL as the value. You can add to the dictionary as each layer is initialized.

For the main layer, you can use MapName.
http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.ArcGISDyna...

For the sub layers, you can use LayerInfo.Name and append the sub layer ID to the main layer's URL.
http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.LayerInfo~...

For FeatureLayer, it's similar.
http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.FeatureSer...

These information are available after the layer has initialized.


No, I didn't mean x:Name.
Basically, the name of the layer is given to me. This is retrieved from data, not from map at all.  Knowing the layer name, now, I want to query the map layer so that I can get the regions of that layer.  For instance, my layer name is USRegions which has 9 features.  I want to run a QueryTask that somehow takes my "USRegions" and after the query is run, it gives me 9 features.  If I knew map layer ID, that would solve the problem, but unfortunately, I don't have layer id.
0 Kudos
ThaoNguyen
Emerging Contributor
One option is you to include an ArcGISDynamicMapService in your map. Then, the 'Layers' property of this ArcGISDynamicMapService is giving you an array with the layer IDs, names, ...

If you don't want to include such a layer in your map, you can request yourself the mapserver endpoint and deserialize the result. Example of mapserver url : http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Median_Age/MapServer?f=pjson


Do you have an example in C# how to get layerID using REST API?
0 Kudos
DominiqueBroux
Esri Frequent Contributor
I do have ArcGISDynamicMapService in my map, but at this point, I have not run the query to get the map layers yet. My code is run after the wpf map page is loaded (it is inside the xx_loaded method). Map.Layers will be null. Hope I'm clear now.

I don't understand why Map.layers is null if you do have an ArcGISDynamicMapService inside it. You should at least have this layer in Map.Layers?

That being said, this will not help you because at this moment the layer is not yet initialized so the sublayers are not yet known.

You could put your code in the 'Initialized' event of the layer. At this moment, the LayerInfo array will be initialized.
0 Kudos
ThaoNguyen
Emerging Contributor
I don't understand why Map.layers is null if you do have an ArcGISDynamicMapService inside it. You should at least have this layer in Map.Layers?

That being said, this will not help you because at this moment the layer is not yet initialized so the sublayers are not yet known.

You could put your code in the 'Initialized' event of the layer. At this moment, the LayerInfo array will be initialized.



Sorry, my bad, I was not clear.

In order to draw a map, you have to tell the map where the map service is or what map service you want to connect to, right?

Here is what I have in xaml:
<esri:Map x:Uid="mapId" x:Name="MainMap"
                  Margin="0"
                  Extent="-150.773,5.7054,-51.813,67.5554"
                  MouseRightButtonDown="MainMap_MouseRightButtonDown"
                  VerticalAlignment="Top"
                  ExtentChanged="MainMap_ExtentChanged"
                  >
            <esri:Map.Layers>
                <esri:ArcGISDynamicMapServiceLayer x:Uid="dynMapSvcId" ID="BigMap" Url="" />
                <!-- graphic map  -->
                <esri:GraphicsLayer x:Uid="graphicLayerId" ID="ResultMap" Opacity="0.7"/>
               
            </esri:Map.Layers>

        </esri:Map>

Code behind:
public MyClass()
        {
            InitializeComponent();
        }

private void MyClass_Loaded(object sender, RoutedEventArgs e)
        {
                QueryTask queryTask = new QueryTask(MapURL);
                queryTask.Failed += QueryTask_Failed;
                queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
               
                // Specify fields to return from query
                Query query = new ESRI.ArcGIS.Client.Tasks.Query();
                query.OutFields.Add("*");
                query.Where = "0=0";
                // Return geometry with result features
                query.ReturnGeometry = true;
                queryTask.ExecuteAsync(query);
}
The "MapURL" variable should have something like this: http://machineName/ArcGIS/rest/services/MyMapServiceName/MapServer/LayerID

I don't have LayerID in order to pass in the URL, but I have the layer name.
So, basically, the map is not drawing yet in this method.  The layer I meant was the layer from the map service Website.  So, Map.Layers should not be null.  You are correct!!  But the ID for the layer from xaml is not what I'm looking for.  I want the ID of the layer when I view the map service from the URL.  Sorry for not being clear! 

So, I think using json to get layer ID is one way, but I don't know how to do it.  Do you have an example in C# code how to do it?  I tried to pass in the url of "http://machineName/ArcGIS/rest/services/MyMapServiceName/MapServer/?f=json" to the code above but the query failed with System.Net.WebException: The remote server returned an error: (400) Bad Request.
I guess I didn't do it right.
0 Kudos