Region.java

  1. package gov.usgs.earthquake.qdm;

  2. import java.util.ArrayList;

  3. /**
  4.  * A polygon without holes.
  5.  *
  6.  * Points are assumed to use x=longitude, y=latitude.
  7.  * A "default" region has no boundary points, and contains all points.
  8.  */
  9. public class Region {

  10.   /** String for net id */
  11.   public String netid;
  12.   /** String for region id */
  13.   public String regionid;

  14.   /** Arraylist of points */
  15.   public ArrayList<Point> points;

  16.   /**
  17.    * Region constructor
  18.    * @param netid string
  19.    * @param regionid string
  20.    */
  21.   public Region(String netid, String regionid) {
  22.     this.netid = netid;
  23.     this.regionid = regionid;
  24.     this.points = new ArrayList<Point>();
  25.   }

  26.   /**
  27.    * Method to determine if this lat-lon in this region? In or out algorithm taken
  28.    * from an algorithm by Edwards and Coleman of Oak Ridge Lab, version for BNL by
  29.    * Benkovitz translated to C by Andy Michael and into Java by Alan Jones.
  30.    *
  31.    * @param xy point
  32.    * @return bool if point is in region
  33.    */
  34.   public boolean inpoly(Point xy) {
  35.     int in;
  36.     double sine;
  37.     boolean bool = false;
  38.     int inside = 0;
  39.     int nvert = this.points.size();
  40.     // If there are no points in the region, assume default region
  41.     // and declare the point inside
  42.     if (nvert == 0)
  43.       return true;
  44.     Point p[] = (Point[]) this.points.toArray(new Point[0]);
  45.     double x = xy.x;
  46.     double y = xy.y;
  47.     for (int i = 0; i < nvert; ++i) {
  48.       in = i + 1;
  49.       if (in >= nvert)
  50.         in = 0;
  51.       if (p[in].y == p[i].y && p[in].x == p[i].x)
  52.         continue;
  53.       sine = (x - p[i].x) * (p[in].y - p[i].y) - (y - p[i].y) * (p[in].x - p[i].x);
  54.       if (sine == 0) {
  55.         if (((x - p[i].x) * (p[in].x - p[i].x) + (y - p[i].y) * (p[in].y - p[i].y))
  56.             * ((x - p[in].x) * (p[in].x - p[i].x) + (y - p[in].y) * (p[in].y - p[i].y)) > 0)
  57.           continue;
  58.         return true;
  59.       }
  60.       if (y > p[in].y && y <= p[i].y && sine < 0 || y <= p[in].y && y > p[i].y && sine > 0) {
  61.         bool = !bool;
  62.       }
  63.     }
  64.     if (bool)
  65.       inside = 1;
  66.     if (inside != 0)
  67.       return true;
  68.     else
  69.       return false;
  70.   }

  71. }