Eclipse is a popular integrated development environment for Java development. Since GeoTools is a Java library we would really like it to work out of the box for you.
|
Thanks for feedback: If you have any problems with these instructions email the user list. |
What about Maven and Eclipse
You will find that most of our documentation is written with use of Maven and Eclipse in mind.
- 03 First Project - Tutorial on the use of Maven and Eclipse
We do recommend Maven as the easiest long term solution for using GeoTools (it is great because maven will figure out jars are needed and make sure you are always up to date).
How to get the GeoTools jars into an Eclipse Project
You can download GeoTools from our download page. The latest stable release is always highlighted, you may consider one of the development releases if you are brave (or want better support on the IRC channel).
For this example we are going to download:
- gt2-2.4-RC0-bin.zip - contains all the jars needed for GeoTools
- gt2-2.4-RC0-src.zip - contains the source code so you can step through in the debugger
Create a new GeoTools Project
We are going to create a new project to hold the GeoTools jars, other projects in our workspace are going to depend on this one making it easier for us to upgrade.
- New Java Project
- Project name: GeoTools
- Press Finish
We can now place our GeoTools downloads into this project:
- Unzip bin download (using 7zip for example) producing:
- A new folder: gt2-2.4-RC0
- You can just copy the src - we can leave it as a zip file

It is full of Jars!
This first thing that you need to know is that there are a lot of files, the binary release above includes 116 jars. The good news is you do not need all of these jars (what you need depends on what you are going to be doing). The bad news is that some of these jars conflict with each other...
Delete these conflicting jars:
- gt2-epsg-access-2.4-RC0.jar
- gt2-epsg-postgresql-2.4-RC0.jar
- gt2-epsg-wkt-2.4-RC0.jar
You can remove the README.txt file and remove any plugins you are not going to use, and we recommend removing any unsupported jars.
Finally you may need to provide some of your own jars:
- oracle-spatial needs you to provide an oracle jdbc driver
- arcsde needs you to provide the arcsde client jars
Adding the Jars to your Java Project
Now that we have a good list of jars we can add them to our Java Build path.
- Right click on the project and select properties
- Choose Java Build Path in the list
- Select the Libraries tab
- Press the Add JARs button
- Select all the jars in your gt2-2.4-RC0 folder

- Switch to the Order and Export tab
- Press Select All
- Deselect your JRE System Library
- And press OK

We will also need to export these jars for others:
Using the GeoTools Project
We can now create additional projects that depend on our GeoTools project.
- From the file menu create a New Java Project
- Project name: Example
- Press Next
- Switch to the Projects tab and press Add...
- Select the GeoTools project we made earlier

Sample Application
Just to make sure everything works we are going to create a quick sample application.
- From the file mentu Create a new Java Class
- Package: org.geotools.example
- Name: Shp2Shpe
- Check of public static main as a method sub you would like to create

