📄 csvlocationhandler.java
字号:
QuadTree qt = new QuadTree(90.0f, -180.0f, -90.0f, 180.0f, 100, 50f); if (!checkIndexSettings()) { return null; } BufferedReader streamReader = null; int lineCount = 0; Object token = null; TokenDecoder tokenHandler = getTokenDecoder(); // readHeader should be set to true if the first line has // been read, or if the csvHasHeader is false. boolean readHeader = !csvHasHeader; try { // This lets the property be specified as a file name // even if it's not specified as file:/<name> in // the properties file. URL csvURL = PropUtils.getResourceOrFileOrURL(null, locationFile); if (csvURL == null) { } streamReader = new BufferedReader(new InputStreamReader(csvURL.openStream())); CSVTokenizer csvt = new CSVTokenizer(streamReader); token = csvt.token(); while (!csvt.isEOF(token)) { int i = 0; Debug.message("csvlocation", "CSVLocationHandler| Starting a line | have" + (readHeader ? " " : "n't ") + "read header"); while (!csvt.isNewline(token) && !csvt.isEOF(token)) { if (readHeader) { tokenHandler.handleToken(token, i); } token = csvt.token(); // For some reason, the check above doesn't always // work if (csvt.isEOF(token)) { break; } i++; } if (!readHeader) { readHeader = true; } else { lineCount++; tokenHandler.createAndAddObjectFromTokens(qt); } token = csvt.token(); } } catch (java.io.IOException ioe) { throw new com.bbn.openmap.util.HandleError(ioe); } catch (ArrayIndexOutOfBoundsException aioobe) { throw new com.bbn.openmap.util.HandleError(aioobe); } catch (NumberFormatException nfe) { throw new com.bbn.openmap.util.HandleError(nfe); } catch (ClassCastException cce) { Debug.error("Problem reading entries in " + locationFile + ", check your index settings, first column = 0."); throw new com.bbn.openmap.util.HandleError(cce); } catch (NullPointerException npe) { Debug.error("Problem reading location file, check " + locationFile); throw new com.bbn.openmap.util.HandleError(npe); } catch (java.security.AccessControlException ace) { throw new com.bbn.openmap.util.HandleError(ace); } Debug.message("csvlocation", "CSVLocationHandler | Finished File:" + locationFile + ", read " + lineCount + " locations"); try { if (streamReader != null) { streamReader.close(); } } catch (java.io.IOException ioe) { throw new com.bbn.openmap.util.HandleError(ioe); } if (lineCount == 0 && readHeader) { Debug.output("CSVLocationHandler has read file, but didn't find any data.\n Check file for a header line, and make sure that the\n properties (csvFileHasHeader) is set properly for this CSVLocationHandler. Trying again without header..."); csvHasHeader = !csvHasHeader; return createData(); } return qt; } protected TokenDecoder getTokenDecoder() { return new DefaultLocationDecoder(); } /** * When a new Location object needs to be created from data read * in the CSV file, this method is called. This method lets you * extend the CSVLocationLayer and easily set what kind of * Location objects to use. * * @param lat latitude of location, decimal degrees. * @param lon longitude of location, decimal degrees. * @param name the label of the location. * @param iconURL the String for a URL for an icon. Can be null. * @return Location object for lat/lon/name/iconURL. */ protected Location createLocation(float lat, float lon, String name, String iconURL) { // This will turn into a regular location if iconURL is null. Location loc = new URLRasterLocation(lat, lon, name, iconURL); // let the layer handler default set these initially... loc.setShowName(isShowNames()); loc.setShowLocation(isShowLocations()); loc.setLocationHandler(this); getLocationDrawingAttributes().setTo(loc.getLocationMarker()); getNameDrawingAttributes().setTo(loc.getLabel()); loc.setDetails(name + " is at lat: " + lat + ", lon: " + lon); if (iconURL != null) { loc.setDetails(loc.getDetails() + " icon: " + iconURL); } Debug.message("csvlocation", "CSVLocationHandler " + loc.getDetails()); return loc; } /** * @param ranFile the file to be read. The file pointer shoutd be * set to the line you want read. * @return Array of strings representing the values between the * commas. */ protected String[] readCSVLineFromFile(BufferedReader ranFile, String[] retPaths) { if (ranFile != null) { try { String newLine = ranFile.readLine(); if (newLine == null) return null; StringTokenizer token = new StringTokenizer(newLine, ","); int numPaths = token.countTokens(); if (retPaths == null) { retPaths = new String[numPaths]; } else numPaths = retPaths.length; for (int i = 0; i < numPaths; i++) { retPaths[i] = token.nextToken(); } } catch (java.io.IOException ioe) { return null; } catch (java.util.NoSuchElementException nsee) { Debug.output("CSVLocationHandler: readCSVLineFromFile: oops"); } } return retPaths; } /** * Prepares the graphics for the layer. This is where the * getRectangle() method call is made on the location. * <p> * Occasionally it is necessary to abort a prepare call. When this * happens, the map will set the cancel bit in the LayerThread, * (the thread that is running the prepare). If this Layer needs * to do any cleanups during the abort, it should do so, but * return out of the prepare asap. * */ public Vector get(float nwLat, float nwLon, float seLat, float seLon, Vector graphicList) { // IF the quadtree has not been set up yet, do it! if (quadtree == null) { Debug.output("CSVLocationHandler: Figuring out the locations and names! (This is a one-time operation!)"); quadtree = createData(); } if (quadtree != null) { if (Debug.debugging("csvlocation")) { Debug.output("CSVLocationHandler|CSVLocationHandler.get() ul.lon = " + nwLon + " lr.lon = " + seLon + " delta = " + (seLon - nwLon)); } quadtree.get(nwLat, nwLon, seLat, seLon, graphicList); } return graphicList; } public void fillLocationPopUpMenu(LocationPopupMenu locMenu) { LocationCBMenuItem lcbi = new LocationCBMenuItem(LocationHandler.showname, locMenu, getLayer()); lcbi.setState(locMenu.getLoc().isShowName()); locMenu.add(lcbi); locMenu.add(new LocationMenuItem(showdetails, locMenu, getLayer())); } protected Box box = null; /** * Provides the palette widgets to control the options of showing * maps, or attribute text. * * @return Component object representing the palette widgets. */ public Component getGUI() { if (box == null) { JCheckBox showCSVLocationCheck, showNameCheck, forceGlobalCheck; JButton rereadFilesButton; showCSVLocationCheck = new JCheckBox("Show Locations", isShowLocations()); showCSVLocationCheck.setActionCommand(showLocationsCommand); showCSVLocationCheck.addActionListener(this); showCSVLocationCheck.setToolTipText("<HTML><BODY>Show location markers on the map.</BODY></HTML>"); showNameCheck = new JCheckBox("Show Location Names", isShowNames()); showNameCheck.setActionCommand(showNamesCommand); showNameCheck.addActionListener(this); showNameCheck.setToolTipText("<HTML><BODY>Show location names on the map.</BODY></HTML>"); forceGlobalCheck = new JCheckBox("Override Location Settings", isForceGlobal()); forceGlobalCheck.setActionCommand(forceGlobalCommand); forceGlobalCheck.addActionListener(this); forceGlobalCheck.setToolTipText("<HTML><BODY>Make these settings override those set<BR>on the individual map objects.</BODY></HTML>"); rereadFilesButton = new JButton("Reload Data From Source"); rereadFilesButton.setActionCommand(readDataCommand); rereadFilesButton.addActionListener(this); rereadFilesButton.setToolTipText("<HTML><BODY>Reload the data file, and put these settings<br>on the individual map objects.</BODY></HTML>"); box = Box.createVerticalBox(); box.add(showCSVLocationCheck); box.add(showNameCheck); box.add(forceGlobalCheck); box.add(rereadFilesButton); } return box; } // ---------------------------------------------------------------------- // ActionListener interface implementation // ---------------------------------------------------------------------- /** * The Action Listener method, that reacts to the palette widgets * actions. */ public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd == showLocationsCommand) { JCheckBox locationCheck = (JCheckBox) e.getSource(); setShowLocations(locationCheck.isSelected()); if (Debug.debugging("location")) { Debug.output("CSVLocationHandler::actionPerformed showLocations is " + isShowLocations()); } getLayer().repaint(); } else if (cmd == showNamesCommand) { JCheckBox namesCheck = (JCheckBox) e.getSource(); setShowNames(namesCheck.isSelected()); if (Debug.debugging("location")) { Debug.output("CSVLocationHandler::actionPerformed showNames is " + isShowNames()); } LocationLayer ll = getLayer(); if (namesCheck.isSelected() && ll.getDeclutterMatrix() != null && ll.getUseDeclutterMatrix()) { ll.doPrepare(); } else { ll.repaint(); } } else if (cmd == forceGlobalCommand) { JCheckBox forceGlobalCheck = (JCheckBox) e.getSource(); setForceGlobal(forceGlobalCheck.isSelected()); getLayer().repaint(); } else if (cmd == readDataCommand) { Debug.output("Re-reading Locations file"); quadtree = null; getLayer().doPrepare(); } else { Debug.error("Unknown action command \"" + cmd + "\" in LocationLayer.actionPerformed()."); } } public interface TokenDecoder { void handleToken(Object token, int column); void createAndAddObjectFromTokens(DataOrganizer organizer); } public class DefaultLocationDecoder implements TokenDecoder { protected String name; protected float lat; protected float lon; protected String iconURL; public DefaultLocationDecoder() {} public void reset() { name = null; lat = 0f; lon = 0f; iconURL = null; } public void handleToken(Object token, int i) { if (i == nameIndex) { if (token instanceof Double) { name = ((Double) token).toString(); } else { name = (String) token; } } else if (i == latIndex) { lat = ((Double) token).floatValue(); } else if (i == lonIndex) { lon = ((Double) token).floatValue(); if (eastIsNeg) { lon *= -1; } } else if (i == iconIndex) { iconURL = (String) token; } } public void createAndAddObjectFromTokens(DataOrganizer organizer) { // Debug.output(iconURL); if (iconURL == null && defaultIconURL != null) { iconURL = defaultIconURL; } Location loc = createLocation(lat, lon, name, iconURL); organizer.put(lat, lon, loc); reset(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -