org.geotools.data.shape
Class ShapefileDataStore

java.lang.Object
  extended byorg.geotools.data.AbstractDataStore
      extended byorg.geotools.data.AbstractFileDataStore
          extended byorg.geotools.data.shape.ShapefileDataStore
All Implemented Interfaces:
DataStore

public class ShapefileDataStore
extends AbstractFileDataStore

A DataStore implementation which allows reading and writing from Shapefiles.

Author:
Ian Schneider, Tommaso Nolli
To Do:
fix file creation bug

Nested Class Summary
protected static class ShapefileDataStore.Reader
          An AttributeReader implementation for shape.
protected  class ShapefileDataStore.Writer
          A FeatureWriter for ShapefileDataStore.
 
Field Summary
 
Fields inherited from class org.geotools.data.AbstractDataStore
isWriteable, listenerManager, LOGGER
 
Constructor Summary
ShapefileDataStore(java.net.URL url)
          Creates a new instance of ShapefileDataStore.
ShapefileDataStore(java.net.URL url, boolean useMemoryMappedBuffer)
          Creates a new instance of ShapefileDataStore.
ShapefileDataStore(java.net.URL url, boolean useMemoryMappedBuffer, boolean createIndex)
          Creates a new instance of ShapefileDataStore.
ShapefileDataStore(java.net.URL url, boolean useMemoryMappedBuffer, boolean createIndex, boolean useIndex)
          Creates a new instance of ShapefileDataStore.
ShapefileDataStore(java.net.URL url, java.net.URI namespace)
          Creates a new instance of ShapefileDataStore.
ShapefileDataStore(java.net.URL url, java.net.URI namespace, boolean useMemoryMappedBuffer)
          Creates a new instance of ShapefileDataStore.
ShapefileDataStore(java.net.URL url, java.net.URI namespace, boolean useMemoryMappedBuffer, boolean createIndex)
          Creates a new instance of ShapefileDataStore.
ShapefileDataStore(java.net.URL url, java.net.URI namespace, boolean useMemoryMappedBuffer, boolean createIndex, boolean useIndex)
          Creates a new instance of ShapefileDataStore.
 
Method Summary
protected  FeatureReader createFeatureReader(java.lang.String typeName, ShapefileDataStore.Reader r, FeatureType readerSchema)
           
protected  java.lang.String createFeatureTypeName()
          Create the type name of the single FeatureType this DataStore represents.
 void createSchema(FeatureType featureType)
          Set the FeatureType of this DataStore.
protected  ShapefileDataStore.Reader getAttributesReader(boolean readDbf, Filter filter)
          Returns the attribute reader, allowing for a pure shape reader, or a combined dbf/shp reader.
protected  com.vividsolutions.jts.geom.Envelope getBounds(Query query)
          Computes the bounds of the features for the specified feature type that satisfy the query provided that there is a fast way to get that result.
protected  int getCount(Query query)
          Gets the number of the features that would be returned by this query for the specified feature type.
protected  java.lang.String getCurrentTypeName()
           
protected  FeatureReader getFeatureReader()
          Singular version, which must be implemented to represent a Reader for the url being read.
protected  FeatureReader getFeatureReader(java.lang.String typeName)
          Create a FeatureReader for the provided type name.
protected  FeatureReader getFeatureReader(java.lang.String typeName, Query query)
          Use the spatial index if available and adds a small optimization: if no attributes are going to be read, don't uselessly open and read the dbf file.
 FeatureSource getFeatureSource(java.lang.String typeName)
          Default implementation based on getFeatureReader and getFeatureWriter.
protected  FeatureWriter getFeatureWriter(java.lang.String typeName)
          Create a FeatureWriter for the given type name.
 FeatureType getSchema()
          Singular version, returns the FeatureType for the url being read.
 FeatureType getSchema(java.lang.String typeName)
          Obtain the FeatureType of the given name.
 java.lang.String[] getTypeNames()
          Get an array of type names this DataStore holds.
 boolean isLocal()
          Determine if the location of this shape is local or remote.
protected  DbaseFileReader openDbfReader()
          Convenience method for opening a DbaseFileReader.
protected  IndexFile openIndexFile()
          Convenience method for opening a ShapefileReader.
protected  PrjFileReader openPrjReader()
          Convenience method for opening a DbaseFileReader.
protected  QuadTree openQuadTree()
          Convenience method for opening a QuadTree index.
protected  RTree openRTree()
          Convenience method for opening an RTree index.
protected  ShapefileReader openShapeReader()
          Convenience method for opening a ShapefileReader.
protected  AttributeType[] readAttributes()
          Create the AttributeTypes contained within this DataStore.
protected  void typeCheck(java.lang.String requested)
          A convenience method to check if a type name is correct.
 
Methods inherited from class org.geotools.data.AbstractFileDataStore
getFeatureSource, getFeatureWriter, getFeatureWriter, getFeatureWriterAppend, updateSchema
 
Methods inherited from class org.geotools.data.AbstractDataStore
createContents, createLockingManager, createMetadata, createTypeEntry, entries, fireAdded, fireChanged, fireRemoved, getFeatureReader, getFeatureWriter, getFeatureWriter, getFeatureWriterAppend, getLockingManager, getUnsupportedFilter, getView, search, updateSchema
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ShapefileDataStore

public ShapefileDataStore(java.net.URL url)
                   throws java.net.MalformedURLException
Creates a new instance of ShapefileDataStore.

Parameters:
url - The URL of the shp file to use for this DataSource.
Throws:
java.net.MalformedURLException - If computation of related URLs (dbf,shx) fails.

ShapefileDataStore

public ShapefileDataStore(java.net.URL url,
                          java.net.URI namespace)
                   throws java.net.MalformedURLException
Creates a new instance of ShapefileDataStore.

Parameters:
url - The URL of the shp file to use for this DataSource.
Throws:
java.net.MalformedURLException - If computation of related URLs (dbf,shx) fails.

ShapefileDataStore

public ShapefileDataStore(java.net.URL url,
                          java.net.URI namespace,
                          boolean useMemoryMappedBuffer)
                   throws java.net.MalformedURLException
Creates a new instance of ShapefileDataStore.

Parameters:
url - The URL of the shp file to use for this DataSource.
useMemoryMappedBuffer - enable/disable memory mapping of files
Throws:
java.net.MalformedURLException

ShapefileDataStore

public ShapefileDataStore(java.net.URL url,
                          boolean useMemoryMappedBuffer)
                   throws java.net.MalformedURLException
Creates a new instance of ShapefileDataStore.

Parameters:
url - The URL of the shp file to use for this DataSource.
useMemoryMappedBuffer - enable/disable memory mapping of files
Throws:
java.net.MalformedURLException

ShapefileDataStore

public ShapefileDataStore(java.net.URL url,
                          boolean useMemoryMappedBuffer,
                          boolean createIndex)
                   throws java.net.MalformedURLException
Creates a new instance of ShapefileDataStore.

Parameters:
url - The URL of the shp file to use for this DataSource.
useMemoryMappedBuffer - enable/disable memory mapping of files
createIndex - enable/disable automatic index creation if needed
Throws:
java.net.MalformedURLException

ShapefileDataStore

public ShapefileDataStore(java.net.URL url,
                          java.net.URI namespace,
                          boolean useMemoryMappedBuffer,
                          boolean createIndex)
                   throws java.net.MalformedURLException
Creates a new instance of ShapefileDataStore.

Parameters:
url - The URL of the shp file to use for this DataSource.
useMemoryMappedBuffer - enable/disable memory mapping of files
createIndex - enable/disable automatic index creation if needed
Throws:
java.net.MalformedURLException

ShapefileDataStore

public ShapefileDataStore(java.net.URL url,
                          boolean useMemoryMappedBuffer,
                          boolean createIndex,
                          boolean useIndex)
                   throws java.net.MalformedURLException
Creates a new instance of ShapefileDataStore.

Parameters:
url - The URL of the shp file to use for this DataSource.
useMemoryMappedBuffer - enable/disable memory mapping of files
createIndex - enable/disable automatic index creation if needed
useIndex - enable/disable index usage (mainly for testing)
Throws:
java.net.MalformedURLException

ShapefileDataStore

