📄 mifloader.java
字号:
// **********************************************************************// // <copyright>// // BBN Technologies// 10 Moulton Street// Cambridge, MA 02138// (617) 873-8000// // Copyright (C) BBNT Solutions LLC. All rights reserved.// // </copyright>// **********************************************************************// // $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/layer/mif/MIFLoader.java,v $// $RCSfile: MIFLoader.java,v $// $Revision: 1.4.2.2 $// $Date: 2005/08/09 21:17:57 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap.layer.mif;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Font;import java.io.BufferedReader;import java.io.IOException;import java.util.StringTokenizer;import java.util.Vector;import com.bbn.openmap.omGraphics.OMGraphic;import com.bbn.openmap.omGraphics.OMGraphicConstants;import com.bbn.openmap.omGraphics.OMGraphicList;import com.bbn.openmap.omGraphics.OMLine;import com.bbn.openmap.omGraphics.OMPoint;import com.bbn.openmap.omGraphics.OMPoly;import com.bbn.openmap.omGraphics.OMText;import com.bbn.openmap.util.Debug;/** * A loader class for MIF files. Each MIF layer loading a file will * create an instance of this The class uses SwingWorker to do * processing in a thread Only the MIF PLine and Region options are * implemented * * 27th January 2004 - added some support for TEXT and POINT options * * @author Colin Mummery, modified January 2004 by Simon Bowen */public class MIFLoader { final static int PROCESS_HEADER = 0; final static int PROCESS_DATA = 1; final static int PROCESS_PLINE = 2; final static int PROCESS_POST_PLINE = 3; final static int PROCESS_MULTIPLE = 4; final static int PROCESS_REGION = 5; final static int PROCESS_REGION_HEADER = 6; final static int PROCESS_POST_REGION = 7; final static int PROCESS_POST_LINE = 8; final static String DATA_WORD = "Data"; final static String VERSION_WORD = "Version"; final static String DELIMITER_WORD = "Delimiter"; final static String COORDSYS_WORD = "Coordsys"; final static String PLINE_WORD = "PLine"; final static String LINE_WORD = "Line"; final static String MULTIPLE_WORD = "Multiple"; final static String PEN_WORD = "Pen"; final static String SMOOTH_WORD = "Smooth"; final static String REGION_WORD = "Region"; final static String BRUSH_WORD = "Brush"; final static String CENTER_WORD = "Center"; final static String POINT_WORD = "Point"; final static String TEXT_WORD = "Text"; BufferedReader br; OMGraphicList list; // if true we do a much faster line only rendering of the regions boolean accurate; // MIF CoordSys value for a Latitude Longitude coodinate system private static final String LATLONG_COORDSYS_DEF = "Earth Projection 1"; private float pointVisible = -1; // default is -1 private float textVisible = -1; // default is -1 /** * Loads a MIF file from the Reader and placing the appropriate * OMGraphics on the OMGraphicList * Parsing is done by a simple * loop and switch statements * * @param br BufferedReader to read the MIF file * @param accurate if true we do a much faster line only rendering * of the regions * @param textVisible the scale at which TEXT primitives should be * rendered * @param pointVisible the scale at which POINT primitives should * be rendered */ public MIFLoader(BufferedReader br, boolean accurate, float textVisible, float pointVisible) { super(); this.br = br; this.accurate = accurate; this.pointVisible = pointVisible; this.textVisible = textVisible; } public boolean isLoaded() { return list != null; } /** * Get the OMGraphicList from the loader, creating it from the * file if it hasn't been created yet. */ public OMGraphicList getList() { return getList(false); } /** * Get the OMGraphicList from the loader, with the option of * forcing it to be recreated from the source file if desired. */ public OMGraphicList getList(boolean reloadList) { try { if (reloadList || !isLoaded()) { if (isLoaded()) list.clear(); list = loadFile(); } return list; } catch (IOException ioe) { list = null; // } catch(MIFException mex) { // list = null; // Debug.error(mex.getMessage()); // mex.printStackTrace(); } return null; } public OMGraphicList loadFile() throws IOException, MIFException { float[] ptarray = null; // Used by region to do the polygon calculation float[] latpts = null; float[] lonpts = null; // Specifies the expected next action in the loop int action = PROCESS_HEADER; int number = 0; int count = 0; int multiple = 0; int multicnt = 0; // setting to true means we don't read the same line again boolean pushback; StringTokenizer st = null; String tok = null; pushback = false; int idx; OMPoly omp = null; OMLine oml = null; MIFPoint ompoint = null; OMText omtext = null; boolean ismultiple = false; OMGraphicList aList = new OMGraphicList(); // a vector of omgraphics for regions that allows adding and // deleting Vector omgs = new Vector(); MAIN_LOOP: while (true) { if (!pushback) { // if it's null then there's no more if ((st = getTokens(br)) == null) break MAIN_LOOP; tok = st.nextToken(); } else { pushback = false; // pushback was true so make it // false so it doesn't happen twice } SWITCH: switch (action) { case PROCESS_HEADER: if (isSame(tok, DATA_WORD)) { action = PROCESS_DATA; } else if (isSame(tok, VERSION_WORD)) { } else if (isSame(tok, DELIMITER_WORD)) { } else if (isSame(tok, COORDSYS_WORD)) { // check the CoordSys header, OpenMap only // directly // supports LatLong type of coordsys String coordSysLine = COORDSYS_WORD; while (st.hasMoreElements()) { coordSysLine += " " + st.nextElement(); } String goodCoordSys = COORDSYS_WORD + " " + LATLONG_COORDSYS_DEF; if (goodCoordSys.length() < coordSysLine.length()) { coordSysLine = coordSysLine.substring(0, goodCoordSys.length()); } else { goodCoordSys = goodCoordSys.substring(0, coordSysLine.length()); } // check that the CoordSys header matches the MIF // specification for LatLong type if (!isSame(coordSysLine, goodCoordSys)) { Debug.error("MIFLoader file has coordinate system: " + coordSysLine + ", requires " + goodCoordSys); // raise error, as the coordsys header was // invalid throw new MIFException("File appears to contain objects with an incompatible coordinate system (Must be Lat/Lon)."); } } break SWITCH; case PROCESS_DATA: omgs.clear(); if (isSame(tok, PLINE_WORD)) { tok = st.nextToken(); if (isSame(tok, MULTIPLE_WORD)) { multiple = Integer.parseInt(st.nextToken()); multicnt = 0; action = PROCESS_MULTIPLE; ismultiple = true; } else { number = Integer.parseInt(tok); ptarray = new float[number + number]; count = 0; action = PROCESS_PLINE; } } else if (isSame(tok, REGION_WORD)) { multiple = Integer.parseInt(st.nextToken()); multicnt = 0; action = PROCESS_REGION_HEADER; } else if (isSame(tok, LINE_WORD)) { float lon1 = Float.parseFloat(st.nextToken()); float lat1 = Float.parseFloat(st.nextToken()); float lon2 = Float.parseFloat(st.nextToken()); float lat2 = Float.parseFloat(st.nextToken()); oml = new OMLine(lat1, lon1, lat2, lon2, OMGraphicConstants.LINETYPE_STRAIGHT); action = PROCESS_POST_LINE; } else if (isSame(tok, POINT_WORD)) // handle a MIF // POINT primitive { // get the coordinates float lon1 = Float.parseFloat(st.nextToken()); float lat1 = Float.parseFloat(st.nextToken()); // construct the OM graphic ompoint = new MIFPoint(lat1, lon1, pointVisible); st = getTokens(br); // set the graphics attributes this.processSymbolWord(st, ompoint); // add to the graphic list for this layer aList.add(ompoint); action = PROCESS_DATA; } else if (isSame(tok, TEXT_WORD)) // handle a MIF // TEXT primitive { String textString = ""; // if the actual text is not on the same line as // the primitive declaration if (st.countTokens() < 1) { // get the next line st = getTokens(br); } // build up the display text string, while (st.hasMoreTokens()) { textString += st.nextToken(); } if (textString.length() >= 1) { // remove any surrounding " characters textString = textString.substring(1, textString.length() - 1); } // get the next line, it contains the coordinates st = getTokens(br); float lon1 = Float.parseFloat(st.nextToken()); float lat1 = Float.parseFloat(st.nextToken()); /* float lon2 = */Float.parseFloat(st.nextToken()); /* float lat2 = */Float.parseFloat(st.nextToken()); // create the OMGraphic for the text object omtext = new MIFText(lat1, lon1, textString, OMText.JUSTIFY_CENTER, textVisible); // the next line contains the text attributes st = getTokens(br); // set the attributes agains the omgraphic this.processFontWord(st, omtext); // add to the layers graphic list aList.add(omtext); action = PROCESS_DATA; } break SWITCH; // We have a line, tok is the first coord and the next // token is the second case PROCESS_PLINE: idx = count + count; ptarray[idx + 1] = Float.parseFloat(tok); ptarray[idx] = Float.parseFloat(st.nextToken()); count++; if (count == number) { omp = new OMPoly(ptarray, OMGraphic.DECIMAL_DEGREES, OMGraphic.LINETYPE_STRAIGHT); aList.add(omp); if (!ismultiple) { action = PROCESS_POST_PLINE; } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -