Creating a Simple Map Application
The application example given in this section is the
JViews Maps application provided in
<installdir> /jviews-maps/samples/buildmap/index.html as part of the Rogue Wave®
JViews Maps demonstration software.
The application:
Creates a map GUI.
Loads a shape file, an image file (a raster), and two data sources.
To create the application:
1. Import the following Java™ packages and classes:
import ilog.views.*;
import ilog.views.maps.*;
import ilog.views.maps.beans.*;
import ilog.views.maps.datasource.*;
import ilog.views.maps.format.image.IlvRasterBasicImageReader;
import ilog.views.maps.graphic.style.IlvPolylineStyle;
import ilog.views.maps.raster.datasource.IlvRasterDataSourceFactory;
import ilog.views.maps.srs.coordsys.IlvCoordinateSystem;
import ilog.views.maps.srs.coordsys.IlvGeographicCoordinateSystem;
import ilog.views.swing.IlvJManagerViewControlBar;
import ilog.views.swing.IlvJScrollManagerView;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
2. Declare the public class BuildMapDemo, which defines the basic building blocks of the Map Builder:
public class BuildMapDemo extends JFrame {
IlvManagerView view;// Map View
IlvJScrollManagerView viewScroll;// scroll around the Map View
IlvJManagerViewControlBar viewToolbar; // zoom & selection toolbar
IlvLayerTreePanel layerTreePanel = new IlvLayerTreePanel(); // layer
visibility and properties control
IlvJCoordinateSystemEditorPanel csPanel;// coordinate system choice.
IlvJMouseCoordinateViewer locator;// coordinate information
3. Create a main frame and set it to exit when the Close button is pressed:
public BuildMapDemo() {
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
4. Create the main components of this example and set them up:
view=new IlvManagerView();
view.setManager(new IlvGrapher());
viewScroll=new IlvJScrollManagerView(view);
viewToolbar = new IlvJManagerViewControlBar();
csPanel=new IlvJCoordinateSystemEditorPanel();
viewToolbar.setView(view);
locator=new IlvJMouseCoordinateViewer();
locator.setView(view);
viewToolbar.add(locator);
view.setKeepingAspectRatio(true);
view.setBackground(new Color(80, 180, 240));
view.setAntialiasing(false);
view.setSize(new Dimension(900, 450));
viewScroll.setPreferredSize(view.getSize());
layerTreePanel.setView(view);
5. Georeference the view:
view.getManager().setNamedProperty(new
IlvCoordinateSystemProperty(IlvGeographicCoordinateSystem.WGS84));
6. Set up the Coordinate System panel to change projection according to panel selection and to reload the map data:
csPanel.setCoordinateSystem(IlvCoordinateSystemProperty.GetCoordinateSystem
(view.getManager()));
csPanel.addCoordinateSystemChangeListener(new PropertyChangeListener(){
public void propertyChange(PropertyChangeEvent evt) {
IlvCoordinateSystem system = (IlvCoordinateSystem) evt.getNewValue();
view.getManager().setNamedProperty(new IlvCoordinateSystemProperty(system));
clearManager();
loadData();
}
});
7. Build the map and configure the main window:
loadData();
getContentPane().add(viewToolbar,BorderLayout.NORTH);
getContentPane().add(viewScroll,BorderLayout.CENTER);
JTabbedPane tabPane=new JTabbedPane();
tabPane.addTab("Map layers",layerTreePanel);
tabPane.addTab("Coordinate System",csPanel);
getContentPane().add(tabPane,BorderLayout.LINE_START);
getContentPane().add(locator,BorderLayout.SOUTH);
}
8. Load the sample map data into the application:
private void loadData() {
URL shpFile = findURL("data/world.shp");
URL gifFile = findURL("data/world.gif");
try {
// Create a data source for the shape file.
IlvShapeDataSource shpDataSource = new
IlvShapeDataSource(shpFile.getFile(), true);
shpDataSource.setCoordinateSystem(IlvGeographicCoordinateSystem.WGS84);
// Create a raster reader for the .gif file (necessary to create the
datasource that follows).
IlvRasterBasicImageReader imageReader = new
IlvRasterBasicImageReader();
imageReader.loadImage(gifFile.getFile());
// Georeference this image (it covers the whole earth).
imageReader.setImageBounds(0,-Math.PI,Math.PI/2,Math.PI,-Math.PI/2);
// Create a datasource for the .gif file.
IlvMapDataSource imageDataSource =
IlvRasterDataSourceFactory.buildImageDataSource(
view.getManager(),imageReader,true,true,true,null);
// Insert it in the data source tree of the manager.
IlvMapDataSourceModel dataSourceModel =
IlvMapDataSourceProperty.GetMapDataSourceModel(view.getManager());
dataSourceModel.insert(shpDataSource);
dataSourceModel.insert(imageDataSource);
// Start reading the model (recursively start all data sources of this
model).
dataSourceModel.start();
// Get the shape map layer used to display the data source.
IlvMapLayer shpLayer = shpDataSource.getInsertionLayer();
shpLayer.setName("ESRI layer (world.shp)");
IlvMapLayer imageLayer = imageDataSource.getInsertionLayer();
// Insert it in the Map layer tree of the manager.
IlvMapLayerTreeModel ltm =
IlvMapLayerTreeProperty.GetMapLayerTreeModel(view.getManager());
ltm.addChild(null, shpLayer);
ltm.addChild(null, imageLayer);
// Set up the shape layer style.
shpLayer.getStyle().setAttribute(IlvPolylineStyle.FOREGROUND,Color.
black);
shpLayer.getStyle().setAttribute(IlvPolylineStyle.BACKGROUND,new
Color(1,1,1,0.25f));
imageLayer.setName("Image layer (world.gif)");
} catch (Exception e1) {
e1.printStackTrace();
}
9. Fit the data source to the size of the application view:
view.fitTransformerToContent();
view.repaint();
}
10. Write the main method:
static public void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(
new Runnable() {
public void run() {
final JFrame frame = new BuildMapDemo();
frame.setTitle("Simple map building demo");
frame.pack();
frame.setVisible(true);
}
);
}
11. Remove all the layers from the manager and clear the associated model when the coordinate system changes:
private void clearManager() {
IlvManager manager = view.getManager();
IlvMapLayerTreeModel model =
IlvMapLayerTreeProperty.GetMapLayerTreeModel(manager);
model.clearAllObjects();
while (manager.getLayersCount() > 0) {
IlvManagerLayer layer = manager.getManagerLayer(0);
manager.removeLayer(0, false);
}
manager.removeNamedProperty(IlvMapLayerTreeProperty.NAME);
manager.removeNamedProperty(IlvMapDataSourceProperty.NAME);
manager.removeNamedProperty(IlvAreasOfInterestProperty.NAME);
}
}
Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.