Regions.java

  1. package gov.usgs.earthquake.qdm;

  2. import java.util.ArrayList;

  3. /**
  4.  * Set of regions.
  5.  *
  6.  * @author - Alan Jones, 1999.09.10 <br>
  7.  *         2002.01.16: Convert from "regions" directory with above files to
  8.  *         regions.xml file. <br>
  9.  *         2003.01.06: Make class public, add support for non-file input so that
  10.  *         we can read from a JAR, add isDefaultNetID method to check for the
  11.  *         default network (US), add isAuthor method to check if the event is
  12.  *         authoritative
  13.  *
  14.  *         2018-08-21: JMF, reimplement parsing logic outside class. Update code
  15.  *         to Java 1.8+.
  16.  */
  17. public class Regions {

  18.     /** Default network */
  19.     public String defaultNetid;
  20.     /** Array of network ids, e.g. nc, us, etc. */
  21.     public ArrayList<String> netids;
  22.     /** Array of regions */
  23.     public ArrayList<Region> regions;

  24.     /**
  25.      * Create a new set of regions.
  26.      */
  27.     public Regions() {
  28.         this.defaultNetid = "";
  29.         this.netids = new ArrayList<String>();
  30.         this.regions = new ArrayList<Region>();
  31.     }

  32.     /**
  33.      * Is this netid in the set of regions? The default net covers the whole world
  34.      * so it is always valid since it has no finite boundaries.
  35.      *
  36.      * @param netid A netid (nc, us, etc.)
  37.      * @return boolean if netid is valid
  38.      */
  39.     public boolean isValidnetID(final String netid) {
  40.         for (String i : this.netids) {
  41.             if (netid.equalsIgnoreCase(i)) {
  42.                 return true;
  43.             }
  44.         }
  45.         return false;
  46.     }

  47.     //
  48.     // [KF] - add methods to check for the default network (US)
  49.     //

  50.     /**
  51.      * Checks if network ID is the default network.
  52.      *
  53.      * @param netid network ID
  54.      * @return true if default network.
  55.      */
  56.     public boolean isDefaultNetID(final String netid) {
  57.         return this.defaultNetid.equalsIgnoreCase(netid);
  58.     }

  59.     /**
  60.      * Checks if an event's network ID is the default network.
  61.      *
  62.      * @param eq EQ event
  63.      * @return true if default network.
  64.      */
  65.     public boolean isDefaultNetID(final EQEvent eq) {
  66.         return this.isDefaultNetID(eq.getNetID());
  67.     }

  68.     //
  69.     // [KF] - add methods to determine if the event is from the authoritative
  70.     // network.
  71.     //

  72.     /**
  73.      * Determines if the event is from the authoritative network.
  74.      *
  75.      * @param netid network ID
  76.      * @param p     event point
  77.      * @return true if event is authoritative
  78.      */
  79.     public boolean isAuthor(final String netid, final Point p) {
  80.         if (this.isDefaultNetID(netid)) {
  81.             // if any non-default regions match, default is not authoritative
  82.             for (Region region : this.regions) {
  83.                 if (region.netid.equalsIgnoreCase(netid)) {
  84.                     continue;
  85.                 }
  86.                 if (region.inpoly(p)) {
  87.                     // another region is authoritative
  88.                     return false;
  89.                 }
  90.             }
  91.             // no other regions authoritative
  92.             return true;
  93.         } else {
  94.             // if any network regions match, network is authoritative
  95.             for (Region region : regions) {
  96.                 if (region.netid.equalsIgnoreCase(netid) && region.inpoly(p)) {
  97.                     // network is authoritative
  98.                     return true;
  99.                 }
  100.             }
  101.             // network is not authoritative
  102.             return false;
  103.         }
  104.     }

  105.     /**
  106.      * Determines if the event is from the authoritative network.
  107.      *
  108.      * @param eq EQ event
  109.      * @return true if event is authoritative
  110.      */
  111.     public boolean isAuthor(final EQEvent eq) {
  112.         return this.isAuthor(eq.getNetID(), eq.getPoint());
  113.     }

  114. }