Overview of data formats

MapWinGIS can access a variety of vector and raster formats and spatial databases.

1. Some of the formats are supported with native drivers:
  • ESRI shapefiles;
  • BMP images;
  • certain binary grids.
2. For others GDAL drivers are used. Check the list of: If particular format is in this list there is a good chance that it can be opened with MapWinGIS. However in some cases the drivers are provided in the form of GDAL plugins, which may or may not be included in MapWinGIS build. To be sure you can try to open the formats in question with Demo application included in MapWinGIS installer. Another option is to check FileManager.SupportedGdalFormats property.

MapWinGIS classes to use

Datasources can be opened by the following classes:
  • Shapefile - for ESRI shapefiles;
  • OgrLayer - for any vector formats and spatial databases supported by OGR / GDAL drivers;
  • OgrDatasource - for any multi-layered formats supported by OGR / GDAL drivers;
  • Image - for RGB images and for direct rendering of grids (native BMP driver, GDAL drivers;
  • Grid - to access and edit grid data (native drivers for plain binary grids, GDAL drivers for others).
Each of these classes has an Open method (OpenFromFile, OpenFromDatasbase for OgrLayer). Instances of these classes can be added to the map by calling Map.AddLayer method.

However only Shapefile and Image classes are used for rendering.
  • OgrLayer class holds its data in the instance of Shapefile class which can be accessed with OgrLayer.GetBuffer method.
  • OgrDatasource is used to access all the layers in multi-layered datasources. After adding to the map all the layers it has are added as separate objects.
  • Grid class is used to access and edit raw data but not for rendering. When adding to the map Grid.OpenAsImage method is called to get image representation of grid.

Opening and adding datasource to the map

Here are samples which demonstrate how to open datasources and add them to the map.
1. Shapefiles:
string filename = @"c:\my_data.shp";
var sf = new Shapefile();
if (sf.Open(filename, null)) {
    int layerHandle = axMap1.AddLayer(sf, true);
}
else {
    Debug.WriteLine("Failed to open shapefile: " + sf.get_ErrorMsg(sf.LastErrorCode));
}
2. Other vector formats:
string filename = @"c:\my_data.tab";  // presumably MapInfo tab
var layer = new OgrLayer();
if (layer.OpenFromFile(filename, false))
{
    int layerHandle = axMap1.AddLayer(layer, true);
}
else
{
    Debug.WriteLine("Failed to open: " + layer.get_ErrorMsg(layer.LastErrorCode));
}
3. Multi-layered vector formats:
string filename = @"c:\my_data.kml";
var ds = new OgrDatasource();
if (ds.Open(filename))
{
    // all layers within datasource will be added to map as separate OgrLayer objects
    // only layer handle for the first layer being added is returned
    int layerHandle = axMap1.AddLayer(ds, true);
}
else
{
    Debug.WriteLine("Failed to open datasource: " + ds.get_ErrorMsg(ds.LastErrorCode));
}
4. Spatial databases:
string connectionString = "PG:host=localhost dbname=london user=postgres password=1234";
var layer = new OgrLayer();
if (layer.OpenFromDatabase(connectionString, "buildings"))
{
    // only layer handle for the first layer being added is returned
    int layerHandle = axMap1.AddLayer(layer, true);
}
else
{
    Debug.WriteLine("Failed to open: " + layer.get_ErrorMsg(layer.LastErrorCode));
}
5. RGB images:
var img = new Image();
if (img.Open(filename, null)) {
    int layerHandle = axMap1.AddLayer(img, true);
}
else {
    Debug.WriteLine("Failed to open image: " + img.get_ErrorMsg(img.LastErrorCode));
}
6. Grids:
string filename = @"c:\my_data.asc";
var grid = new Grid();
if (grid.Open(filename))
{
    var colorScheme = grid.RetrieveOrGenerateColorScheme();
    var img = grid.OpenAsImage(colorScheme, tkGridProxyMode.gpmAuto);
    if (img != null)
    {
        // grid object can be update to the map directly as well
        // grid.OpenAsImage will be called internally all the same
        int layerHandle = axMap1.AddLayer(img, true);
    }
}
else
{
    MessageBox.Show("Failed to open: " + grid.get_ErrorMsg(grid.LastErrorCode));
}

Quick way to add datasources to the map

In version 4.9.1 a simplified method to open file-based datasources was added: Map.AddLayerFromFilename. The method will take the burden of choosing of appropriate COM class to do the job:

string filename = "c:\\some_filename.ext";
var strategy = tkFileOpenStrategy.fosAutoDetect;
int layerHandle = axMap1.AddLayerFromFilename(filename, strategy, true);

For spatial databases an equivalent shortcut is Map.AddLayerFromDatabase:

string connectionString= "PG:host=localhost dbname=london user=postgres password=1234";
string sqlOrLayerName = "SELECT * FROM Buildings WHERE gid < 50";    
int handle = map.AddLayerFromDatabase(connectionString, sqlOrLayerName, true);

In both cases to verify that everything has gone well check the layer handle returned by the methods:

if (layerHandle == -1) {
     var fm  = axMap1.FileManager;
     Debug.WriteLine("Failed to open datasource: " + fm.get_ErrorMsg(fm.LastErrorCode);
 }

Accessing the data added to the map

1. To access vector data:
  • Use Map.get_Shapefile property to access the data. It will work both for actual shapefiles and for other vector formats since Shapefile class is still used for them to hold the data.
var sf = axMap1.get_Shapefile(layerHandle);
if (sf != null) {
    // do something with shapefile
}
  • For OGR layers (formats other than ESRI shapefile) Map.get_OgrLayer can be used to access the layer. To access the geometry data OgrLayer.GetBuffer method is used.
var ogr = axMap1.get_OgrLayer(layerHandle);
if (ogr != null) {
    var sf = ogr.GetBuffer();    // represented by instance of Shapefile class
    // do something with the buffer
}

When you call Map.getShapefile(layerHandle) on OgrLayer, MapWinGIS internally calls Map.getOgrLayer first and then OgrLayer.GetBuffer in the same way as demonstrated in the previous code snippet.
2. To access raster data:
  • Use Map.get_Image property to access any raster datasource added to the map (both for RGB images and grids):
var img = axMap1.get_Image(layerHandle);
if (img != null) {
    // do something with the image
}
  • Having Image object the underlying grid datasource can be accessed like this:
string gridFilename = img.SourceGridName;
if (!string.IsNullOrWhiteSpace(gridFilename))
{
     var grid = new Grid();
     if (grid.Open(gridFilename)) {
          // do something with grid
     }
}
3. To access all the layers of particular type added to the map:
// accessing all vector layers added to the map
for (int i = 0; i < axMap1.NumLayers; i++)
{
    int layerHandle = axMap1.get_LayerHandle(i);
    var sf = axMap1.get_Shapefile(layerHandle);
    if (sf != null)
    {
        // do something
    }
}

Last edited Jan 4, 2015 at 3:02 PM by sleschinski, version 34

Comments

costeakai Oct 27, 2016 at 5:51 AM 
never mind that, the case is solved

costeakai Oct 13, 2016 at 11:50 AM 
hi, thanks, its working for postgis;
now how do i set layers visibility for certain zooming levels ... please?