Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this article, we’ll go through the basics of the GeoTools open source Java library – for working with geospatial data. This library provides compliant methods for implementing Geographic Information Systems (GIS) and implements and supports many Open Geospatial Consortium (OGC) standards.

As the OGC develops new standards, they’re implemented by the GeoTools, which makes it quite handy for geospatial work.

2. Dependencies

We’ll need to add the GeoTools dependencies to our pom.xml file. Since these dependencies are not hosted on Maven Central, we also need to declare their repositories so that Maven can download them:

<repositories>
    <repository>
        <id>osgeo</id>
        <name>Open Source Geospatial Foundation Repository</name>
        <url>http://download.osgeo.org/webdav/geotools/</url>
    </repository>
    <repository>
        <id>opengeo</id>
        <name>OpenGeo Maven Repository</name>
        <url>http://repo.opengeo.org</url>
    </repository>
</repositories>

After that, we can add our dependencies:

<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-shapefile</artifactId>
    <version>28.1</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-epsg-hsql</artifactId>
    <version>28.1</version>
</dependency>

3. GIS and Shapefiles

To have any practical use of the GeoTools library, we’ll need to know a few things about geographic information systems and shapefiles.

3.1. GIS

If we want to work with geographical data, we’ll need a geographic information system (GIS). This system can be used to present, capture, store, manipulate, analyze, or manage geographical data.

Some portion of the geographical data is spatial – it references concrete locations on earth. The spatial data is usually accompanied by the attribute data. Attribute data can be any additional information about each of the spatial features.

An example of geographical data would be cities. The actual location of the cities is the spatial data. Additional data such as the city name and population would make up the attribute data.

3.2. Shapefiles

Different formats are available for working with geospatial data. Raster and vector are the two primary data types.

In this article, we’re going to see how to work with the vector data type. This data type can be represented as points, lines, or polygons.

To store vector data in a file, we will use a shapefile. This file format is used when working with the geospatial vector data type. Also, it is compatible with a wide range of GIS software.

We can use GeoTools to add features like cities, schools, and landmarks to shapefiles.

4. Creating Features

The GeoTools documentation specifies that a feature is anything that can be drawn on a map, like a city or some landmark. And, as we mentioned, once created, features can then be saved into files called shapefiles.

4.1. Keeping Geospatial Data

Before creating a feature, we need to know its geospatial data or the longitude and latitude coordinates of its location on earth. As for attribute data, we need to know the name of the feature we want to create.

This information can be found on the web. Some sites like simplemaps.com or maxmind.com offer free databases with geospatial data.

When we know the longitude and latitude of a city, we can easily store them in some object. We can use a Map object that will hold the city name and a list of its coordinates.

Let’s create a helper method to ease the storing of data inside our Map object:

private static void addToLocationMap(
  String name,
  double lat,
  double lng,
  Map<String, List<Double>> locations) {
    List<Double> coordinates = new ArrayList<>();

    coordinates.add(lat);
    coordinates.add(lng);
    locations.put(name, coordinates);
}

Now let’s fill in our Map object:

Map<String, List<Double>> locations = new HashMap<>();

addToLocationMap("Bangkok", 13.752222, 100.493889, locations);
addToLocationMap("New York", 53.083333, -0.15, locations);
addToLocationMap("Cape Town", -33.925278, 18.423889, locations);
addToLocationMap("Sydney", -33.859972, 151.211111, locations);
addToLocationMap("Ottawa", 45.420833, -75.69, locations);
addToLocationMap("Cairo", 30.07708, 31.285909, locations);

If we download some CSV database that contains this data, we can easily create a reader to retrieve the data instead of keeping it in an object like here.

4.2. Defining Feature Types

So, now we have a map of cities. To be able to create features with this data, we’ll need to define their type first. GeoTools offers two ways of defining feature types.

One way is to use the createType method of the DataUtilites class:

SimpleFeatureType TYPE = DataUtilities.createType(
  "Location", "location:Point:srid=4326," + "name:String");

Another way is to use a SimpleFeatureTypeBuilder, which provides more flexibility. For example, we can set the Coordinate Reference System for the type, and we can set a maximum length for the name field:

SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName("Location");
builder.setCRS(DefaultGeographicCRS.WGS84);

builder
  .add("Location", Point.class);
  .length(15)
  .add("Name", String.class);

SimpleFeatureType CITY = builder.buildFeatureType();

Both types store the same information. The location of the city is stored as a Point, and the name of the city is stored as a String.

You probably noticed that the type variables TYPE and CITY are named with all capital letters, like constants. Type variables should be treated as final variables and should not be changed after they are created, so this way of naming can be used to indicate just that.

4.3. Feature Creation and Feature Collections

Once we have the feature type defined and we have an object that has the data needed to create features, we can start creating them with their builder.

Let’s instantiate a SimpleFeatureBuilder providing our feature type:

SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(CITY);

We’ll also need a collection to store all the created feature objects:

DefaultFeatureCollection collection = new DefaultFeatureCollection();

Since we declared in our feature type to hold a Point for the location, we’ll need to create points for our cities based on their coordinates. We can do this with the GeoTools’s JTSGeometryFactoryFinder:

GeometryFactory geometryFactory
  = JTSFactoryFinder.getGeometryFactory(null);

Note that we can also use other Geometry classes like Line and Polygon.

We can create a function that will help us put features in the collection:

private static Function<Map.Entry<String, List<Double>>, SimpleFeature>
  toFeature(SimpleFeatureType CITY, GeometryFactory geometryFactory) {
    return location -> {
        Point point = geometryFactory.createPoint(
           new Coordinate(location.getValue()
             .get(0), location.getValue().get(1)));

        SimpleFeatureBuilder featureBuilder
          = new SimpleFeatureBuilder(CITY);
        featureBuilder.add(point);
        featureBuilder.add(location.getKey());
        return featureBuilder.buildFeature(null);
    };
}

Once we have the builder and the collection, by using the previously created function, we can create features and store them in our collection:

locations.entrySet().stream()
  .map(toFeature(CITY, geometryFactory))
  .forEach(collection::add);

The collection now contains all the features created based on our Map object that held the geospatial data.

5. Creating a DataStore

GeoTools contains a DataStore API that is used to represent a source of geospatial data. This source can be a file, a database, or some service that returns data. We can use a DataStoreFactory to create our DataStore, which will contain our features.

Let’s set the file that will contain the features:

File shapeFile = new File(
  new File(".").getAbsolutePath() + "shapefile.shp");

Now, let’s set the parameters that we are going to use to tell the DataStoreFactory which file to use and indicate that we need to store a spatial index when we create our DataStore:

Map<String, Serializable> params = new HashMap<>();
params.put("url", shapeFile.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);

Let’s create the DataStoreFactory using the parameters we just created, and use that factory to create the DataStore:

ShapefileDataStoreFactory dataStoreFactory
  = new ShapefileDataStoreFactory();

ShapefileDataStore dataStore 
  = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
dataStore.createSchema(CITY);

6. Writing to a Shapefile

The last step that we need to do is to write our data to a shapefile. To do this safely, we are going to use the Transaction interface that is a part of the GeoTools API.

This interface gives us the possibility to easily commit our the changes to the file. It also provides a way to perform a rollback of the unsuccessful changes if some problem occurs while writing to the file:

Transaction transaction = new DefaultTransaction("create");

String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource
  = dataStore.getFeatureSource(typeName);

if (featureSource instanceof SimpleFeatureStore) {
    SimpleFeatureStore featureStore
      = (SimpleFeatureStore) featureSource;

    featureStore.setTransaction(transaction);
    try {
        featureStore.addFeatures(collection);
        transaction.commit();

    } catch (Exception problem) {
        transaction.rollback();
    } finally {
        transaction.close();
    }
}

The SimpleFeatureSource is used to read features, and the SimpleFeatureStore is used for read/write access. It is specified in the GeoTools documentation that using the instanceof method for checking if we can write to the file is the right way to do so.

This shapefile can later be opened with any GIS viewer that has shapefile support.

7. Conclusion

In this article, we saw how we can make use of the GeoTools library to do some very interesting geo-spatial work.

Although the example was simple, it can be extended and used for creating rich shapefiles for various purposes.

We should have in mind that GeoTools is a vibrant library, and this article just serves as a basic introduction to the library. Also, GeoTools is not limited to creating vector data types only – it can also be used to create or to work with raster data types.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW