org.geotools.data.shapefile
Class ShapefileDataStore

Object
  extended by AbstractDataStore
      extended by AbstractFileDataStore
          extended by ShapefileDataStore
All Implemented Interfaces:
DataAccess<SimpleFeatureType,SimpleFeature>, DataStore, FileDataStore
Direct Known Subclasses:
IndexedShapefileDataStore

public class ShapefileDataStore
extends AbstractFileDataStore

A DataStore implementation which allows reading and writing from Shapefiles.

Author:
Ian Schneider
Module:
modules/plugin/shapefile (gt-shapefile.jar)

Field Summary
protected  Charset dbfCharset
           
static Charset DEFAULT_STRING_CHARSET
           
protected  URI namespace
           
protected  SimpleFeatureType schema
           
protected  ShpFiles shpFiles
           
protected  boolean useMemoryMappedBuffer
           
 
Fields inherited from class AbstractDataStore
isWriteable, listenerManager, LOGGER
 
Constructor Summary
ShapefileDataStore(URL url)
          Creates a new instance of ShapefileDataStore.
ShapefileDataStore(URL url, boolean useMemoryMappedBuffer)
           
ShapefileDataStore(URL url, boolean useMemoryMappedBuffer, Charset dbfCharset)
           
ShapefileDataStore(URL url, URI namespace)
          this sets the datastore's namespace during construction (so the schema - FeatureType - will have the correct value) You can call this with namespace = null, but I suggest you give it an actual namespace.
ShapefileDataStore(URL url, URI namespace, boolean useMemoryMapped)
          this sets the datastore's namespace during construction (so the schema - FeatureType - will have the correct value) You can call this with namespace = null, but I suggest you give it an actual namespace.
ShapefileDataStore(URL url, URI namespace, boolean useMemoryMapped, Charset dbfCharset)
          this sets the datastore's namespace during construction (so the schema - FeatureType - will have the correct value) You can call this with namespace = null, but I suggest you give it an actual namespace.
 
Method Summary
protected static DbaseFileHeader createDbaseHeader(SimpleFeatureType featureType)
          Attempt to create a DbaseFileHeader for the FeatureType.
protected  FIDFeatureReader createFeatureReader(String typeName, ShapefileAttributeReader reader, SimpleFeatureType readerSchema)
           
protected  String createFeatureTypeName()
          Create the type name of the single FeatureType this DataStore represents.
protected  FeatureWriter<SimpleFeatureType,SimpleFeature> createFeatureWriter(String typeName, Transaction transaction)
          Create a FeatureWriter for the given type name.
protected  Map createMetadata(String typeName)
          Latch onto xmlURL if it is there, we may be able to get out of calculating the bounding box!
 void createSchema(SimpleFeatureType featureType)
          Set the FeatureType of this DataStore.
 void dispose()
          Dummy implementation, it's a no-op.
 void forceSchemaCRS(CoordinateReferenceSystem crs)
          This method is used to force the creation of a .prj file.
protected  ShapefileAttributeReader getAttributesReader(boolean readDbf, GeometryFactory gf)
          Returns the attribute reader, allowing for a pure shapefile reader, or a combined dbf/shp reader.
protected  ReferencedEnvelope getBounds()
          Gets the bounding box of the file represented by this data store as a whole (that is, off all of the features in the shapefile)
protected  ReferencedEnvelope 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.
 int getCount(Query query)
          Gets the number of the features that would be returned by this query for the specified feature type.
protected  String getCurrentTypeName()
           
 FeatureReader<SimpleFeatureType,SimpleFeature> getFeatureReader()
          Singular version, which must be implemented to represent a Reader for the url being read.
protected  FeatureReader<SimpleFeatureType,SimpleFeature> getFeatureReader(String typeName)
          Create a FeatureReader for the provided type name.
protected  FeatureReader<SimpleFeatureType,SimpleFeature> getFeatureReader(String typeName, Query query)
          Just like the basic version, but adds a small optimization: if no attributes are going to be read, don't uselessly open and read the dbf file.
 FeatureSource<SimpleFeatureType,SimpleFeature> getFeatureSource(String typeName)
          Default implementation based on getFeatureReader and getFeatureWriter.
protected  GeometryFactory getGeometryFactory(Hints hints)
          Builds the most appropriate geometry factory depending on the available query hints
 ServiceInfo getInfo()
          Access a ServiceInfo object for this shapefile.
 SimpleFeatureType getSchema()
          Singular version, returns the FeatureType for the url being read.
 SimpleFeatureType getSchema(String typeName)
          Obtain the FeatureType of the given name.
 Charset getStringCharset()
          Returns the Charset used to decode strings in the DBF file
protected  Set getSupportedHints()
          If you are using the automated FeatureSource/Store/Locking creation, this method allows for the specification of the supported hints.
 String[] getTypeNames()
          Get an array of type names this DataStore holds.
 boolean isLocal()
          Determine if the location of this shapefile is local or remote.
protected  DbaseFileReader openDbfReader()
          Convenience method for opening a DbaseFileReader.
protected  IndexFile openIndexFile()
          Convenience method for opening an index file.
protected  PrjFileReader openPrjReader()
          Convenience method for opening a DbaseFileReader.
protected  ShapefileReader openShapeReader(GeometryFactory gf)
          Convenience method for opening a ShapefileReader.
protected  List<AttributeDescriptor> readAttributes()
          Create the AttributeDescriptor contained within this DataStore.
 void setStringCharset(Charset stringCharset)
          Set this if you need BDF strings to be decoded in a Charset other than ISO-8859-1
 String toString()
           
protected  void typeCheck(String requested)
          A convenience method to check if a type name is correct.
 
Methods inherited from class AbstractFileDataStore
getFeatureSource, getFeatureWriter, getFeatureWriter, getFeatureWriterAppend, updateSchema
 
Methods inherited from class AbstractDataStore
createLockingManager, getFeatureReader, getFeatureSource, getFeatureWriter, getFeatureWriter, getFeatureWriter, getFeatureWriterAppend, getLockingManager, getNames, getSchema, getUnsupportedFilter, getView, state, updateSchema, updateSchema
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface DataStore
getFeatureReader, getFeatureWriter, getFeatureWriter, getFeatureWriterAppend, getLockingManager, getView, updateSchema
 
Methods inherited from interface DataAccess
getFeatureSource, getNames, getSchema, updateSchema
 

Field Detail

DEFAULT_STRING_CHARSET

public static final Charset DEFAULT_STRING_CHARSET

shpFiles

protected final ShpFiles shpFiles

namespace

protected URI namespace

schema

protected SimpleFeatureType schema

useMemoryMappedBuffer

protected boolean useMemoryMappedBuffer

dbfCharset

protected Charset dbfCharset
Constructor Detail

ShapefileDataStore

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

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

ShapefileDataStore

public ShapefileDataStore(URL url,
                          boolean useMemoryMappedBuffer)
                   throws MalformedURLException
Throws:
MalformedURLException

ShapefileDataStore

public ShapefileDataStore(URL url,
                          boolean useMemoryMappedBuffer,
                          Charset dbfCharset)
                   throws MalformedURLException
Throws:
MalformedURLException

ShapefileDataStore

public ShapefileDataStore(URL url,
                          URI namespace)
                   throws MalformedURLException
this sets the datastore's namespace during construction (so the schema - FeatureType - will have the correct value) You can call this with namespace = null, but I suggest you give it an actual namespace.

Parameters:
url -
namespace -
Throws:
MalformedURLException

ShapefileDataStore

public ShapefileDataStore(URL url,
                          URI namespace,
                          boolean useMemoryMapped,
                          Charset dbfCharset)
                   throws MalformedURLException
this sets the datastore's namespace during construction (so the schema - FeatureType - will have the correct value) You can call this with namespace = null, but I suggest you give it an actual namespace.

Parameters:
url -
namespace -
useMemoryMapped -
dbfCharset -
Throws:
MalformedURLException

ShapefileDataStore

public ShapefileDataStore(URL url,
                          URI namespace,
                          boolean useMemoryMapped)
                   throws MalformedURLException
this sets the datastore's namespace during construction (so the schema - FeatureType - will have the correct value) You can call this with namespace = null, but I suggest you give it an actual namespace.

Parameters:
url -
namespace -
useMemoryMapped -
Throws:
MalformedURLException
Method Detail

getInfo

public ServiceInfo getInfo()
Access a ServiceInfo object for this shapefile.

Specified by:
getInfo in interface DataAccess<SimpleFeatureType,SimpleFeature>
Overrides:
getInfo in class AbstractDataStore
Returns:
ShapefileServiceInfo describing service.

setStringCharset

public void setStringCharset(Charset stringCharset)
Set this if you need BDF strings to be decoded in a Charset other than ISO-8859-1

Parameters:
stringCharset -
Since:
2.3.3

getStringCharset

public Charset getStringCharset()
Returns the Charset used to decode strings in the DBF file

Returns:

createMetadata

protected Map createMetadata(String typeName)
Latch onto xmlURL if it is there, we may be able to get out of calculating the bounding box!

This method is called by the createTypeEntry anonymous inner class DefaultTypeEntry.

Overrides:
createMetadata in class AbstractDataStore
Parameters:
typeName - DOCUMENT ME!
Returns:
Map with xmlURL parsed, or an EMPTY_MAP.

isLocal

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

Returns:
true if local, false if remote

getFeatureReader

protected FeatureReader<SimpleFeatureType,SimpleFeature> getFeatureReader(String typeName)
                                                                   throws 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:
IOException - If an error occurs during creation

getFeatureReader

public FeatureReader<SimpleFeatureType,SimpleFeature> getFeatureReader()
                                                                throws 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 interface FileDataStore
Overrides:
getFeatureReader in class AbstractFileDataStore
Throws:
IOException
See Also:
org.geotools.data.DataStore#getFeatureReader(java.lang.String)

getFeatureReader

protected FeatureReader<SimpleFeatureType,SimpleFeature> getFeatureReader(String typeName,
                                                                          Query query)
                                                                   throws IOException
Just like the basic version, but adds a small optimization: if no attributes are going to be read, don't uselessly open and read the dbf file. Makes sure to consider also attributes in the query.

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

getGeometryFactory

protected GeometryFactory getGeometryFactory(Hints hints)
Builds the most appropriate geometry factory depending on the available query hints

Parameters:
query -
Returns:

createFeatureReader

protected FIDFeatureReader createFeatureReader(String typeName,
                                               ShapefileAttributeReader reader,
                                               SimpleFeatureType readerSchema)
                                        throws SchemaException
Throws:
SchemaException

getAttributesReader

protected ShapefileAttributeReader getAttributesReader(boolean readDbf,
                                                       GeometryFactory gf)
                                                throws IOException
Returns the attribute reader, allowing for a pure shapefile reader, or a combined dbf/shp reader.

Parameters:
readDbf - - if true, the dbf fill will be opened and read
Throws:
IOException

openShapeReader

protected ShapefileReader openShapeReader(GeometryFactory gf)
                                   throws IOException
Convenience method for opening a ShapefileReader.

Returns:
A new ShapefileReader.
Throws:
IOException - If an error occurs during creation.

openDbfReader

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

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

openIndexFile

protected IndexFile openIndexFile()
                           throws IOException
Convenience method for opening an index file.

Returns:
An IndexFile
Throws:
IOException

openPrjReader

protected PrjFileReader openPrjReader()
                               throws IOException,
                                      FactoryException
Convenience method for opening a DbaseFileReader.

Returns:
A new DbaseFileReader
Throws:
IOException - If an error occurs during creation.
FactoryException - DOCUMENT ME!

getTypeNames

