RegionsJSON.java

  1. package gov.usgs.earthquake.geoserve;


  2. import javax.json.JsonArray;
  3. import javax.json.JsonObject;
  4. import javax.json.JsonValue;

  5. import gov.usgs.earthquake.qdm.Point;
  6. import gov.usgs.earthquake.qdm.Region;
  7. import gov.usgs.earthquake.qdm.Regions;

  8. /**
  9.  * RegionsJSON reads GeoJSON formatted ANSS Authoritative Regions.
  10.  */
  11. public class RegionsJSON {

  12.     /**
  13.      * Parse {@link gov.usgs.earthquake.qdm.Regions} from a GeoJSON feature collection.
  14.      *
  15.      * @param json geojson feature collection.
  16.      * @return Regions
  17.      */
  18.     public Regions parseRegions(final JsonObject json) {
  19.         Regions regions = new Regions();
  20.         // NEIC is always the default
  21.         regions.defaultNetid = "us";

  22.         JsonArray features = json.getJsonArray("features");
  23.         for (JsonValue value : features) {
  24.             JsonObject jsonRegion = value.asJsonObject();
  25.             Region region = parseRegion(jsonRegion);
  26.             regions.netids.add(region.netid);
  27.             regions.regions.add(region);
  28.         }

  29.         return regions;
  30.     }

  31.     /**
  32.      * Parse {@link gov.usgs.earthquake.qdm.Region} from a GeoJSON feature.
  33.      *
  34.      * @param json geojson feature.
  35.      * @return region
  36.      */
  37.     public Region parseRegion(final JsonObject json) {
  38.         JsonObject properties = json.getJsonObject("properties");
  39.         String networkId = properties.getString("network");
  40.         String regionId = properties.getString("region");

  41.         Region region = new Region(networkId, regionId);
  42.         JsonArray coordinates = json.getJsonObject("geometry")
  43.                 .getJsonArray("coordinates");
  44.         for (JsonValue coord : coordinates.getJsonArray(0)) {
  45.             JsonArray point = coord.asJsonArray();
  46.             region.points.add(new Point(
  47.                 point.getJsonNumber(0).doubleValue(),
  48.                 point.getJsonNumber(1).doubleValue()));
  49.         }

  50.         return region;
  51.     }

  52. }