- Press Finish
You can then cut and paste the following:
package org.geotools.demo; import java.io.File; import java.io.FileNotFoundException; import java.util.HashMap; import java.util.Map; import java.util.Set; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.filechooser.FileFilter; import org.geotools.data.DataStore; import org.geotools.data.DataStoreFactorySpi; import org.geotools.data.DataStoreFinder; import org.geotools.data.DataUtilities; import org.geotools.data.DefaultQuery; import org.geotools.data.DefaultTransaction; import org.geotools.data.FeatureSource; import org.geotools.data.FeatureStore; import org.geotools.data.Transaction; import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.data.shapefile.ShapefileDataStoreFactory; import org.geotools.factory.GeoTools; import org.geotools.feature.FeatureCollection; import org.geotools.feature.FeatureType; import org.geotools.referencing.CRS; import org.opengis.referencing.crs.CoordinateReferenceSystem; public class Shp2Shp { public static void main(String[] args) throws Exception { System.out.println("Welcome to GeoTools:" + GeoTools.getVersion()); File file = getShapeFile(args); Map<String, Object> connect = new HashMap<String, Object>(); connect.put("url", file.toURI().toURL()); DataStore dataStore = DataStoreFinder.getDataStore(connect); String[] typeNames = dataStore.getTypeNames(); String typeName = typeNames[0]; System.out.println("Reading content " + typeName); FeatureSource featureSource = dataStore.getFeatureSource(typeName); FeatureType schema = featureSource.getSchema(); System.out.println("Header: " + DataUtilities.spec(schema)); DefaultQuery query = new DefaultQuery(); query.setTypeName(typeName); CoordinateReferenceSystem prj = schema.getPrimaryGeometry() .getCoordinateSystem(); if (prj == null) { prj = getCoordinateReferenceSystem("No projection fround for " + file + " please choose one:"); query.setCoordinateSystem(prj); } CoordinateReferenceSystem crs = getCoordinateReferenceSystem("Project " + file + " to:"); query.setCoordinateSystemReproject(crs); FeatureCollection collection = featureSource.getFeatures(query); File newFile = getNewShapeFile(file); DataStoreFactorySpi factory = new ShapefileDataStoreFactory(); Map<String, Object> create = new HashMap<String, Object>(); create.put("url", newFile.toURI().toURL()); create.put("create spatial index", Boolean.TRUE); DataStore newDataStore = factory.createNewDataStore(create); newDataStore.createSchema(collection.getSchema()); String newTypeNames[] = newDataStore.getTypeNames(); String newTypeName = newTypeNames[0]; FeatureStore featureStore = (FeatureStore) newDataStore.getFeatureSource(newTypeName); if( collection.getSchema().getDefaultGeometry().getCoordinateSystem() != crs ){ ((ShapefileDataStore)newDataStore).forceSchemaCRS( crs ); } Transaction transaction = new DefaultTransaction("Reproject"); featureStore.setTransaction(transaction); try { featureStore.addFeatures(collection); transaction.commit(); } catch (Exception problem) { problem.printStackTrace(); transaction.rollback(); } finally { transaction.close(); } System.exit(0); } private static File getNewShapeFile(File file) { String path = file.getAbsolutePath(); String newPath = path.substring(0, path.length() - 4) + "2.shp"; JFileChooser chooser = new JFileChooser(); chooser.setDialogTitle("Save reprojected shapefile"); chooser.setSelectedFile(new File(newPath)); chooser.setFileFilter(new FileFilter() { public boolean accept(File f) { return f.isDirectory() || f.getPath().endsWith("shp") || f.getPath().endsWith("SHP"); } public String getDescription() { return "Shapefiles"; } }); int returnVal = chooser.showSaveDialog(null); if (returnVal != JFileChooser.APPROVE_OPTION) { System.exit(0); } File newFile = chooser.getSelectedFile(); if (newFile.equals(file)) { System.out.println("Cannot replace " + file); System.exit(0); } return newFile; } private static CoordinateReferenceSystem getCoordinateReferenceSystem( String message) throws Exception { Set codes = CRS.getSupportedCodes("EPSG"); String selected = (String) JOptionPane.showInputDialog(null, message, "Choose a Projection", JOptionPane.QUESTION_MESSAGE, null, codes.toArray(), "4326"); if (selected == null) { System.exit(0); } return CRS.decode("EPSG:" + selected); } private static File getShapeFile(String[] args) throws FileNotFoundException { File file; if (args.length == 0) { JFileChooser chooser = new JFileChooser(); chooser.setDialogTitle("Open Shapefile for Reprojection"); chooser.setFileFilter(new FileFilter() { public boolean accept(File f) { return f.isDirectory() || f.getPath().endsWith("shp") || f.getPath().endsWith("SHP"); } public String getDescription() { return "Shapefiles"; } }); int returnVal = chooser.showOpenDialog(null); if (returnVal != JFileChooser.APPROVE_OPTION) { System.exit(0); } file = chooser.getSelectedFile(); System.out .println("You chose to open this file: " + file.getName()); } else { file = new File(args[0]); } if (!file.exists()) { throw new FileNotFoundException(file.getAbsolutePath()); } return file; } }
Running From Eclipse
- Right click on the Shp2Shp class and run as a Java Application
Making Associations with the SRC download
The first time you go to look at any of the GeoTools classes you will need to tell Eclipse where the source code for that jar is.
- From the "Source not found" editor press the Attach Source... Button.
- Press the Workspace button
- Select the gt2-2.4-RC0-src.zip
- And press OK

Power Users
If you are a interested in the details, the above steps made a change to your .classpath file:
<classpathentry exported="true" kind="lib" path="gt2-2.4-RC0/gt2-api-2.4-RC0.jar" sourcepath="gt2-2.4-RC0-src.zip"/>
You can quickly cut and paste sourcepath="gt2-2.4-RC0-src.zip" for the rest of the gt2 jars.