org.geotools.data.postgis
Class PostgisDataStore

java.lang.Object
  extended byorg.geotools.data.jdbc.JDBC1DataStore
      extended byorg.geotools.data.jdbc.JDBC2DataStore
          extended byorg.geotools.data.jdbc.JDBCDataStore
              extended byorg.geotools.data.postgis.PostgisDataStore
All Implemented Interfaces:
DataStore

public class PostgisDataStore
extends JDBCDataStore
implements DataStore

Postgis DataStore implementation.

This datastore by default will read/write geometries in WKT format.
Optionally use of WKB can be turned on, in which case you may want to turn on also the use of the bytea function, that fasten the data trasfer, but that it's available only from version 0.7.2 onwards.

Version:
$Id: PostgisDataStore.java 18021 2006-02-14 20:36:18Z jdeolive $
Author:
Chris Holmes, TOPP, Andrea Aime, Paolo Rizzi
Task:
REVISIT: So Paolo Rizzi has a number of improvements in http://jira.codehuas.org/browse/GEOT-379 I rolled in a few of them, but some beg more fundamental questions - like the use of primary keys - in the geotools model. See the issue for a bit more discussion, and I will attempt to write my thoughts up on wiki soon. -ch

Field Summary
protected  boolean byteaEnabled
          If true, the bytea function will be used to optimize even further data loading when using WKB format
protected  boolean byteaWKB
          postgis 1.0 changed the way WKB is handled, this needs to be set if version >1.
protected static com.vividsolutions.jts.geom.GeometryFactory geometryFactory
          Factory for producing geometries (from JTS).
protected static com.vividsolutions.jts.io.WKTReader geometryReader
          Well Known Text reader (from JTS).
protected static java.util.logging.Logger LOGGER
          The logger for the postgis module.
protected  boolean looseBbox
          If true then the bounding box filters will use the && postgis operator, which uses the spatial index and performs against the envelope of the geom, leading to greater speed and slightly less accuracy.
 int OPTIMIZE_MODE
          Current optimize mode
static int OPTIMIZE_SAFE
          OPTIMIZE constants
static int OPTIMIZE_SQL
           
protected  boolean useGeos
          Enables the use of geos operators
protected  boolean WKBEnabled
          If true, WKB format is used instead of WKT
 
Fields inherited from class org.geotools.data.jdbc.JDBC2DataStore
connectionPool
 
Fields inherited from class org.geotools.data.jdbc.JDBC1DataStore
allowWriteOnVolatileFIDs, config, listenerManager, sqlNameEscape, TYPE_MAPPINGS, typeHandler
 
Constructor Summary
protected PostgisDataStore(ConnectionPool connPool)
           
  PostgisDataStore(ConnectionPool connectionPool, JDBCDataStoreConfig config, int optimizeMode)
           
protected PostgisDataStore(ConnectionPool connPool, java.lang.String namespace)
           
protected PostgisDataStore(ConnectionPool connPool, java.lang.String schema, java.lang.String namespace)
           
protected PostgisDataStore(ConnectionPool connPool, java.lang.String schema, java.lang.String namespace, int optimizeMode)
           
 
Method Summary
protected  boolean allowTable(java.lang.String tablename)
          Provides a hook for sub classes to filter out specific tables in the data store that are not to be used as geospatial tables.
protected  java.lang.String[] attributeNames(FeatureType featureType, Filter filter)
          Gets the list of attribute names required for both featureType and filter
protected  AttributeType buildAttributeType(java.sql.ResultSet metadataRs)
          Constructs an AttributeType from a row in a ResultSet.
protected  FIDMapperFactory buildFIDMapperFactory(JDBCDataStoreConfig config)
           
protected  FeatureType buildSchema(java.lang.String typeName, FIDMapper mapper)
          Override this method to perform a few permission checks before the super class has a chance to do its thing.
 PostgisFeatureLocking createFeatureLockingInternal(PostgisDataStore ds, FeatureType type)
           
protected  JDBCFeatureWriter createFeatureWriter(FeatureReader fReader, QueryData queryData)
          DOCUMENT ME!
protected  LockingManager createLockingManager()
          Allows subclass to create LockingManager to support their needs.
 void createSchema(FeatureType featureType)
          Creates storage for a new featureType.
