arcgissamples\scenario\map\InternetConnectionInfo.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
GIS Client
arcgissamples\scenario\map\InternetConnectionInfo.java
/* Copyright 2015 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 at <your ArcGIS install location>/DeveloperKit10.4/userestrictions.txt.
* 
*/
package arcgissamples.scenario.map;

import java.io.IOException;
import java.util.Hashtable;

import com.esri.arcgis.carto.IMapDescription;
import com.esri.arcgis.carto.IMapServerInfo;
import com.esri.arcgis.carto.ImageDescription;
import com.esri.arcgis.carto.ImageDisplay;
import com.esri.arcgis.carto.ImageType;
import com.esri.arcgis.carto.MapExtent;
import com.esri.arcgis.carto.MapServerIP;
import com.esri.arcgis.carto.esriImageFormat;
import com.esri.arcgis.carto.esriImageReturnType;
import com.esri.arcgis.geometry.Envelope;
import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.geometry.IPointCollection;
import com.esri.arcgis.gisclient.AGSServerObjectName;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.LongArray;


public class InternetConnectionInfo extends MapServerConnectionInfo {

  private Hashtable<String, IMapDescription> mapDescriptionTable = null;

  public InternetConnectionInfo() {

  }

  public void init() {
    try {

      if(serverObjectstable == null) return;

      //get name object
      AGSServerObjectName mapServerName = (AGSServerObjectName)serverObjectstable.get(mapConfigurationName);
      MapServerIP agsServerObject = (MapServerIP)mapServerName.open();

      //get map server
      server = agsServerObject;
      resetMapInfo();

    }
    catch (Exception e) {
      e.printStackTrace();
      stop();
    }


  }

/**Returns mapdescription object for particular map
 * @param activeMap
 * @return IMapDescription
 */

 public void setExtent(Envelope env) {
   try {
     MapExtent  mapExtent = new MapExtent();
     mapExtent.setExtent(env);
     getMapDescription(activeMap).setMapArea(mapExtent);
   }
   catch (IOException e) {
     e.printStackTrace();
   }
 }

 /**Returns the current extent
  * @return
  */

  public Envelope getExtent() {
    try {
      return (Envelope) getMapDescription(activeMap).getMapArea().getExtent();
    }
    catch (IOException ex) {
      return null;
    }
  }

 /**Method to reset the map information.
 * Normally used when new map configuration is selected
 */

public void resetMapInfo() {
  try {
      if (mapDescriptionTable == null) {
        mapDescriptionTable = new Hashtable<String, IMapDescription>();
      }
      mapDescriptionTable.clear();
      activeMap = server.getDefaultMapName();
      for (int i = 0; i < server.getMapCount(); i++) {
        String name = server.getMapName(i);
        IMapServerInfo info = server.getServerInfo(name);
        IMapDescription mapDescription = info.getDefaultMapDescription();
        mapDescriptionTable.put(name, mapDescription);
      }
    }
    catch (IOException ex) {
    }

}
  /** Creates new envelope
   *
   */

  public Envelope createNewEnvelope() {
    Envelope envelope = null;
    try {
      envelope = new Envelope();
    }
    catch (IOException ex) {
    }
    return envelope;
  }

/* @param activeMap
  * @return IMapDescription
  */

 public IMapDescription getMapDescription(String activeMap) {
   return (
       (IMapDescription) mapDescriptionTable.get(activeMap));
 }

 /**Sets the extent and exports the map image.
 * @param envelope
 * @return String containg the url
 */

public byte[] exportImage(Envelope envelope) {
   this.setExtent(envelope);
   return exportImage();

}

 /**Exports the map image.
  * @param envelope
  * @return String containg image location
  */


 public synchronized byte[] exportImage() {
   try {

     if(imageDescription == null)
       imageDescription = new ImageDescription();

     if(imageType == null) {
       imageType = new ImageType();
     }

     if(imageDisplay == null) {
       imageDisplay= new ImageDisplay();
     }
    imageDisplay.setDeviceResolution(dpi);
    imageDisplay.setHeight(imageHeight);
    imageDisplay.setWidth(imageWidth);
    imageType.setFormat(esriImageFormat.esriImageJPG);
    imageDescription.setDisplay(imageDisplay);
    imageType.setReturnType(esriImageReturnType.esriImageReturnMimeData);
    imageDescription.setType(imageType);
    mapImage = server.exportMapImage(getMapDescription(activeMap), imageDescription);
    return mapImage.getMimeData();
   }
   catch (IOException ex) {
     ex.printStackTrace();
     return null;
   }
 }

 /**Converts an Envelope screen location to a map coordinate with Envelope as input
 * @param rect Envelope
 * @return IEnvelope with the transformed points
 */

 public Envelope transformScreenToMap(Envelope1 rect) {
   com.esri.arcgis.geometry.Envelope env = null;
   try {
     env = new com.esri.arcgis.geometry.Envelope();
     LongArray longArrayX, longArrayY;
     IPointCollection p1;
     longArrayX = new LongArray();
     longArrayY = new LongArray();

     longArrayX.add((int)rect.x);
     longArrayY.add((int)rect.y);
     longArrayX.add((int)(rect.x + rect.width));
     longArrayY.add((int)(rect.y  + rect.height));
     p1 = server.toMapPoints(getMapDescription(activeMap), imageDisplay, longArrayX, longArrayY);
     env.setXMin(p1.getPoint(0).getX());
     env.setYMin(p1.getPoint(1).getY());
     env.setXMax(p1.getPoint(1).getX());
     env.setYMax(p1.getPoint(0).getY());

   }catch (IOException ex) {}

        return env;
  }

  /**Converts an Envelope screen location to a map coordinate
   * @param int x
   * @param int y
   * @return IPoint
   */

  public IPoint transformScreenToMap(int x, int y) {
    try {
     LongArray longArray1 = new LongArray();
     longArray1.add(x);
     LongArray longArray2 = new LongArray();
     longArray2.add(y);

      IPointCollection p = server.toMapPoints(getMapDescription(activeMap), imageDisplay, longArray1, longArray2);
      return p.getPoint(0);
    }

    catch (IOException ex) {
      return null;
    }

  }

  /** In case of pooled objects, creates servercontext, loads the mapDescriptions
 * and imagedescription.For non pooled objects resets the mapdescription
 * Applicable only in case of LAN Connection
 */

 public void activate() {
      //do nothing
 }

 /**Releases context and save mapdescription and imagedescription objects
 * Applicable only in case of LAN Connection
 */

 public void passivate() {
   //do nothing
 }



 /**Stops the map server. Frees all the resources used by the server.
  */

 public void stop() {
   try {
    if (mapDescriptionTable != null)
       mapDescriptionTable.clear();
     if(serverObjectstable != null)
         serverObjectstable.clear();

     EngineInitializer.trackObjectsInCurrentThread();
     EngineInitializer.releaseAllInCurrentThread();
   }
   catch (Exception e) {
   }
 }
}