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

📄 svgwriter.java

📁 电子地图服务器,搭建自己的地图服务
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
         * Writes the content of the <b>d</b> attribute in a <i>path</i> SVG
         * element
         *
         * <p>
         * While iterating over the coordinate array passed as parameter, this
         * method performs a kind of very basic path generalization, verifying
         * that the distance between the current coordinate and the last
         * encoded one is greater than the minimun distance expressed by the
         * field <code>minCoordDistance</code> and established by the method
         * {@link #setReferenceSpace(Envelope, float)
         * setReferenceSpace(Envelope, blurFactor)}
         * </p>
         *
         * @param coords
         *
         * @throws IOException
         */
        protected void writePathContent(Coordinate[] coords)
            throws IOException {
            write('M');

            Coordinate prev = coords[0];
            Coordinate curr = null;
            write(getX(prev.x));
            write(' ');
            write(getY(prev.y));

            int nCoords = coords.length;
            write('l');

            for (int i = 1; i < nCoords; i++) {
                curr = coords[i];

                //let at least 3 points in case it is a polygon
                if ((i > 3) && (prev.distance(curr) <= minCoordDistance)) {
                    ++coordsSkipCount;

                    continue;
                }

                ++coordsWriteCount;
                write((getX(curr.x) - getX(prev.x)));
                write(' ');
                write(getY(curr.y) - getY(prev.y));
                write(' ');
                prev = curr;
            }
        }

        /**
         * DOCUMENT ME!
         *
         * @param coords DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        protected void writeClosedPathContent(Coordinate[] coords)
            throws IOException {
            writePathContent(coords);
            write('Z');
        }
    }

    /**
     *
     */
    private class PointWriter extends SVGFeatureWriter {
        /**
         * Creates a new PointWriter object.
         */
        public PointWriter() {
        }

        /**
         * DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        protected void startElement(Feature feature) throws IOException {
            write(pointsAsCircles ? "<circle r='0.25%' fill='blue'" : "<use");
        }

        /**
         * DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */

        /**
         * protected void writeAttributes(Feature ft) throws IOException { if
         * (!pointsAsCircles) { write(" xlink:href=\"#"); if (attributeStyle
         * != null) { write(String.valueOf(ft.getAttribute(attributeStyle)));
         * } else { write("point"); } write("\""); }
         * super.writeAttributes(ft); }
         *
         * @throws IOException DOCUMENT ME!
         */

        /**
         * DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        protected void startGeometry(Geometry geom) throws IOException {
        }

        /**
         * overrides writeBounds for points to do nothing. You can get the
         * position of the point with the x and y attributes of the "use" SVG
         * element written to represent each point
         *
         * @param env DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        protected void writeBounds(Envelope env) throws IOException {
        }

        /**
         * DOCUMENT ME!
         *
         * @param geom DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        protected void writeGeometry(Geometry geom) throws IOException {
            Point p = (Point) geom;

            if (pointsAsCircles) {
                write(" cx=\"");
                write(getX(p.getX()));
                write("\" cy=\"");
                write(getY(p.getY()));
            } else {
                write(" x=\"");
                write(getX(p.getX()));
                write("\" y=\"");
                write(getY(p.getY()));
                //Issue GEOS-193, from John Steining.
                write("\" xlink:href=\"#point");

                //putting this in to fix the issue, but I am not sure about
                //the broader implications - I don't think we need it for
                //pointsAsCircles.  And it looks like the quote gets closed
                //somewhere else, but I'm not sure where.
            }
        }
    }

    /**
     *
     */
    private class MultiPointWriter extends PointWriter {
        /**
         * Creates a new MultiPointWriter object.
         */
        public MultiPointWriter() {
        }

        /**
         * DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        protected void startElement(Feature feature) throws IOException {
            write("<g ");
        }

        /**
         * DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        protected void startGeometry(Geometry geom) throws IOException {
            write("/>\n");
        }

        /**
         * DOCUMENT ME!
         *
         * @param geom DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        protected void writeGeometry(Geometry geom) throws IOException {
            MultiPoint mp = (MultiPoint) geom;

            for (int i = 0; i < mp.getNumGeometries(); i++) {
                super.startElement(null);
                super.writeGeometry(mp.getGeometryN(i));
                super.endGeometry(mp.getGeometryN(i));
                super.endElement(null);
            }
        }

        /**
         * DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        protected void endElement(Feature feature) throws IOException {
            write("</g>\n");
        }
    }

    /**
     * Writer to handle feature types which contain a Geometry attribute that
     * is actually of the class Geometry. This can occur in heterogeneous data
     * sets.
     *
     * @author Justin Deoliveira, jdeolive@openplans.org
     *
     */
    private class GeometryWriter extends SVGFeatureWriter {
        SVGFeatureWriter delegate;

        protected void startElement(Feature feature) throws IOException {
            Geometry g = feature.getDefaultGeometry();
            delegate = null;

            if (g != null) {
                delegate = (SVGFeatureWriter) writers.get(g.getClass());
            }

            if (delegate == null) {
                throw new IllegalArgumentException("No SVG Feature writer defined for " + g);
            }

            delegate.startElement(feature);
        }

        protected void startGeometry(Geometry geom) throws IOException {
            delegate.startGeometry(geom);
        }

        protected void writeGeometry(Geometry geom) throws IOException {
            delegate.writeGeometry(geom);
        }
    }

    /**
     *
     */
    private class LineStringWriter extends SVGFeatureWriter {
        /**
         * Creates a new LineStringWriter object.
         */
        public LineStringWriter() {
        }

        /**
         * DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        protected void startElement(Feature feature) throws IOException {
            write("<path");
        }

        /**
         * DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        protected void startGeometry(Geometry geom) throws IOException {
            write(" d=\"");
        }

        /**
         * DOCUMENT ME!
         *
         * @param geom DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        protected void writeGeometry(Geometry geom) throws IOException {
            writePathContent(((LineString) geom).getCoordinates());
        }
    }

    /**
     *
     */
    private class MultiLineStringWriter extends LineStringWriter {
        /**
         * Creates a new MultiLineStringWriter object.
         */
        public MultiLineStringWriter() {
        }

        /**
         * DOCUMENT ME!
         *
         * @param geom DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        protected void writeGeometry(Geometry geom) throws IOException {
            MultiLineString ml = (MultiLineString) geom;

            for (int i = 0; i < ml.getNumGeometries(); i++) {
                super.writeGeometry(ml.getGeometryN(i));
            }
        }
    }

    /**
     *
     */
    private class PolygonWriter extends SVGFeatureWriter {
        /**
         * Creates a new PolygonWriter object.
         */
        public PolygonWriter() {
        }

        /**
         * DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        protected void startElement(Feature feature) throws IOException {
            write("<path");
        }

        /**
         * DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        protected void startGeometry(Geometry geom) throws IOException {
            write(" d=\"");
        }

        /**
         * DOCUMENT ME!
         *
         * @param geom DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        protected void writeGeometry(Geometry geom) throws IOException {
            Polygon poly = (Polygon) geom;
            LineString shell = poly.getExteriorRing();
            int nHoles = poly.getNumInteriorRing();
            writeClosedPathContent(shell.getCoordinates());

            for (int i = 0; i < nHoles; i++)
                writeClosedPathContent(poly.getInteriorRingN(i).getCoordinates());
        }
    }

    /**
     *
     */
    private class MultiPolygonWriter extends PolygonWriter {
        /**
         * Creates a new MultiPolygonWriter object.
         */
        public MultiPolygonWriter() {
        }

        /**
         * DOCUMENT ME!
         *
         * @param geom DOCUMENT ME!
         *
         * @throws IOException DOCUMENT ME!
         */
        protected void writeGeometry(Geometry geom) throws IOException {
            MultiPolygon mpoly = (MultiPolygon) geom;

            for (int i = 0; i < mpoly.getNumGeometries(); i++) {
                super.writeGeometry(mpoly.getGeometryN(i));
            }
        }
    }
}

⌨️ 快捷键说明

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