ThinkGeo.com    |     Documentation    |     Premium Support

How to convert a Layer to AsyncLayer

Hi,

we want to draw our map onto a bitmap.

In the past we’ve used this method:

private static void DrawLayer(Layer layer, GeoCanvas myCanvas, Collection<SimpleCandidate> labelsInAllLayers)
{
    layer. Open();
    layer. Draw(myCanvas, labelsInAllLayers);
    layer. Close();
}

Then we got the message

Please call DrawAsync() instead of Draw().

So we’ve changed out method to this:

private static async Task DrawLayerAsync(Layer layer, GeoCanvas myCanvas, Collection<SimpleCandidate> labelsInAllLayers)
{
    LayerAsync layerAsync = (LayerAsync)layer;
    await layerAsync.OpenAsync();
    await layerAsync.DrawAsync(myCanvas, labelsInAllLayers);
    await layerAsync.CloseAsync();
}

But now you’ve deprecated LayerAsync and say

Please use ThinkGeo.Core.AsyncLayer instead.

But how can we convert a Layer from a map to an AsyncLayer?
The old cast does not work anymore.

Thank you.

Regards,
Peter

Hi Peter,

Sorry for the confusing.

I don’t know which layer you are working with, but just switch to the corresponding async layer should be fine. For example if you are using ThinkGeo.Core.Async.OpenStreetMapLayer, just use ThinkGeo.Core.OpenStreetMapAsyncLayer instead would work. This new class gives you the same OpenAsync() DrawAsync methods.

Similarly, if you have a custom class inheriting from ThinkGeo.Copre.Async.LayerAsync, just inherit it from ThinkGeo.Core.AsyncLayer would be fine.

I’m wondering which class are you using? I think we’ve included the recommended async class name in the obsolete message, maybe some of the classes are missing?

By the way in case you are wondering, some layers (such as OpenStreetMapAsyncLayer) are Async because they call .NET Async methods to fetch data, while those non async layers (such as ShapeFileFeatureLayer) don’t.

Thanks,
Ben

HI Ben,
essentially we are just going through the Layer collection of an overlay to draw these layers onto an image. The Layers collection gives me access to LayerBase objects which I then draw one after the other onto the image. AsyncLayer is derived from LayerBase but not all derivatives of LayerBase are derived from AsyncLayer, so I cannot just cast it.

And yes with WmsAsyncLayer there is an async alternative to WmsLayer. But I cannot find one for e.g. InMemoryFeatureLayer or PostgreSqlFeatureLayer.

Does that mean, that there are now some layers with async support derived from AsyncLayer and some without derived from LayerBase as usual?

Thank you for your support.

Regards,
Peter

Hi Peter,

That’s right. Some layers (such as OpenStreetMapAsyncLayer) inherit from AsyncLayer and some other Layers (such as InMemoryFeatureLayer or PostgreSqlFeatureLayer) inherit from Layer, LayerAsync has methods OpenAsync() and DrawAsync() while Layer has methods Open() and Draw(). Both of AsyncLayer and Layer inherit from LayerBase.

So for a collection of LayerBase, you can loop it through and if it’s a Layer, call Layer.Draw(); if it’s an AsyncLayer, do AsyncLayer.DrawAsync().

Thanks,
Ben