Right now the only way I see to create a GridFeatureLayer is by using a file or the default constructor. Is there a way to use a memory stream instead of requiring a file?
GridFeatureLayer using Memory Stream
Hi Cheryle,
In fact we read file is also read it to a stream, so please let me know whether this code work for you:
GridFeatureLayer layer = new GridFeatureLayer();
layer.StreamLoading += new EventHandler<StreamLoadingEventArgs>(layer_StreamLoading);
void layer_StreamLoading(object sender, StreamLoadingEventArgs e)
{
e.AlternateStream = "Your Stream here"
}
Regards,
Don
Thanks Don.
I’ve tried your suggestion. I have 2 questions:
1) We have this block of code and when outputStream is returned it is already disposed. Does GridFreatureSource.GenerateGrid not work with MemoryStream? We can replace this code with FileStream and it works just fine, but we would like to use MemoryStream.
MemoryStream outputStream = new MemoryStream();
GridFeatureSource.GenerateGrid(gridDefinition, interpolationModel, outputStream);
return outputStream;
2) When does the StreamLoading event get triggered?
Thank you for your assistance,
Cheryle
Hi Cheryle,
1. We will read the memory by StreamReader and call reader.ReadLine to get data, so I think you should make sure your MemoryStream support this operation. As below is my simple test code and it looks works.
private void DisplayMap_Load(object sender, EventArgs e)
{
GridFeatureLayer layer = new GridFeatureLayer("ReadFromMemory");
layer.StreamLoading += new EventHandler<StreamLoadingEventArgs>(layer_StreamLoading);
LayerOverlay layerOverlay = new LayerOverlay();
layerOverlay.Layers.Add(layer);
winformsMap1.Overlays.Add(layerOverlay);
winformsMap1.CurrentExtent = new RectangleShape(-139.2, 92.4, 120.9, -93.2);
winformsMap1.Refresh();
}
void layer_StreamLoading(object sender, StreamLoadingEventArgs e)
{
string filePath = @"D:\1.txt";
byte[] data = File.ReadAllBytes(filePath);
MemoryStream ms = new MemoryStream(data);
e.AlternateStream = ms;
}
2. When the layer called to draw, this event will be triggered.
Regards,
Don