AddLayers.cs

This example demonstrates how to add all the shapefiles and images with .tif and .png extensions from the specified folder to the map. Here is a screenshot with the results of the code execution.

using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using AxMapWinGIS;
using MapWinGIS;
namespace Examples
{
public partial class MapExamples
{
// <summary>
// Adds all the shapefiles and images with .tif and .png extentions from the specified folder to the map
// </summary>
public bool AddLayers(AxMap axMap1, string dataPath)
{
axMap1.RemoveAllLayers();
axMap1.LockWindow(tkLockMode.lmLock);
try
{
string[] files = Directory.GetFiles(dataPath);
foreach (string file in files)
{
int layerHandle = -1;
if (file.ToLower().EndsWith(".shp"))
{
Shapefile sf = new Shapefile();
if (sf.Open(file, null))
{
layerHandle = axMap1.AddLayer(sf, true);
}
else
MessageBox.Show(sf.ErrorMsg[sf.LastErrorCode]);
}
else if (file.ToLower().EndsWith(".tif") ||
file.ToLower().EndsWith(".png"))
{
Image img = new Image();
if (img.Open(file, ImageType.TIFF_FILE, false, null))
{
layerHandle = axMap1.AddLayer(img, true);
}
else
MessageBox.Show(img.ErrorMsg[img.LastErrorCode]);
}
if (layerHandle != -1)
axMap1.set_LayerName(layerHandle, Path.GetFileName(file));
}
}
finally
{
axMap1.LockWindow(tkLockMode.lmUnlock);
Debug.Print("Layers added to the map: " + axMap1.NumLayers);
}
return axMap1.NumLayers > 0;
}
}
}
ImageType
The type of images supported by MapWinGIS.
Definition: Enumerations.cs:92
tkLockMode
The lock mode of the map, either locked or not.
Definition: Enumerations.cs:940
Map component for visualization of vector, raster or grid data.
Definition: AxMap.cs:56
Represents an raster image of particular format which may be added to the map.
Definition: Image.cs:66
Provides a functionality for accessing and editing ESRI shapefiles.
Definition: Shapefile.cs:72
int LastErrorCode
Gets the code of last error which took place inside this object.
Definition: Shapefile.cs:249
int LastErrorCode
Retrieves the last error generated in the object.
Definition: Image.cs:466
bool Open(string ImageFileName, ImageType fileType, bool InRam, ICallback cBack)
Opens an image from file.
Definition: Image.cs:357
void LockWindow(tkLockMode lockMode)
Locks the window so that any changes will not be displayed until it is unlocked.
Definition: AxMap.cs:524
int NumLayers
Gets the number of layers loaded in the map.
Definition: AxMap.cs:1422
int AddLayer(object Object, bool visible)
Adds a layer to the map.
Definition: AxMap.cs:1342
void RemoveAllLayers()
Removes all layers from the map.
Definition: AxMap.cs:1429
void set_LayerName(int layerHandle, string newValue)
Sets the name of the specified layer.
Definition: AxMap.cs:1168
bool Open(string shapefileName, ICallback cBack)
Opens shapefile from the disk.
Definition: Shapefile.cs:1430