?? miffile.java
字號:
/*
* GeoTools - OpenSource mapping toolkit
* http://geotools.org
* (C) 2005-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;
* version 2.1 of the License.
*
* 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.mif;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URI;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Vector;
import java.util.logging.Logger;
import org.geotools.data.FeatureReader;
import org.geotools.data.FeatureWriter;
import org.geotools.feature.AttributeTypeBuilder;
import org.geotools.feature.AttributeTypes;
import org.geotools.feature.FeatureTypes;
import org.geotools.feature.SchemaException;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.MultiLineString;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.geom.PrecisionModel;
import com.vividsolutions.jts.geom.TopologyException;
import com.vividsolutions.jts.io.ParseException;
/**
* <p>
* MIFFile class allows sequential reading and writing of Features in MapInfo
* MIF/MID text file format with a FeatureReader<SimpleFeatureType, SimpleFeature> and FeatureWriter.
* </p>
*
* <p>
* This class has been developed starting from MapInfoDataSource.
* </p>
*
* <p>
* Open issues:
* </p>
*
* <ul>
* <li>
* CoordSys clause parsing is still not supported
* </li>
* </ul>
*
*
* @author Luca S. Percich, AMA-MI
* @author Paolo Rizzi, AMA-MI
* @source $URL: http://svn.geotools.org/trunk/modules/unsupported/mif/src/main/java/org/geotools/data/mif/MIFFile.java $
* @version $Id: MIFFile.java 29414 2008-02-21 12:35:44Z groldan $
*/
public class MIFFile {
// Geometry type identifier constants
private static final String TYPE_NONE = "none";
private static final String TYPE_POINT = "point";
private static final String TYPE_LINE = "line";
private static final String TYPE_PLINE = "pline";
private static final String TYPE_REGION = "region";
private static final String TYPE_TEXT = "text";
// The following object types are still not supported
private static final String TYPE_ARC = "arc";
private static final String TYPE_RECT = "rect"; // could be converted to polygon
private static final String TYPE_ROUNDRECT = "roundrect";
private static final String TYPE_ELLIPSE = "ellipse";
// New types introduced after version 6.0, still not supported
private static final String TYPE_MULTIPOINT = "multipoint";
private static final String TYPE_COLLECTION = "collection";
// String Style Constants
private static final String CLAUSE_SYMBOL = "symbol";
private static final String CLAUSE_PEN = "pen";
private static final String CLAUSE_SMOOTH = "smooth";
private static final String CLAUSE_CENTER = "center";
private static final String CLAUSE_BRUSH = "brush";
private static final String CLAUSE_FONT = "font";
private static final String CLAUSE_ANGLE = "angle";
private static final String CLAUSE_JUSTIFY = "justify";
private static final String CLAUSE_SPACING = "spacing";
private static final String CLAUSE_RIGHT = "right";
private static final String CLAUSE_LABEL = "label";
// Header parse Constants (& parameter names)
private static final String CLAUSE_COLUMNS = "columns";
public static final int MAX_STRING_LEN = 255; // Max length for MapInfo Char() fields
// Some (by now useless) default values
private static final String DEFAULT_PEN = "Pen (1,2,0)";
private static final String DEFAULT_BRUSH = "Brush (2,16777215,16777215)";
private static final String DEFAULT_SYMBOL = "Symbol (34,0,12)";
private static Logger LOGGER = org.geotools.util.logging.Logging.getLogger(
"org.geotools.data.mif.MIFFile");
// Header information
private HashMap header = new HashMap();
// File IO Variables
private File mifFile = null;
// File IO Variables
private File midFile = null;
// File IO Variables
private File mifFileOut = null;
// File IO Variables
private File midFileOut = null;
private Object[] featureDefaults = null;
private char chDelimiter = '\t'; // TAB is the default delimiter if not specified in header
// Schema variables
private SimpleFeatureType featureType = null;
private int numAttribs = 0;
private int geomFieldIndex = -1;
private URI namespace = null;
// Parameters for coordinate transformation during file i/o
private boolean useTransform = false;
private float multX = 1;
private float multY = 1;
private float sumX = 0;
private float sumY = 0;
// Options & parameters
private GeometryFactory geomFactory = null;
private Integer SRID = new Integer(0);
private String fieldNameCase;
private String geometryName;
private String geometryClass;
private boolean toGeometryCollection = false;
/**
* <p>
* This constructor opens an existing MIF/MID file, and creates the
* corresponding schema from the file header
* </p>
*
* <p>
* Allowed parameters in params Map:
* </p>
*
* <ul>
* <li>
* "namespace" = URI of the namespace prefix for FeatureTypes
* </li>
* <li>
* PARAM_GEOMFACTORY = GeometryFactory object to be used for creating
* geometries; alternatively, use PARAM_SRID;
* </li>
* <li>
* PARAM_SRID = SRID to be used for creating geometries;
* </li>
* <li>
* PARAM_FIELDCASE = field names tranformation: "upper" to uppercase |
* "lower" to lowercase | "" none;
* </li>
* <li>
* PARAM_GEOMNAME = <String>, name of the geometry field (defaults to
* "the_geom");
* </li>
* <li>
* PARAM_GEOMTYPE = geometry type handling: "untyped" uses Geometry class |
* "typed" force geometry to the type of the first valid geometry found in
* file | "multi" like typed, but forces LineString to MultilineString and
* Polygon to MultiPolygon; | "Point" | "LineString" | "MultiLineString" |
* "Polygon" | "MultiPolygon" | "Text" forces Geometry to Point and
* creates a MIF_TEXT String field in the schema
* </li>
* </ul>
*
* <p>
* Header clauses values can also be set in the params Map, but they might
* be overridden by values read from MIF header.
* </p>
*
* <p>
* Basic usage:
* </p>
* <pre><code>
* HashMap params = new HashMap();
* // params.put(MIFFile.PARAM_GEOMFACTORY, new GeometryFactory(new PrecisionModel(PrecisionModel.FLOATING_SINGLE), SRID));
* params.put(MIFFile.PARAM_SRID, new Integer(SRID));
* params.put(MIFFile.PARAM_FIELDCASE, "upper");
* params.put(MIFFile.PARAM_GEOMNAME, "GEOM");
* params.put(MIFFile.PARAM_GEOMTYPE, "typed");
* MIFFile mf = new MIFFile("c:/some_path/file.mif",params);
* FeatureType ft = mf.getSchema();
* FeatureReader<SimpleFeatureType, SimpleFeature> fr = mf.getFeatureReader();
* while (fr.hasNext()) {
* Feature in = fr.next();
* doSomethingWithFeature(in);
* }
* fr.close(); // closes file resources
* </code></pre>
*
* @param path Full pathName of the mif file, can be specified without the
* .mif extension
* @param params Parameters map
*
* @throws IOException If the specified mif file could not be opened
*/
public MIFFile(String path, Map params) throws IOException {
// TODO use url instead of String
super();
parseParams(params);
initFiles(path, true);
MIFFileTokenizer mifTokenizer = new MIFFileTokenizer(new BufferedReader(
new FileReader(mifFile)));
try {
readMifHeader(false, mifTokenizer);
} catch (Exception e) {
throw new IOException("Can't read MIF header: " + e.toString());
} finally {
try {
mifTokenizer.close();
} catch (Exception e) {
}
}
}
/**
* <p>
* This constructor creates a a new MIF/MID file given schema and path. If
* a .mif/.mid file pair already exists, it will be overwritten.
* </p>
*
* <p>
* Basic usage:
* </p>
* <pre><code>
* HashMap params = new HashMap();
* params.put(MIFFile.MIFDataStore.HCLAUSE_COORDSYS, "Nonearth \"m\"");
*
* MIFFile mf = new MIFFile("c:/some_path/", ft, params);
*
*
* FeatureWriter fw = mf.getFeatureWriter();
*
* while(...) {
* Feature f = fw.next();
* f.setAttribute(...,...);
* fw.write();
* }
*
* fw.close();
* </code></pre>
*
* @param path Full path & file name of the MIF file to create, can be
* specified without the .mif extension
* @param featureType
* @param params Parameter map
*
* @throws IOException Couldn't open the specified mif file for writing
* header
* @throws SchemaException Error setting the given FeatureType as the MIF
* schema
*/
public MIFFile(String path, SimpleFeatureType featureType, HashMap params)
throws IOException, SchemaException {
// TODO use url instead of String
super();
parseParams(params);
setSchema(featureType);
initFiles(path, false);
PrintStream outMif = new PrintStream(new FileOutputStream(mifFile, false));
PrintStream outMid = new PrintStream(new FileOutputStream(midFile, false));
// writes out header
outMif.println(exportHeader());
outMif.close();
outMid.close();
}
/**
* Parses the parameters map into fields:
*
* @param params
*
* @throws IOException Error getting parameters from the specified map
*/
private void parseParams(Map params) throws IOException {
if (params == null) {
params = new HashMap();
}
// Sets defaults for header
setHeaderClause(MIFDataStore.HCLAUSE_VERSION,
(String) getParam(MIFDataStore.HCLAUSE_VERSION, "300", false, params));
setHeaderClause(MIFDataStore.HCLAUSE_CHARSET,
(String) getParam(MIFDataStore.HCLAUSE_CHARSET, "WindowsLatin1",
false, params));
setHeaderClause(MIFDataStore.HCLAUSE_DELIMITER,
(String) getParam(MIFDataStore.HCLAUSE_DELIMITER,
String.valueOf(chDelimiter), false, params));
chDelimiter = getHeaderClause(MIFDataStore.HCLAUSE_DELIMITER).charAt(0);
setHeaderClause(MIFDataStore.HCLAUSE_UNIQUE,
(String) getParam(MIFDataStore.HCLAUSE_UNIQUE, "", false, params));
setHeaderClause(MIFDataStore.HCLAUSE_INDEX,
(String) getParam(MIFDataStore.HCLAUSE_INDEX, "", false, params));
setHeaderClause(MIFDataStore.HCLAUSE_COORDSYS,
(String) getParam(MIFDataStore.HCLAUSE_COORDSYS, "", false, params));
setHeaderClause(MIFDataStore.HCLAUSE_TRANSFORM,
(String) getParam(MIFDataStore.HCLAUSE_TRANSFORM, "", false, params));
SRID = (Integer) getParam(MIFDataStore.PARAM_SRID, new Integer(0),
false, params);
geomFactory = (GeometryFactory) getParam(MIFDataStore.PARAM_GEOMFACTORY,
null, false, params);
if (geomFactory == null) {
geomFactory = new GeometryFactory(new PrecisionModel(
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -