Table Of Contents

Previous topic

Map Style Tutorial

This Page

DataStore Developers Guide


The GeoTools2 project strives to support as many geographical data formats as possible because getting data into the GeoTools2 API allows access to a vast suite of tools. In order to transform a data format into the GeoTools2 feature representation one must write an implementation of the DataStore interface.

Once a DataStore implementation is written, any data written in that format becomes available not only for GeoTools2 users, but also for projects built on top of GeoTools2.

Writing a new DataStore for GeoTools2 is one of the best ways to get involved in the project, as writing it will make clear many of the core concepts of the API. Finally, the modular nature of GeoTools2 allows new DataStores to quickly become part of the next release, so that new formats are quickly available to all GeoTools2 users.

The DataStore interface borrows most of its concepts and some of its syntax from the OpenGIS Consortium (OGC) Web Feature Server Specification:

  • Feature - atomic unit of geographic information
  • FeatureType - keeps track of what attributes each Feature can hold
  • FeatureID - a unique id associated with each Feature (must start with a non-numeric character)


Earlier was made a bit of code to read in a comma seperated value file; and produce a feature collection (which we could save out using the shapefile datastore class).

This time out we are going to make a DataStore.

Here was the sample file we used:

  1. Create a text file location.csv and copy and paste the following locations into it:

    46.066667, 11.116667, Trento, 140
    44.9441, -93.0852, St Paul, 125
    13.752222, 100.493889, Bangkok, 150
    45.420833, -75.69, Ottawa, 200
    44.9801, -93.251867, Minneapolis, 350
    46.519833, 6.6335, Lausanne, 560
    48.428611, -123.365556, Victoria, 721
    -33.925278, 18.423889, Cape Town, 550
    -33.859972, 151.211111, Sydney, 436
  2. Or download locations.csv.


As you walk through this tutorial, please remember the following:

  • FeatureID or FID - uniquely defines a Feature (row in our csv file)
  • FeatureType - same as the name of the .csv file (ie. “locations” for locations.csv)
  • DataStore - assess all the FeatureTypes (.csv files) in a directory
  • FeatureType or Schema - names of the columns and their types

JavaCSV Reader

To read csv files this time out we are going to make use of the Java CSV Reader project.

Time to create a new project making use of this library:

# Create a csv project using maven # Use the following maven dependencies


# Or download pom.xml

Creating CSVDataStore

The first step is to create a basic DataStore that only supports feature extraction. We will read data from a csv file into the GeoTools feature model.

To implement a DataStore we will subclass ContentDataStore. This is a helpful base class for making new kinds of content available to GeoTools. The GeoTools library works with an interaction model very similar to a database - with transactions and locks. ContentDataStore is going to handle all of this for us - as long as we can teach it how to access our content.

ContentDataStore requires us to implement the following two methods:

  • createTypeNames() - name of all the different kinds of content (tables or types). In a CSV file we will only have one kind of content
  • createFeatureSource(ContentEntry entry)

The class ContentEntry is a bit of a scratch pad used to keep track of things for each type.

Initially we are going to make a read-only datastore accessing CSV content:

# To begin create the file CSVDataStore extending ContentDataStore

package org.geotools.tutorial.datastore;

import java.util.Collections;
import java.util.List;

import org.geotools.feature.NameImpl;
import org.opengis.feature.type.Name;

import com.csvreader.CsvReader;

public class CSVDataStore extends ContentDataStore {

# We are going to be working with a single CSV file

    File file;
    public CSVDataStore( File file ){
        this.file = file;

Listing TypeNames

A DataStore may provide access to several different types of information. The method createTypeNames provides a list of the available types. This is called once; and then the same list is returned by ContentDataStore.getTypeNames() each time. (This allows you to do some real work; such as connecting to a web service or parsing a large file, without worrying about doing it many times).

For our purposes this list will be the name of the csv file.

# We can now implement createTypeNames() returning a the filename

    protected List<Name> createTypeNames() throws IOException {
        String name = file.getName();
        name = name.substring(0, name.lastIndexOf('.'));
        Name typeName = new NameImpl( name );
        return Collections.singletonList(typeName);