The Map Loader

The package format provides the class IlvMapLoader that you can use to load in a very simple way any file format for which Views provides a predefined reader (Shapefile, DTED, CADRG, and so on). These predefined readers are described in the Predefined Readers.

This section covers the following topics:

Loading a Predefined Map Format

To load a predefined map format, use the following method:

load(const char* fileName, IlvMapsError& status)

This method first tries to determine the file format according to the naming rules set forth in the specifications of the different formats, and initializes the appropriate reader. The method then loads the map into the manager associated with the map loader.

The following example shows how to import a map file into a Views manager using the map loader. This file can be either a Shapefile file, a CADRG file, or a DTED file:

IlvMapLoader* loader = new IlvMapLoader(&manager);

IlvMapsError status = IlvMaps::NoError();

loader->load(filename, status);

if(status != IlvMaps::NoError())

IlvWarning(IlvMaps::GetErrorMessage(status, display));

Loading Nongeoreferenced Files

When you load a map into a Views manager using the map loader, this map is automatically displayed in the projection system associated with the manager provided that the format of the source data is georeferenced. The IlvMapFeatureIterator abstract class has a getProjection method that you can use to know whether a file is georeferenced. If the getProjection method returns 0 or IlvUnknownProjection, the file is not georeferenced. Otherwise, the file is georeferenced. See the section Feature Iterators for more information.

Most of the cartographic files are georeferenced. This is the case for files of the DTED and CADRG formats. Some other cartographic formats, such as Shapefile, are not georeferenced.

When loading data from a file that is not georeferenced, and in the absence of any other indications, the map loader is unable to reproject the source data within the target projection system (the one associated with the manager). See the section Selecting a Target Projection.

Note

If you load several source files of the Shapefile format whose projection is unknown in the same manager, objects can be positioned correctly if the data is expressed in the same coordinate system. However, if you try to import data of another format in the manager, the relative position of objects from different source formats will be inaccurate.

If you read a file whose format is not georeferenced, but you know the projection system in which the data is expressed, you can provide this information to the IlvMapLoader using the setDefaultSourceProjection method.

The following example shows how to import a Shapefile whose projection is known to be geographic into a manager that is in a Mercator projection:

// Initialize the manager for the mercator projection.

IlvProjection* projection = new IlvMercatorProjection();

IlvMapInfo* mapinfo = new IlvMapInfo(projection, 0, IlvFalse);

mapinfo->attach(manager);

// Create a map loader.

IlvMapLoader mapLoader = new IlvMapLoader(manager);

// Load other data.

....

// Load a shape file that is in the geographic projection.

IlvProjection* geographic = new IlvGeographicProjection();

mapLoader->setDefaultSourceProjection(geographic);

mapLoader->load("myShapeFile.shp");

Specifying a Renderer

If you want an IlvMapLoader object to use a specific renderer, specify it as the second argument of its load(IlvMapFeatureIterator* featureIterator, IlvFeatureRenderer* renderer, IlvMapsError& status) method, as shown below:

IlvMapLoader* loader = new IlvMapLoader(&manager);

IlvMapsError status = IlvMaps::NoError();

IlvMapFeatureIterator* iterator =

loader->makeFeatureIterator(filename);

IlvDefaultCurveRenderer* renderer =

new IlvDefaultCurveRenderer(display);

IlvMapLineRenderingStyle* style =

new IlvMapLineRenderingStyle(display);

style->setForeground(display->getColor("green"));

renderer->setLineRenderingStyle(style);

loader->load(iterator, renderer, status);

if(status != IlvMaps::NoError())

IlvWarning(IlvMaps::GetErrorMessage(status, display));

Extending the IlvMapLoader Class

This section shows how to subtype the IlvMapLoader class so that it can recognize a file format other than the Views Maps predefined formats.

The method makeFeatureIterator method creates the reader that recognizes the format of the file specified as its parameter. We are going to derive the IlvMapLoader class and override this method so that it can recognize the format of the polyline file presented in the section Writing a New Readerand initialize the appropriate reader. We assume that the file has the .pol extension.

#include <strings.h>

#include <ilviews/maps/format/maploader.h>

#include "polread.h"

class MyMapLoader

:public IlvMapLoader

{

public:

MyMapLoader(IlvDisplay* display,

IlvManager* manager);

/**

* Overrides the makeFeatureIterator method from super class.

*/

virtual IlvMapFeatureIterator* makeFeatureIterator(const char* fileName);

private:

IlvDisplay* _display;

};

MyMapLoader::MyMapLoader(IlvDisplay* display,

IlvManager* manager)

:IlvMapLoader(manager),

_display(display)

{

}

IlvMapFeatureIterator*

MyMapLoader::makeFeatureIterator(const char* fileName)

{

// Does superclass know the format of provided file?

IlvMapFeatureIterator* result =

IlvMapLoader::makeFeatureIterator(fileName);

// If not, try with the polygon reader.

if (!result) {

// test extension

int length = strlen(fileName);

// .pol are polylines files.

if (length > 4) {

char* ptr = strrchr(fileName, ’.’);

if(ptr)

if(strcasecmp(ptr, ".pol") == 0)

return new SimplePolylineReader(_display, fileName);

}

}

return result;

}

The makeFeatureIterator method first attempts to get an IlvMapFeatureIterator from its superclass. If the file is not recognized, it tries to determine whether the file extension provided (in this example, .pol) corresponds to that of the file to be read. If the result of the test is successful, it creates the appropriate reader, which in this case is the reader created in the section Writing a New Reader.

If the file does not contain a header, the method returns the null pointer to indicate that it was not able to identify the file format.