protected  int determineSRID(java.lang.String tableName, java.lang.String geometryColumnName)
          DOCUMENT ME!
protected  int getConcurrency(boolean forWrite)
           
 com.vividsolutions.jts.geom.Envelope getEnvelope(java.lang.String typeName)
          Retrieve approx bounds of all Features.
 FeatureReader getFeatureReader(FeatureType requestType, Filter filter, Transaction transaction)
          This is a public entry point to the DataStore.
 FeatureSource getFeatureSource(java.lang.String typeName)
          Default implementation based on getFeatureReader and getFeatureWriter.
 FeatureWriter getFeatureWriter(java.lang.String typeName, Transaction transaction)
          Retrieve a FeatureWriter over entire dataset.
 FeatureWriter getFeatureWriterAppend(java.lang.String typeName, Transaction transaction)
          Retrieve a FeatureWriter for creating new content.
protected  AttributeIO getGeometryAttributeIO(AttributeType type, QueryData queryData)
          Hook to create the geometry attribute IO for a vendor specific data source.
protected  int getResultSetType(boolean forWrite)
           
 SQLBuilder getSqlBuilder(java.lang.String typeName)
          DOCUMENT ME!
 java.lang.String[] getTypeNames()
          Retrieves a list of of the available FeatureTypes.
protected  void guessDataStoreOptions()
          Attempts to figure out some optimization options, based on some postgis metadata.
 boolean isByteaEnabled()
          Returns true if the data store is using the bytea function to fasten WKB data transfer, false otherwise
 boolean isByteaWKB()
           
 boolean isLooseBbox()
          Whether the bounding boxes issued against this postgis datastore are on the envelope of the geometry or the actual geometry.
 boolean isWKBEnabled()
          Returns true if the WKB format is used to transfer geometries, false otherwise
protected static java.lang.String schema(java.lang.String schema)
          Simple helper method to ensure that a schema is always set.
 void setByteaEnabled(boolean byteaEnabled)
          Enables the use of bytea function for WKB data transfer (will improve performance).
 void setByteaWKB(boolean byteaWKB)
           
 void setLooseBbox(boolean isLooseBbox)
          Sets this postgis instance to use a less strict but faster bounding box query.
 void setWKBEnabled(boolean enabled)
          If turned on, WKB will be used to transfer geometry data instead of WKT
 void updateSchema(java.lang.String typeName, FeatureType featureType)
          Used to force namespace and CS info into a persistent change.
 
Methods inherited from class org.geotools.data.jdbc.JDBC2DataStore
createConnection
 
Methods inherited from class org.geotools.data.jdbc.JDBC1DataStore
allSameOrder, createFeatureReader, determineFidColumnName, executeQuery, getAttributeIO, getAttributeTypes, getConnection, getFeatureReader, getFeatureTypeHandler, getFeatureWriter, getFIDMapper, getFIDMapperFactory, getJDBCFeatureReader, getLockingManager, getNameSpace, getSchema, getSqlNameEscape, getView, setAutoCommit, setFIDMapper, setFIDMapperFactory, setSqlNameEscape
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.geotools.data.DataStore
getFeatureReader, getFeatureWriter, getLockingManager, getSchema, getView
 

Field Detail

LOGGER

protected static final java.util.logging.Logger LOGGER
The logger for the postgis module.


geometryFactory

protected static com.vividsolutions.jts.geom.GeometryFactory geometryFactory
Factory for producing geometries (from JTS).


geometryReader

protected static com.vividsolutions.jts.io.WKTReader geometryReader
Well Known Text reader (from JTS).


OPTIMIZE_SAFE

public static final int OPTIMIZE_SAFE
OPTIMIZE constants

See Also:
Constant Field Values

OPTIMIZE_SQL

public static final int OPTIMIZE_SQL
See Also:
Constant Field Values

useGeos

protected boolean useGeos
Enables the use of geos operators


OPTIMIZE_MODE

public final int OPTIMIZE_MODE
Current optimize mode


WKBEnabled

protected boolean WKBEnabled
If true, WKB format is used instead of WKT


byteaEnabled

protected boolean byteaEnabled
If true, the bytea function will be used to optimize even further data loading when using WKB format


byteaWKB

protected boolean byteaWKB
postgis 1.0 changed the way WKB is handled, this needs to be set if version >1. (it affects the way you send WKB to the database)


