1

I want to add a bunch (several hundreds) of shapefiles (all feature layers) to a GroupLayer.

When I add them file by file this takes too long:

IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(dir, 0) as IFeatureWorkspace;
IFeatureLayer pFeatureLayer = new FeatureLayer();
pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(filename);
pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
groupLayer.Add(pFeatureLayer);

I found IMapLayers.InsertLayersInGroup which takes an IEnumLayer but I can't find a way to load the shapefiles as an IEnumLayer. So I can't even say, if this would be faster.

Does anyone have a clue?

Update I tried Kirk's hint with IName like this (totally ignoring possible null values):

IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(dir, 0) as IFeatureWorkspace;

IFeatureLayer pFeatureLayer = new FeatureLayer();
IWorkspace w = pFeatureWorkspace as IWorkspace;
var names = w.get_DatasetNames(esriDatasetType.esriDTFeatureClass);
names.Reset();
IDataLayer pDataLayer = pFeatureLayer as IDataLayer;
pDataLayer.DataSourceName = names.Next() as IName;                   
groupLayer.Add(pFeatureLayer);

It takes more than double as long as my first shot. So I think I'll stick with that one.

1
3

I suspect you are being unrealistic, adding several hundred layers to a map document or even grouplayer will make the map document unwieldy and almost certainly unstable. If you think about it on each insertion it needs to place it in the TOC, symbolize the data and if left on refresh the map display.

IEnumLayer to my knowledge only comes from IMap so your layers (that's the hint) need to be already loaded. You could get a handle on a IEnumDataSet from IWorkspace.Datasets Property and add the datasets to the map that way. But adding hundreds will always be a slow process.

Consider turning off display while loading? You can use the IMxApplication2.PauseDrawing Property for that.

2
  • The layers are not very big. Most of them only have a couple of features. Unfortunatly IMxApplication2.PauseDrawing isn't available in ArcEngine. I'll have a look at IEnumDataSet.
    – gumo
    Sep 9 '16 at 7:59
  • If speed is a priority and you really do have so few features per dataset, why not preprocess the data and merge them with a field Identifying which dataset they came from? Just an idea.
    – Hornbydd
    Sep 9 '16 at 8:22
1

I've never tried this, but it might be worth a try.

Opening featureclasses is what takes a long time. In general, it's much faster to deal with IName objects instead of featureclasses. So perhaps IDataLayer could be used. Instead of setting IFeatureLayer.Featureclass, try setting IDataLayer.DataSourceName to a FeatureClassName cast as an IName.

If the featurelayer doesn't try to open it when this is set, it should be faster.

1
  • I tried it (see my update) but it is slower.
    – gumo
    Sep 9 '16 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.