GeoserveRegionsService.java

  1. package gov.usgs.earthquake.geoserve;

  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.math.BigDecimal;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import java.net.URLEncoder;
  8. import java.nio.charset.StandardCharsets;

  9. import javax.json.Json;
  10. import javax.json.JsonObject;
  11. import javax.json.JsonReader;

  12. import gov.usgs.util.StreamUtils;

  13. /**
  14.  * Access regions from the Geoserve regions service.
  15.  */
  16. public class GeoserveRegionsService {
  17.   /** Default URL for GeoServe Regions service. */
  18.   public static final String DEFAULT_ENDPOINT_URL = "https://earthquake.usgs.gov/ws/geoserve/regions.json";
  19.   /** Default connection timeout */
  20.   public static final int DEFAULT_CONNECT_TIMEOUT = 300; // ms
  21.   /** Default read timeout */
  22.   public static final int DEFAULT_READ_TIMEOUT = 1700; // ms

  23.   /** Configured URL for GeoServe Regions service. */
  24.   private String endpointUrl;
  25.   private int connectTimeout;
  26.   private int readTimeout;

  27.   /** Default constructor */
  28.   public GeoserveRegionsService() {
  29.     this(DEFAULT_ENDPOINT_URL, DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT);
  30.   }

  31.   /**
  32.    * Constructor taking in endpointURL
  33.    * @param endpointUrl for places service
  34.    */
  35.   public GeoserveRegionsService(final String endpointUrl) {
  36.     this(endpointUrl, DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT);
  37.   }

  38.   /**
  39.    * Constructor taking in timeouts and using default endpoint URL
  40.    * @param connectTimeout in ms
  41.    * @param readTimeout in ms
  42.    */
  43.   public GeoserveRegionsService(final int connectTimeout, final int readTimeout) {
  44.     this(DEFAULT_ENDPOINT_URL, connectTimeout, readTimeout);
  45.   }

  46.   /**
  47.    * Custom constructor
  48.    * @param endpointUrl for Places service
  49.    * @param connectTimeout in ms
  50.    * @param readTimeout in ms
  51.    */
  52.   public GeoserveRegionsService(final String endpointUrl, final int connectTimeout, final int readTimeout) {
  53.     this.setEndpointURL(endpointUrl);
  54.     this.setConnectTimeout(connectTimeout);
  55.     this.setReadTimeout(readTimeout);
  56.   }

  57.   /** @return connectTimemout */
  58.   public int getConnectTimeout() {
  59.     return this.connectTimeout;
  60.   }

  61.   /** @return endpointURL */
  62.   public String getEndpointURL() {
  63.     return this.endpointUrl;
  64.   }

  65.   /**
  66.    * Find an event in the Region service via a latitude and longitude
  67.    * @param latitude of event
  68.    * @param longitude of event
  69.    * @return JSONObject of Fe Region
  70.    * @throws IOException on IO error
  71.    * @throws MalformedURLException or URL error
  72.    */
  73.   public JsonObject getFeRegion(BigDecimal latitude, BigDecimal longitude)
  74.       throws IOException, MalformedURLException {
  75.     final URL url = new URL(this.endpointUrl +
  76.         "?type=fe" +
  77.         "&latitude=" + URLEncoder.encode(latitude.toString(), StandardCharsets.UTF_8.toString()) +
  78.         "&longitude=" + URLEncoder.encode(longitude.toString(), StandardCharsets.UTF_8.toString())
  79.     );

  80.     try (InputStream in = StreamUtils.getURLInputStream(url, this.connectTimeout, this.readTimeout)) {
  81.       JsonReader reader = Json.createReader(in);
  82.       JsonObject json = reader.readObject();
  83.       reader.close();
  84.       return json.getJsonObject("fe");
  85.     }
  86.   }

  87.   /**
  88.    * Get name of FeRegion
  89.    * @param latitude of event
  90.    * @param longitude of event
  91.    * @return string of FeRegion name
  92.    * @throws IOException on IO error
  93.    * @throws MalformedURLException or URL error
  94.    */
  95.   public String getFeRegionName(BigDecimal latitude, BigDecimal longitude) throws IOException, MalformedURLException {
  96.     JsonObject region = this.getFeRegion(latitude, longitude);
  97.     String feRegionName = region.getJsonArray("features")
  98.         .getJsonObject(0)
  99.         .getJsonObject("properties")
  100.         .getJsonString("name")
  101.         .getString();

  102.     return feRegionName;
  103.   }

  104.   /** @return readTimeout */
  105.   public int getReadTimeout() {
  106.     return this.readTimeout;
  107.   }

  108.   /** @param connectTimeout int to set */
  109.   public void setConnectTimeout(final int connectTimeout) {
  110.     this.connectTimeout = connectTimeout;
  111.   }

  112.   /** @param endpointUrl string to set */
  113.   public void setEndpointURL(final String endpointUrl) {
  114.     this.endpointUrl = endpointUrl;
  115.   }

  116.   /** @param readTimeout int to set */
  117.   public void setReadTimeout(final int readTimeout) {
  118.     this.readTimeout = readTimeout;
  119.   }

  120.   // as needed, implement full GeoServe places API options
  121. }