How to create a FeatureLayer based on a FeatureCollectionTable

181
5
Jump to solution
05-06-2019 02:28 PM
Highlighted
Occasional Contributor II

Hi,

the Runtime documentation describes five constructor, which could be used to create a FeatureTable. One of them creates a class based on a FeatureTable.

Normally, a FeatureTable is based on a service, such as the ServiceFeatureTable, and is usually stored in a database. But I need a FeatureLayer on the fly based on in memory data. 

So I thought in my case the FeatureCollectionTable would be the right way, because it is based on a FeatureTable and so I should be able to create a FeatureLayer from it.

The following code is compileable:

var fields = new List<Field> {new Field(FieldType.Text, "Field1", "Field1", 50)};
var table = new FeatureCollectionTable(fields, GeometryType.Point, SpatialReferences.WebMercator);
var featureLayer = new FeatureLayer(table);
MyMapView.Map.OperationalLayers.Add(featureLayer);

But when executing this code, I got an exception:

System.ArgumentException: 'Invalid argument: Cannot create a feature layer from a feature collection table. Add the feature collection table to a feature collection.'

Now I'm a bit confused: Why can't I do that, a FeatureCollectionTable inherits from the FeatureTable?
Is that a bug? Is this work as designed?

A FeatureCollection doesn't get me where I want to be: Among other things, I want to be able to restrict data by DefinitionExpression, but this is only possible with the FeatureLayer.

Kind regards,

Max

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Esri Regular Contributor

You should use a Feature Collection Layer: FeatureCollectionLayer Class 

The layers property on it gives access to individual FeatureLayers.

A Feature Collection can include multiple tables, so when you instantiate a Feature Collection Layer, you do so using a Feature Collection, which is instantiated using the FeatureCollectionTable you created.

See also this sample.

View solution in original post

5 Replies
Highlighted
Esri Regular Contributor

You should use a Feature Collection Layer: FeatureCollectionLayer Class 

The layers property on it gives access to individual FeatureLayers.

A Feature Collection can include multiple tables, so when you instantiate a Feature Collection Layer, you do so using a Feature Collection, which is instantiated using the FeatureCollectionTable you created.

See also this sample.

View solution in original post

Highlighted
Occasional Contributor

Is adding just one of the sublayers of the FeatureCollectionLayer supported?

I changed the Create s New Feature Collection sample to add just the first sublayer and get an exception:

Reply
0 Kudos
Highlighted
Esri Regular Contributor

Don't add the sublayers to the map's operational layers. They're already "owned" by the FeatureCollectionLayer (hence the error you're seeing - the map can't take "ownership" as they already belong to something else). Instead, just add the FeatureCollectionLayer itself. In your case, it will only have one sublayer and you'll get the result you want.

In the case of the sample, simply don't add the other tables to the FeatureCollection.

Reply
0 Kudos
Highlighted
Occasional Contributor II

Thanks Nicholas. I saw the example and I tried it, but I did not get the Property Layers, which contains a collection of FeatureLayers in it, which allow to do what I want to do.

But could you still explain why you can't create a FeatureLayer directly from a FeatureCollectionTable, but it's syntactically allowed by the API? I find that very misleading.

Reply
0 Kudos
Highlighted
Esri Regular Contributor

I wasn't involved in the design phase, but I suspect that this is down to inheriting from FeatureTable (I'm not sure if the .NET API has OMDs so here's the one from the iOS documentation😞

I further suspect that to inherit the properties of a FeatureTable without allowing the type model to accept it into the FeatureLayer constructor would involve some convoluted hierarchy and protocol designs that might not even work well in some SDKs. The solution I can think of that would solve this through typing would really ugly up that diagram.

To be fair, the error you get when you do try isn't bad…

System.ArgumentException: 'Invalid argument: Cannot create a feature layer from a feature collection table. Add the feature collection table to a feature collection.'

…but I agree it could perhaps a little more usefully point you at the FeatureCollectionLayer as well the FeatureCollection. I'll see if we can get that updated.

Reply
0 Kudos