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

📄 polygongeometry.java

📁 openmap java写的开源数字地图程序. 用applet实现,可以像google map 那样放大缩小地图.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// **********************************************************************// // <copyright>// //  BBN Technologies//  10 Moulton Street//  Cambridge, MA 02138//  (617) 873-8000// //  Copyright (C) BBNT Solutions LLC. All rights reserved.// // </copyright>// **********************************************************************// // $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/omGraphics/geom/PolygonGeometry.java,v $// $RCSfile: PolygonGeometry.java,v $// $Revision: 1.4.2.2 $// $Date: 2005/08/09 21:17:59 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap.omGraphics.geom;import java.awt.Point;import java.awt.geom.GeneralPath;import java.io.Serializable;import java.util.ArrayList;import com.bbn.openmap.omGraphics.OMGeometry;import com.bbn.openmap.omGraphics.OMGraphic;import com.bbn.openmap.proj.DrawUtil;import com.bbn.openmap.proj.ProjMath;import com.bbn.openmap.proj.Projection;import com.bbn.openmap.util.Debug;/** * Graphic object that represents a polygon. * <p> * All of the OMGraphics are moving to having their internal * representation as java.awt.Shape objects. Unfortunately, this has * the side effect of slowing OMPolys down, because the way that the * projection classes handle transformations cause more objects to be * allocated and more loops to be run through. So, by default, the * OMPoly does NOT use Shape objects internally, to keep layers that * throw down many, many polys running quickly. If you want to do some * spatial analysis on an OMPoly, call setDoShapes(true) on it, then * generate(Projection), and then call getShapes() to get the * java.awt.Shape objects for the poly. You can then run the different * Shape spatial analysis methods on the Shape objects. *  * <h3>NOTES:</h3> * <ul> * <li>See the <a * href="../../../../com.bbn.openmap.proj.Projection.html#poly_restrictions"> * RESTRICTIONS </a> on Lat/Lon polygons/polylines. Not following the * guidelines listed may result in ambiguous/undefined shapes! Similar * assumptions apply to the other vector graphics that we define: * circles, ellipses, rects, lines. * <li>LatLon OMPolys store latlon coordinates internally in radian * format for efficiency in projecting. Subclasses should follow this * model. * <li>Holes in the poly are not supported. If you want holes, use * multiple PolyGeometrys in a OMGeometryList. * <p> * </ul> * <h3>TODO:</h3> * <ul> * <li>Polar filled-polygon correction for Cylindrical projections * (like OMCircle). * </ul> */public abstract class PolygonGeometry extends BasicGeometry implements        Serializable, OMGeometry {    /** Internal array of projected x coordinate arrays. */    protected int[][] xpoints = new int[0][0];    /** Internal array of projected y coordinate arrays. */    protected int[][] ypoints = new int[0][0];    /**     * Whether it is a polygon, as opposed to a polyline. Should be a     * polygon, since that is what is being created. The     * PolylineGeometry subclasses set this to false.     */    protected boolean isPolygon = true;    /**     * Flag for telling the PolygonGeometry to use the Shape objects     * to represent itself internally. See intro for more info.     */    protected boolean doShapes = true;    protected PolygonGeometry() {}    public void setDoShapes(boolean set) {        doShapes = set;    }    public boolean getDoShapes() {        return doShapes;    }    protected void setIsPolygon(boolean set) {        isPolygon = set;    }    public boolean getIsPolygon() {        return isPolygon;    }    /**     * Since OMPoly has the option to not create a Shape, this method     * is here to create it if it is asked for. The OMPoly needs to be     * generated.     */    protected abstract void createShape();    /**     * Return the shortest distance from the graphic to an XY-point.     * This works if generate() has been successful.     *      * @param x horizontal pixel location.     * @param y vertical pixel location.     * @return the distance of the object to the location given.     */    public float distance(int x, int y) {        if (shape != null) {            return super.distance(x, y);        }        // If shape is null, then we have to do things the old way.        float temp, distance = Float.POSITIVE_INFINITY;        if (getNeedToRegenerate()) {            return distance;        }        // safety: grab local reference of projected points        int[][] xpts = xpoints;        int[][] ypts = ypoints;        int[] _x, _y;        int len = xpts.length;        for (int i = 0; i < len; i++) {            _x = xpts[i];            _y = ypts[i];            // check if point inside polygon            if (DrawUtil.inside_polygon(_x, _y, x, y))                return 0f; // close as can be            // get the closest point            temp = DrawUtil.closestPolyDistance(_x, _y, x, y, false);            if (temp < distance)                distance = temp;        }        return distance;    }    /**     * Get the array of java.awt.Shape objects that represent the     * projected graphic. The array will contain more than one Shape     * object of the object wraps around the earth and needs to show     * up in more than one place on the map.     * <p>     *      * The java.awt.Shape object gives you the ability to do a little     * spatial analysis on the graphics.     *      * @return java.awt.Shape[], or null if the graphic needs to be     *         generated with the current map projection, or null if     *         the OMGeometry hasn't been updated to use Shape objects     *         for its internal representation.     */    public GeneralPath getShape() {        if (shape == null && !getNeedToRegenerate() && !doShapes) {            // Since polygons have the option of not creating shape            // objects, should create one if asked.            createShape();        }        return shape;    }    public static class LL extends PolygonGeometry {        /** raw float lats and lons stored internally in radians. */        protected float[] rawllpts = null;        /**         * Number of segments to draw (used only for         * LINETYPE_GREATCIRCLE or LINETYPE_RHUMB lines).         */        protected int nsegs = -1;        /**         * Create an OMPoly from a list of float lat/lon pairs.         * <p>         * NOTES:         * <ul>         * <li>llPoints array is converted into radians IN PLACE for         * more efficient handling internally if it's not already in         * radians! For even better performance, you should send us an         * array already in radians format!         * <li>If you want the poly to be connected (as a polygon),         * you need to ensure that the first and last coordinate pairs         * are the same.         * </ul>         *          * @param llPoints array of lat/lon points, arranged lat, lon,         *        lat, lon, etc.         * @param units radians or decimal degrees. Use         *        OMGraphic.RADIANS or OMGraphic.DECIMAL_DEGREES         * @param lType line type, from a list defined in OMGraphic.         * @param nsegs number of segment points (only for         *        LINETYPE_GREATCIRCLE or LINETYPE_RHUMB line types,         *        and if &lt; 1, this value is generated internally)         */        public LL(float[] llPoints, int units, int lType, int nsegs) {            setLineType(lType);            setLocation(llPoints, units);            setNumSegs(nsegs);        }        /**         * Create an LL PolygonGeometry from a list of float lat/lon         * pairs.         * <p>         * NOTES:         * <ul>         * <li>llPoints array is converted into radians IN PLACE for         * more efficient handling internally if it's not already in         * radians! For even better performance, you should send us an         * array already in radians format!         * <li>If you want the poly to be connected (as a polygon),         * you need to ensure that the first and last coordinate pairs         * are the same.         * </ul>         *          * @param llPoints array of lat/lon points, arranged lat, lon,         *        lat, lon, etc.         * @param units radians or decimal degrees. Use         *        OMGraphic.RADIANS or OMGraphic.DECIMAL_DEGREES         * @param lType line type, from a list defined in OMGraphic.         */        public LL(float[] llPoints, int units, int lType) {            this(llPoints, units, lType, -1);        }        /**         * Set an OMPoly from a list of float lat/lon pairs.         * <p>         * NOTES:         * <ul>         * <li>llPoints array is converted into radians IN PLACE for         * more efficient handling internally if it's not already in         * radians! If you don't want the array to be changed, send in         * a copy.         * <li>If you want the poly to be connected (as a polygon),         * you need to ensure that the first and last coordinate pairs         * are the same.         * </ul>         * This is for RENDERTYPE_LATLON polys.         *          * @param llPoints array of lat/lon points, arranged lat, lon,         *        lat, lon, etc.         * @param units radians or decimal degrees. Use         *        OMGraphic.RADIANS or OMGraphic.DECIMAL_DEGREES         */        public void setLocation(float[] llPoints, int units) {            if (units == OMGraphic.DECIMAL_DEGREES) {                ProjMath.arrayDegToRad(llPoints);            }            rawllpts = llPoints;            setNeedToRegenerate(true);        }        /**         * Return the rawllpts array. NOTE: this is an unsafe method         * to access the rawllpts array. Use with caution. These are         * RADIANS!         *          * @return float[] rawllpts of lat, lon, lat, lon         */        public float[] getLatLonArray() {            return rawllpts;        }        /**         * Set the number of subsegments for each segment in the poly.         * (This is only for LINETYPE_GREATCIRCLE or LINETYPE_RHUMB         * line types, and if &lt; 1, this value is generated         * internally).         *          * @param nsegs number of segment points         */        public void setNumSegs(int nsegs) {            this.nsegs = nsegs;        }        /**         * Get the number of subsegments for each segment in the poly.         * (This is only for LINETYPE_GREATCIRCLE or LINETYPE_RHUMB         * line types).         *          * @return int number of segment points         */        public int getNumSegs() {            return nsegs;        }        public boolean generate(Projection proj) {            int i, j;            shape = null;            if (proj == null) {                Debug.message("omgraphic",                        "OMPoly: null projection in generate!");                return false;            }            // polygon/polyline project the polygon/polyline.            // Vertices should already be in radians.            ArrayList vector = proj.forwardPoly(rawllpts,                    lineType,                    nsegs,                    isPolygon);            int size = vector.size();            if (!doShapes) {                xpoints = new int[(int) (size / 2)][0];                ypoints = new int[xpoints.length][0];            }            // We could call create shape, but this is more efficient.            for (i = 0, j = 0; i < size; i += 2, j++) {                if (doShapes) {                    GeneralPath gp = BasicGeometry.createShape((int[]) vector.get(i),                            (int[]) vector.get(i + 1),                            isPolygon);                    if (shape == null) {                        shape = gp;                    } else {                        ((GeneralPath) shape).append(gp, false);                    }                } else {                    xpoints[j] = (int[]) vector.get(i);                    ypoints[j] = (int[]) vector.get(i + 1);                }            }            setNeedToRegenerate(false);            return true;        }        protected void createShape() {            if (getNeedToRegenerate()) {                return;            }            int size = xpoints.length;            for (int i = 0; i < size; i++) {                GeneralPath gp = BasicGeometry.createShape(xpoints[i],                        ypoints[i],                        isPolygon);                if (shape == null) {                    shape = gp;                } else {                    ((GeneralPath) shape).append(gp, false);                }            }        }        public int getRenderType() {            return RENDERTYPE_LATLON;

⌨️ 快捷键说明

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