001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.data.ogr;
017:
018: import java.io.IOException;
019: import java.net.MalformedURLException;
020: import java.net.URI;
021: import java.net.URL;
022: import java.util.Collections;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import org.gdal.ogr.DataSource;
027: import org.gdal.ogr.Driver;
028: import org.gdal.ogr.ogr;
029: import org.geotools.data.DataSourceException;
030: import org.geotools.data.DataStore;
031: import org.geotools.data.DataStoreFactorySpi;
032: import org.geotools.data.FileDataStoreFactorySpi;
033:
034: /**
035: * Implementation of the DataStore service provider interface for OGR.
036: *
037: * @author Andrea Aime, TOPP
038: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/ogr/src/main/java/org/geotools/data/ogr/OGRDataStoreFactory.java $
039: * @version $Id: OGRDataStoreFactory.java 25075 2007-04-09 19:20:46Z desruisseaux $
040: */
041: public class OGRDataStoreFactory implements DataStoreFactorySpi {
042: public static final Param OGR_NAME = new Param("OGRName",
043: String.class,
044: "Name of the file, or data source to try and open", true);
045:
046: public static final Param OGR_DRIVER_NAME = new Param(
047: "OGRDriverName",
048: String.class,
049: "Name of the OGR driver to be used. Required to create a new data source, optional when opening an existing one",
050: false);
051:
052: public static final Param NAMESPACEP = new Param("namespace",
053: URI.class, "uri to a the namespace", false); //not required
054:
055: static {
056: // perform OGR format registration once
057: if (ogr.GetDriverCount() == 0)
058: ogr.RegisterAll();
059: }
060:
061: /**
062: * Caches opened data stores.
063: * TODO: is this beneficial or problematic? It's a static cache, so opening a lot of
064: * datastore (thousands, hundreds of thousands...) may become a memory problem.
065: * Plus OGR is not designed to be thread safe.
066: */
067: private Map liveStores = new HashMap();
068:
069: public boolean canProcess(Map params) {
070: boolean accept = false;
071: String ogrName = null;
072: String ogrDriver = null;
073: try {
074: ogrName = (String) OGR_NAME.lookUp(params);
075: } catch (IOException ioe) {
076: // yes, I am eating this
077: }
078: try {
079: ogrDriver = (String) OGR_DRIVER_NAME.lookUp(params);
080: } catch (IOException ioe) {
081: // yes, I am eating this
082: }
083:
084: accept = canProcess(ogrName, ogrDriver);
085: return accept;
086: }
087:
088: public DataStore createDataStore(Map params) throws IOException {
089: DataStore ds = null;
090: if (!liveStores.containsKey(params)) {
091:
092: URL url = null;
093: try {
094: ds = createNewDataStore(params);
095: liveStores.put(params, ds);
096: } catch (MalformedURLException mue) {
097: throw new DataSourceException(
098: "Unable to attatch datastore to " + url, mue);
099: }
100: } else
101: ds = (DataStore) liveStores.get(params);
102: return ds;
103: }
104:
105: /**
106: * Not implemented yet.
107: *
108: * @param params
109: *
110: * @throws IOException
111: *
112: * @throws IOException DOCUMENT ME!
113: * @throws UnsupportedOperationException
114: */
115: public DataStore createNewDataStore(Map params) throws IOException {
116:
117: DataStore ds = null;
118:
119: String ogrName = (String) OGR_NAME.lookUp(params);
120: String ogrDriver = (String) OGR_DRIVER_NAME.lookUp(params);
121: URI namespace = (URI) NAMESPACEP.lookUp(params);
122: ds = new OGRDataStore(ogrName, ogrDriver, namespace);
123:
124: return ds;
125: }
126:
127: public String getDisplayName() {
128: return "Shapefile";
129: }
130:
131: /**
132: * Describes the type of data the datastore returned by this factory works
133: * with.
134: *
135: * @return String a human readable description of the type of restore
136: * supported by this datastore.
137: */
138: public String getDescription() {
139: return "ESRI(tm) Shapefiles (*.shp)";
140: }
141:
142: // public DataSourceMetadataEnity createMetadata( Map params )
143: // throws IOException {
144: //
145: // URL url = (URL) URLP.lookUp(params);
146: // Boolean mm = (Boolean) MEMORY_MAPPED.lookUp(params);
147: //
148: // String server;
149: // String name;
150: // if( url.getProtocol().equals("file")){
151: // server = "localhost";
152: // name = url.getPath();
153: // }
154: // else {
155: // server = url.getHost()+":"+url.getPort();
156: // name = url.getFile();
157: // }
158: // return new DataSourceMetadataEnity( server, name, "Shapefile access for "+url );
159: // }
160: /**
161: * Test to see if this datastore is available, if it has all the
162: * appropriate libraries to construct a datastore. This datastore just
163: * returns true for now.
164: *
165: * @return <tt>true</tt> if and only if this factory is available to create
166: * DataStores.
167: *
168: * @task REVISIT: I'm just adding this method to compile, maintainer should
169: * revisit to check for any libraries that may be necessary for
170: * datastore creations.
171: */
172: public boolean isAvailable() {
173: return true;
174: }
175:
176: /**
177: * Describe parameters.
178: *
179: *
180: * @see org.geotools.data.DataStoreFactorySpi#getParametersInfo()
181: */
182: public Param[] getParametersInfo() {
183: return new Param[] { OGR_NAME };
184: }
185:
186: /**
187: * @see org.geotools.data.dir.FileDataStoreFactorySpi#getFileExtensions()
188: */
189: public String[] getFileExtensions() {
190: return new String[] { ".shp", };
191: }
192:
193: /**
194: * Assume we can process an ogrName if the ogrName exists and can be opened, or if
195: * the specified driver does exist.
196: * @param ogrName
197: * @param driver
198: * @return
199: */
200: public boolean canProcess(String ogrName, String driver) {
201: DataSource ds = ogr.OpenShared(ogrName, OGRDataStore.FALSE);
202: if (ds != null) {
203: ds.delete();
204: return true;
205: }
206:
207: Driver dr = ogr.GetDriverByName(driver);
208: if (dr != null) {
209: dr.delete();
210: return true;
211: }
212:
213: return false;
214:
215: }
216:
217: public Map getImplementationHints() {
218: return Collections.EMPTY_MAP;
219: }
220:
221: }
|