public ShapefileDataStore(java.net.URL url,
                          java.net.URI namespace,
                          boolean useMemoryMappedBuffer,
                          boolean createIndex,
                          boolean useIndex)
                   throws java.net.MalformedURLException
Creates a new instance of ShapefileDataStore.

Parameters:
url - The URL of the shp file to use for this DataSource.
useMemoryMappedBuffer - enable/disable memory mapping of files
createIndex - enable/disable automatic index creation if needed
useIndex - enable/disable index usage (mainly for testing)
Throws:
java.net.MalformedURLException
Method Detail

isLocal

public boolean isLocal()
Determine if the location of this shape is local or remote.

Returns:
true if local, false if remote

getFeatureReader

protected FeatureReader getFeatureReader(java.lang.String typeName)
                                  throws java.io.IOException
Create a FeatureReader for the provided type name.

Specified by:
getFeatureReader in class AbstractDataStore
Parameters:
typeName - The name of the FeatureType to create a reader for.
Returns:
A new FeatureReader.
Throws:
java.io.IOException - If an error occurs during creation
DataSourceException - DOCUMENT ME!

getFeatureReader

protected FeatureReader getFeatureReader()
                                  throws java.io.IOException
Description copied from class: AbstractFileDataStore
Singular version, which must be implemented to represent a Reader for the url being read.

Specified by:
getFeatureReader in class AbstractFileDataStore
Throws:
java.io.IOException
See Also:
org.geotools.data.DataStore#getFeatureReader(java.lang.String)

getFeatureReader

protected FeatureReader getFeatureReader(java.lang.String typeName,
                                         Query query)
                                  throws java.io.IOException
Use the spatial index if available and adds a small optimization: if no attributes are going to be read, don't uselessly open and read the dbf file.

Overrides:
getFeatureReader in class AbstractDataStore
Throws:
java.io.IOException
See Also:
AbstractDataStore.getFeatureReader(java.lang.String, org.geotools.data.Query)

createFeatureReader

protected FeatureReader createFeatureReader(java.lang.String typeName,
                                            ShapefileDataStore.Reader r,
                                            FeatureType readerSchema)
                                     throws SchemaException,
                                            java.io.IOException
Parameters:
typeName -
r -
readerSchema -
Returns:
Throws:
SchemaException
java.io.IOException

getAttributesReader

protected ShapefileDataStore.Reader getAttributesReader(boolean readDbf,
                                                        Filter filter)
                                                 throws java.io.IOException
Returns the attribute reader, allowing for a pure shape reader, or a combined dbf/shp reader.

Parameters:
readDbf - - if true, the dbf fill will be opened and read
filter - - a Filter to use
Returns:
Throws:
java.io.IOException

openIndexFile

protected IndexFile openIndexFile()
                           throws java.io.IOException
Convenience method for opening a ShapefileReader.

Returns:
An IndexFile
Throws:
java.io.IOException

openShapeReader

protected ShapefileReader openShapeReader()
                                   throws java.io.IOException
Convenience method for opening a ShapefileReader.

Returns:
A new ShapefileReader.
Throws:
java.io.IOException - If an error occurs during creation.
DataSourceException - DOCUMENT ME!

openDbfReader

protected DbaseFileReader openDbfReader()
                                 throws java.io.IOException
Convenience method for opening a DbaseFileReader.

Returns:
A new DbaseFileReader
Throws:
java.io.IOException - If an error occurs during creation.

openPrjReader

protected PrjFileReader openPrjReader()
                               throws java.io.IOException,
                                      org.opengis.referencing.FactoryException
Convenience method for opening a DbaseFileReader.

Returns:
A new DbaseFileReader
Throws:
java.io.IOException - If an error occurs during creation.
org.opengis.referencing.FactoryException

openRTree

protected RTree openRTree()
                   throws java.io.IOException
Convenience method for opening an RTree index.

Returns:
A new RTree.
Throws:
java.io.IOException - If an error occurs during creation.
DataSourceException - DOCUMENT ME!

openQuadTree

protected QuadTree openQuadTree()
                         throws StoreException
Convenience method for opening a QuadTree index.

Returns:
A new QuadTree
Throws:
StoreException

getTypeNames

public java.lang.String[] getTypeNames()
Get an array of type names this DataStore holds.
ShapefileDataStore will always return a single name.

