?? earthquakelayer.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/EarthquakeLayer.java,v $// $RCSfile: EarthquakeLayer.java,v $// $Revision: 1.5.2.2 $// $Date: 2005/08/09 19:21:28 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap.layer;import java.awt.Color;import java.awt.Component;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.net.Socket;import java.util.NoSuchElementException;import java.util.Properties;import java.util.StringTokenizer;import java.util.Vector;import javax.swing.JButton;import javax.swing.JPanel;import com.bbn.openmap.event.MapMouseListener;import com.bbn.openmap.omGraphics.OMGraphic;import com.bbn.openmap.omGraphics.OMGraphicList;import com.bbn.openmap.omGraphics.OMPoint;import com.bbn.openmap.omGraphics.OMText;import com.bbn.openmap.util.Debug;import com.bbn.openmap.util.PaletteHelper;import com.bbn.openmap.util.PropUtils;/** * Get data about recent earthquakes from the USGS finger sites and * display it. * <p> * Debugging information is printed when the OpenMap Viewer is launch * with -Ddebug.earthquake flag. * <P> * * <pre> * * * # Properties for the Earthquake Layer * earthquake.sites=<finger site> <finger site> ... * # in seconds * earthquake.queryinterval=300 * * * </pre> */public class EarthquakeLayer extends OMGraphicHandlerLayer implements MapMouseListener { public final static transient String fingerSitesProperty = "sites"; public final static transient String queryIntervalProperty = "queryInterval"; /** * Sites to finger user the user `quake'. */ protected String fingerSites[] = { "scec.gps.caltech.edu", "geophys.washington.edu", "giseis.alaska.edu", "mbmgsun.mtech.edu", "quake.eas.slu.edu" }; // Old sites // "gldfs.cr.usgs.gov", // "andreas.wr.usgs.gov", // "seismo.unr.edu", // "eqinfo.seis.utah.edu", // "sisyphus.idbsu.edu", // "info.seismo.usbr.gov", // "vtso.geol.vt.edu", // "tako.wr.usgs.gov", // "ldeo.columbia.edu" /** * Sites that are actively being queried. */ protected boolean activeSites[] = new boolean[fingerSites.length]; /** Default to 5 minutes. */ private long fetchIntervalMillis = 300 * 1000; // lat-lon data of the earthquakes protected float llData[] = new float[0]; // floating information about the earthquakes protected String infoData[] = new String[0]; // floating information about the earthquakes protected String drillData[] = new String[0]; private long lastDataFetchTime = 0; protected Color lineColor = Color.red; protected boolean showingInfoLine = false; /** The layer GUI. */ protected JPanel gui = null; /** * Construct an EarthquakeLayer. */ public EarthquakeLayer() { activeSites[0] = true; setProjectionChangePolicy(new com.bbn.openmap.layer.policy.ListResetPCPolicy(this)); } /** * Fetch data from finger sites, if needed, generate the * OMGraphics with the current projection regardless. */ public synchronized OMGraphicList prepare() { if (needToRefetchData()) { parseData(getEarthquakeData()); } return generateGraphics(); } /** * Fetches data if it hasn't been fetched in a while. */ protected boolean needToRefetchData() { long now = System.currentTimeMillis(); long last = lastDataFetchTime; if ((last + fetchIntervalMillis) < now) { lastDataFetchTime = now; return true; } return false; } /** * Create the graphics. */ protected OMGraphicList generateGraphics() { OMGraphicList omgraphics = new OMGraphicList(); OMPoint circ; OMText text; int circle_r = 2; int circle_h = 5; for (int i = 0, j = 0; i < llData.length; i += 2, j++) { // grouping OMGraphicList group = new OMGraphicList(2); // XY-Circle at LatLonPoint circ = new OMPoint(llData[i], llData[i + 1], circle_r); circ.setOval(true); circ.setFillPaint(lineColor); group.add(circ); // Info text = new OMText(llData[i], llData[i + 1], 0, circle_h + 10, infoData[j], java.awt.Font.decode("SansSerif"), OMText.JUSTIFY_CENTER); text.setLinePaint(lineColor); group.add(text); group.setAppObject(new Integer(j));//remember index omgraphics.add(group); } omgraphics.generate(getProjection(), false); return omgraphics; } /** * Parse the finger site data. * * @param data Vector */ protected void parseData(Vector data) { int nLines = data.size(); llData = new float[2 * nLines]; infoData = new String[nLines]; drillData = new String[nLines]; for (int i = 0, j = 0, k = 0; i < nLines; i++) { String line = (String) data.elementAt(i); // Read a line of input and break it down StringTokenizer tokens = new StringTokenizer(line); String sdate = tokens.nextToken(); String stime = tokens.nextToken(); String slat = tokens.nextToken(); String slon = tokens.nextToken(); if (slon.startsWith("NWSE"))// handle ` ' in LatLon data slon = tokens.nextToken(); String sdep = tokens.nextToken(); if (sdep.startsWith("NWSE"))// handle ` ' in LatLon data sdep = tokens.nextToken(); String smag = tokens.nextToken(); String q = tokens.nextToken(); String scomment = tokens.nextToken("\r\n"); if (q.length() > 1) { scomment = q + " " + scomment; } infoData[j] = smag; drillData[j++] = sdate + " " + stime + " (UTC) " + slat + " " + slon + " " + smag + " " + scomment; // Remove NESW from lat and lon before converting to float int west = slon.indexOf("W"); int south = slat.indexOf("S"); if (west >= 0) slon = slon.replace('W', '\0'); else slon = slon.replace('E', '\0'); if (south >= 0) slat = slat.replace('S', '\0'); else slat = slat.replace('N', '\0'); slon = slon.trim(); slat = slat.trim(); float flat = 0, flon = 0; try { flat = new Float(slat).floatValue(); flon = new Float(slon).floatValue(); } catch (NumberFormatException e) { Debug.error("EarthquakeLayer.parseData(): " + e + " line: " + line); } // replace West and South demarcations with minus sign if (south >= 0) flat = -flat; if (west >= 0) flon = -flon; llData[k++] = flat; llData[k++] = flon; } } /** * Get the earthquake data from the USGS. Should be called in a * SwingWorker thread, or you will freeze the application. * * @return Vector containing information from the websites. */ protected Vector getEarthquakeData() { Vector linesOfData = new Vector(); Socket quakefinger = null; PrintWriter output = null; BufferedReader input = null; String line; for (int i = 0; i < activeSites.length; i++) { // skip sites which aren't on the active list if (!activeSites[i]) continue; try { if (Debug.debugging("earthquake")) { Debug.output("Opening socket connection to " + fingerSites[i]); } quakefinger = new Socket(fingerSites[i], 79);//open // connection // to // finger // port quakefinger.setSoTimeout(120 * 1000);// 2 minute // timeout output = new PrintWriter(new OutputStreamWriter(quakefinger.getOutputStream()), true); input = new BufferedReader(new InputStreamReader(quakefinger.getInputStream()), 1); output.println("/W quake");// use `/W' flag for long // output } catch (IOException e) { Debug.error("EarthquakeLayer.getEarthquakeData(): " + "can't open or write to socket: " + e); continue; } try { // add data lines to list while ((line = input.readLine()) != null) { if (Debug.debugging("earthquake")) { Debug.output("EarthquakeLayer.getEarthQuakeData(): " + line); } if (line.length() == 0) continue; if (!Character.isDigit(line.charAt(0))) continue; line = hackY2K(line); if (line == null) continue; linesOfData.addElement(line); } } catch (IOException e) { Debug.error("EarthquakeLayer.getEarthquakeData(): " + "can't read from the socket: " + e); if (cancelled) { return null; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -