📄 shapefiledatastorefactory.java
字号:
/*
* GeoTools - OpenSource mapping toolkit
* http://geotools.org
* (C) 2002-2006, Geotools Project Managment Committee (PMC)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.geotools.data.shapefile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import org.geotools.data.DataSourceException;
import org.geotools.data.DataStore;
import org.geotools.data.FileDataStoreFactorySpi;
import org.geotools.data.shapefile.indexed.IndexType;
import org.geotools.data.shapefile.indexed.IndexedShapefileDataStore;
import com.vividsolutions.jts.geom.Geometry;
/**
* Implementation of the DataStore service provider interface for Shapefiles.
* <p>
* The specific implementation of ShapefileDataStore created by this class is
* not specified. For more information on the connection parameters please
* review the following public Param constants.
* <ul>
* <li>{@link URLP}
* <li>{@link NAMESPACEP}
* <li>{@link CREATE_SPATIAL_INDEX}
* <li>{@link MEMORY_MAPPED}
* <li>{@link DBFCHARSET}
* </ul>
*
* @author Chris Holmes, TOPP
* @source $URL:
* http://svn.geotools.org/geotools/trunk/gt/modules/plugin/shapefile/src/main/java/org/geotools/data/shapefile/ShapefileDataStoreFactory.java $
* @version $Id: ShapefileDataStoreFactory.java 27856 2007-11-12 17:23:35Z
* desruisseaux $
*/
public class ShapefileDataStoreFactory implements FileDataStoreFactorySpi {
public static final Logger LOGGER = org.geotools.util.logging.Logging
.getLogger("org.geotools.data.shapefile");
/**
* url to the .shp file.
*/
public static final Param URLP = new Param("url", URL.class,
"url to a .shp file");
/**
* Optional - uri of the FeatureType's namespace
*/
public static final Param NAMESPACEP = new Param("namespace", URI.class,
"uri to a the namespace", false); // not required
/**
* Optional - enable/disable the use of memory-mapped io
*/
public static final Param MEMORY_MAPPED = new Param("memory mapped buffer",
Boolean.class, "enable/disable the use of memory-mapped io", false);
/**
* Optional - Enable/disable the automatic creation of spatial index
*/
public static final Param CREATE_SPATIAL_INDEX = new Param(
"create spatial index", Boolean.class,
"enable/disable the automatic creation of spatial index", false);
/**
* Optional - character used to decode strings from the DBF file
*/
public static final Param DBFCHARSET = new Param("charset", Charset.class,
"character used to decode strings from the DBF file", false,
Charset.forName("ISO-8859-1")) {
/*
* This is an example of a non simple Param type where a custom parse
* method is required.
*
* @see org.geotools.data.DataStoreFactorySpi.Param#parse(java.lang.String)
*/
public Object parse(String text) throws IOException {
return Charset.forName(text);
}
public String text(Object value) {
return ((Charset) value).name();
}
};
/**
* It is the users responsibility to prevent the creation of duplicate
* DataStore instances (they should hold the results in a Map, Repository or
* Catalog as described in the documenation).
* <p>
* However in the real world we are patching the problem, this Map is used
* to store previously created DataStores so we can hand them out again.
* There are two problems with this approach:
* <ul>
* <li>The ShapefileDataStoreFactory may be created more than once with
* different Hints, for example when used to read a shapefile into alternate
* Feature or Geometry representations.
* <li>It flys in the face of a DataStore lifecycle, the JDBC DataStore
* implementations have taken to support a dispose operation.
* </ul>
* As such I expect this "feature" to disappear, I am keeping it now only so
* we can produce good warnings.
*/
private static Map liveStores = Collections.synchronizedMap(new HashMap());
/**
* Takes a map of parameters which describes how to access a DataStore and
* determines if it can be read by the ShapefileDataStore or
* IndexedShapefileDataStore implementations.
*
* @param params
* A map of parameters describing the location of a
* datastore. Files should be pointed to by a 'url' param.
*
* @return true iff params contains a url param which points to a file
* ending in shp
*/
public boolean canProcess(Map params) {
boolean accept = false;
if (params.containsKey(URLP.key)) {
try {
URL url = (URL) URLP.lookUp(params);
accept = canProcess(url);
} catch (IOException ioe) {
// yes, I am eating this - since it is my job to return a
// true/false
}
}
return accept;
}
/**
* Returns an instance of DataStore iff the resource pointed to the Map of
* paramers can be handled as a shapefile.
* <p>
* The specific implementation of ShapefileDataStore returned is not
* specified, and depends on the parameters given. For more information
* please review the public static Param instances available for this class.
* </p>
* <ul>
* <li>{@link URLP}
* <li>{@link NAMESPACEP}
* <li>{@link CREATE_SPATIAL_INDEX}
* <li>{@link MEMORY_MAPPED}
* <li>{@link DBFCHARSET}
* </ul>
*
* @param params
* A param list with information on the location of a
* restore. For shapefiles this should contain a 'url' param
* which points to a file which ends in shp.
*
* @return DataStore A ShapefileDatastore
* @throws IOException
* If a connection error (such as the file not existing
* occurs)
* @throws DataSourceException
* Thrown if the datastore which is created cannot be
* attached to the restore specified in params.
*/
public ShapefileDataStore createDataStore(Map params) throws IOException {
DataStore ds = null;
synchronized (liveStores) {
if (!liveStores.containsKey(params)) {
URL url = null;
try {
ds = createDataStoreInstance(params);
liveStores.put(params, ds);
} catch (MalformedURLException mue) {
throw new DataSourceException(
"Unable to attatch datastore to " + url, mue);
}
} else {
ds = (DataStore) liveStores.get(params);
}
}
return (ShapefileDataStore) ds;
}
/**
* Creates a new DataStore - for a file that does not exist yet.
* <p>
* This method has different logic than createDataStore. It is willing to be
* memory mapped, and generate an index for a local file that does not exist
* yet.
*
*/
public DataStore createNewDataStore(Map params) throws IOException {
DataStore ds = null;
synchronized (liveStores) {
if (!liveStores.containsKey(params)) {
URL url = null;
try {
ds = createNewShapefile(params);
liveStores.put(params, ds);
} catch (MalformedURLException mue) {
throw new DataSourceException(
"Unable to attatch datastore to " + url, mue);
}
} else {
ds = (DataStore) liveStores.get(params);
}
}
return ds;
}
/**
* Will create a new shapefile baed on the provided parameters.
*
* @param params
* Map of parameters
* @throws IOException
* If the filename is not valid.
* @throws UnsupportedOperationException
*/
DataStore createNewShapefile(Map params) throws IOException {
URL url = (URL) URLP.lookUp(params);
Boolean isMemoryMapped = (Boolean) MEMORY_MAPPED.lookUp(params);
URI namespace = (URI) NAMESPACEP.lookUp(params);
Charset dbfCharset = (Charset) DBFCHARSET.lookUp(params);
Boolean isCreateSpatialIndex = (Boolean) CREATE_SPATIAL_INDEX
.lookUp(params);
if (isCreateSpatialIndex == null) {
// should not be needed as default is TRUE
assert (true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -