Fundamentals 8 min read

Getting Started with Geotools: Load and Visualize Shapefiles Quickly

This guide introduces the Java‑based Geotools library, explains how to set up a Spring Boot project with Maven dependencies, and walks through a complete quickstart example that loads a shapefile, renders it with a map frame, and clarifies the required .shp, .shx, and .dbf components.

The Dominant Programmer
The Dominant Programmer
The Dominant Programmer
Getting Started with Geotools: Load and Visualize Shapefiles Quickly

Geotools is a Java library that provides a rich set of classes and methods for processing spatial data and implements the OGC (Open Geospatial Consortium) standards, making it a core component for many open‑source GIS tools such as uDig and GeoServer.

The official site (https://geotools.org/) offers a quickstart guide; instead of the Maven quickstart shown there, the author creates a Spring Boot project via Spring Initializr, adds the basic web starter, and prepares the project for Geotools.

In pom.xml the following sections are added:

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <geotools.version>26-SNAPSHOT</geotools.version>
</properties>

<repositories>
    <repository>
        <id>osgeo</id>
        <name>OSGeo Release Repository</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>OSGeo Snapshot Repository</name>
        <url>https://repo.osgeo.org/repository/snapshot/</url>
        <snapshots><enabled>true</enabled></snapshots>
        <releases><enabled>false</enabled></releases>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </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>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

After Maven resolves the dependencies, a Java class named Quickstart is created with the following code:

package com.badao.geotoolsdemo;

import java.io.File;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;

public class Quickstart {
    public static void main(String[] args) throws Exception {
        // Open a file chooser for shapefiles
        File file = JFileDataStoreChooser.showOpenFile("shp", null);
        if (file == null) {
            return;
        }
        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        SimpleFeatureSource featureSource = store.getFeatureSource();

        // Create map content and add the shapefile layer
        MapContent map = new MapContent();
        map.setTitle("Quickstart");
        Style style = SLD.createSimpleStyle(featureSource.getSchema());
        Layer layer = new FeatureLayer(featureSource, style);
        map.addLayer(layer);

        // Display the map
        JMapFrame.showMap(map);
    }
}

Running the main method opens a dialog to select a .shp file. The article explains that a shapefile consists of three mandatory files: .shp (geometry), .shx (spatial index), and .dbf (attribute data). A sample Chinese provincial boundary shapefile is provided (https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/15785012), with the caution that file names and paths must not contain Chinese characters.

After selecting a valid shapefile, the map window displays the vector data, demonstrating a complete end‑to‑end workflow for loading and visualizing shapefiles with Geotools.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaMavenspring-bootGISGeotoolsShapefile
The Dominant Programmer
Written by

The Dominant Programmer

Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.