The DataStore interface is general to all of the implemented DataStores. The Data access basic described how to use a DataStore, using a ShapefileDataStore as an example. The following document extends this to using a PostGISDataStore created with a DataStoreFinder.
Creating a PostGISDataStore
While the Data access basic uses the ShapefileDataStore constructor to create a DataStore, a more general method is to use the DataStoreFinder. The DataStoreFinder uses the interface for pluggable DataStores, allowing you to use different DataStores just by changing a few parameters. See DataStoreFinder Parameters for a list of available parameters for other DataStores. The following code shows how to use the DataStoreFinder to create a PostGISDataStore.
Once the PostGISDataStore is created, you can use it like any other DataStore.
SpearfishSample using PostGIS
The attached file SpearfishPostGIS.java is a modified version of the Maps and styles's SpearfishSample.java that uses data from a PostGIS database. The attached file spearfish_data_pg.sql.zip contains all of the sample data as zipped SQL insert statements.
To create the PostGIS database with the spearfish data, use the following commands. These create the database 'spearfish,' owned by 'postgres.'
To run and compile SpearfishPostGIS, you will need the following jars on your classpath:
- vecmath-1.2.1.jar - also part of java 3D
- a recent postgresql jdbc driver from http://jdbc.postgresql.org
This code was tested and works with the geotools b5 release and geotools trunk (requires wkb4j-1.0-RC1.jar and vecmath-1.3.jar).