GraphicsLayer Spatial Reference

8782
7
05-02-2011 04:04 AM
KeremCelik
New Contributor
Hi,

I want to add a graphics layer in Map. But this layer is lack of Spatial Reference. I cannot specify the spatial reference, because it's property is read only(just get is implemented in the API).

Here is xaml (It is generated when I create an ESRI SL Application in Visual Studio)
<esri:Map x:Name="Map" Background="White">
    ....
    ....
      <esri:Map.Layers>
             <esri:ArcGISTiledMapServiceLayer ID="BaseLayer"
 Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
      </esri:Map.Layers>
</esri:Map>


I want to create Graphics Layer with the following code behind C# code
public GraphicsLayer CreatePointGraphicLayer(string ID,
            SpatialReference spatial_reference,
            List<double> x_list,
            List<double> y_list,
            List<string> fields,
            List<string[]> other_fields_values)
{
 GraphicsLayer graphics_layer = new GraphicsLayer();
 graphics_layer.ID = ID;

 SimpleMarkerSymbol simple_marker_symbol = new SimpleMarkerSymbol();
 simple_marker_symbol.Color = new SolidColorBrush(Colors.Red);

 for (int i = 0; i < x_list.Count; i++)
 {
  Graphic graphic = new Graphic()
  {
   Symbol = simple_marker_symbol,
   Geometry = new MapPoint(x_list, y_list, spatial_reference)
  };

  for (int j = 0; j < fields.Count; j++)
  {
   graphic.Attributes[fields] = other_fields_values;
  }

  graphics_layer.Graphics.Add(graphic);
 }

 return graphics_layer;
}


The code above create the graphics layer and when I put it into Map with the following lines of code, I can see the created graphics but lack of spatial reference.

GraphicsLayer g_layer = CreatePointGraphicLayer(....) //I omit this part
Map.Layers.Add(g_layer); 


How can I use spatial reference in graphics layer.

Thanks in advance.
Kerem.
0 Kudos
7 Replies
DominiqueBroux
Esri Frequent Contributor
You are right, we cant' set the spatial reference of a graphicslayer.

If your graphics layer is the only layer in the map, set the spatial reference of the map instead (by setting the extent of the map). If in your map there is a tiled layer, the spatial reference of the map will be defined by the spatial reference of the tiled layer.
0 Kudos
KevinKing
New Contributor
I'm having the same issue, and I have a tiled layer in the map, but the graphics layer is NOT getting it's spacial reference set...
0 Kudos
deleted-user-Ohz6rwd1kavx
New Contributor III
I also see this problem. I am using ESRI SL 3.0 API.

My GraphicsLayer has a null SpatialReference. My map has a spatial reference.

This is causing a problem for me because I am trying to use the PrintTask (for ArcGIS 10.1 printing). In this state, I am finding that a GraphicsLayer with a FeatureSet containing text graphics is NOT properly translated to web map json, and therefore does not print any of the text graphics.

However, if I hand edit the json and add a spatial reference to GraphicsLayer's featureSet:, then the text prints.

So, does anyone know how to set the SpatialReference on a GraphicsLayer?

Thanks

-Cory
0 Kudos
deleted-user-Ohz6rwd1kavx
New Contributor III
Answering my own question, but here goes....

....I found a solution which fixed my specific issue.

The spatial reference of my graphic's geometry was null.

I fixed my code to set to mySpatialReference before adding to graphicLayer.

This did NOT update the graphicLayer.SpatialReference. It is still null

[INDENT][/INDENT]... I suspect this _could_ be because graphicLayer.IsInitialized = false. Perhaps if one called Initialize and waits for the response, the graphicLayer.SpatialReference will be non null... I did not try this.

However, now when I send my map to the PrintTask, the featureSet's spatialReference is now set to mySpatialReference and I see points in the exported map.

So, the orig problem described in the first post is not solved, but the export problem I was seeing (which I thought was related) is solved.

Perhaps someone will find this information useful.

Thanks,

-Cory
0 Kudos
DominiqueBroux
Esri Frequent Contributor
So, the orig problem described in the first post is not solved, but the export problem I was seeing (which I thought was related) is solved.

A graphicslayer has no spatial reference associated to it. The spatial reference is defined by graphic (and can be different by graphic).
0 Kudos
MarkHiscocks
New Contributor

If that is the case, then the GraphicsLayer definition should not include a SpatialReference property at all. I am encountering a similar problem to this where adding points - which their SR set correctly - to a GraphicLayer is hanging the application. The map is fully loaded with it's SR set and the layers initialized, yet the problem persists.

The workaround for my issue is to set the GraphicsLayer.SpatialReference via reflection (since it's private), and the issue is solved.

0 Kudos
ScottKutz
New Contributor II

Mark,

Just to note ... the information in the following thread provided insights that may be helpful:

https://community.esri.com/thread/117206

Following that example, the fragment below shows a class derived from GraphicsLayer that overrides the OnInitializeGraphicsLayerRequestedAsync() method.

---------------------

public class MyGraphicsLayer : GraphicsLayer
{
  private SpatialReference _layerSpatialReference = null;

  public MyGraphicsLayer(SpatialReference layerSpatialReference)
  {
    // Capture the specified spatial reference so it is available
    // for use in the OnInitializeGraphicsLayerRequestedAsync() override.
    _layerSpatialReference = layerSpatialReference;
  }

  protected override Task<GraphicsLayerInitializationInfo> OnInitializeGraphicsLayerRequestedAsync()
  {
    return Task.FromResult(
      new GraphicsLayerInitializationInfo() { PreferredSpatialReference = _layerSpatialReference }
      );
  }
}

-------------------

The fragment below demonstrates using the derived class (plus .InitializeAsynch) to assign the Spatial Reference:

--------------------

// Source of the graphic data is a FeatureSet loaded from the content of a *.json file.
// The returned FeatureSet has its SpatialReference property set.

// The intent is to assign that SpatialReference to the graphics layer prior to populating the layer.

string jsonFileContent = (... previously read from a *.json file ...)
FeatureSet featureSet = FeatureSet.FromJson(jsonFileContent);

MyGraphicsLayer graphicsLayer = new MyGraphicsLayer(featureSet.SpatialReference);

// Execution of the .InitializeAsync() call does transfer control into

// the OnInitializeGraphicsLayerRequestedAsync() override above.

// The effect is to assign the spatial reference to the graphics layer.

await graphicsLayer.InitializeAsync();

---------------------

  While the above approach seems to achieve the objective, it does require more code than one might expect (at least, in my opinion, it seems there could be a more straightforward technique for assigning a Spatial Reference to a graphics layer).

Scott

0 Kudos