The package ilog.views.maps.format provides the class IlvMapLoader that you can use to load in a
very simple way any file format for which JViews Maps provides a
predefined reader (Shapefile, DTED®
, and MID/MIF). These predefined readers are described at length in
Readers and writers.
Loading a predefined map format
To load a predefined map format, use the load method. 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
.shp
file (Shapefile format) into a JViews Maps manager using
the map loader:
IlvMapLoader loader = new IlvMapLoader(manager); try { loader.load("myShapeFile.shp"); } catch (IlvMapFormatException e) { // Occurs if there is a format error in the file. e.printStackTrace(); } catch (IOException e) { // Occurs if there is another IO error. e.printStackTrace(); }
A complete example of how to import a file using the map loader
and save it in the
.ilv
format can be found in the following file:
Loading nongeoreferenced files
When you load a map into a JViews
Maps manager using the map loader, this map is automatically
displayed in the coordinate system associated with the manager
provided that the format of the source data is georeferenced. The
IlvMapFeatureIterator interface has an isGeoreferenced method that you can use to
know whether a file is georeferenced. Most of the cartographic
files are georeferenced. This is the case for files of the DTED format. 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 coordinate system (the one associated with the manager).
Note
If you load several source files of the
Shapefile format whose projection is unknown in the same
manager, objects are positioned correctly. However, if you try
to import data of another format in the manager, the relative
position of objects from different source formats are
inaccurate.
If you read a file whose format is not georeferenced, but you
know the coordinate system in which the data is expressed, you
can provide this information to the IlvMapLoader using the setDefaultCoordinateSystem 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 projected coordinate system:
// Initialize the manager for the mercator projection. IlvProjection p = new IlvMercatorProjection(); IlvProjectedCoordinateSystem pcs = new IlvProjectedCoordinateSystem("mercator", p); IlvCoordinateSystemProperty csProperty = new IlvCoordinateSystemProperty(pcs); manager.setNamedProperty(csProperty); // Create a map loader. IlvMapLoader mapLoader = new IlvMapLoader(manager); // Load other data. .... // Load a Shapefile expressed in geographic coordinate system. mapLoader.setDefaultCoordinateSystem(IlvGeographicCoordinateSystem.WGS84); 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 method, as shown below:
IlvFeatureRenderer renderer = new IlvDefaultCurveRenderer(); mapLoader.load(myIterator, renderer); IlvMapLoader mapLoader = new IlvMapLoader(manager);
The file <installdir> /jviews-maps810/codefragments/renderer/src/Viewer.java
shows how to use specific renderers with a map loader reading
data that has the Shapefile format. For specific renderers,
see the examples provided in Creating a colored line renderer and Extending an existing renderer.
Executing this file loads the following
two files into the viewer:
- The
<installdir>/doc/usermansrc/maps/renderer/HYLINE.SHP
file defines contour lines for the region of Manila (Philippines). TheHYLNVAL
attribute holds the elevation associated with each contour line. Colors are applied to contour lines using the default elevation color model supplied with JViews Maps. SeeIlvIntervalColorModel.MakeElevationColorModel
. - The
<installdir>/doc/usermansrc/maps/renderer/PPPOINT.SHP
file defines points representing populated areas in the region of Manila (Philippines). ThePPPTNAME
attribute holds the name of the town, if it is known. In this case, this name appears next to theIlvMarker
object that represents the town.
Attaching attributes to graphic objects
The map loader can automatically attach attributes to the graphic
objects that it creates when it loads a map. For this, use the setAttachingAttributes method, as shown in
the following example:
IlvMapLoader loader = new IlvMapLoader(manager); loader.setAttachingAttributes(true); try { loader.load("myShapeFile.shp"); } catch (IlvMapFormatException e) { // Occurs if there is a format error in the file. e.printStackTrace(); } catch (IOException e) { // Occurs if there is an other IO error. e.printStackTrace(); }
Extending the IlvMapLoader class
This section shows how to subtype the IlvMapLoader class so that it can recognize
a file format other than the JViews
Maps predefined formats.
The method makeFeatureIterator of this class creates
the reader that recognizes the format of the file specified as
its parameter. In the following example, the IlvMapLoader class is derived and the
method overridden so that it can recognize the format of the
polyline file presented in the section Developing a new reader and initialize the
appropriate reader. It is assumed that the file has the
.pol
extension.
public class MyMapLoader extends IlvMapLoader { /** * Constructor. */ public MyMapLoader(IlvManager manager) { super(manager); } /** * Overrides the makeFeatureIterator method from super class. */ public IlvMapFeatureIterator makeFeatureIterator(String fileName) throws IOException { // Does superclass know the format of provided file? IlvMapFeatureIterator result = super.makeFeatureIterator(fileName); // If not, try with the polygon reader. if (result == null) { // Test extension. int length = fileName.length(); // .pol are polylines files. if (length > 4) { String suffix = fileName.substring(length - 4); if (suffix.toLowerCase().equals(".pol")) { try { return new OptimizedPolylineReader(fileName); } catch (IlvMapFormatException e) { return null; } } } } 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
true
, it creates the appropriate reader, which in this case is the
optimized reader created in Optimizing the reader.
If the file does not contain a header, an
IlvMapFormatException
is thrown and the method returns the null pointer to indicate
that it was not able to identify the file format.
The complete source code for this
customized map loader can be found in the following file: