Welcome to Geospatial for Java. This workbook is aimed at Java developers who
are new to geospatial and would like to get started.
We are going to start out carefully with the steps needed to set up your Eclipse IDE. This workbook
is also available for NetBeans or Maven command line use. If you are comfortable with the build tool
Maven, it is our preferred option for downloading and managing jars but we will also document how
to set up things by hand.
These are visual tutorials that allows you to see what you are working with while you learn.
These examples make use of Swing; be assured that this is only to make the examples easy and
fun to use. These sessions are applicable to both server side and client side development.
Eclipse is a popular integrated development environment most often used for all kinds of Java
development. For this tutorial we are doing straight up Java programming using the smallest
download available - if you already have an Eclipse download please go ahead and use it and
switch to the “Java Perspective”.
Visit the Eclipse download page and download “Eclipse IDE for Java Developers”.
Maven is a build system for Java which is very good at managing dependencies. The GeoTools library
is plugin based and you get to pick and choose what features you need for your application. While
this is useful when determining just what is needed for delivery - it can be a pain to manage by
hand so we encourage the use of a tool such as maven.
The M2E plugin is included by default, this can be confirmed if the Maven preference page is listed.
For this Quickstart we are going to produce a simple maven project, hook it up to GeoTools, and
then display a shapefile.
This tutorial is really focused on your development environment and making sure you have GeoTools
ready to go. We will cover what a shapefile is and how the map is displayed shortly.
Maven works by asking you to describe your project, the name, the version number, where the source
code is, how you want it packaged, and what libraries it makes use of. Based on the description it
can figure out most things: how to compile your code, creating javadocs, or even downloading the
library jars for you.
To use M2E plugin to create a create a new maven project:
File > New > Other from the menu bar
Select the wizard Maven > Maven Project and press Next to open the New Maven Project wizard
The New Maven project page defaults are fine, press Next
A large number of sample projects are available, type in maven-archetype-quickstart to search
for the apache one.
The archetype acts a template using the parameters we supply to create the project.
Group Id: org.geotools
Artifact Id: tutorial
Version: 0.0.1-SNAPSHOT (default)
Package: org.geotools.tutorial
Press Finish to create the new project.
You can see that an application has been created; complete with App.java and a JUnit test case
Open up src/main/java and select org.geotools.tutorial.App and press the Run button in the
toolbar:
Hello World!
You may also open up src/main/test and run org.geotools.tutorial.AppTest as a JUnit Test.
The pom.xml file is used to describe the care and feeding of your maven project; we are going to
focus on the dependencies needed for your project
When downloading jars maven makes use of a “local repository” to store jars.
PLATFORM
LOCAL REPOSITORY
Windows XP:
C:\DocumentsandSettings\You\.m2\repository
Windows:
C:\Users\You\.m2repository
Linux and Mac:
~/.m2/repository
To download jars maven makes use of public maven repositories on the internet where projects
such as GeoTools publish their work.
Open up pom.xml in your new project. You can see some of the information we entered
earlier.
This editor allows you to describe all kinds of things; in the interest of time we are going to
skip the long drawn out explanation and ask you to click on the pom.xml tab.
To make use of GeoTools we are going to add several things to this pom.xml file.
At the top after moduleVersion add a properties element defining the version of GeoTools we
want to use.
This workbook was written for 34.0 although you may wish to try a different version.
For GeoTools 34 release set the geotools.version property to 34.0.
We use the GeoTools Bill of Materials (BOM) to manage dependency versions. This ensures that all GeoTools modules use compatible versions:
<dependencyManagement><dependencies><!-- Import the GeoTools BOM --><dependency><groupId>org.geotools</groupId><artifactId>gt-bom</artifactId><version>${gt.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.junit</groupId><artifactId>junit-bom</artifactId><version>5.11.0</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
The BOM (Bill of Materials) pattern centralizes version management. By importing the gt-bom, we don’t need to specify version numbers for individual GeoTools modules.
We add dependencies to GeoTools modules. Note that we don’t specify version numbers since these are managed by the BOM:
Note the snapshot repository above is only required if you are using a nightly build (such as 34-SNAPSHOT)
The project was generated with a build dependencyManagement section
locking down plugin versions to avoid using Maven defaults.0
<build><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --><plugin><artifactId>maven-clean-plugin</artifactId><version>3.4.0</version></plugin><!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.3.1</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.13.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>3.3.0</version></plugin><plugin><artifactId>maven-jar-plugin</artifactId><version>3.4.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>3.1.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>3.1.2</version></plugin><!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --><plugin><artifactId>maven-site-plugin</artifactId><version>3.12.1</version></plugin><plugin><artifactId>maven-project-info-reports-plugin</artifactId><version>3.6.1</version></plugin></plugins></pluginManagement></build>
GeoTools now requires Java 17 - add build configuration to ask maven to use Java 17 source level.
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.geotools.tutorial</groupId><artifactId>quickstart</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>GeoToolsQuickstart</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.release>17</maven.compiler.release><gt.version>34-SNAPSHOT</gt.version><maven.deploy.skip>true</maven.deploy.skip></properties><dependencyManagement><dependencies><!-- Import the GeoTools BOM --><dependency><groupId>org.geotools</groupId><artifactId>gt-bom</artifactId><version>${gt.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.junit</groupId><artifactId>junit-bom</artifactId><version>5.11.0</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.geotools</groupId><artifactId>gt-shapefile</artifactId></dependency><dependency><groupId>org.geotools</groupId><artifactId>gt-swing</artifactId></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><scope>test</scope></dependency></dependencies><repositories><repository><id>osgeo</id><name>OSGeoReleaseRepository</name><url>https://repo.osgeo.org/repository/release/</url><snapshots><enabled>false</enabled></snapshots><releases><enabled>true</enabled></releases></repository><repository><id>osgeo-snapshot</id><name>OSGeoSnapshotRepository</name><url>https://repo.osgeo.org/repository/snapshot/</url><snapshots><enabled>true</enabled></snapshots><releases><enabled>false</enabled></releases></repository></repositories><build><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --><plugin><artifactId>maven-clean-plugin</artifactId><version>3.4.0</version></plugin><!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.3.1</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.13.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>3.3.0</version></plugin><plugin><artifactId>maven-jar-plugin</artifactId><version>3.4.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>3.1.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>3.1.2</version></plugin><!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --><plugin><artifactId>maven-site-plugin</artifactId><version>3.12.1</version></plugin><plugin><artifactId>maven-project-info-reports-plugin</artifactId><version>3.6.1</version></plugin></plugins></pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><release>17</release></configuration></plugin></plugins></build></project>
Recommend cutting and pasting the above to avoid mistakes when typing
You may also download pom.xml, if this opens in your browser use Save As to save to disk.
The download has an optional quality assurance profile you can safely ignore.
Tips:
If maven has trouble downloading any jar; you can try again by selecting
Project ‣ Update All Maven Dependencies.
If the dependencies do not update automatically
use Project ‣ Clean
Now that your environment is setup we can put together a simple Quickstart. This example will display a shapefile on screen.
Create the package org.geotools.tutorial.quickstart using your IDE.
Create the org.geotools.tutorial.quickstart.Quickstart class using your IDE.
Fill in the following code Quickstart.java:
/* * GeoTools Sample code and Tutorials by Open Source Geospatial Foundation, and others * https://docs.geotools.org * * To the extent possible under law, the author(s) have dedicated all copyright * and related and neighboring rights to this software to the public domain worldwide. * This software is distributed without any warranty. * * You should have received a copy of the CC0 Public Domain Dedication along with this * software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. */packageorg.geotools.tutorial.quickstart;importjava.io.File;importjava.util.logging.Logger;importorg.geotools.api.data.FileDataStore;importorg.geotools.api.data.FileDataStoreFinder;importorg.geotools.api.data.SimpleFeatureSource;importorg.geotools.map.FeatureLayer;importorg.geotools.map.Layer;importorg.geotools.map.MapContent;importorg.geotools.styling.SLD;importorg.geotools.api.style.Style;importorg.geotools.swing.JMapFrame;importorg.geotools.swing.data.JFileDataStoreChooser;/** * Prompts the user for a shapefile and displays the contents on the screen in a map frame. * * <p>This is the GeoTools Quickstart application used in documentationa and tutorials. * */publicclassQuickstart{privatestaticfinalLoggerLOGGER=org.geotools.util.logging.Logging.getLogger(Quickstart.class);/** * GeoTools Quickstart demo application. Prompts the user for a shapefile and displays its * contents on the screen in a map frame * @param args Ignored no args required. * @throws Exception If there are errors finding the shapefile. */publicstaticvoidmain(String[]args)throwsException{// display a data store file chooser dialog for shapefilesLOGGER.info("Quickstart");LOGGER.config("Welcome Developers");LOGGER.info("java.util.logging.config.file="+System.getProperty("java.util.logging.config.file"));Filefile=JFileDataStoreChooser.showOpenFile("shp",null);if(file==null){return;}LOGGER.config("File selected "+file);FileDataStorestore=FileDataStoreFinder.getDataStore(file);SimpleFeatureSourcefeatureSource=store.getFeatureSource();// Create a map content and add our shapefile to itMapContentmap=newMapContent();map.setTitle("Quickstart");Stylestyle=SLD.createSimpleStyle(featureSource.getSchema());Layerlayer=newFeatureLayer(featureSource,style);map.addLayer(layer);// Now display the mapJMapFrame.showMap(map);}}
You may find cutting and pasting from the documentation to be easier then typing.
We need to download some sample data to work with. The http://www.naturalearthdata.com/ project
is a great project supported by the North American Cartographic Information Society. Head to the link below and download some cultural vectors. You can use the ‘Download all 50m cultural themes’ at top.
Please unzip the above data into a location you can find easily such as the desktop.
Run the application to open a file chooser. Choose a shapefile from the example data set.
The application will connect to your shapefile, produce a map content, and display the shapefile.
A couple of things to note about the code example:
The shapefile is not loaded into memory - instead it is read from disk each and every time it is needed
This approach allows you to work with data sets larger than available memory.
We are using a very basic display style here that just shows feature outlines. In the examples that follow we will see how to specify more sophisticated styles.
Each tutorial consists of very detailed steps followed by a series of extra questions. If you get
stuck at any point please ask your instructor; or sign up to the geotools-users email list.
Here are some additional challenges for you to try:
Try out the different sample data sets.
Zoom in, zoom out and show the full extent and use the info tool to examine individual
countries in the sample countries.shp file.
Download the largest shapefile you can find and see how quickly it can be rendered. You should
find that the very first time it will take a while as a spatial index is generated. After that
rendering will become much faster.
Fast: We know that one of the ways people select a spatial library is based on speed. By design
GeoTools does not load the above shapefile into memory (instead it streams it off of disk
each time it is drawn using a spatial index to only bring the content required for display).
If you would like to ask GeoTools to cache the shapefile in memory try the following code:
/** * This method demonstrates using a memory-based cache to speed up the display (e.g. when * zooming in and out). * * <p>There is just one line extra compared to the main method, where we create an instance of * CachingFeatureStore. * @param args Ignored no args required. * @throws Exception If there are errors finding the shapefile. */publicstaticvoidmain(String[]args)throwsException{// display a data store file chooser dialog for shapefilesFilefile=JFileDataStoreChooser.showOpenFile("shp",null);if(file==null){return;}FileDataStorestore=FileDataStoreFinder.getDataStore(file);SimpleFeatureSourcefeatureSource=store.getFeatureSource();SimpleFeatureSourcecachedSource=DataUtilities.source(newSpatialIndexFeatureCollection(featureSource.getFeatures()));// Create a map content and add our shapefile to itMapContentmap=newMapContent();map.setTitle("Using cached features");Stylestyle=SLD.createSimpleStyle(featureSource.getSchema());Layerlayer=newFeatureLayer(cachedSource,style);map.addLayer(layer);// Now display the mapJMapFrame.showMap(map);}
For the above example to compile you will need to add the necessary imports.
Note
When building you may see a message that CachingFeatureSource is deprecated. It’s OK to ignore
it, it’s just a warning. The class is still under test but usable.
Try and sort out what all the different “side car” files are – and what they are for. The sample
data set includes shp, dbf and shx. How many other side car files are there?
Advanced: The use of FileDataStoreFinder allows us to work easily with files. The other way to do
things is with a map of connection parameters. This techniques gives us a little more control over
how we work with a shapefile and also allows us to connect to databases and web feature servers.
Important: GeoTools is an active open source project – you can quickly use maven to try out the
latest nightly build by changing your pom.xml file to use a SNAPSHOT release.
At the time of writing 34.x-SNAPSHOT is under active development.
When cutting and pasting GeoTools code examples use Control-Shift-O to organise imports (resolving any missing imports).
So what jars did maven actually use for the Quickstart application? Open up your pom.xml
and switch to the dependency hierarchy or dependency graph tabs to see
what is going on.
We will be making use of some of the project in greater depth in the remaining tutorials.
The first alternative to using eclipse to setup a maven project … is using a maven to setup an eclipse project.
The maven build tool also works directly on the command line; and includes a plugin for
generating eclipse .project and .classpath files used by Eclipse.
Unzip the file apache-maven-3.3.3-bin.zip to C:\java\apache-maven-3.3.3
You need to have a couple of environmental variables set for maven to work. Use
Control Panel ‣ System ‣ Advanced ‣ Environmental Variables to set the following System Variables:
You can now give Eclipse the background information it needs to talk to your “maven repository”
(maven downloaded something like 30 jars for you)
Return to Eclipse
Use Windows ‣ Preferences to open the Preference Dialog.
Using the tree on the left navigate to the Java > Build path > Classpath Variables preference
Page.
Add an M2_REPO classpath variable pointing to your “local repository”
PLATFORM
LOCAL REPOSITORY
Windows XP:
C:\DocumentsandSettings\Jody\.m2\repository
Windows:
C:\Users\Jody.m2\repository
Linux and Mac:
~/.m2/repository
We can now import your new project into eclipse using File ‣ Import
Choose Existing Projects into Workspace from the list, and press Next
Select the project you created: C:javatutorial
Press Finish to import your project
Navigate to the pom.xml file and double click to open it up.
We are going to start by defining the version number of GeoTools we wish to use. This workbook
was written for 34.0 although you may wish to try a newer version, or make use of a
nightly build by using something like 15-SNAPSHOT.
<dependencyManagement><dependencies><!-- Import the GeoTools BOM --><dependency><groupId>org.geotools</groupId><artifactId>gt-bom</artifactId><version>${gt.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.junit</groupId><artifactId>junit-bom</artifactId><version>5.11.0</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.geotools</groupId><artifactId>gt-shapefile</artifactId></dependency><dependency><groupId>org.geotools</groupId><artifactId>gt-swing</artifactId></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><scope>test</scope></dependency></dependencies>
Setup repositories to download GeoTools from:
</dependencyManagement><dependencies><dependency><groupId>org.geotools</groupId><artifactId>gt-shapefile</artifactId></dependency><dependency><groupId>org.geotools</groupId><artifactId>gt-swing</artifactId></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><scope>test</scope></dependency></dependencies><repositories><repository><id>osgeo</id><name>OSGeoReleaseRepository</name><url>https://repo.osgeo.org/repository/release/</url><snapshots><enabled>false</enabled></snapshots><releases><enabled>true</enabled></releases></repository><repository><id>osgeo-snapshot</id><name>OSGeoSnapshotRepository</name><url>https://repo.osgeo.org/repository/snapshot/</url><snapshots><enabled>true</enabled></snapshots><releases><enabled>false</enabled></releases></repository></repositories><build><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --><plugin><artifactId>maven-clean-plugin</artifactId><version>3.4.0</version></plugin><!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.3.1</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.13.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>3.3.0</version></plugin><plugin><artifactId>maven-jar-plugin</artifactId><version>3.4.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>3.1.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>3.1.2</version></plugin><!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --><plugin><artifactId>maven-site-plugin</artifactId><version>3.12.1</version></plugin><plugin><artifactId>maven-project-info-reports-plugin</artifactId><version>3.6.1</version></plugin></plugins></pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><release>17</release></configuration></plugin></plugins></build><reporting><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-javadoc-plugin</artifactId><version>3.11.2</version><configuration><release>17</release><!-- Force non-modular mode for projects without module-info.java --><legacyMode>true</legacyMode><!-- Specifies whether or not the version text is included.--><version>false</version><!-- Omit qualifying package name before class names in output.--><noqualifier>all</noqualifier><!-- Shuts off non-error and non-warning messages.--><quiet>true</quiet><!-- The maximum Java heap size to be used to run javadoc. --><maxmemory>${javadoc.maxHeapSize}</maxmemory><!-- Specifies the encoding name of the source files.--><encoding>UTF-8</encoding><!-- Set an additional parameter for the command line. --><additionalOptions>-keywords</additionalOptions><breakiterator>true</breakiterator><excludePackageNames>org.geotools.resources:org.geotools.maven:com:net.opengis:org.w3:javax:it.geosolutions</excludePackageNames><!-- Enables the Javadoc tool to interpret a simple, one-argument --><!-- custom block tag tagname in doc comments. Note: <placement/> --><!-- should be a combination of the letters Xaoptcmf. --><tags><tag><name>todo</name><placement>a</placement><head>TODO:</head></tag><tag><name>todo:</name><placement>a</placement><head>TODO:</head></tag><tag><name>TODO</name><placement>a</placement><head>TODO:</head></tag><tag><name>task</name><placement>tfmc</placement><head>TODO:</head></tag><tag><name>revisit</name><placement>tfmc</placement><head>TODO:</head></tag><tag><name>generated</name><placement>Xt</placement><head>Generated</head></tag><tag><name>UML</name><placement>a</placement><head>UML:</head></tag><tag><name>uml.property</name><placement>a</placement><head>UMLproperty:</head></tag><tag><name>generated</name><placement>X</placement></tag></tags><!-- Creates links to existing javadoc-generated --><!-- documentation of external referenced classes. --><links><link>https://docs.oracle.com/en/java/javase/17/docs/api/</link><link>https://javaee.github.io/javaee-spec/javadocs/</link><link>https://locationtech.github.io/jts/javadoc/</link></links></configuration><!-- only works with package javadoc:aggregate for use of META-INF/MANIFEST.MF Automatic-Module-Name --><reportSets><reportSet><id>aggregate</id><reports><report>aggregate</report></reports><inherited>false</inherited></reportSet><reportSet><id>default</id><reports><report>javadoc</report></reports></reportSet></reportSets></plugin></plugins></reporting><profiles><profile><id>qa</id><activation><property><name>qa</name></property></activation><properties><maven.pmd.plugin.version>3.24.0</maven.pmd.plugin.version><pmd.version>7.14.0</pmd.version><pom.fmt.action>sort</pom.fmt.action></properties><build><plugins><plugin><artifactId>maven-pmd-plugin</artifactId><version>${maven.pmd.plugin.version}</version><executions><execution><goals><goal>check</goal></goals></execution></executions><dependencies><dependency><groupId>net.sourceforge.pmd</groupId><artifactId>pmd-core</artifactId><version>${pmd.version}</version></dependency><dependency><groupId>net.sourceforge.pmd</groupId><artifactId>pmd-java</artifactId><version>${pmd.version}</version></dependency></dependencies><configuration><linkXRef>false</linkXRef><rulesets><ruleset>${project.basedir}/../../build/qa/pmd-ruleset.xml</ruleset><ruleset>${project.basedir}/../../build/qa/pmd-junit-ruleset.xml</ruleset></rulesets><failurePriority>3</failurePriority><minimumPriority>3</minimumPriority><verbose>true</verbose><printFailingErrors>true</printFailingErrors><includeTests>true</includeTests></configuration></plugin><plugin><groupId>com.github.spotbugs</groupId><artifactId>spotbugs-maven-plugin</artifactId><version>4.7.3.5</version><executions><execution><goals><goal>check</goal></goals></execution></executions><configuration><effort>More</effort><!-- threshold>High</threshold --><xmlOutput>true</xmlOutput><maxRank>15</maxRank><excludeFilterFile>${project.basedir}/../../build/qa/spotbugs-exclude.xml</excludeFilterFile><jvmArgs>-XX:+TieredCompilation-XX:TieredStopAtLevel=1</jvmArgs><compilerArgscombine.children="append"><arg>-Xlint:${lint}</arg></compilerArgs></configuration></plugin><plugin><artifactId>maven-checkstyle-plugin</artifactId><version>3.6.0</version><executions><execution><goals><goal>check</goal></goals></execution></executions><dependencies><dependency><groupId>com.puppycrawl.tools</groupId><artifactId>checkstyle</artifactId><version>9.3</version></dependency></dependencies><configuration><logViolationsToConsole>true</logViolationsToConsole><configLocation>${project.basedir}/../..//build/qa/checkstyle-cc0.xml</configLocation></configuration></plugin><plugin><groupId>com.github.ekryd.sortpom</groupId><artifactId>sortpom-maven-plugin</artifactId><version>2.15.0</version><executions><execution><phase>verify</phase><goals><goal>${pom.fmt.action}</goal></goals></execution></executions><configuration><skip>true</skip></configuration></plugin></plugins></build></profile></profiles>
Note
Note the snapshot repository above is only required if you are using a nightly build (such as 34-SNAPSHOT)
GeoTools now requires Java 17 - you need to tell Maven to use the 17 source level.
<build><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --><plugin><artifactId>maven-clean-plugin</artifactId><version>3.4.0</version></plugin><!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.3.1</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.13.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>3.3.0</version></plugin><plugin><artifactId>maven-jar-plugin</artifactId><version>3.4.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>3.1.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>3.1.2</version></plugin><!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --><plugin><artifactId>maven-site-plugin</artifactId><version>3.12.1</version></plugin><plugin><artifactId>maven-project-info-reports-plugin</artifactId><version>3.6.1</version></plugin></plugins></pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><release>17</release></configuration></plugin></plugins></build><reporting><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-javadoc-plugin</artifactId><version>3.11.2</version><configuration><release>17</release><!-- Force non-modular mode for projects without module-info.java --><legacyMode>true</legacyMode><!-- Specifies whether or not the version text is included.--><version>false</version><!-- Omit qualifying package name before class names in output.--><noqualifier>all</noqualifier><!-- Shuts off non-error and non-warning messages.--><quiet>true</quiet><!-- The maximum Java heap size to be used to run javadoc. --><maxmemory>${javadoc.maxHeapSize}</maxmemory><!-- Specifies the encoding name of the source files.--><encoding>UTF-8</encoding><!-- Set an additional parameter for the command line. --><additionalOptions>-keywords</additionalOptions><breakiterator>true</breakiterator><excludePackageNames>org.geotools.resources:org.geotools.maven:com:net.opengis:org.w3:javax:it.geosolutions</excludePackageNames><!-- Enables the Javadoc tool to interpret a simple, one-argument --><!-- custom block tag tagname in doc comments. Note: <placement/> --><!-- should be a combination of the letters Xaoptcmf. --><tags><tag><name>todo</name><placement>a</placement><head>TODO:</head></tag><tag><name>todo:</name><placement>a</placement><head>TODO:</head></tag><tag><name>TODO</name><placement>a</placement><head>TODO:</head></tag><tag><name>task</name><placement>tfmc</placement><head>TODO:</head></tag><tag><name>revisit</name><placement>tfmc</placement><head>TODO:</head></tag><tag><name>generated</name><placement>Xt</placement><head>Generated</head></tag><tag><name>UML</name><placement>a</placement><head>UML:</head></tag><tag><name>uml.property</name><placement>a</placement><head>UMLproperty:</head></tag><tag><name>generated</name><placement>X</placement></tag></tags><!-- Creates links to existing javadoc-generated --><!-- documentation of external referenced classes. --><links><link>https://docs.oracle.com/en/java/javase/17/docs/api/</link><link>https://javaee.github.io/javaee-spec/javadocs/</link><link>https://locationtech.github.io/jts/javadoc/</link></links></configuration><!-- only works with package javadoc:aggregate for use of META-INF/MANIFEST.MF Automatic-Module-Name --><reportSets><reportSet><id>aggregate</id><reports><report>aggregate</report></reports><inherited>false</inherited></reportSet><reportSet><id>default</id><reports><report>javadoc</report></reports></reportSet></reportSets></plugin></plugins></reporting><profiles><profile><id>qa</id><activation><property><name>qa</name></property></activation><properties><maven.pmd.plugin.version>3.24.0</maven.pmd.plugin.version><pmd.version>7.14.0</pmd.version><pom.fmt.action>sort</pom.fmt.action></properties><build><plugins><plugin><artifactId>maven-pmd-plugin</artifactId><version>${maven.pmd.plugin.version}</version><executions><execution><goals><goal>check</goal></goals></execution></executions><dependencies><dependency><groupId>net.sourceforge.pmd</groupId><artifactId>pmd-core</artifactId><version>${pmd.version}</version></dependency><dependency><groupId>net.sourceforge.pmd</groupId><artifactId>pmd-java</artifactId><version>${pmd.version}</version></dependency></dependencies><configuration><linkXRef>false</linkXRef><rulesets><ruleset>${project.basedir}/../../build/qa/pmd-ruleset.xml</ruleset><ruleset>${project.basedir}/../../build/qa/pmd-junit-ruleset.xml</ruleset></rulesets><failurePriority>3</failurePriority><minimumPriority>3</minimumPriority><verbose>true</verbose><printFailingErrors>true</printFailingErrors><includeTests>true</includeTests></configuration></plugin><plugin><groupId>com.github.spotbugs</groupId><artifactId>spotbugs-maven-plugin</artifactId><version>4.7.3.5</version><executions><execution><goals><goal>check</goal></goals></execution></executions><configuration><effort>More</effort><!-- threshold>High</threshold --><xmlOutput>true</xmlOutput><maxRank>15</maxRank><excludeFilterFile>${project.basedir}/../../build/qa/spotbugs-exclude.xml</excludeFilterFile><jvmArgs>-XX:+TieredCompilation-XX:TieredStopAtLevel=1</jvmArgs><compilerArgscombine.children="append"><arg>-Xlint:${lint}</arg></compilerArgs></configuration></plugin><plugin><artifactId>maven-checkstyle-plugin</artifactId><version>3.6.0</version><executions><execution><goals><goal>check</goal></goals></execution></executions><dependencies><dependency><groupId>com.puppycrawl.tools</groupId><artifactId>checkstyle</artifactId><version>9.3</version></dependency></dependencies><configuration><logViolationsToConsole>true</logViolationsToConsole><configLocation>${project.basedir}/../..//build/qa/checkstyle-cc0.xml</configLocation></configuration></plugin><plugin><groupId>com.github.ekryd.sortpom</groupId><artifactId>sortpom-maven-plugin</artifactId><version>2.15.0</version><executions><execution><phase>verify</phase><goals><goal>${pom.fmt.action}</goal></goals></execution></executions><configuration><skip>true</skip></configuration></plugin></plugins></build></profile></profiles>
Here is what the completed pom.xml looks like:
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.geotools.tutorial</groupId><artifactId>quickstart</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>GeoToolsQuickstart</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.release>17</maven.compiler.release><gt.version>34-SNAPSHOT</gt.version><maven.deploy.skip>true</maven.deploy.skip></properties><dependencyManagement><dependencies><!-- Import the GeoTools BOM --><dependency><groupId>org.geotools</groupId><artifactId>gt-bom</artifactId><version>${gt.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.junit</groupId><artifactId>junit-bom</artifactId><version>5.11.0</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.geotools</groupId><artifactId>gt-shapefile</artifactId></dependency><dependency><groupId>org.geotools</groupId><artifactId>gt-swing</artifactId></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><scope>test</scope></dependency></dependencies><repositories><repository><id>osgeo</id><name>OSGeoReleaseRepository</name><url>https://repo.osgeo.org/repository/release/</url><snapshots><enabled>false</enabled></snapshots><releases><enabled>true</enabled></releases></repository><repository><id>osgeo-snapshot</id><name>OSGeoSnapshotRepository</name><url>https://repo.osgeo.org/repository/snapshot/</url><snapshots><enabled>true</enabled></snapshots><releases><enabled>false</enabled></releases></repository></repositories><build><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --><plugin><artifactId>maven-clean-plugin</artifactId><version>3.4.0</version></plugin><!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.3.1</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.13.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>3.3.0</version></plugin><plugin><artifactId>maven-jar-plugin</artifactId><version>3.4.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>3.1.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>3.1.2</version></plugin><!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --><plugin><artifactId>maven-site-plugin</artifactId><version>3.12.1</version></plugin><plugin><artifactId>maven-project-info-reports-plugin</artifactId><version>3.6.1</version></plugin></plugins></pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><release>17</release></configuration></plugin></plugins></build><reporting><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-javadoc-plugin</artifactId><version>3.11.2</version><configuration><release>17</release><!-- Force non-modular mode for projects without module-info.java --><legacyMode>true</legacyMode><!-- Specifies whether or not the version text is included.--><version>false</version><!-- Omit qualifying package name before class names in output.--><noqualifier>all</noqualifier><!-- Shuts off non-error and non-warning messages.--><quiet>true</quiet><!-- The maximum Java heap size to be used to run javadoc. --><maxmemory>${javadoc.maxHeapSize}</maxmemory><!-- Specifies the encoding name of the source files.--><encoding>UTF-8</encoding><!-- Set an additional parameter for the command line. --><additionalOptions>-keywords</additionalOptions><breakiterator>true</breakiterator><excludePackageNames>org.geotools.resources:org.geotools.maven:com:net.opengis:org.w3:javax:it.geosolutions</excludePackageNames><!-- Enables the Javadoc tool to interpret a simple, one-argument --><!-- custom block tag tagname in doc comments. Note: <placement/> --><!-- should be a combination of the letters Xaoptcmf. --><tags><tag><name>todo</name><placement>a</placement><head>TODO:</head></tag><tag><name>todo:</name><placement>a</placement><head>TODO:</head></tag><tag><name>TODO</name><placement>a</placement><head>TODO:</head></tag><tag><name>task</name><placement>tfmc</placement><head>TODO:</head></tag><tag><name>revisit</name><placement>tfmc</placement><head>TODO:</head></tag><tag><name>generated</name><placement>Xt</placement><head>Generated</head></tag><tag><name>UML</name><placement>a</placement><head>UML:</head></tag><tag><name>uml.property</name><placement>a</placement><head>UMLproperty:</head></tag><tag><name>generated</name><placement>X</placement></tag></tags><!-- Creates links to existing javadoc-generated --><!-- documentation of external referenced classes. --><links><link>https://docs.oracle.com/en/java/javase/17/docs/api/</link><link>https://javaee.github.io/javaee-spec/javadocs/</link><link>https://locationtech.github.io/jts/javadoc/</link></links></configuration><!-- only works with package javadoc:aggregate for use of META-INF/MANIFEST.MF Automatic-Module-Name --><reportSets><reportSet><id>aggregate</id><reports><report>aggregate</report></reports><inherited>false</inherited></reportSet><reportSet><id>default</id><reports><report>javadoc</report></reports></reportSet></reportSets></plugin></plugins></reporting><profiles><profile><id>qa</id><activation><property><name>qa</name></property></activation><properties><maven.pmd.plugin.version>3.24.0</maven.pmd.plugin.version><pmd.version>7.14.0</pmd.version><pom.fmt.action>sort</pom.fmt.action></properties><build><plugins><plugin><artifactId>maven-pmd-plugin</artifactId><version>${maven.pmd.plugin.version}</version><executions><execution><goals><goal>check</goal></goals></execution></executions><dependencies><dependency><groupId>net.sourceforge.pmd</groupId><artifactId>pmd-core</artifactId><version>${pmd.version}</version></dependency><dependency><groupId>net.sourceforge.pmd</groupId><artifactId>pmd-java</artifactId><version>${pmd.version}</version></dependency></dependencies><configuration><linkXRef>false</linkXRef><rulesets><ruleset>${project.basedir}/../../build/qa/pmd-ruleset.xml</ruleset><ruleset>${project.basedir}/../../build/qa/pmd-junit-ruleset.xml</ruleset></rulesets><failurePriority>3</failurePriority><minimumPriority>3</minimumPriority><verbose>true</verbose><printFailingErrors>true</printFailingErrors><includeTests>true</includeTests></configuration></plugin><plugin><groupId>com.github.spotbugs</groupId><artifactId>spotbugs-maven-plugin</artifactId><version>4.7.3.5</version><executions><execution><goals><goal>check</goal></goals></execution></executions><configuration><effort>More</effort><!-- threshold>High</threshold --><xmlOutput>true</xmlOutput><maxRank>15</maxRank><excludeFilterFile>${project.basedir}/../../build/qa/spotbugs-exclude.xml</excludeFilterFile><jvmArgs>-XX:+TieredCompilation-XX:TieredStopAtLevel=1</jvmArgs><compilerArgscombine.children="append"><arg>-Xlint:${lint}</arg></compilerArgs></configuration></plugin><plugin><artifactId>maven-checkstyle-plugin</artifactId><version>3.6.0</version><executions><execution><goals><goal>check</goal></goals></execution></executions><dependencies><dependency><groupId>com.puppycrawl.tools</groupId><artifactId>checkstyle</artifactId><version>9.3</version></dependency></dependencies><configuration><logViolationsToConsole>true</logViolationsToConsole><configLocation>${project.basedir}/../..//build/qa/checkstyle-cc0.xml</configLocation></configuration></plugin><plugin><groupId>com.github.ekryd.sortpom</groupId><artifactId>sortpom-maven-plugin</artifactId><version>2.15.0</version><executions><execution><phase>verify</phase><goals><goal>${pom.fmt.action}</goal></goals></execution></executions><configuration><skip>true</skip></configuration></plugin></plugins></build></profile></profiles></project>
Recommend cutting and pasting the above to avoid mistakes when typing
You may also download pom.xml, if this opens in your browser use Save As to save to disk.
Return to the command line and maven to download the required jars and tell eclipse about it:
C:\java\example>mvneclipse:eclipse
Return to eclipse and select the project folder. Refresh your project using the context menu
or by pressing F5. If you open up referenced libraries you will see the required jars
listed.
Using this technique of running mvneclipse:eclipse and refreshing in eclipse you can proceed
through all the tutorial examples.
We can also download the GeoTools project bundle from source forge and set up our project to use
them. Please follow these steps carefully as not all the GeoTools jars can be used at the same
time.