looseBbox

protected boolean looseBbox
If true then the bounding box filters will use the && postgis operator, which uses the spatial index and performs against the envelope of the geom, leading to greater speed and slightly less accuracy.

Constructor Detail

PostgisDataStore

protected PostgisDataStore(ConnectionPool connPool)
                    throws java.io.IOException

PostgisDataStore

protected PostgisDataStore(ConnectionPool connPool,
                           java.lang.String namespace)
                    throws java.io.IOException

PostgisDataStore

protected PostgisDataStore(ConnectionPool connPool,
                           java.lang.String schema,
                           java.lang.String namespace)
                    throws java.io.IOException

PostgisDataStore

protected PostgisDataStore(ConnectionPool connPool,
                           java.lang.String schema,
                           java.lang.String namespace,
                           int optimizeMode)
                    throws java.io.IOException

PostgisDataStore

public PostgisDataStore(ConnectionPool connectionPool,
                        JDBCDataStoreConfig config,
                        int optimizeMode)
                 throws java.io.IOException
Method Detail

schema

protected static java.lang.String schema(java.lang.String schema)
Simple helper method to ensure that a schema is always set.


createLockingManager

protected LockingManager createLockingManager()
Allows subclass to create LockingManager to support their needs.

Overrides:
createLockingManager in class JDBC1DataStore
Returns:

guessDataStoreOptions

protected void guessDataStoreOptions()
                              throws java.io.IOException
Attempts to figure out some optimization options, based on some postgis metadata. If the version is later than 0.7.2 then bytea will be used to read geometries if WKB is enabled. And it will read if GEOS is enabled from the version string as well.

Throws:
java.io.IOException - DOCUMENT ME!
DataSourceException - DOCUMENT ME!

getTypeNames

public java.lang.String[] getTypeNames()
                                throws java.io.IOException
Description copied from interface: DataStore
Retrieves a list of of the available FeatureTypes.

This is simply a list of the FeatureType names as aquiring the actual FeatureType schemas may be expensive.

Warning: this list may not be unique - the types may be in separate namespaces.

If you need to worry about such things please consider the use of the Catalog and CatalogEntry interface - many DataStores support this. getTypeNames is really a convience method for a Catalog.iterator() where the name of each entry is returned.

Specified by:
getTypeNames in interface DataStore
Overrides:
getTypeNames in class JDBC1DataStore
Throws:
java.io.IOException
See Also:
org.geotools.data.DataStore#getFeatureTypes()

getEnvelope

public com.vividsolutions.jts.geom.Envelope getEnvelope(java.lang.String typeName)
Retrieve approx bounds of all Features.

This result is suitable for a quick map display, illustrating the data. This value is often stored as metadata in databases such as oraclespatial.

Overrides:
getEnvelope in class JDBC1DataStore
Returns:
null as a generic implementation is not provided.

allowTable

protected boolean allowTable(java.lang.String tablename)
Description copied from class: JDBC1DataStore
Provides a hook for sub classes to filter out specific tables in the data store that are not to be used as geospatial tables. The default implementation of this method is to allow all tables.

Overrides:
allowTable in class JDBC1DataStore
Parameters:
tablename - A table name to check.
Returns:
True if the table should be exposed as a FeatureType, false if it should be ignored.

buildSchema

protected FeatureType buildSchema(java.lang.String typeName,
                                  FIDMapper mapper)
                           throws java.io.IOException
Override this method to perform a few permission checks before the super class has a chance to do its thing.

Overrides:
buildSchema in class JDBC1DataStore
Parameters:
typeName - The name of the table to construct a feature type for.
mapper - The name of the column holding the fid.
Returns:
The FeatureType for the table.
Throws:
java.io.IOException
See Also:
JDBC1DataStore.buildAttributeType(ResultSet)

getFeatureReader

public FeatureReader getFeatureReader(FeatureType requestType,
                                      Filter filter,
                                      Transaction transaction)
                               throws java.io.IOException
This is a public entry point to the DataStore.

We have given some though to changing this api to be based on query.

Currently the is is the only way to retype your features to different name spaces.

(non-Javadoc)

Overrides:
getFeatureReader in class JDBC1DataStore
Throws:
java.io.IOException
See Also:
org.geotools.data.DataStore#getFeatureReader(org.geotools.feature.FeatureType, org.geotools.filter.Filter, org.geotools.data.Transaction)

