arcgissamples\scenario\map\MapInfo.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. * */ /* * ArcGIS Engine Developer Sample * Application Name: MapInfo.java */ package arcgissamples.scenario.map; /** Helper class for performing map server tasks such as * opening mxd document, exporting the map, setting the image type ..etc. */ import java.io.IOException; import java.util.Hashtable; import com.esri.arcgis.carto.IImageDisplay; import com.esri.arcgis.carto.IImageType; import com.esri.arcgis.carto.ILayerDescription; import com.esri.arcgis.carto.ILayerDescriptions; import com.esri.arcgis.carto.IMapDescription; import com.esri.arcgis.carto.IMapImage; import com.esri.arcgis.carto.IMapLayerInfo; import com.esri.arcgis.carto.IMapLayerInfos; import com.esri.arcgis.carto.IMapServerIdentifyResult; import com.esri.arcgis.carto.IMapServerIdentifyResults; 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.MapServer; 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.geometry.esriGeometryType; import com.esri.arcgis.system.IPropertySet; import com.esri.arcgis.system.LongArray; public class MapInfo { protected static MapInfo mapInfo; private MapServer server = null; private IMapServerInfo mapServerInfo = null; private ImageDescription imageDescription; private IImageType imageType = null; private IImageDisplay imageDisplay = null; private IMapImage mapImage = null; private MapExtent mapExtent = null; private String activeMap = null; private int dpi = 96; private int imageWidth = 0; private int imageHeight = 0; private int imageFormat = esriImageFormat.esriImageJPG; private Hashtable<String, IMapDescription> mapDescriptionTable = null; public MapInfo() { } public MapInfo(String mxdFile) { opeMxdDocument(mxdFile); } /**Returns the map info instance * @return MapInfo */ public static MapInfo getmapInfoInstance() { if (mapInfo == null) mapInfo = new MapInfo(); return mapInfo; } /**Sets the image file format * @param type */ public void setImageType(String type) { if(type.equalsIgnoreCase("JPG")) imageFormat = esriImageFormat.esriImageJPG; else if(type.equalsIgnoreCase("PNG")) imageFormat = esriImageFormat.esriImagePNG; else if(type.equalsIgnoreCase("BMP")) imageFormat = esriImageFormat.esriImageBMP; else if(type.equalsIgnoreCase("DIB")) imageFormat = esriImageFormat.esriImageDIB; else if(type.equalsIgnoreCase("EMF")) imageFormat = esriImageFormat.esriImageEMF; else if(type.equalsIgnoreCase("PDF")) imageFormat = esriImageFormat.esriImagePDF; else if(type.equalsIgnoreCase("PNG24")) imageFormat = esriImageFormat.esriImagePNG24; else if(type.equalsIgnoreCase("TIFF")) imageFormat = esriImageFormat.esriImageTIFF; else if(type.equalsIgnoreCase("AI")) imageFormat = esriImageFormat.esriImageAI; else if(type.equalsIgnoreCase("PS")) imageFormat = esriImageFormat.esriImagePS; else imageFormat = esriImageFormat.esriImageJPG; } /**Returns the image format that is set * @return String */ public String getImageType() { if(imageFormat == esriImageFormat.esriImageJPG) return "JPG"; else if(imageFormat == esriImageFormat.esriImagePNG) return "PNG"; else if(imageFormat == esriImageFormat.esriImageBMP) return "BMP"; else if(imageFormat == esriImageFormat.esriImageDIB) return "DIB"; else if(imageFormat == esriImageFormat.esriImageEMF) return "EMF"; else if(imageFormat == esriImageFormat.esriImagePDF) return "PDF"; else if(imageFormat == esriImageFormat.esriImagePNG24) return "PNG24"; else if(imageFormat == esriImageFormat.esriImageTIFF) return "TIFF"; else if(imageFormat == esriImageFormat.esriImageAI) return "AI"; else if(imageFormat == esriImageFormat.esriImagePS) return "PS"; else return "NONE"; } /**Sets the image width and height * @param width * @param height */ public void setImageSize(int width, int height) { imageWidth = width; imageHeight = height; } /**Sets the extent to the map description *@param env IEnvelope */ 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 of the map * @return */ public Envelope getExtent() { try { return (Envelope) getMapDescription(activeMap).getMapArea().getExtent(); } catch (IOException ex) { return null; } } /**Returns number of maps in the current mxd document * @return int */ public int getMapCount() { try { return server.getMapCount(); } catch (IOException ex) { return 0; } } /**Returns the map name at the specified index * @param i * @return String */ public String getMapName(int i) { try { return server.getMapName(i); } catch (IOException ex) { return null; } } /**Returns an name of the Map object that currently has focus * @return String */ public String getActiveMap() { return activeMap; } /**Sets the focusmap */ public void setActiveMap(String mapName) { activeMap = mapName; } /**Opens the specified mxd document * @param mxdDoc * @throws java.lang.NullPointerException */ public void opeMxdDocument(String mxdDoc) throws NullPointerException { try { if (mxdDoc == null) throw new NullPointerException("MxdFile is not set"); //L1N if (mxdDoc != null) { if(server == null) server = new MapServer(); server.connect(mxdDoc); resetMapInfo(); } } catch (Exception e) { e.printStackTrace(); } } /**Method to reset the map information. * Normally used when extent of the map is changed */ public void resetMapInfo() { try { activeMap = server.getDefaultMapName(); if (mapDescriptionTable == null) { mapDescriptionTable = new java.util.Hashtable<String, IMapDescription>(); } mapDescriptionTable.clear(); for(int i = 0; i < server.getMapCount(); i++) { String mapName = server.getMapName(i); IMapServerInfo mapServerInfo = server.getServerInfo(mapName); IMapDescription mapDescription = mapServerInfo.getDefaultMapDescription(); mapDescriptionTable.put(mapName, mapDescription); } } catch (Exception e) { e.printStackTrace(); } } /**Returns mapdescription object for particular map * @param activeMap * @return IMapDescription */ public IMapDescription getMapDescription(String activeMap) { return( (IMapDescription) mapDescriptionTable.get(activeMap) ); } /**Makes the ILayer and LayerDescriptor visibility properties same. * @param layer * @param dataFrameName * @param value */ public void setLayerDescriptorVisibility(int ID, String mapFrameName, boolean visible) { try { ILayerDescriptions layerDescriptions = getMapDescription(mapFrameName).getLayerDescriptions(); int count = layerDescriptions.getCount(); ILayerDescription ld = null; for(int i = 0; i < count ; i++ ) { ld = layerDescriptions.getElement(i); if(ld.getID() == ID) break; } ld.setVisible(visible); } catch (IOException ex) { ex.printStackTrace(); } } /**Returns list of map objects contained in the mxd document * @return java.util.Vector */ public java.util.Vector getMapNames() { java.util.Vector<String> v = new java.util.Vector<String>(); try { for (int i = 0; i < server.getMapCount(); i++) { String mapName = server.getMapName(i); if (mapName != null) v.add(mapName); } } catch (IOException ex) { return null; } return v; } /**Helper method to create an envlope object * @return Envelope */ public Envelope createNewEnvelope() { try { Envelope envelope = new Envelope(); return envelope; } catch (IOException ex) { return null; } } /**Stops the map server */ public void stop() { try { if (server != null) server.stop(); } catch (Exception e) { e.printStackTrace(); } } /**Sets the extent and exports the map image. * @param envelope * @return String containg image location */ public byte[] exportImage(Envelope envelope) { try { mapExtent.setExtent(envelope); mapServerInfo.getDefaultMapDescription().setMapArea(mapExtent); byte[] bytes = exportImage(); return bytes; } catch (IOException ex) { return null; } } /**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(); } if (mapExtent == null) { mapExtent = new MapExtent(); } imageDisplay.setDeviceResolution(dpi); imageDisplay.setHeight(imageHeight); imageDisplay.setWidth(imageWidth); imageType.setFormat(imageFormat); imageDescription.setDisplay(imageDisplay); imageType.setReturnType(esriImageReturnType.esriImageReturnMimeData); imageDescription.setType(imageType); mapImage = server.exportMapImage(getMapDescription(activeMap), imageDescription); byte bytes[] = mapImage.getMimeData(); return bytes; } catch (IOException ex) { 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(EnvelopeHelper rect) { Envelope env = null; try { env = mapInfo.createNewEnvelope(); 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 = this.server.toMapPoints(getMapDescription(this.activeMap), this.imageDisplay, longArrayX, longArrayY); IPoint ip1, ip2; ip1 = p1.getPoint(0); ip2 = p1.getPoint(1); env.setXMin(ip1.getX()); env.setYMin(ip2.getY()); env.setXMax(ip2.getX()); env.setYMax(ip1.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 p1, p2; p1 = new LongArray(); p2 = new LongArray(); p1.add(x); p2.add(y); IPointCollection p = server.toMapPoints(getMapDescription(activeMap), this.imageDisplay, p1, p2); return p.getPoint(0); } catch (IOException ex) { return null; } } /*Method which returns a treemap containing layername and identifyinfo. * IdentifyInfo class is used to store the keys and values for a layer * @param env * @param tolerence * @param option * @param layerids * @return java.util.TreeMap */ public java.util.TreeMap identify(Envelope env, int tolerence, int option, LongArray layerids) { try { java.util.TreeMap<String, IdentifyInfo> treeMap = new java.util.TreeMap<String, IdentifyInfo>(); IMapServerIdentifyResults identifyResults = this.server.identify(getMapDescription(this.activeMap), this.imageDisplay, env, tolerence, option, layerids); if ( identifyResults != null && identifyResults.getCount() > 0 ) { IMapServerIdentifyResult result; IPropertySet pset; Object objkeys[] = new Object[]{ new Object() }; Object objvalues[] = new Object[]{ new Object() }; String[] keys; Object[] vals; int lid = -1; for ( int i = 0; i < identifyResults.getCount(); i++ ) { result = identifyResults.getElement(i); lid = result.getLayerID(); if (lid == -1) continue; pset = result.getProperties(); pset.getAllProperties(objkeys, objvalues); keys = (String[]) objkeys[0]; vals = (Object[]) objvalues[0]; setShapeValue(keys, vals, result.getShape().getGeometryType()); IMapLayerInfo mapLayerInfo = getLayerInfoFromID(result.getLayerID(), this.activeMap); String idField = mapLayerInfo.getDisplayField(); if(idField != null) { String layerName = mapLayerInfo.getName(); IdentifyInfo identifyData = null; if(treeMap.containsKey(layerName)) identifyData = (IdentifyInfo)treeMap.get(layerName); else identifyData = new IdentifyInfo(); Object idFieldValue = null; try { idFieldValue = pset.getProperty(idField); if(idFieldValue == null) throw new NullPointerException(); } catch (Exception ex1) { idFieldValue = vals[1]; if(idFieldValue == null) idFieldValue = vals[0]; } identifyData.setLayerData(mapLayerInfo.getName(), keys, vals, idFieldValue); treeMap.put(layerName, identifyData); } } } return treeMap; } catch (Exception ex) { ex.printStackTrace(); return null; } } /**Method to convert geometry type from integer to string * @param env * @param tolerence * @param option * @param layerids * @return java.util.TreeMap */ public static String getGeometryTypeString(int geometryType) { switch(geometryType) { case esriGeometryType.esriGeometryAny: return "Any"; case esriGeometryType.esriGeometryBag: return "Bag"; case esriGeometryType.esriGeometryBezier3Curve: return "Bezier 3 Curve"; case esriGeometryType.esriGeometryCircularArc: return "Circular Arc"; case esriGeometryType.esriGeometryEllipticArc: return "Elliptic Arc"; case esriGeometryType.esriGeometryEnvelope: return "Envelope"; case esriGeometryType.esriGeometryLine: return "Line"; case esriGeometryType.esriGeometryMultiPatch: return "Multi Patch"; case esriGeometryType.esriGeometryMultipoint: return "Multi Point"; case esriGeometryType.esriGeometryNull: return "Null"; case esriGeometryType.esriGeometryPath: return "Path"; case esriGeometryType.esriGeometryPoint: return "Point"; case esriGeometryType.esriGeometryPolygon: return "Polygon"; case esriGeometryType.esriGeometryPolyline: return "Polyline"; case esriGeometryType.esriGeometryRay: return "Ray"; case esriGeometryType.esriGeometryRing: return "Ring"; case esriGeometryType.esriGeometrySphere: return "Sphere"; case esriGeometryType.esriGeometryTriangleFan: return "Triangle Fan"; case esriGeometryType.esriGeometryTriangleStrip: return "Triangle Strip"; default: return "Unknown"; } } /**Returns an array of string containing geometry types * @param keys array of geometry type in integer * @return values array of geometry type in string * @param geometryType */ public void setShapeValue(String keys[], Object values[], int geometryType) { for(int i =0 ; i < keys.length; i++) { if(keys[i].compareToIgnoreCase("shape") == 0) values[i] = getGeometryTypeString(geometryType); } } /**Returns the layer info object based on the layer id * @param int ID * @return IMapLayerInfo */ public IMapLayerInfo getLayerInfoFromID(int ID, String mapName) { try { IMapServerInfo iMapServerInfo = this.server.getServerInfo(mapName); IMapLayerInfos layerInfos = iMapServerInfo.getMapLayerInfos(); IMapLayerInfo layerInfo = null; int count = layerInfos.getCount(); for (int i = 0; i < count; i++) { layerInfo = layerInfos.getElement(i); if (layerInfo.getID() == ID) { break; } } return layerInfo; } catch (IOException ex) { return null; } } /**Returns the layer name from the layer id * @param ID * @return string */ public String layerNameFromID(int ID, String mapName) { try { IMapServerInfo serverInfo = this.server.getServerInfo(mapName); IMapLayerInfos layerInfos = serverInfo.getMapLayerInfos(); IMapLayerInfo layerInfo; String name = null; int count = layerInfos.getCount(); for (int i = 0; i < count; i++) { layerInfo = layerInfos.getElement(i); if (layerInfo.getID() == ID) { name = layerInfo.getName(); break; } } return name; } catch (IOException ex) { return null; } } /**Returns list of layerID and their visibility value in a hashtable * @return hashTable containing layerIds and visibility */ public java.util.Vector getLayerInfo(String mapName) { java.util.Vector<LayerInfo> layerTable = new java.util.Vector<LayerInfo>(); try { IMapDescription mapDescription = this.server.getServerInfo(mapName).getDefaultMapDescription(); ILayerDescriptions layerDescriptions = mapDescription.getLayerDescriptions(); ILayerDescription layerDescription; for (int i = 0; i < layerDescriptions.getCount(); i++) { layerDescription = layerDescriptions.getElement(i); int id = layerDescription.getID(); boolean visible = layerDescription.isVisible(); String name = this.layerNameFromID(id, mapName); LayerInfo layerinfo = new LayerInfo(name, visible, id); layerTable.add(layerinfo); } } catch (IOException ex) { } return layerTable; } }