Added by jgarnett, last edited by jgarnett on Jun 25, 2007  (view change)

Labels

 
(None)

Code leveraging GeoTools usually works against the Java interfaces only but interfaces in Java don't provide any way to create actual objects. GeoTools therefore provides Factories which are concrete implementations through whose interface users can create actual GeoTools objects such as Features, Styles, Filters, DataStores, and MathTransforms.

This page explains how to use the FactoryFinder system to find the appropriate Factory implementations to instantiate particular objects. The next page will show alternative approaches to obtain and use a particular implementation of an appropriate DataStore interface; those examples show the utility of the FactoryFinder system.

Creating Anything

To create an implementation (and not get your hands dirty by choosing an explicit class) you are forced to use a factory in Java (other languages allow the definition of constructors as part of the interface).

Here is a quick example showing how to create and use a Filter:

FilterFactory2 factory = CommonFactoryFinder.getFilterFactory2( null );
Filter filter = factory.less( factory.property( "size" ), factory.literal( 2 ) );

if( filter.evaulate( feature )){
   System.out.println( feature.getId() + " had a size larger than 2" );
}

In this example we:

  1. Found an object which implements the GeoAPI FilterFactory2 interface using a FactoryFinder (CommonFactoryFinder gave us FilterFactoryImpl in this case)
  2. Used the Factory to produce our Instance (FilterFactory2 less(..) method was used to create a PropertyIsLessThan Filter)
  3. Used the instance to accomplish something (we used the filter to check the size of a Feature )

Where to get a Factory

It really depends on your application, depending on your environment you may locate a factory by either:

  • Using a GeoTools "FactoryFinder" - most of them are available in main module, they will hunt you down a implementation on the CLASSPATH for you
  • Use of "Container" - you may find an implementation provided as part of your application container (especially for a Java EE application). You can take this approach in normal applications with a container implementation like Spring, or PicoContainer
  • Use of "JNDI" - your application may also store an implementation in JNDI (this approach is often used to locate a DataSource in a JEE application)
  • Direct use of a known factory. You can always create a new Factory yourself and make use of it to create interfaces.
  • Direct use of an implementation. You may decide to duck the factory game completely and make use of a specific implementation using new.

These examples will usually use a factory finder of some sort. For the scary details please review the How to Find a Factory page.

Finding a FactoryFinder

Ideally, we would "follow" a naming convention where each major layer of the library has a single "mega" factory finder that can locate everything you care about and often has helper methods so you can quickly perform common searches. For example, for the Filter module, we have:

Interface Factory FactoryFinder
Filter FilterFactory FilterFactoryFinder

Unfortunately, we have not been consistent following this naming convention (too many cooks in the kitchen I guess). Each major layer of the library did end up with a "mega" factory finder that chains to all the other factory finders in the sub-packages but the names we chose were inconsistent, and some of the classes are current deprecated.

The names as they currently stand, including some deprecated classes (indicated with strikethrough):

Module FactoryFinders Description
Data DataAccessFinder experiment in unified access for vector, raster, pojo
Main CommonFactoryFinder Common lookup up for factories for filter, style, functions, etc..
  FilterFactoryFinder rolled into common factory finder
  FunctionFinder rolled into common factory finder
  DataStoreFinder access to vector (ie feature) data
  FileDataStoreFinder access to just file based feature data
  FactoryFinder used to find JTS GeometryFactory and PercisionModel
  StyleFactoryFinder rolled into common factory finder
API ServiceFinder Both vector and raster services for the catalog
Coverage GridFormatFinder Locate additional raster formats
  FactoryFinder This should be renamed to prevent confusion
Referencing ReferencingFactoryFinder Used to locate referencing factories
  FactoryFinder This has been renamed to prevent confusion
  IdentifiedObjectFinder