attributeNames

protected java.lang.String[] attributeNames(FeatureType featureType,
                                            Filter filter)
                                     throws java.io.IOException
Gets the list of attribute names required for both featureType and filter

Overrides:
attributeNames in class JDBC1DataStore
Parameters:
featureType - The FeatureType to get attribute names for.
filter - The filter which needs attributes to filter.
Returns:
The list of attribute names required by a filter.
Throws:
java.io.IOException - If we can't get the schema.

getSqlBuilder

public SQLBuilder getSqlBuilder(java.lang.String typeName)
                         throws java.io.IOException
DOCUMENT ME!

Overrides:
getSqlBuilder in class JDBC1DataStore
Parameters:
typeName -
Returns:
DOCUMENT ME!
Throws:
java.io.IOException - DOCUMENT ME!

determineSRID

protected int determineSRID(java.lang.String tableName,
                            java.lang.String geometryColumnName)
                     throws java.io.IOException
DOCUMENT ME!

Overrides:
determineSRID in class JDBC1DataStore
Parameters:
tableName -
geometryColumnName -
Returns:
Throws:
java.io.IOException - DOCUMENT ME!
DataSourceException - DOCUMENT ME!

buildAttributeType

protected AttributeType buildAttributeType(java.sql.ResultSet metadataRs)
                                    throws java.io.IOException
Constructs an AttributeType from a row in a ResultSet. The ResultSet contains the information retrieved by a call to getColumns() on the DatabaseMetaData object. This information can be used to construct an Attribute Type.

This implementation construct an AttributeType using the default JDBC type mappings defined in JDBCDataStore. These type mappings only handle native Java classes and SQL standard column types. If a geometry type is found then getGeometryAttribute is called.

Note: Overriding methods must never move the current row pointer in the result set.

Overrides:
buildAttributeType in class JDBC1DataStore
Parameters:
metadataRs - The ResultSet containing the result of a DatabaseMetaData.getColumns call.
Returns:
The AttributeType built from the ResultSet.
Throws:
java.io.IOException - If an error occurs processing the ResultSet.

buildFIDMapperFactory

protected FIDMapperFactory buildFIDMapperFactory(JDBCDataStoreConfig config)
Overrides:
buildFIDMapperFactory in class JDBC1DataStore
See Also:
JDBC1DataStore.buildFIDMapperFactory(org.geotools.data.jdbc.JDBCDataStoreConfig)

createSchema

public void createSchema(FeatureType featureType)
                  throws java.io.IOException
Description copied from interface: DataStore
Creates storage for a new featureType.

The provided featureType we be accessable by the typeName provided by featureType.getTypeName().

Specified by:
createSchema in interface DataStore
Overrides:
createSchema in class JDBC1DataStore
Parameters:
featureType -
Throws:
java.io.IOException
See Also:
DataStore.createSchema(org.geotools.feature.FeatureType)

updateSchema

public void updateSchema(java.lang.String typeName,
                         FeatureType featureType)
                  throws java.io.IOException
Description copied from interface: DataStore
Used to force namespace and CS info into a persistent change.

The provided featureType should completely cover the existing schema. All attributes should be accounted for and the typeName should match.

Suggestions:

Specified by:
updateSchema in interface DataStore
Overrides:
updateSchema in class JDBC1DataStore
Throws:
java.io.IOException
See Also:
DataStore.updateSchema(java.lang.String, org.geotools.feature.FeatureType)

getFeatureSource

public FeatureSource getFeatureSource(java.lang.String typeName)
                               throws java.io.IOException
Default implementation based on getFeatureReader and getFeatureWriter.

We should be able to optimize this to only get the RowSet once

Specified by:
getFeatureSource in interface DataStore
Overrides:
getFeatureSource in class JDBC1DataStore
Throws:
java.io.IOException
See Also:
DataStore.getFeatureSource(java.lang.String)

createFeatureLockingInternal

public PostgisFeatureLocking createFeatureLockingInternal(PostgisDataStore ds,
                                                          FeatureType type)
                                                   throws java.io.IOException
Throws:
java.io.IOException

createFeatureWriter

protected JDBCFeatureWriter createFeatureWriter(FeatureReader fReader,
                                                QueryData queryData)
                                         throws java.io.IOException