Specified by:
getTypeNames in interface DataStore
Specified by:
getTypeNames in class AbstractDataStore
Returns:
An array of length one containing the single type held.

createFeatureTypeName

protected java.lang.String createFeatureTypeName()
Create the type name of the single FeatureType this DataStore represents.
For example, if the urls path is file:///home/billy/mytheme.shp, the type name will be mytheme.

Returns:
A name based upon the last path component of the url minus the extension.

getCurrentTypeName

protected java.lang.String getCurrentTypeName()

typeCheck

protected void typeCheck(java.lang.String requested)
                  throws java.io.IOException
A convenience method to check if a type name is correct.

Parameters:
requested - The type name requested.
Throws:
java.io.IOException - If the type name is not available

getFeatureWriter

protected FeatureWriter getFeatureWriter(java.lang.String typeName)
                                  throws java.io.IOException
Create a FeatureWriter for the given type name.

Overrides:
getFeatureWriter in class AbstractDataStore
Parameters:
typeName - The typeName of the FeatureType to write
Returns:
A new FeatureWriter.
Throws:
java.io.IOException - If the typeName is not available or some other error occurs.

getSchema

public FeatureType getSchema(java.lang.String typeName)
                      throws java.io.IOException
Obtain the FeatureType of the given name. ShapefileDataStore contains only one FeatureType.

Specified by:
getSchema in interface DataStore
Specified by:
getSchema in class AbstractDataStore
Parameters:
typeName - The name of the FeatureType.
Returns:
The FeatureType that this DataStore contains.
Throws:
java.io.IOException - If a type by the requested name is not present.
DataSourceException - DOCUMENT ME!

getSchema

public FeatureType getSchema()
                      throws java.io.IOException
Description copied from class: AbstractFileDataStore
Singular version, returns the FeatureType for the url being read.

Specified by:
getSchema in class AbstractFileDataStore
Throws:
java.io.IOException
See Also:
DataStore.getSchema(java.lang.String)

readAttributes

protected AttributeType[] readAttributes()
                                  throws java.io.IOException
Create the AttributeTypes contained within this DataStore.

Returns:
An array of new AttributeTypes
Throws:
java.io.IOException - If AttributeType reading fails

createSchema

public void createSchema(FeatureType featureType)
                  throws java.io.IOException
Set the FeatureType of this DataStore. This method will delete any existing local resources or throw an IOException if the DataStore is remote.

Specified by:
createSchema in interface DataStore
Overrides:
createSchema in class AbstractDataStore
Parameters:
featureType - The desired FeatureType.
Throws:
java.io.IOException - If the DataStore is remote.

getBounds

protected com.vividsolutions.jts.geom.Envelope getBounds(Query query)
                                                  throws java.io.IOException
Description copied from class: AbstractDataStore
Computes the bounds of the features for the specified feature type that satisfy the query provided that there is a fast way to get that result.

Will return null if there is not fast way to compute the bounds. Since it's based on some kind of header/cached information, it's not guaranteed to be real bound of the features

Overrides:
getBounds in class AbstractDataStore
Parameters:
query -
Returns:
the bounds, or null if too expensive
Throws:
java.io.IOException
See Also:
AbstractDataStore.getBounds(org.geotools.data.Query)

getFeatureSource

public FeatureSource getFeatureSource(java.lang.String typeName)
                               throws java.io.IOException
Description copied from class: AbstractDataStore
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 AbstractDataStore
Throws:
java.io.IOException
See Also:
DataStore.getFeatureSource(java.lang.String)

getCount

protected int getCount(Query query)
                throws java.io.IOException
Description copied from class: AbstractDataStore
Gets the number of the features that would be returned by this query for the specified feature type.

If getBounds(Query) returns -1 due to expense consider using getFeatures(Query).getCount() as a an alternative.

Overrides:
getCount in class AbstractDataStore
Parameters:
query - Contains the Filter and MaxFeatures to find the bounds for.
Returns:
The number of Features provided by the Query or -1 if count is too expensive to calculate or any errors or occur.
Throws:
java.io.IOException
See Also:
AbstractDataStore.getCount(org.geotools.data.Query)


Copyright © GeoTools. All Rights Reserved.