public 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 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 String getCurrentTypeName()

typeCheck

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

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

createFeatureWriter

protected FeatureWriter<SimpleFeatureType,SimpleFeature> createFeatureWriter(String typeName,
                                                                             Transaction transaction)
                                                                      throws IOException
Create a FeatureWriter for the given type name.

Overrides:
createFeatureWriter in class AbstractDataStore
Parameters:
typeName - The typeName of the FeatureType to write
transaction - DOCUMENT ME!
Returns:
A new FeatureWriter.
Throws:
IOException - If the typeName is not available or some other error occurs.

getSchema

public SimpleFeatureType getSchema(String typeName)
                            throws 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:
IOException - If a type by the requested name is not present.

getSchema

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

Specified by:
getSchema in interface FileDataStore
Overrides:
getSchema in class AbstractFileDataStore
Returns:
FeatureType of the file being read
Throws:
IOException
See Also:
DataStore.getSchema(java.lang.String)

readAttributes

protected List<AttributeDescriptor> readAttributes()
                                            throws IOException
Create the AttributeDescriptor contained within this DataStore.

Returns:
List of new AttributeDescriptor
Throws:
IOException - If AttributeType reading fails

forceSchemaCRS

public void forceSchemaCRS(CoordinateReferenceSystem crs)
                    throws IOException
This method is used to force the creation of a .prj file.

The internally cached FeatureType will be removed, so the next call to getSchema() will read in the created file. This method is not thread safe and will have dire consequences for any other thread making use of the shapefile.

Parameters:
crs -
Throws:
IOException

createSchema

public void createSchema(SimpleFeatureType featureType)
                  throws 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 DataAccess<SimpleFeatureType,SimpleFeature>
Overrides:
createSchema in class AbstractDataStore
Parameters:
featureType - The desired FeatureType.
Throws:
IOException - If the DataStore is remote.

getBounds

protected ReferencedEnvelope getBounds()
                                throws DataSourceException
Gets the bounding box of the file represented by this data store as a whole (that is, off all of the features in the shapefile)

Returns:
The bounding box of the datasource or null if unknown and too expensive for the method to calculate.
Throws:
DataSourceException - DOCUMENT ME!

getBounds

protected ReferencedEnvelope getBounds(Query query)
                                throws 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
Returns:
the bounds, or null if too expensive
Throws:
SchemaNotFoundException
IOException

getFeatureSource

public FeatureSource<SimpleFeatureType,SimpleFeature> getFeatureSource(String typeName)
                                                                throws 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
Returns:
FeatureSource (or subclass) providing operations for typeName
Throws:
IOException
See Also:
DataStore.getFeatureSource(java.lang.String)

getCount

public int getCount(Query query)
             throws 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:
IOException
See Also:
AbstractDataStore.getCount(org.geotools.data.Query)

createDbaseHeader

protected static DbaseFileHeader createDbaseHeader(SimpleFeatureType featureType)
                                            throws IOException,
                                                   DbaseFileException
Attempt to create a DbaseFileHeader for the FeatureType. Note, we cannot set the number of records until the write has completed.

Parameters:
featureType - DOCUMENT ME!
Returns:
DOCUMENT ME!
Throws:
IOException - DOCUMENT ME!
DbaseFileException - DOCUMENT ME!

toString

public String toString()
Overrides:
toString in class Object

dispose

public void dispose()
Description copied from class: AbstractDataStore
Dummy implementation, it's a no-op. Subclasses holding to system resources must override this method and release them.

Specified by:
dispose in interface DataAccess<SimpleFeatureType,SimpleFeature>
Overrides:
dispose in class AbstractDataStore

getSupportedHints

protected Set getSupportedHints()
Description copied from class: AbstractDataStore
If you are using the automated FeatureSource/Store/Locking creation, this method allows for the specification of the supported hints.

Overrides:
getSupportedHints in class AbstractDataStore
Returns:


Copyright © 1996-2009 Geotools. All Rights Reserved.