DOCUMENT ME!

Overrides:
createFeatureWriter in class JDBC1DataStore
Parameters:
fReader -
queryData -
Returns:
Throws:
java.io.IOException - DOCUMENT ME!

getFeatureWriter

public FeatureWriter getFeatureWriter(java.lang.String typeName,
                                      Transaction transaction)
                               throws java.io.IOException
Retrieve a FeatureWriter over entire dataset.

Quick notes: This FeatureWriter is often used to add new content, or perform summary calculations over the entire dataset.

Subclass may wish to implement an optimized featureWriter for these operations.

It should provide Feature for next() even when hasNext() is false.

Subclasses are responsible for checking with the lockingManger unless they are providing their own locking support.

Specified by:
getFeatureWriter in interface DataStore
Overrides:
getFeatureWriter in class JDBC1DataStore
Parameters:
typeName -
transaction -
Returns:
Throws:
java.io.IOException
See Also:
org.geotools.data.DataStore#getFeatureWriter(java.lang.String, boolean, org.geotools.data.Transaction)

getFeatureWriterAppend

public FeatureWriter getFeatureWriterAppend(java.lang.String typeName,
                                            Transaction transaction)
                                     throws java.io.IOException
Retrieve a FeatureWriter for creating new content.

Subclass may wish to implement an optimized featureWriter for this operation. One based on prepared statements is a possibility, as we do not require a ResultSet.

To allow new content the FeatureWriter should provide Feature for next() even when hasNext() is false.

Subclasses are responsible for checking with the lockingManger unless they are providing their own locking support.

Specified by:
getFeatureWriterAppend in interface DataStore
Overrides:
getFeatureWriterAppend in class JDBC1DataStore
Parameters:
typeName -
transaction -
Returns:
Throws:
java.io.IOException
See Also:
org.geotools.data.DataStore#getFeatureWriter(java.lang.String, boolean, org.geotools.data.Transaction)

getGeometryAttributeIO

protected AttributeIO getGeometryAttributeIO(AttributeType type,
                                             QueryData queryData)
Description copied from class: JDBC1DataStore
Hook to create the geometry attribute IO for a vendor specific data source.

Specified by:
getGeometryAttributeIO in class JDBC1DataStore
Parameters:
type - The AttributeType to read.
queryData - The connection holder
Returns:
The AttributeIO that will read and write the geometry from the results.
See Also:
org.geotools.data.jdbc.JDBCDataStore#getGeometryAttributeIO(org.geotools.feature.AttributeType)

getResultSetType

protected int getResultSetType(boolean forWrite)
Overrides:
getResultSetType in class JDBC1DataStore

getConcurrency

protected int getConcurrency(boolean forWrite)
Overrides:
getConcurrency in class JDBC1DataStore

isWKBEnabled

public boolean isWKBEnabled()
Returns true if the WKB format is used to transfer geometries, false otherwise

Returns:

setWKBEnabled

public void setWKBEnabled(boolean enabled)
If turned on, WKB will be used to transfer geometry data instead of WKT

Parameters:
enabled -

setLooseBbox

public void setLooseBbox(boolean isLooseBbox)
Sets this postgis instance to use a less strict but faster bounding box query. Setting this to true will have PostGIS issue bounding box queries against the envelope of the geometry, so some may be slighty wrong, but will perform much faster. The intersects function can still be used to obtain the exact query.

Parameters:
isLooseBbox - true if this should have a loose Bbox.

isLooseBbox

public boolean isLooseBbox()
Whether the bounding boxes issued against this postgis datastore are on the envelope of the geometry or the actual geometry.

Returns:
true if the bounding box is 'loose', against the envelope instead of the actual geometry.

isByteaEnabled

public boolean isByteaEnabled()
Returns true if the data store is using the bytea function to fasten WKB data transfer, false otherwise

Returns:

setByteaWKB

public void setByteaWKB(boolean byteaWKB)

isByteaWKB

public boolean isByteaWKB()

setByteaEnabled

public void setByteaEnabled(boolean byteaEnabled)
Enables the use of bytea function for WKB data transfer (will improve performance). Note this function need not be set by the programmer, as the datastore will use it to optimize performance whenever it can (when postGIS is 0.7.2 or later)

Parameters:
byteaEnabled -


Copyright © GeoTools. All Rights Reserved.