org.geotools.data.shapefile
Class ShapefileDataStore

java.lang.Object
  extended byorg.geotools.data.AbstractDataStore
      extended byorg.geotools.data.AbstractFileDataStore
          extended byorg.geotools.data.shapefile.ShapefileDataStore
All Implemented Interfaces:
DataStore
Direct Known Subclasses:
IndexedShapefileDataStore

public class ShapefileDataStore
extends AbstractFileDataStore

A DataStore implementation which allows reading and writing from Shapefiles.

Author:
Ian Schneider
To Do:
fix file creation bug

Nested Class Summary
protected static class ShapefileDataStore.Reader
          An AttributeReader implementation for Shapefile.
protected  class ShapefileDataStore.Writer
          A FeatureWriter for ShapefileDataStore.
 
Field Summary
protected  java.net.URL dbfURL
           
protected  java.net.URI namespace
           
protected  java.net.URL prjURL
           
protected  Lock readWriteLock
           
protected  FeatureType schema
           
protected  java.net.URL shpURL
           
protected  java.net.URL shxURL
           
protected  boolean useMemoryMappedBuffer
           
protected  java.net.URL xmlURL
           
 
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, java.net.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(java.net.URL url, java.net.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.
 
Method Summary
protected  void copyAndDelete(java.net.URL src, long temp)
          Copy the file at the given URL to the original
protected static DbaseFileHeader createDbaseHeader(FeatureType featureType)
          Attempt to create a DbaseFileHeader for the FeatureType.
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.
protected  FeatureWriter createFeatureWriter(java.lang.String typeName, Transaction transaction)
          Create a FeatureWriter for the given type name.
protected  java.util.Map createMetadata(java.lang.String typeName)
          Latch onto xmlURL if it is there, we may be able to get out of calculating the bounding box!
 void createSchema(FeatureType featureType)
          Set the FeatureType of this DataStore.
protected  ShapefileDataStore.Reader getAttributesReader(boolean readDbf)
          Returns the attribute reader, allowing for a pure shapefile 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)
          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 getFeatureSource(java.lang.String typeName)
          Default implementation based on getFeatureReader and getFeatureWriter.
protected  java.nio.channels.ReadableByteChannel getReadChannel(java.net.URL url)
          Obtain a ReadableByteChannel from the given URL.
 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.
protected  java.io.File getStorageFile(java.net.URL url, long temp)
          Get a temproray File based on the URL passed in
protected  java.net.URL getStorageURL(java.net.URL url, long temp)
          Get a temporary URL for storage based on the one passed in
 java.lang.String[] getTypeNames()
          Get an array of type names this DataStore holds.
protected  java.nio.channels.WritableByteChannel getWriteChannel(java.net.URL url)
          Obtain a WritableByteChannel from the given URL.
 boolean isLocal()
          Determine if the location of this shapefile is local or remote.
protected  DbaseFileReader openDbfReader()
          Convenience method for opening a DbaseFileReader.
protected  PrjFileReader openPrjReader()
          Convenience method for opening a DbaseFileReader.
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
createLockingManager, getFeatureReader, getFeatureWriter, getFeatureWriter, getFeatureWriter, getFeatureWriterAppend, getLockingManager, getUnsupportedFilter, getView, updateSchema
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

shpURL

protected final java.net.URL shpURL

dbfURL

protected final java.net.URL dbfURL

shxURL

protected final java.net.URL shxURL

prjURL

protected final java.net.URL prjURL

xmlURL

protected final java.net.URL xmlURL

readWriteLock

protected Lock readWriteLock

namespace

protected java.net.URI namespace

schema

protected FeatureType schema

useMemoryMappedBuffer

protected boolean useMemoryMappedBuffer
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.lang.NullPointerException - DOCUMENT ME!
. - If computation of related URLs (dbf,shx) fails.
java.net.MalformedURLException

ShapefileDataStore

public ShapefileDataStore(java.net.URL url,
                          java.net.URI namespace)
                   throws java.net.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 -

ShapefileDataStore

public ShapefileDataStore(java.net.URL url,
                          java.net.URI namespace,
                          boolean useMemoryMapped)
                   throws java.net.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 -
Method Detail

createMetadata

protected java.util.Map createMetadata(java.lang.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

getReadChannel

protected java.nio.channels.ReadableByteChannel getReadChannel(java.net.URL url)
                                                        throws java.io.IOException
Obtain a ReadableByteChannel from the given URL. If the url protocol is file, a FileChannel will be returned. Otherwise a generic channel will be obtained from the urls input stream.

Parameters:
url - DOCUMENT ME!
Returns:
DOCUMENT ME!
Throws:
java.io.IOException - DOCUMENT ME!

getWriteChannel

protected java.nio.channels.WritableByteChannel getWriteChannel(java.net.URL url)
                                                         throws java.io.IOException
Obtain a WritableByteChannel from the given URL. If the url protocol is file, a FileChannel will be returned. Currently, this method will return a generic channel for remote urls, however both shape and dbf writing can only occur with a local FileChannel channel.

Parameters:
url - DOCUMENT ME!
Returns:
DOCUMENT ME!
Throws:
java.io.IOException - DOCUMENT ME!

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

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
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.

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
Throws:
SchemaException

getAttributesReader

protected ShapefileDataStore.Reader getAttributesReader(boolean readDbf)
                                                 throws java.io.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
Returns:
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 - DOCUMENT ME!

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

createFeatureWriter

protected FeatureWriter createFeatureWriter(java.lang.String typeName,
                                            Transaction transaction)
                                     throws java.io.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:
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.

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

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 - if there are errors getting the count
See Also:
AbstractDataStore.getCount(org.geotools.data.Query)

createDbaseHeader

protected static DbaseFileHeader createDbaseHeader(FeatureType featureType)
                                            throws java.io.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:
java.io.IOException - DOCUMENT ME!
DbaseFileException - DOCUMENT ME!

getStorageURL

protected java.net.URL getStorageURL(java.net.URL url,
                                     long temp)
                              throws java.net.MalformedURLException
Get a temporary URL for storage based on the one passed in

Parameters:
url - DOCUMENT ME!
temp - DOCUMENT ME!
Returns:
DOCUMENT ME!
Throws:
java.net.MalformedURLException

getStorageFile

protected java.io.File getStorageFile(java.net.URL url,
                                      long temp)
Get a temproray File based on the URL passed in

Parameters:
url - DOCUMENT ME!
temp - DOCUMENT ME!
Returns:
DOCUMENT ME!

copyAndDelete

protected void copyAndDelete(java.net.URL src,
                             long temp)
                      throws java.io.IOException
Copy the file at the given URL to the original

Parameters:
src - DOCUMENT ME!
temp - DOCUMENT ME!
Throws:
java.io.IOException - DOCUMENT ME!


Copyright © GeoTools. All Rights Reserved.