Loading layer from shape file, setting correct extent

1790
1
01-31-2013 08:00 AM
Labels (1)
GeorgeFaraj
Occasional Contributor III
I'm using the sample below to add shape files as layers in my map.

http://www.arcgis.com/home/item.html?id=953a530ada30477d95cc69e788268bc9

When I start with an empty map and try adding a shape file, I see it added to the Legend, but I don't see it in the map. I tried clicking the "Full Extent" button in the Navigation control, but I'm getting a NullReferenceException when I do that.

I also tried setting the map's extent to the layer's extent when the Initialized event is fired:

MyMap.Extent = arcGisLocalDynamicMapServiceLayer.FullExtent;


And I also get a NullReferenceException when I do this. What is the proper way to position the map so that I can view the layer?

Thanks.
0 Kudos
1 Reply
MichaelBranscomb
Esri Frequent Contributor
Hi,

The FullExtent property of the ArcGISLocalDynamicMapServiceLayer will not be alterred by any changes you make via the DynamicLayerInfos and LayerDrawingOptions properties - these define per request modification of the layers and layer rendering in the service.

To determine the full extent of a specific layer you have two options:

#1. Create a FeatureLayer and set the Source property with a LayerDataSource based on the new TableDataSource object you have created - this is the easiest approach.

// Create a new FeatureLayer instance passing in the local service.
FeatureLayer featureLayer = new FeatureLayer()
{
    // Construct the URL to include the /dynamicLayer resource.
    Url = localMapService.UrlMapService + "/dynamicLayer",
    // Assign ID
    ID = fileName,
    // Display all fields
    OutFields = new ESRI.ArcGIS.Client.Tasks.OutFields() { "*" },
    // Yellow is generally a nice selection color
    SelectionColor = new SolidColorBrush(Colors.Yellow), 
};
                    
// The workspace is a feature class so create a new TableDataSource
DataSource dataSource = new TableDataSource
{
    // Match the DataSourceName to the physical filename on disk (excluding extension).
    DataSourceName = fileName,
    // Provide the WorkspaceID (the unique workspace identifier created earlier).
    WorkspaceID = workspaceInfo.Id 
};

// Set the Source property of the DynamicLayerInfo object.
LayerDataSource layerDataSource = new LayerDataSource { DataSource = dataSource };

// Assign the LayerDataSource
featureLayer.Source = layerDataSource;


#2. Construct a request which includes the JSON definition and use WebClient to make the request and download the response then parse it - more effort but more flexible.

e.g.

// Create a new WebClient instance to make the request and download the response.
WebClient webClient = new WebClient();

// Register an asynchronous handler in which to create the renderers and apply the to the dynamic map service layer.
webClient.DownloadDataCompleted += (client, downloadDataEventArgs) => 
{

    // Read the JSON response as XML
    XmlReader reader = System.Runtime.Serialization.Json.JsonReaderWriterFactory.CreateJsonReader(downloadDataEventArgs.Result, new XmlDictionaryReaderQuotas());

    // Get the root XML element
    XElement root = XElement.Load(reader);

    // Query for the "geometryType" element
    XElement geometryType = root.XPathSelectElement("//geometryType");

    // Create the render based on the geometry type
    switch (geometryType.Value)
    {
        case "esriGeometryPoint":
            layerDrawOpt.Renderer = new SimpleRenderer() { Symbol = new SimpleMarkerSymbol() { Color = new SolidColorBrush(GetRandomColor()), Size=8 } };
            break;
        case "esriGeometryPolyline":
            layerDrawOpt.Renderer = new SimpleRenderer() { Symbol = new SimpleLineSymbol() { Color = new SolidColorBrush(GetRandomColor()) } };
            break;
        case "esriGeometryPolygon":
            layerDrawOpt.Renderer = new SimpleRenderer() { Symbol = new SimpleFillSymbol() { Fill = new SolidColorBrush(GetRandomColor()), BorderBrush = new SolidColorBrush(GetRandomColor()) } };
            break;
    }

    // Set the LayerDrawingOptions property on the local dynamic map service layer (the LayerID property ties this to the DynamicLayerInfo object).
    layerDrawingOptionsCollection.Add(layerDrawOpt);

    // Update the layer drawing options property on the dynamic map service layer.
    arcGisLocalDynamicMapServiceLayer.LayerDrawingOptions = layerDrawingOptionsCollection;

    // Need to refresh the layer after the renderer(s) have been applied.
    arcGisLocalDynamicMapServiceLayer.Refresh();

};

// Make the request for the service metadata
// e.g. http://127.0.0.1:<PORT>/arcgis/rest/services/<MPK_NAME>/MapServer/dynamicLayer?layer={"id":0,"source":{"type":"dataLayer","dataSource":{"type":"table","workspaceId":"MyWorkspace","dataSourceName":"MyFeatureClassName"}}}
webClient.DownloadDataAsync(new Uri(arcGisLocalDynamicMapServiceLayer.Url 
    + "/dynamicLayer?layer={'id':" + counter.ToString() + ","
    + "'source':{'type':'dataLayer','dataSource':{"
    + "'type':'table',"
    + "'workspaceId':'"+ workspaceInfo.Id + "',"
    + "'dataSourceName':'" + fileName + "'"
    + "}}}"));



We added a pair of new methods in 10.1.1: ArcGISLocalDynamicMapServiceLayer.GetDetails(int layer ID) and ArcGISLocalDynamicMapServiceLayer.GetAllDetails() which return either a single FeatureLayerInfo object or a collection of FeatureLayerInfo objects, one for each of the layers in the map service. However, these methods do not currently honour any dynamic layer changes you have made. This is on the roadmap for a future release.

Cheers

Mike
0 Kudos