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 com.vividsolutions.jts.geom.PrecisionModel;
020: import org.geotools.feature.AttributeType;
021: import org.geotools.feature.AttributeTypes;
022: import org.geotools.feature.Feature;
023: import org.geotools.feature.FeatureType;
024: import org.geotools.feature.FeatureTypeBuilder;
025: import org.geotools.feature.SchemaException;
026: import org.geotools.filter.ExpressionBuilder;
027: import org.opengis.filter.Filter;
028: import org.geotools.filter.parser.ParseException;
029: import org.geotools.test.TestData;
030: import java.io.File;
031: import java.io.FileInputStream;
032: import java.io.FileNotFoundException;
033: import java.io.FileOutputStream;
034: import java.io.IOException;
035: import java.net.URI;
036: import java.nio.channels.FileChannel;
037: import java.util.HashMap;
038: import java.util.logging.Logger;
039:
040: /**
041: * DOCUMENT ME!
042: *
043: * @author Luca S. Percich, AMA-MI
044: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/mif/src/test/java/org/geotools/data/mif/MIFTestUtils.java $
045: */
046: public class MIFTestUtils {
047: public static final int SRID = 26591;
048: public static final GeometryFactory geomFactory = new GeometryFactory(
049: new PrecisionModel(PrecisionModel.FLOATING_SINGLE), SRID);
050: public static final String coordsysClause = "Earth Projection 8, 87, \"m\", 9, 0, 0.9996, 1500000, 0 Bounds (-6746230.6469, -9998287.38389) (9746230.6469, 9998287.38389)";
051:
052: /**
053: * DOCUMENT ME!
054: *
055: * @return DOCUMENT ME!
056: */
057: private static File getDataPath() {
058: try {
059: return TestData.file(MIFTestUtils.class, null);
060: } catch (IOException e) {
061: return null;
062: }
063: }
064:
065: /**
066: * DOCUMENT ME!
067: *
068: * @param inMif DOCUMENT ME!
069: * @param outMif DOCUMENT ME!
070: *
071: * @throws IOException
072: */
073: public static void copyMif(String inMif, String outMif)
074: throws IOException {
075: File path = getDataPath();
076:
077: copyFileUsingChannels(MIFFile.getFileHandler(path, inMif,
078: ".mif", true), new File(path, outMif + ".mif"));
079: copyFileUsingChannels(MIFFile.getFileHandler(path, inMif,
080: ".mid", true), new File(path, outMif + ".mid"));
081: }
082:
083: /**
084: * DOCUMENT ME!
085: *
086: * @param in DOCUMENT ME!
087: * @param out DOCUMENT ME!
088: *
089: * @throws IOException
090: */
091: public static void copyFileUsingChannels(File in, File out)
092: throws IOException {
093: FileChannel sourceChannel = new FileInputStream(in)
094: .getChannel();
095: FileChannel destinationChannel = new FileOutputStream(out)
096: .getChannel();
097: destinationChannel.transferFrom(sourceChannel, 0, sourceChannel
098: .size());
099: sourceChannel.close();
100: destinationChannel.close();
101: }
102:
103: /**
104: * DOCUMENT ME!
105: *
106: * @param mifName MIF file to be deleted (no extension)
107: */
108: public static void safeDeleteMif(String mifName) {
109: File f;
110:
111: try {
112: f = MIFFile.getFileHandler(getDataPath(), mifName, ".mif",
113: false);
114:
115: if (f.exists()) {
116: f.delete();
117: }
118:
119: f = MIFFile.getFileHandler(getDataPath(), mifName, ".mid",
120: false);
121:
122: if (f.exists()) {
123: f.delete();
124: }
125: } catch (FileNotFoundException e) {
126: }
127: }
128:
129: /**
130: * Deletes temporary files in test-data
131: */
132: public static void cleanFiles() {
133: safeDeleteMif("grafo_new");
134: safeDeleteMif("grafo_out");
135: safeDeleteMif("mixed_wri");
136: safeDeleteMif("grafo_append");
137: safeDeleteMif("newschema");
138: safeDeleteMif("mixed_fs");
139: }
140:
141: /**
142: * DOCUMENT ME!
143: *
144: * @param f DOCUMENT ME!
145: * @param logger DOCUMENT ME!
146: */
147: public static void printFeature(Feature f, Logger logger) {
148: print(f.toString(), logger);
149: }
150:
151: /**
152: * Utility print method
153: *
154: * @param msg DOCUMENT ME!
155: * @param logger DOCUMENT ME!
156: */
157: public static void print(String msg, Logger logger) {
158: logger.fine(msg);
159: }
160:
161: /**
162: * DOCUMENT ME!
163: *
164: * @param ft DOCUMENT ME!
165: * @param logger DOCUMENT ME!
166: */
167: public static void printSchema(FeatureType ft, Logger logger) {
168: print(ft.getTypeName(), logger);
169:
170: AttributeType[] attrs = ft.getAttributeTypes();
171:
172: for (int i = 0; i < attrs.length; i++) {
173: print(" " + attrs[i].getName() + " - "
174: + attrs[i].getType().toString() + "("
175: + AttributeTypes.getFieldLength(attrs[i], 0) + ")",
176: logger);
177: }
178: }
179:
180: /**
181: * DOCUMENT ME!
182: *
183: * @param dbtype DOCUMENT ME!
184: * @param path DOCUMENT ME!
185: * @param uri DOCUMENT ME!
186: * @param geomType DOCUMENT ME!
187: *
188: * @return DOCUMENT ME!
189: */
190: protected static HashMap getParams(String dbtype, String path,
191: URI uri, String geomType) {
192: HashMap params = new HashMap();
193:
194: params.put("dbtype", dbtype);
195: params.put("path", path);
196:
197: if (uri != null) {
198: params.put("namespace", uri);
199: }
200:
201: params.put(MIFDataStore.PARAM_FIELDCASE, "upper");
202: params.put(MIFDataStore.PARAM_GEOMNAME, "the_geom");
203: params.put(MIFDataStore.PARAM_GEOMTYPE, geomType);
204:
205: // params.put(MIFDataStore.PARAM_GEOMFACTORY, MIFTestUtils.geomFactory);
206: params.put(MIFDataStore.PARAM_SRID, new Integer(SRID));
207:
208: params.put(MIFDataStore.HCLAUSE_COORDSYS,
209: MIFTestUtils.coordsysClause);
210:
211: return params;
212: }
213:
214: /**
215: * DOCUMENT ME!
216: *
217: * @param dbtype DOCUMENT ME!
218: * @param path DOCUMENT ME!
219: * @param uri DOCUMENT ME!
220: *
221: * @return DOCUMENT ME!
222: */
223: protected static HashMap getParams(String dbtype, String path,
224: URI uri) {
225: return getParams(dbtype, path, uri, "untyped");
226: }
227:
228: /**
229: * Duplicates a given feature type
230: *
231: * @param inFeatureType
232: * @param typeName
233: *
234: *
235: * @throws SchemaException
236: */
237: protected static FeatureType duplicateSchema(
238: FeatureType inFeatureType, String typeName)
239: throws SchemaException {
240: FeatureTypeBuilder builder = FeatureTypeBuilder
241: .newInstance(typeName);
242:
243: for (int i = 0; i < inFeatureType.getAttributeCount(); i++) {
244: builder.addType(inFeatureType.getAttributeType(i));
245: }
246:
247: return builder.getFeatureType();
248: }
249:
250: /**
251: * DOCUMENT ME!
252: *
253: * @param expression DOCUMENT ME!
254: *
255: * @return DOCUMENT ME!
256: */
257: protected static Filter parseFilter(String expression) {
258: try {
259: return (Filter) ExpressionBuilder.parse(expression);
260: } catch (ParseException e) {
261: return Filter.EXCLUDE;
262: }
263: }
264:
265: /**
266: * DOCUMENT ME!
267: *
268: * @param fileName DOCUMENT ME!
269: *
270: * @return DOCUMENT ME!
271: */
272: protected static String fileName(String fileName) {
273: if (fileName.equals("")) {
274: return getDataPath().getAbsolutePath();
275: }
276:
277: File file = new File(getDataPath(), fileName);
278:
279: return file.getAbsolutePath();
280: }
281: }
|