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

📄 imageserver.java

📁 openmap java写的开源数字地图程序. 用applet实现,可以像google map 那样放大缩小地图.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            Vector formatters = ComponentFactory.create(markerNames, p);            int size = formatters.size();            if (imageFormatters == null) {                imageFormatters = new Hashtable(size);            }            for (int i = 0; i < size; i++) {                ImageFormatter formatter = (ImageFormatter) formatters.get(i);                imageFormatters.put(formatter.getFormatLabel(), formatter);                if (i == 0) {                    iFormatter = formatter;                }            }        } else {            Debug.message("imageserver",                    "ImageServer.getFormatters: no formatters specified");        }        return iFormatter;    }    /**     * Create an array of Layers from a properties object.     */    protected Layer[] getLayers(Properties p) {        return getLayers(p, (Hashtable) null);    }    /**     * Create an array of Layers from a properties object. Reuse the     * layer from the hashtable if it's there under the same property     * name. The Hashtable is kept for an ImageServer that is used buy     * an ImageMaster or another object that is using different layers     * for it's image. It will reuse the layers it's already created     * if the marker names are the same.     *      * @param p properties     * @param instantiatedLayers a hashtable containing layers, with     *        the prefix layer name used as the key.     */    protected Layer[] getLayers(Properties p, Hashtable instantiatedLayers) {        String layersValue;        String prefix = PropUtils.getScopedPropertyPrefix(this);        layersValue = p.getProperty(prefix + ImageServerLayersProperty);        if (layersValue == null) {            // get openmap.layers value            layersValue = p.getProperty(OpenMapPrefix                    + ImageServerLayersProperty);            if (layersValue == null) {                Debug.error("ImageServer: No property \""                        + ImageServerLayersProperty                        + "\" found in ImageServer properties.");                return new Layer[0];            }        }        Vector layerNames = PropUtils.parseSpacedMarkers(layersValue);        if (Debug.debugging("imageserver")) {            Debug.output("OpenMap.getLayers(): " + layerNames);        }        int nLayerNames = layerNames.size();        Vector layers = new Vector(nLayerNames);        for (int i = 0; i < nLayerNames; i++) {            String layerName = (String) layerNames.elementAt(i);            // Check to see if some other ImageServer has used this            // layer, and reuse it.            if (instantiatedLayers != null) {                Layer iLayer = (Layer) instantiatedLayers.get(layerName);                if (iLayer != null) {                    // We might want to consider adding this:                    // iLayer.setProperties(layerName, p);                    layers.add(iLayer);                    if (Debug.debugging("imageserver")) {                        Debug.output("ImageServer: adding instantiated layer /"                                + layerName + "/");                    }                    continue;                }            }            // Brand new layer, so instantiate it.            String classProperty = layerName + ".class";            String className = p.getProperty(classProperty);            if (className == null) {                Debug.error("Failed to locate property \"" + classProperty                        + "\"");                Debug.error("Skipping layer \"" + layerName + "\"");                continue;            }            Object obj = ComponentFactory.create(className, layerName, p);            if (obj instanceof Layer || obj instanceof PlugIn) {                Layer l = null;                if (obj instanceof PlugIn) {                    PlugIn pi = (PlugIn) obj;                    PlugInLayer pil = new PlugInLayer();                    pil.setPlugIn(pi);                    pil.setName(p.getProperty(PropUtils.getScopedPropertyPrefix(pi)                            + Layer.PrettyNameProperty));                    l = pil;                } else {                    l = (Layer) obj;                }                layers.addElement(l);                if (instantiatedLayers != null) {                    instantiatedLayers.put(layerName, l);                    if (Debug.debugging("imageserver")) {                        Debug.output("ImageServer: Saving /" + layerName                                + "/ to instantiated layers hashtable.");                    }                }            }        }        int nLayers = layers.size();        if (nLayers == 0) {            return new Layer[0];        } else {            Layer[] value = new Layer[nLayers];            layers.copyInto(value);            return value;        }    }    // protected void finalize() {    // if (Debug.debugging("gc")) {    // Debug.output("ImageServer: GC'd.");    // }    // }    /**     * For convenience, to create an image file based on the contents     * of a properties file (like an openmap.properties file).     *      * @param prefix The prefix for the ImageServer properties (layers     *        and formatters) to use in the properties file. If     *        defined, then this method will look for 'prefix.layers'     *        and prefix.formatters' properties. If null, then this     *        method will look 'layers' and 'formatters' properties.     *      * @param props The properties to use for defining the layers and     *        plugins to use on the map image. Standard     *        openmap.properties formats for layer definitions. See     *        the standard openmap.properties file for more details on     *        how to define layers and plugins.     *      * @param proj The projection to use for the map. If null, then     *        the Environment projection properties will be looked for     *        in the Properties.     *      * @param outputPath The output path for the image file. The image     *        file should not have an appendix defined. This method     *        will check which formatter is being used, and will     *        assign one based on the image format (leave off the .,     *        too).     *      * @return the final path of the written image file, with the     *         chosen appendix attached.     */    public static String createImageFile(String prefix, Properties props,                                         Projection proj, String outputPath)    throws MalformedURLException, IOException {        String appendix = "";        ImageServer is = new ImageServer(props);        ImageFormatter formatter = is.getFormatter();        if (formatter == null) {            is.setFormatter(new SunJPEGFormatter());            appendix = ".jpg";        } else {            String fileType = formatter.getFormatLabel();            if (fileType.equals(WMTConstants.IMAGEFORMAT_JPEG)) {                appendix = ".jpg";            } else {                appendix = "." + fileType.toLowerCase();            }        }        Color background = MapBean.DEFAULT_BACKGROUND_COLOR;        background = (Color) PropUtils.parseColorFromProperties(props,                Environment.BackgroundColor,                background);        is.setBackground(background);        // Initialize the map projection, scale, center with        // user prefs or defaults        if (proj == null) {            String projName = props.getProperty(Environment.Projection);            Class projClass = ProjectionFactory.getProjClassForName(projName);            if (projClass == null) {                projClass = Mercator.class;            }            proj = ProjectionFactory.makeProjection(projClass,                    PropUtils.floatFromProperties(props,                            Environment.Latitude,                            0f),                    PropUtils.floatFromProperties(props,                            Environment.Longitude,                            0f),                    PropUtils.floatFromProperties(props,                            Environment.Scale,                            MapBean.DEFAULT_SCALE),                    PropUtils.intFromProperties(props,                            Environment.Width,                            MapBean.DEFAULT_WIDTH),                    PropUtils.intFromProperties(props,                            Environment.Height,                            MapBean.DEFAULT_HEIGHT));        }        if (Debug.debugging("imageserver")) {            Debug.output("ImageServer: creating image with projection " + proj);        }        byte[] imageBytes = is.createImage(proj);        String finalOutputPath = outputPath + appendix;        FileOutputStream fos = new FileOutputStream(finalOutputPath);        fos.write(imageBytes);        fos.flush();        fos.close();        return finalOutputPath;    }    /**     * Paint object used for map backgrounds.     */    protected Paint background;    /**     * Set the Paint to use for image backgrounds.     */    public void setBackground(Paint bg) {        background = bg;    }    /**     * Get the Paint to use for image backgrounds.     */    public Paint getBackground() {        return background;    }    /**     * The ImageServer class main function will create a map image     * from a modified openmap.properties file.     *      * <pre>     *       *        *    java com.bbn.openmap.image.ImageServer -properties (path     *     to properties file) -file (path to output image)      *         *        * </pre>     *      * <P>     * The path to the output image should not have an appendix on it,     * that will get assigned depending on what image format is used.     */    public static void main(String[] argv) {        Debug.init();        Debug.put("imageserver");        Debug.put("image");        com.bbn.openmap.util.ArgParser ap = new com.bbn.openmap.util.ArgParser("ImageServer");        ap.add("properties", "The properties file to use for the image.", 1);        ap.add("file",                "The output image file, without appendix (default is 'image').",                1);        if (!ap.parse(argv)) {            ap.printUsage();            System.exit(0);        }        String imagefile = "image";        String arg[];        arg = ap.getArgValues("file");        if (arg != null) {            imagefile = arg[0];        }        Properties props = null;        arg = ap.getArgValues("properties");        if (arg != null) {            String ps = arg[0];            try {                URL url = PropUtils.getResourceOrFileOrURL(null, ps);                InputStream inputStream = url.openStream();                props = new Properties();                props.load(inputStream);                Projection proj = null;                String finalOutputPath = ImageServer.createImageFile(null,                        props,                        proj,                        imagefile);                if (Debug.debugging("imageserver")) {                    Debug.output("Writing image file to: " + finalOutputPath);                }            } catch (MalformedURLException murle) {                Debug.error("ImageServer can't find properties file: " + arg[0]);            } catch (IOException ioe) {                Debug.error("ImageServer can't write output image: IOException");            }        }        System.exit(0);    }}

⌨️ 快捷键说明

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