⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webmapimageexporter.java

📁 esri的ArcGIS Server超级学习模板程序(for java)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.esri.solutions.jitk.common.export.image;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.esri.adf.web.ADFException;
import com.esri.adf.web.data.GISFunctionality;
import com.esri.adf.web.data.GISResource;
import com.esri.adf.web.data.MapFunctionality;
import com.esri.adf.web.data.WebMap;
import com.esri.adf.web.data.export.ExportFunctionality;
import com.esri.adf.web.data.export.ExportProperties;
import com.esri.adf.web.data.geometry.WebExtent;
import com.esri.adf.web.data.graphics.GraphicsResource;
import com.esri.adf.web.util.ImageUtil;


/**
 * Exports the image produced by a {@link WebMap}.  This implementation
 * will allow the user to specify the Extent of the map in which to export.
 * If the extent is not specified, then the Current Extent of the Map
 * will be used.  This implementation will also allow the user to
 * specify the desired DPI (dots per inch) of the exported image.
 * A {@link WebMap} object is required for this implementation.  If a
 * {@link WebMap} is not specified then {@link #exportImage(OutputStream)}
 * will throw an {@link IllegalStateException}.
 */
public class WebMapImageExporter extends BaseImageExporter implements IImageExporter {
    
    private static final Logger LOG = LogManager.getLogger(WebMapImageExporter.class);
    private static final String ERROR_INVALID_DPI = "DPI must be greater than 0.";
    private static final String ERROR_NULL_IMAGE_FORMAT = "Image Format is required, and cannot be null.";
    private static final String ERROR_NULL_WEB_MAP = "WebMap object is required, and cannot be null.";
    private static final String INFO_NULL_EXTENT = "null WebExtent passed to setExtent, Current Extent will be used.";

    /**
     * Reference to WebMap object that will generate the image to export.
     */
    private WebMap m_map;

    /**
     * Desired dots per inch.
     */
    private int m_dpi;

    /**
     * Desired extent of map
     */
    private WebExtent m_extent;

    /**
     * List of IDs of GIS Resources that should be exported.  If this list is
     * blank then all GIS Resources will be exported.
     */
    private final List<String> m_resourceIdList;

    /**
     * List of GIS Resources that should be exported.
     */
    private final List<GISResource> m_resources;

    /**
     * Flag indicating if GraphicsResources should be exported. Default: true
     */
    private boolean m_exportGraphicResources = true;

    /**
     * Constructs a new <code>WebMapImageExporter</code> object.
     */
    public WebMapImageExporter() {
        m_resourceIdList = new ArrayList<String>();
        m_resources = new ArrayList<GISResource>();
    }

    /*
     * (non-Javadoc)
     * @see com.esri.solutions.jitk.common.export.image.BaseImageExporter#exportImage(java.io.OutputStream)
     */
    public int exportImage(OutputStream out) throws IOException {
        int bytesWritten = -1;

        ExportProperties props = createExportProperties();

        List<InputStream> images = new ArrayList<InputStream>();
        List<Double> trans = new ArrayList<Double>();

        exportMapImages(props, images, trans);
        exportOther(props, images, trans);

        byte[] finalImage = mergeImages(props, images, trans);

        out.write(finalImage);

        bytesWritten = finalImage.length;

        return bytesWritten;
    }

    /*
     * (non-Javadoc)
     * @see com.esri.solutions.jitk.common.export.image.BaseImageExporter#exportImageToBytes()
     */
    public byte[] exportImageToBytes() {
        ExportProperties props = createExportProperties();

        List<InputStream> images = new ArrayList<InputStream>();
        List<Double> trans = new ArrayList<Double>();

        exportMapImages(props, images, trans);
        exportOther(props, images, trans);

        byte[] finalImage = mergeImages(props, images, trans);

        return finalImage;
    }

    /**
     * Creates an Export Properties object to use for exporting the images
     * that compose the Map.  If only one GIS Resource is to be exported, then
     * the extent is the Full Extent of that GIS Resource.  If all GIS Resources
     * are to be exported, and the Extent property of this object has not been
     * set then the Current Extent of the Map will be used.  The other properties
     * of the Export Properties object will have their values set based on their
     * counterparts within this object.
     *
     * @return Export Properties
     */
    protected ExportProperties createExportProperties() {
        
    	ExportProperties props = new ExportProperties();
        props.setDpi(m_dpi);

        WebExtent extent = null;

        if(m_extent != null) {
            extent = m_extent;
        }

        if((extent == null) && (m_resourceIdList.size() > 0)) {
            String resourceId = m_resourceIdList.get(0);

            if((resourceId != null) && (m_map.getWebContext() != null)) {
                GISResource res = m_map.getWebContext().getResourceById(resourceId);
                extent = getMapFullExtent(res);
            }
        }

        if ((extent == null) && (m_resources.size() > 0)) {
            GISResource res = m_resources.get(0);
            extent = getMapFullExtent(res);
        }

        if (extent == null) {
            extent = m_map.getCurrentExtent();
            LOG.info(INFO_NULL_EXTENT);
        }

        props.setExtent(extent);
        props.setImageFormat(getImageFormat().name().toLowerCase());
        props.setHeight(getImageHeight());
        props.setWidth(getImageWidth());

        return props;
    }

    /**
     * Exports the Map images using the {@link ExportFunctionality} of each
     * GIS Resource.  The returned {@link InputStream} objects will be added
     * to the <code>images</code> argument.  The transparency of each image will
     * be added to the <code>transValues</code> argument.  The <code>images</code>
     * and <code>transValues</code> arguments can be considered out parameters.
     *
     * @param props                Properties to use for export.
     * @param images        Contains {@link InputStream} objects for each image of the map.
     * @param transValues        Contains transparency values for each image of the map.
     *
     * @throws NullPointerException                Thrown if any argument is <code>null</code>.
     * @throws IllegalStateException        Thrown if the <code>WebMap</code> property
     *                                                                         is <code>null</code>.
     */
    protected void exportMapImages(ExportProperties props, List<InputStream> images, List<Double> transValues) {
        
    	if(props == null || images == null || transValues == null) {
    		throw new NullPointerException();
    	}
        
    	if(m_map == null) {
    		throw new IllegalStateException();
    	}

        // used to store mapFunc that the spatial reference needs set to.  Can't
        // modify the webmap spatial reference while iterating through the list
        MapFunctionality selectedMapFunc = null;
        Collection<MapFunctionality> mapFuncs = m_map.getMapFunctionalities();
        boolean setRef = false;
        InputStream imageIn = null;

        // Get the current spatial reference to compare against
        WebExtent currentExtent = m_map.getWebContext().getWebMap().getCurrentExtent();
        
        for(MapFunctionality func : mapFuncs) {
        	
        	if(func == null || func.isDisabled()) continue;

            GISResource resource = func.getResource();

            if(resource instanceof GraphicsResource && !m_exportGraphicResources) {
                continue;
            }

            String resourceId = null;
            
            if((resource != null) && (m_map.getWebContext() != null)) {
                resourceId = m_map.getWebContext().getResourceId(resource);
            }

            if((resourceId != null) && !m_resourceIdList.isEmpty() && !m_resourceIdList.contains(resourceId)) {
            	continue;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -