29
nvironmental ngineering ydroloGIS HydroloGIS S.r.l. - Via Siemens, 19 - 39100 Bolzano www.hydrologis.com Java Open Source GIS Development From the building blocks to extending an existing GIS application. Geoinformation Research Group, Department of Geography University of Potsdam August 2011 Part 1: Introduction and main tools Tutor: Andrea Antonello

Opensource gis development - part 1

Embed Size (px)

Citation preview

Page 1: Opensource gis development - part 1

nvironmental ngineeringydroloGISHydroloGIS S.r.l. - Via Siemens, 19 - 39100 Bolzano www.hydrologis.com

Java Open Source GISDevelopment

From the building blocks to extendingan existing GIS application.

Geoinformation Research Group, Department of GeographyUniversity of Potsdam

August 2011

Part 1: Introduction and main toolsTutor: Andrea Antonello

Page 2: Opensource gis development - part 1

Who am I?

Andrea Antonello

Environmental Engineer, graduated with GRASS development thesis

PhD in Environmental Sciences, working on development of digital

field mapping tools

GIS development

Involved for years in open source GIS development, mainly GRASS,

JGrass, uDig, BeeGIS, Geopaparazzi

HydroloGIS

Work for HydroloGIS, a company that writes and uses GFOSS

products for environmental analyses, mostly hydrology and

geomorphology.

Page 3: Opensource gis development - part 1

Buzzwords: Java, Maven and Eclipse

Java

The Java technology is a programming language and a platform.

Maven

Maven is build system for Java which is very good at managing

dependencies.

Eclipse IDE

Eclipse is a popular integrated development environment most often

used for all kinds of Java development.

Eclipse RCP

The rich client platform used to build applications like uDig, but also

the eclipse IDE itself.

Page 4: Opensource gis development - part 1

Buzzwords: GeoTools, JTS and OGC

GeoTools

GeoTools is an open source (LGPL) Java code library which provides

standards compliant methods for the manipulation of geospatial data,

for example to implement Geographic Information Systems (GIS).

Java Topology Suite

The JTS Topology Suite is a library of spatial predicates and

functions for processing geometry.

Open Geospatial Consortium

The OGC is a non-profit, international, voluntary consensus

standards organization that is leading the development of standards

for geospatial and location based services.

Page 5: Opensource gis development - part 1

Buzzwords: uDig, OMS and JGrasstools

User-friendly Desktop Internet GIS

uDig is an open source (LGPL) desktop GIS, built with Eclipse Rich

Client (RCP) technology.

Object Modeling System

The OMS object-oriented modeling framework. OMS allows model

construction and model application based on components.

JGrasstools

A geospatial processing library based on OMS that works in uDig's

Spatial Toolbox.

Page 6: Opensource gis development - part 1

Useful spatial links

Spatial:

• GeoTools Homepage

• uDig Homepage

• OMS3 Homepage

• JGrasstools Homepage

• Imageio-ext Homepage

• JTS Homepage

Page 8: Opensource gis development - part 1

Contents of the course

Part 1: Introduction and main tools

• setup of java, eclipse, maven

• introduction to the GeoTools project

• introduction to the Natural Earth project

• creation of a simple GeoTools based project

• visualization of a map

Page 9: Opensource gis development - part 1

Part 2: Introduction of the main Geo-Objects

• geometry objects

• projections

• vector objects

• raster objects

• filters

• style

Page 10: Opensource gis development - part 1

Part 3: Vector data

• vector basics, handling shapefiles

• geometry basics

• predicates

• functions

• tips and tricks

Page 11: Opensource gis development - part 1

Part 4: Raster data

• reading/writing raster data

• processing rasters

• image vs. world space

Page 12: Opensource gis development - part 1

Part 5: Integrating with uDig

• introduction to OMS

• introduction to uDig

• introduction to uDig's Spatial Toolbox

• introduction to jgrasstools

• create example OMS GeoTools based modules

Page 13: Opensource gis development - part 1

Preparing the development environment

Java Installation

Perform the following installations, based on the download and instructions

links supplied in the previous section:

• Java Development Kit (JDK)

• Java Advanced Imaging (JAI core), used by GeoTools for raster support

• JAI ImageIO, used to read and write raster files

• ImageIO-ext, used to support raster formats through gdal drivers:

Page 14: Opensource gis development - part 1

Eclipse Installation

1. Visit the Eclipse HomePage and download Eclipse IDE for Java

developers.

2. Eclipse does not provide an installer, just unzip to a directory run.

3. To start out create a folder /home/moovida/giscourse/ somewhere

on your disk to keep all our java development in one spot. To keep an

existing reference we will use /home/moovida/ as base, which should

be substituted with a folder of choice.

4. Unzip the downloaded file to your /home/moovida/giscourse/ folder,

which will create /home/moovida/giscourse/eclipse/.

5. Open up the eclipse.ini file and changing the Xmx argument to

-Xmx2000m, if you have enough RAM.

6. Run the eclipse executable.

Page 15: Opensource gis development - part 1

7. When you start up eclipse for the first time it will prompt you for a

workspace. To keep our java work in one spot you can type in:

/home/moovida/giscourse/eclipse/workspace

8. On the Welcome view press Workbench along the right hand side and we

can get started

Page 16: Opensource gis development - part 1

Maven Installation

The maven build tool can work on the command line and includes a plugin

for generating eclipse .project and .classpath files.

1. Download Maven from the Maven Homepage

During the course we will use Maven 2.2.1.

2. Unzip the file apache-maven-2.2.1-bin.zip to

/home/moovida/giscourse/apache-maven-2.2.1/.

3. You need to have a couple of environmental variables set for maven to

work. Open inside your favourite text editor the .bashrc file inside you

home folder and add the following lines at the end (if similar ones are

not there already):

