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

📄 omgrid.java

📁 openmap java写的开源数字地图程序. 用applet实现,可以像google map 那样放大缩小地图.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                || renderType == RENDERTYPE_OFFSET) {            width = Math.round((float) columns * horizontalResolution);            height = Math.round((float) rows * verticalResolution);            if (renderType == RENDERTYPE_OFFSET) {                upLat = latitude + columns * verticalResolution;                point1 = proj.forward(upLat, longitude);                point1.x += point.x;                point1.y += point.y;            } else {                point1 = point;            }            point2 = new Point(point1.x + width, point1.y + height);        } else {            return false;        }        if (Debug.debugging("grid")) {            Debug.output("OMGrid generated grid, at " + point1 + " and "                    + point2 + " with height " + height + " and width " + width);        }        // THis has to happen here, in case the generator wants to        // check the OMGrid coverage before deciding to do the work        // for creating OMGraphics.        setShape();        /** Now generate the grid in the desired way... */        if (generator != null && generator.needGenerateToRender()) {            add(generator.generate(this, proj));        } else if (gridObjects != null) {            add(generateGridObjects(proj));        }        setNeedToRegenerate(false);        return true;    }    /**     * Set a bounding rectangle as this OMGrid's shape, based on the     * location and size of the coverage of the grid.     */    public void setShape() {        // If nothing is available as the shape, generate shape        // that is a boundary of the generated image.        // We'll make it a GeneralPath rectangle.        int w = width;        int h = height;        setShape(createBoxShape(point1.x, point1.y, w, h));    }    /**     * Render the OMGraphics created to represent the grid data.     */    public void render(Graphics g) {        if (generator != null) {            if ((needToRegenerate && generator.needGenerateToRender())                    || !isVisible()) {                Debug.message("grid",                        "OMGrid: need to generate or is not visible!");                return;            }        }        super.render(g);    }    /**     * Called from generate() if there isn't a OMGridGenerator. Goes     * through the grid, figuring out which data array indexes are on     * the map, and then calls generate on those grid objects.     */    public OMGraphic generateGridObjects(Projection proj) {        OMGraphicList graphiclist = new OMGraphicList();        /**         * There could be some way to optimize the search for objects         * in the grid that are actually visible, but that would         * require knowledge of the specifices of projections. Keeping         * this as generic as possible at this point.         */        // Since GridObjects only work with a int array, we need to        // check to see if the GridObject is of type GridData.Int and        // only bother returning if it is.        GridData gd = getData();        if (gd instanceof GridData.Int) {            GridData.Int gdi = (GridData.Int) gd;            Point pt = new Point();            boolean major = gdi.getMajor();            int[][] data = gdi.getData();            for (int x = 0; x < data.length; x++) {                for (int y = 0; y < data[0].length; y++) {                    // First, calculate if the grid post is even on                    // the map                    if (major == COLUMN_MAJOR) {                        if (renderType == RENDERTYPE_LATLON) {                            pt = proj.forward(latitude + y * verticalResolution,                                    longitude + x * horizontalResolution,                                    pt);                        } else {                            pt.y = point1.y + (int) (y * verticalResolution);                            pt.x = point1.x + (int) (x * horizontalResolution);                        }                    } else {                        if (renderType == RENDERTYPE_LATLON) {                            pt = proj.forward(latitude + x * verticalResolution,                                    longitude + y * horizontalResolution,                                    pt);                        } else {                            pt.y = point1.y + (int) (x * verticalResolution);                            pt.x = point1.x + (int) (y * horizontalResolution);                        }                    }                    if ((pt.x >= 0 || pt.x <= proj.getWidth())                            && (pt.y >= 0 || pt.y <= proj.getHeight())) {                        // It's on the map! Get a graphic from it!                        graphiclist.add(gridObjects.generate(data[x][y], proj));                    }                }            }        }        return graphiclist;    }    /**     * The value at the closest SW post to the given lat/lon. This is     * just a go-to-the-closest-post solution.     *      * @param lat latitude in decimal degrees.     * @param lon longitude in decimal degrees.     * @param proj map projection, which is needed for XY or OFFSET     *        grids.     * @return value found at the nearest grid point. This is an     *         object returned from the GridObject data object, so     *         what it is depends on that. You can test if it's a     *         java.lang.Number object to get different values out of     *         it if it is.     */    public Object valueAt(float lat, float lon, Projection proj) {        int lat_index = -1;        int lon_index = -1;        if (renderType == RENDERTYPE_LATLON) {            lat_index = Math.round((lat - latitude) / verticalResolution);            lon_index = Math.round((lon - longitude) / horizontalResolution);        } else if (renderType == RENDERTYPE_XY                || renderType == RENDERTYPE_OFFSET) {            if (getNeedToRegenerate()) {                /** Only care about this if we need to... */                if (proj == null) {                    return null;                }                generate(proj);            }            Point pt = proj.forward(lat, lon);            lat_index = Math.round((pt.y - point1.y) / verticalResolution);            lon_index = Math.round((pt.x - point1.x) / horizontalResolution);        }        GridData gd = getData();        if (gd != null && (lat_index >= 0 || lat_index < rows)                && (lon_index >= 0 || lon_index < columns)) {            Object obj = null;            if (major == COLUMN_MAJOR) {                obj = gd.get(lon_index, lat_index);            } else {                obj = gd.get(lat_index, lon_index);            }            return obj;        }        return null;    }    /**     * Interpolated value at a given lat/lon - should be more precise     * than valueAt(), but that depends on the resolution of the data.     * Works with GridData.Int data objects.     *      * @param lat latitude in decimal degrees.     * @param lon longitude in decimal degrees.     * @param proj map projection, which is needed for XY or OFFSET     *        grids.     * @return value at lat/lon     */    public int interpValueAt(float lat, float lon, Projection proj) {        float lat_index = -1;        float lon_index = -1;        GridData gridData = getData();        if (!(gridData instanceof GridData.Int)) {            Debug.error("OMGrid.interpValueAt only works for integer data.");            return 0;        }        int[][] data = ((GridData.Int) gridData).getData();        boolean major = gridData.getMajor();        if (renderType == RENDERTYPE_LATLON) {            lat_index = (lat - latitude) / verticalResolution;            lon_index = (lon - longitude) / horizontalResolution;        } else if (renderType == RENDERTYPE_XY                || renderType == RENDERTYPE_OFFSET) {            if (getNeedToRegenerate()) {                /** Only care about this if we need to... */                if (proj == null) {                    return GRID_NULL;                }                generate(proj);            }            Point pt = proj.forward(lat, lon);            lat_index = (pt.y - point1.y) / verticalResolution;            lon_index = (pt.x - point1.x) / horizontalResolution;        }        if ((lat_index >= 0 || lat_index < rows)                && (lon_index >= 0 || lon_index < columns)) {            int lflon_index = (int) Math.floor(lon_index);            int lclon_index = (int) Math.ceil(lon_index);            int lflat_index = (int) Math.floor(lat_index);            int lclat_index = (int) Math.ceil(lat_index);            //////////////////////////////////////////////////////            // Print out grid of 20x20 elevations with            // the "asked for" point being in the middle            if (Debug.debugging("grid")) {                System.out.println("***Elevation Map***");                for (int l = lclat_index + 5; l > lflat_index - 5; l--) {                    System.out.println();                    for (int k = lflon_index - 5; k < lclon_index + 5; k++) {                        if ((l >= 0 || l < rows) && (k >= 0 || k < columns)) {                            if (major == COLUMN_MAJOR) {                                System.out.print(data[k][l] + " ");                            } else {                                System.out.print(data[l][k] + " ");                            }                        }                    }                }                System.out.println();                System.out.println();            }            //////////////////////////////////////////////////////            int ul, ur, ll, lr;            if (major == COLUMN_MAJOR) {                ul = data[lflon_index][lclat_index];                ur = data[lclon_index][lclat_index];                ll = data[lflon_index][lclat_index];                lr = data[lclon_index][lclat_index];            } else {                ul = data[lclat_index][lflon_index];                ur = data[lclat_index][lclon_index];                ll = data[lclat_index][lflon_index];                lr = data[lclat_index][lclon_index];            }            float answer = resolve_four_points(ul,                    ur,                    lr,                    ll,                    lat_index,                    lon_index);            return Math.round(answer);        }        return GRID_NULL; // Considered a null value    }    /**     * A try at interoplating the corners of the surrounding posts,     * given a lat lon. Called from a function where the data for the     * lon has been read in.     */    private float resolve_four_points(int ul, int ur, int lr, int ll,                                      float lat_index, float lon_index) {        float top_avg = ((lon_index - new Double(Math.floor(lon_index)).floatValue()) * (float) (ur - ul))                + ul;        float bottom_avg = ((lon_index - new Double(Math.floor(lon_index)).floatValue()) * (float) (lr - ll))                + ll;        float right_avg = ((lat_index - new Double(Math.floor(lat_index)).floatValue()) * (float) (ur - lr))                + lr;        float left_avg = ((lat_index - new Double(Math.floor(lat_index)).floatValue()) * (float) (ul - ll))                / 100.0F + ll;        float lon_avg = ((lat_index - new Double(Math.floor(lat_index)).floatValue()) * (top_avg - bottom_avg))                + bottom_avg;        float lat_avg = ((lon_index - new Double(Math.floor(lon_index)).floatValue()) * (right_avg - left_avg))                + left_avg;        float result = (lon_avg + lat_avg) / 2.0F;        return result;    }}

⌨️ 快捷键说明

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