001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-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;
009: * version 2.1 of the License.
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.mif;
017:
018: import com.vividsolutions.jts.geom.GeometryFactory;
019: import org.geotools.data.DataStore;
020: import org.geotools.data.DataStoreFactorySpi;
021: import java.io.File;
022: import java.io.IOException;
023: import java.net.URI;
024: import java.util.Collections;
025: import java.util.HashMap;
026: import java.util.Map;
027:
028: /**
029: * Builds a MIFDataStore. Required parameters are:
030: *
031: * <ul>
032: * <li>
033: * MIFDataStore.PARAM_DBTYPE: String, must be "mif"
034: * </li>
035: * <li>
036: * MIFDataStore.PARAM_PATH: String, full path of the directory containing MIF
037: * files, or path of a single mif file (with or without .mif extension)
038: * </li>
039: * </ul>
040: *
041: * <p>
042: * For a full description of creation parameters, see MIFDataStore().
043: * </p>
044: *
045: * @author Luca S. Percich, AMA-MI
046: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/mif/src/main/java/org/geotools/data/mif/MIFDataStoreFactory.java $
047: * @version $Id: MIFDataStoreFactory.java 21857 2006-10-05 17:50:53Z acuster $
048: *
049: * @see MIFDataStore#MIFDataStore(String, HashMap)
050: */
051: public class MIFDataStoreFactory implements DataStoreFactorySpi {
052: // DataStore - specific parameters
053: public static final Param PARAM_DBTYPE = new Param("dbtype",
054: String.class, "Must be \"mif\"", true, "mif");
055: public static final Param PARAM_PATH = new Param(
056: "path",
057: String.class,
058: "Full path of directory containing mifs or single mif file",
059: true, "c:/data/mifpath/");
060: public static final Param PARAM_NAMESPACE = new Param(
061: "namespace",
062: URI.class,
063: "URI of the namespace prefix for FeatureTypes returned by this DataStore",
064: false);
065:
066: // Options
067: public static final Param PARAM_FIELDCASE = new Param(
068: MIFDataStore.PARAM_FIELDCASE,
069: String.class,
070: "Field name case transformation, can be \"\" (no transform), \"upper\" (to uppercase) or \"lower\" (to lowercase).",
071: false, "upper");
072: public static final Param PARAM_GEOMNAME = new Param(
073: MIFDataStore.PARAM_GEOMNAME,
074: String.class,
075: "Name of the geometry field, if not specified defaults to \"the_geom\".",
076: false, "the_geom");
077: public static final Param PARAM_GEOMFACTORY = new Param(
078: MIFDataStore.PARAM_GEOMFACTORY, GeometryFactory.class,
079: "GeometryFactory object used for building geometries",
080: false, "new GeometryFactory()");
081: public static final Param PARAM_GEOMTYPE = new Param(
082: MIFDataStore.PARAM_GEOMTYPE, String.class,
083: "Can be typed, untyped or multi (implies typed).", false,
084: "untyped");
085: public static final Param PARAM_SRID = new Param(
086: MIFDataStore.PARAM_SRID,
087: Integer.class,
088: "SRID code for Geometry, use as alternative for GEOMFACTORY",
089: false, new Integer(26591));
090:
091: // Header clauses
092: public static final Param PARAM_COORDSYS = new Param(
093: MIFDataStore.HCLAUSE_COORDSYS,
094: String.class,
095: "CoordSys clause for new files",
096: false,
097: "CoordSys Earth Projection 8, 87, \"m\", 9, 0, 0.9996, 1500000, 0 Bounds (-6746230.6469, -9998287.38389) (9746230.6469, 9998287.38389)");
098: public static final Param PARAM_CHARSET = new Param(
099: MIFDataStore.HCLAUSE_CHARSET, String.class,
100: "Charset clause", false, "WindowsLatin1");
101: public static final Param PARAM_DELIMITER = new Param(
102: MIFDataStore.HCLAUSE_DELIMITER, String.class,
103: "Delimiter to be used in output MID files", false, ";");
104: public static final Param PARAM_INDEX = new Param(
105: MIFDataStore.HCLAUSE_INDEX,
106: String.class,
107: "Index clasue (comma separated list of indexed field numbers",
108: false, "1,2,4");
109: public static final Param PARAM_TRANSFORM = new Param(
110: MIFDataStore.HCLAUSE_TRANSFORM, String.class,
111: "Transform clause to be uised for output", false,
112: "0.5,0.5,0,0");
113: public static final Param PARAM_UNIQUE = new Param(
114: MIFDataStore.HCLAUSE_UNIQUE,
115: String.class,
116: "Unique clause - comma separated list of field numbers forming unique values.",
117: false, "1,2");
118: public static final Param PARAM_VERSION = new Param(
119: MIFDataStore.HCLAUSE_VERSION, String.class, "Version ID",
120: false, "410");
121:
122: /**
123: * Creates a new MIFDataStoreFactory object.
124: */
125: public MIFDataStoreFactory() {
126: super ();
127: }
128:
129: /**
130: */
131: public String getDisplayName() {
132: return "MIFDataStore";
133: }
134:
135: /* (non-Javadoc)
136: * @see org.geotools.data.DataStoreFactorySpi#createDataStore(java.util.Map)
137: */
138: public DataStore createDataStore(Map params) throws IOException {
139: if (!processParams(params)) {
140: throw new IOException("The parameters map isn't correct.");
141: }
142:
143: MIFDataStore mif = null;
144:
145: try {
146: HashMap parameters = new HashMap();
147:
148: String path = (String) PARAM_PATH.lookUp(params);
149:
150: addParamToMap(PARAM_NAMESPACE, params, parameters, null);
151:
152: // Options
153: addParamToMap(PARAM_FIELDCASE, params, parameters, null);
154: addParamToMap(PARAM_GEOMNAME, params, parameters, null);
155: addParamToMap(PARAM_GEOMTYPE, params, parameters, null);
156: addParamToMap(PARAM_GEOMFACTORY, params, parameters, null);
157: addParamToMap(PARAM_SRID, params, parameters, null);
158:
159: // Header
160: addParamToMap(PARAM_COORDSYS, params, parameters, null);
161: addParamToMap(PARAM_CHARSET, params, parameters, null);
162: addParamToMap(PARAM_DELIMITER, params, parameters, null);
163: addParamToMap(PARAM_INDEX, params, parameters, null);
164: addParamToMap(PARAM_TRANSFORM, params, parameters, null);
165: addParamToMap(PARAM_UNIQUE, params, parameters, null);
166: addParamToMap(PARAM_VERSION, params, parameters, null);
167:
168: mif = new MIFDataStore(path, parameters);
169:
170: return mif;
171: } catch (IOException ex) {
172: throw ex;
173: } catch (Exception ex) {
174: throw new IOException(ex.toString());
175: }
176: }
177:
178: private void addParamToMap(Param param, Map params, HashMap map,
179: Object defa) {
180: Object val = null;
181:
182: try {
183: val = param.lookUp(params);
184: } catch (Exception e) {
185: }
186:
187: if (val != null) {
188: map.put(param.key, val);
189: } else if (defa != null) {
190: map.put(param.key, defa);
191: }
192: }
193:
194: /**
195: * <p>
196: * As the creation of new MIF files is simply achieved by createSchema()
197: * calls, this method simply calls createDataStore().
198: * </p>
199: *
200: * @param params The parameter map
201: *
202: * @return the MIFDataStore instance returned by createDataStore(params)
203: *
204: * @throws IOException
205: *
206: * @see #createDataStore(Map)
207: */
208: public DataStore createNewDataStore(Map params) throws IOException {
209: return createDataStore(params); // return null????
210: }
211:
212: /**
213: * @see org.geotools.data.DataStoreFactorySpi#getDescription()
214: */
215: public String getDescription() {
216: return "MapInfo MIF/MID format datastore";
217: }
218:
219: /**
220: * @see org.geotools.data.DataStoreFactorySpi#getParametersInfo()
221: */
222: public Param[] getParametersInfo() {
223: Param[] params = { PARAM_DBTYPE, PARAM_PATH, PARAM_NAMESPACE,
224:
225: PARAM_FIELDCASE, PARAM_GEOMNAME, PARAM_GEOMFACTORY,
226: PARAM_GEOMTYPE, PARAM_SRID,
227:
228: PARAM_COORDSYS, PARAM_CHARSET, PARAM_DELIMITER,
229: PARAM_INDEX, PARAM_TRANSFORM, PARAM_UNIQUE,
230: PARAM_VERSION };
231:
232: return params;
233: }
234:
235: /**
236: * @see org.geotools.data.DataStoreFactorySpi#canProcess(java.util.Map)
237: */
238: public boolean canProcess(Map params) {
239: try {
240: return processParams(params);
241: } catch (Exception e) {
242: return false;
243: }
244: }
245:
246: /*
247: * Utility function for processing params
248: */
249: private boolean processParams(Map params) throws IOException {
250: if (!String.valueOf(PARAM_DBTYPE.lookUp(params))
251: .equalsIgnoreCase("mif")) {
252: throw new IOException("mif dbtype expected");
253: }
254:
255: String path = String.valueOf(PARAM_PATH.lookUp(params));
256: File file = new File(path);
257:
258: if (!file.isDirectory()) {
259: // Try to build a File object pointing to a .mif file
260: // Will throw an exception if the file cannot be found
261: MIFFile.getFileHandler(file.getParentFile(), MIFFile
262: .getMifName(file.getName()), ".mif", true);
263: }
264:
265: return true;
266: }
267:
268: /**
269: * <p>
270: * This method always returns true, because no specific libraries are
271: * required by MIFDataStore.
272: * </p>
273: *
274: * @see org.geotools.data.DataStoreFactorySpi#isAvailable()
275: */
276: public boolean isAvailable() {
277: return true;
278: }
279:
280: /**
281: * <p>
282: * Always return Collections#EMPTY_MAP, because no hints are available for
283: * now.
284: * </p>
285: *
286: * @see org.geotools.factory.Factory#getImplementationHints()
287: */
288: public Map getImplementationHints() {
289: // TODO Check possible use of hints for GeometryFactory, SRID.
290: return Collections.EMPTY_MAP;
291: }
292: }
|