Bookmark
com\esri\arcgis\sample\util\ExtentUtil.java
/* Copyright 2010 ESRI
* 
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
* 
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
* 
* See the use restrictions.
* 
*/
package com.esri.arcgis.sample.util;

import com.esri.adf.web.data.geometry.WebExtent;
import com.esri.adf.web.data.geometry.WebSpatialReference;
import java.util.StringTokenizer;

/**
 * Utility to work with WebExtent object
 */
public class ExtentUtil {
  public static String EXTENT_SEPARATOR = "|";

  /**
   * Convert argument String to its WebExtent representation
   */
  public static WebExtent stringToExtent(String string) {
    try {
      StringTokenizer tokenizer = new StringTokenizer(string, EXTENT_SEPARATOR);
      return new WebExtent(Double.parseDouble(tokenizer.nextToken()),
        Double.parseDouble(tokenizer.nextToken()),
        Double.parseDouble(tokenizer.nextToken()),
        Double.parseDouble(tokenizer.nextToken()),
        tokenizer.hasMoreTokens() ? WebSpatialReference.getWebSpatialReference(tokenizer.nextToken()) : null);
    }
    catch (Exception e) {
      return null;
    }
  }

  /**
   * Convert argument WebExtent to a String representation
   */
  public static String extentToString(WebExtent extent) {
    try {
      WebSpatialReference sr = extent.getSpatialReference();
      return new String(extent.getMinX() + EXTENT_SEPARATOR + extent.getMinY() + EXTENT_SEPARATOR + extent.getMaxX() + EXTENT_SEPARATOR + extent.getMaxY() + (sr != null ? EXTENT_SEPARATOR + sr.getDefinitionString() : ""));
    }
    catch (Exception e) {
      return null;
    }
  }
}