Page 17: Opensource gis development - part 1

JAVA_HOME /home/moovida/java/jdk1.6.0_16

M2_HOME /home/moovida/giscourse/apache-maven-2.2.1/

PATH $PATH:$JAVA_HOME/bin:$M2_HOME/bin

4. Open up a terminal

5. Type the following command to confirm you are set up correctly:

mvn -version

Page 18: Opensource gis development - part 1

6. This should produce something like:

Apache Maven 2.2.1 (r801777; 2009-08-06 21:16:01+0200)Java version: 1.6.0_14Java home: /home/moovida/SOFTWARE/JAVA/JDKS/jdk1.6.0_14/jreDefault locale: en_US, platform encoding: UTF-8OS name: "linux" version: "2.6.38-8-generic" arch: "amd64" Family: "unix"

Page 19: Opensource gis development - part 1

Setting up the example project in Eclipse

To get started quickly we will use a template project that already contains the

dependencies we will need. The example project can be downloaded from:

http://jgrasstools.googlecode.com/files/jgt-dev-example.tar.gz

Create the folder /home/moovida/giscourse/code/ that will hold all the

code we will write during the course. Extract the downloaded package into

the code folder (tar zcvf jgt-dev-example.tar.gz). You now have

the folder: /home/moovida/giscourse/code/jgt-dev-example.

Page 20: Opensource gis development - part 1

If starting from scratch, we would have created a new maven project inside

/home/moovida/giscourse/ with the command:

mvn archetype:create -DgroupId=org.geotools -DartifactId=tutorial

and after that we would have added the dependencies to the project's

configuration file.

Starting with an existing project helps to how the dependencies rules work,

without the need to understand all of maven.

Page 21: Opensource gis development - part 1

Dependencies

The downloaded project is a maven project that already contains all the

needed dependencies (the references).

Everything needed is defined inside the pom.xml file in the root of the

project.

A dependency definition looks like:

<dependency> <groupId>org.jgrasstools</groupId> <artifactId>jgt-jgrassgears</artifactId> <version>0.7.1</version> </dependency>

To find such dependencies when you have no template, websites like:

http://mvnrepository.com/ get handy.

Page 22: Opensource gis development - part 1

Repositories

Most of the packages are kept in a central maven repository. GeoTools'

packages are hosted by the Open Source Geospatial Foundation (Osgeo

Homepage).

Any repository can be added to the pom.xml project file. In our example we

find:

<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>

Page 23: Opensource gis development - part 1

Create the Eclipse project

To actually get the dependencies downloaded, we can proceed like the

following:

1. From within /home/moovida/giscourse/code/jgt-dev-example

And ask for our project to be set up for eclipse:

mvn eclipse:eclipse

This will generate the eclipse project files and download the needed jars

for you.

2. Return to Eclipse

3. Use Windows -> Preferences to open the Preference Dialog. Using

the tree on the left navigate to the Java -> Build path ->

Classpath Variables preference page.

Page 24: Opensource gis development - part 1

Load the Eclipse project

4. Add an M2_REPO classpath variable pointing to your local repository,

which is created automatically by maven itself and by default is located in:

/home/moovida/.m2/repository.

5. We can now import your new project into eclipse using File ->

Import

6. Choose Existing Projects into Workspace from the list, and

press Next

7. Select the project you created in:

/home/moovida/giscourse/code/jgt-dev-example.

8. Press Finish to import your project

Page 25: Opensource gis development - part 1

Testing the development environment

Download a test dataset

The best way to check if the development environment is properly installed,

is to show a map. First we need some data to work with. The Natural Earth

data project is a place we can get some freely available datasets. It is

supported by the North American Cartographic Information Society.

Let's download 110m-cultural.zip, which contains various vector layers of

countries, boundaries and cities of the whole world.

create a datafolder: /home/moovida/giscourse/data

and unzip them into it in order to have:

/home/moovida/giscourse/data/110m_cultural

Page 26: Opensource gis development - part 1

Write and run your first GIS application

• copy the following code snippet

import java.io.File;import org.geotools.data.*;import org.geotools.data.simple.SimpleFeatureSource;import org.geotools.map.*;import org.geotools.styling.*;import org.geotools.swing.JMapFrame;import org.geotools.swing.data.JFileDataStoreChooser;

public class Quickstart { public static void main( String[] args ) throws Exception { File file = JFileDataStoreChooser.showOpenFile("shp", null); FileDataStore store = FileDataStoreFinder.getDataStore(file); SimpleFeatureSource featureSource = store.getFeatureSource(); MapContext map = new MapContext(); map.setTitle("Quickstart"); Style style = SLD.createSimpleStyle(featureSource.getSchema()); Layer layer = new FeatureLayer(featureSource, style); map.addLayer(layer); JMapFrame.showMap(map); }}

• select the org.jgrasstools.part1 package in the project

• paste the code in (eclipse will create the right class for it)

Page 27: Opensource gis development - part 1

• run the application by right clicking on the class and run as -> java

application

• browse in the filechooser to select the file:

110m_admin_0_countries.shp

You should see the following map:

Page 28: Opensource gis development - part 1

The first (and most boring) step is left behind.Take a deep breath.

QUESTIONS?

Page 29: Opensource gis development - part 1

This work is released under Creative Commons Attribution ShareAlike (CC-BY-SA)

Much of the knowledge needed to create this training material hasbeen produced by the sparkling knights of the GeoTools, JTS anduDig community. Another essential source has been the Wikipediacommunity effort.

Particular thanks go to those friends that directly or indirectly helpedout in the creation and review of this developer's handbook: JodyGarnett from the uDig/GeoTools community and the TANTO team.

This tutorial was written with the support of the GeoinformationResearch Group of the University of Potsdam and HydroloGIS.