Giving a REST URL, how can one gets the Geometry type

794
8
07-10-2012 05:13 AM
MiriEshel
New Contributor III
Hi,

I'm looking for a way to get the Geometry Type of a layer, giving its URL
(e.g. http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Average_Household_Size/MapSer...
returns esriGeometryPolygon in REST but how can I get it programmatically?

Thanks a lot,
Miri
0 Kudos
8 Replies
JoeHershman
MVP Regular Contributor
Hi,

I'm looking for a way to get the Geometry Type of a layer, giving its URL
(e.g. http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Average_Household_Size/MapSer...
returns esriGeometryPolygon in REST but how can I get it programmatically?

Thanks a lot,
Miri


AFAIK the only way would be to actually make the rest call and find it from the returned json object using WebClient.


        private void GetLayerDescription()
        {
            var webClient = new WebClient();


            webClient.OpenReadCompleted += DownloadComplete; 
            webClient.OpenReadAsync(new Uri("http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Average_Household_Size/MapServer/1?f=json"));
        }


        private string _geometryType;
        private void DownloadComplete(object sender, OpenReadCompletedEventArgs e)
        {
            if ( e.Error != null ) return;


            var jsonObject = JsonValue.Load(e.Result) as JsonObject;
            if ( jsonObject == null ) return;


            JsonValue geometryTypeJsonValue;


            if ( jsonObject.TryGetValue("geometryType", out geometryTypeJsonValue) )
            {
                _geometryType = geometryTypeJsonValue;
            }
        }



(JsonObject is in the System.Json assembly)

Good Luck
-Joe
Thanks,
-Joe
0 Kudos
MiriEshel
New Contributor III
Hi Joe,

It works great. I wonder why Esri doesn't give a method/propery for this purpose but never mind.

As always, thanks a lot...

Miri
0 Kudos
PreetiMaske
Esri Contributor
The API does have a property called GeometryType on LayerInfo. You don't need a webclient request to get this property.

Basically when the Featurelayer is initialized you can get it like this:

  private void FeatureLayer_Initialized(object sender, System.EventArgs e)
    {
      if ((sender as FeatureLayer).LayerInfo.GeometryType == GeometryType.Polygon)
      {
        // do  something
      }
    }
0 Kudos
MiriEshel
New Contributor III
Hi Preeti,

I did not go through featureLayer_initialized event. What I tried to do in the code is:

FeatureLayer fl = new FeatureLayer();
fl.Url = "http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Average_Household_Size/MapSer...";

but then:

fl.LayerInfo.GeometryType returns null

So now I understand, that if I would have used the method:
fl.initialized

It could work? I will try it.

Thanks,
Miri
0 Kudos
PreetiMaske
Esri Contributor
Yes until layer is intialzed you can't get its info. So code will look like :
public Test()
        {
            InitializeComponent();
            FeatureLayer fl = new FeatureLayer();
            fl.Url ="http://services.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Average_Household_Size/MapSer...";
            fl.Initialized += fl_Initialized;
            fl.Initialize();
        }

        void fl_Initialized(object sender, System.EventArgs e)
        {
          // you can get layerinfo here
        }
0 Kudos
MiriEshel
New Contributor III
Hi Preeti,

I tried it and it works. My apology....

Now I wonder which way is better: Joe's suggestion or yours.
For your suggestion, I have to initializes the featureLayer each  time I click on a layer name in the legend.
I don't know what will be more economical in resources.

Thanks a lot,
Miri
0 Kudos
JoeHershman
MVP Regular Contributor
I would say pretty much 6 of one half dozen of the other from a performance standpoint.  It may pretty much be the exact same rest call and then they hydrate the objects afterwards   Really a very large portion of the entire API is wrapper for rest calls like what I showed http://servicesbeta2.esri.com/arcgis/sdk/rest/index.html.  For anything that has a response method if you really wanted to you could build your own rest call and use the WebClient.  Obviously, using the API is a much simpler approach.
Thanks,
-Joe
0 Kudos
MiriEshel
New Contributor III
Joe,

Thank you. You are fast!

Miri
0 Kudos