JavaFX with Geotools Tutorial – Part 1: Load shape file

global country boundaries

Prerequisites

Build with Maven

First you set up a basic build script.

Create the project structure
  1. Create directory structure following maven standard
  2. Download global
    country boundaries shape files
    and put to resource directory
  3. Create 2 classes for Main.java and MapCanvas.java
map_example
└── src
    └── main
        └── java
            └── map_example
                └── Main.java
                └── MapCanvas.java
        └── resource
            └── map_example
                └── countries.dbf
                └── countries.prj
                └── countries.shp
                └── countries.shx
└── pom.xml
Configure pom.xml
  1. Define the version number of libraries that you
    wish to use.
  2. 	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<geotools.version>15-SNAPSHOT</geotools.version>
    		<fxgraphics2d.version>1.3</fxgraphics2d.version>
    	</properties>
    	
  3. Specify the following dependencies.
  4. Geotools
    	<dependency>
    		<groupId>junit</groupId>
    		<artifactId>junit</artifactId>
    		<version>4.11</version>
    		<scope>test</scope>
    	</dependency>
    	<dependency>
    		<groupId>org.geotools</groupId>
    		<artifactId>gt-shapefile</artifactId>
    		<version>${geotools.version}</version>
    	</dependency>
    	<dependency>
    		<groupId>org.geotools</groupId>
    		<artifactId>gt-swing</artifactId>
    		<version>${geotools.version}</version>
    	</dependency>
    	
    FXGraphics2D
    	<dependency>
    		<groupId>org.jfree</groupId>
    		<artifactId>fxgraphics2d</artifactId>
    		<version>1.3</version>
    	</dependency>
    	
  5. Define repository to download jar files.
  6. 	<repositories>
    		<repository>
    			<id>maven2-repository.dev.java.net</id>
    			<name>Java.net repository</name>
    			<url>http://download.java.net/maven/2</url>
    		</repository>
    		<repository>
    			<id>osgeo</id>
    			<name>Open Source Geospatial Foundation Repository</name>
    			<url>http://download.osgeo.org/webdav/geotools/</url>
    		</repository>
    		<repository>
    			<snapshots>
    				<enabled>true</enabled>
    			</snapshots>
    			<id>boundless</id>
    			<name>Boundless Maven Repository</name>
    			<url>http://repo.boundlessgeo.com/main</url>
    		</repository>
    	</repositories>
    	

Implementation

Now that you’ve set up the project and build system, you can
create your map canvas.

Create map canvas

  1. Initial java fx canvas and GraphicsContext. The
    GraphicsContext will be used to draw map content on the canvas
  2. 	private Canvas canvas;
    	private MapContent map;
    	private GraphicsContext gc;
    
    	public MapCanvas(int width, int height) { 
    		canvas = new Canvas(width, height);
    		gc = canvas.getGraphicsContext2D();
    		initMap(); 
    		drawMap(gc); 
    	}
    
    	public Node getCanvas() {
    		return canvas;
    	}
    	
  3. Load shape files and setting up style for
    displaying on the screen.
  4.  
    	private void initMap() {
    		try {
    			FileDataStore store = FileDataStoreFinder.getDataStore(this.getClass().getResource("countries.shp"));
    			SimpleFeatureSource featureSource = store.getFeatureSource();
    			map = new MapContent();
    			map.setTitle("Quickstart");
    			Style style = SLD.createSimpleStyle(featureSource.getSchema());
    			FeatureLayer layer = new FeatureLayer(featureSource, style);
    			map.addLayer(layer);
    			map.getViewport().setScreenArea(new Rectangle((int) canvas.getWidth(), (int) canvas.getHeight()));
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	
  5. Use StreamingRender to draw MapContent on the canvas
  6. We create new instance of FXGraphic2D for rendering Graphic2D on GraphicsContext.

     
    	private void drawMap(GraphicsContext gc) {
    		StreamingRenderer draw = new StreamingRenderer();
    		draw.setMapContent(map);
    		FXGraphics2D graphics = new FXGraphics2D(gc);
    		graphics.setBackground(java.awt.Color.WHITE);
    		Rectangle rectangle = new Rectangle((int) canvas.getWidth(), (int) canvas.getHeight());
    		draw.paint(graphics, rectangle, map.getViewport().getBounds());
    	}
    	

Initial main application and screen size for display map

	public class Main extends Application {
		@Override 
		public void start(Stage primaryStage) { 
			try { 
				MapCanvas canvas = new MapCanvas(1024, 768); 
				Pane pane = new	Pane(canvas.getCanvas());
				Scene scene = new Scene(pane);
				primaryStage.setScene(scene);
				primaryStage.setTitle("Map Example");
				primaryStage.show(); 
			} catch (Exception e) { 
					e.printStackTrace(); 
			} 
		}

		public static void main(String[] args) {
			launch(args);
		}
	}
	

Source code

What’s next?

In Tutorial Part 2, we will add more functionality like pan, zoom
in and zoom out.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s