georssparser.java

来自「world wind java sdk 源码」· Java 代码 · 共 554 行 · 第 1/2 页

JAVA
554
字号
            return makeGMLineStringShape(typeNode);

        typeNode = findChildByLocalName(node, "Point");
        if (typeNode != null)
            return makeGMLPointShape(typeNode);

        Logging.logger().log(Level.WARNING, "GeoRSS.MissingElementContent", "where");
        return null;
    }

    private static Renderable makeGMLPolygonShape(Node node)
    {
        Node n = findChildByLocalName(node, "exterior");
        if (n == null)
        {
            Logging.logger().log(Level.WARNING, "GeoRSS.MissingElement", "exterior");
            return null;
        }

        n = findChildByLocalName(n, "LinearRing");
        if (n == null)
        {
            Logging.logger().log(Level.WARNING, "GeoRSS.MissingElement", "LinearRing");
            return null;
        }

        return makePolygonShape(n, null);
    }

    private static Renderable makePolygonShape(Node node, Iterable<Node> attrs)
    {
        String valuesString = node.getTextContent();
        if (valuesString == null)
        {
            Logging.logger().log(Level.WARNING, "GeoRSS.NoCoordinates", node.getLocalName());
            return null;
        }

        ArrayList<Double> values = getDoubleValues(valuesString);
        if (values.size() < 8 || values.size() % 2 != 0)
        {
            Logging.logger().log(Level.WARNING, "GeoRSS.InvalidCoordinateCount", node.getLocalName());
            return null;
        }

        ArrayList<LatLon> positions = new ArrayList<LatLon>();
        for (int i = 0; i < values.size(); i += 2)
        {
            positions.add(LatLon.fromDegrees(values.get(i), values.get(i + 1)));
        }

        double elevation = attrs != null ? getElevation(node, attrs) : 0d;
        if (elevation != 0)
        {
            Polyline pl = new Polyline(positions, elevation);
            pl.setFilled(true);
            pl.setPathType(Polyline.GREAT_CIRCLE);

            return pl;
        }
        else
        {
            return new SurfacePolygon(positions);
        }
    }

    private static Renderable makeGMLEnvelopeShape(Node node)
    {
        Node n = findChildByLocalName(node, "lowerCorner");
        if (n == null)
        {
            Logging.logger().log(Level.WARNING, "GeoRSS.MissingElement", " lowerCorner");
            return null;
        }

        String lowerCornerString = n.getTextContent();
        if (lowerCornerString == null)
        {
            Logging.logger().log(Level.WARNING, "GeoRSS.InvalidCoordinateCount", " lowerCorner");
            return null;
        }

        n = findChildByLocalName(node, "upperCorner");
        if (n == null)
        {
            Logging.logger().log(Level.WARNING, "GeoRSS.InvalidCoordinateCount", " upperCorner");
            return null;
        }

        String upperCornerString = n.getTextContent();
        if (upperCornerString == null)
        {
            Logging.logger().log(Level.WARNING, "GeoRSS.InvalidCoordinateCount", " upperCorner");
            return null;
        }

        ArrayList<Double> lv = getDoubleValues(lowerCornerString);
        if (lv.size() != 2)
        {
            Logging.logger().log(Level.WARNING, "GeoRSS.InvalidCoordinateCount", " lowerCorner");
            return null;
        }

        ArrayList<Double> uv = getDoubleValues(upperCornerString);
        if (uv.size() != 2)
        {
            Logging.logger().log(Level.WARNING, "GeoRSS.InvalidCoordinateCount", " upperCorner");
            return null;
        }

        return new SurfaceSector(Sector.fromDegrees(lv.get(0), uv.get(0), lv.get(1), uv.get(1)));
    }

    private static Renderable makeBoxShape(Node node, Iterable<Node> attrs)
    {
        String valuesString = node.getTextContent();
        if (valuesString == null)
        {
            Logging.logger().log(Level.WARNING, "GeoRSS.NoCoordinates", node.getLocalName());
            return null;
        }

        ArrayList<Double> p = getDoubleValues(valuesString);
        if (p.size() != 4)
        {
            Logging.logger().log(Level.WARNING, "GeoRSS.InvalidCoordinateCount", node.getLocalName());
            return null;
        }

        double elevation = getElevation(node, attrs);
        if (elevation != 0)
            return new Quadrilateral(LatLon.fromDegrees(p.get(0), p.get(1)),
                LatLon.fromDegrees(p.get(2), p.get(3)), elevation);
        else
            return new SurfaceSector(Sector.fromDegrees(p.get(0), p.get(2), p.get(1), p.get(3)));
    }

    private static Renderable makeGMLineStringShape(Node node)
    {
        Node n = findChildByLocalName(node, "posList");
        if (n == null)
        {
            Logging.logger().log(Level.WARNING, "GeoRSS.MissingElement", "posList");
            return null;
        }

        return makeLineShape(n, null);
    }

    private static Renderable makeLineShape(Node node, Iterable<Node> attrs)
    {
        String valuesString = node.getTextContent();
        if (valuesString == null)
        {
            Logging.logger().log(Level.WARNING, "GeoRSS.NoCoordinates", node.getLocalName());
            return null;
        }

        ArrayList<Double> values = getDoubleValues(valuesString);
        if (values.size() < 4)
        {
            Logging.logger().log(Level.WARNING, "GeoRSS.InvalidCoordinateCount", node.getLocalName());
            return null;
        }

        ArrayList<LatLon> positions = new ArrayList<LatLon>();
        for (int i = 0; i < values.size(); i += 2)
        {
            positions.add(LatLon.fromDegrees(values.get(i), values.get(i + 1)));
        }

        double elevation = attrs != null ? getElevation(node, attrs) : 0d;
        if (elevation != 0)
        {
            Polyline pl = new Polyline(positions, elevation);
            pl.setPathType(Polyline.GREAT_CIRCLE);
            return pl;
        }
        else
        {
            return new Polyline(positions, 0);
        }
    }

    @SuppressWarnings({"UnusedDeclaration"})
    private static Renderable makeGMLPointShape(Node node)
    {
        return null; // No shape provided for points. Expect app to use icons.
    }

    @SuppressWarnings({"UnusedDeclaration"})
    private static Renderable makePointShape(Node node, Iterable<Node> attrs)
    {
        return null; // No shape provided for points. Expect app to use icons.
    }

    private static Node findChildByLocalName(Node parent, String localName)
    {
        NodeList children = parent.getChildNodes();
        if (children == null || children.getLength() < 1)
            return null;

        for (int i = 0; i < children.getLength(); i++)
        {
            String ln = children.item(i).getLocalName();
            if (ln != null && ln.equals(localName))
                return children.item(i);
        }

        return null;
    }

    private static Node findSiblingAttribute(String attrName, Iterable<Node> attribs, Node shapeNode)
    {
        for (Node attrib : attribs)
        {
            if (!attrib.getLocalName().equals(attrName))
                continue;

            if (attrib.getParentNode().equals(shapeNode.getParentNode()))
                return attrib;
        }

        return null;
    }

    private static ArrayList<Double> getDoubleValues(String stringValues)
    {
        String[] tokens = stringValues.trim().split("[ ,\n]");
        if (tokens.length < 1)
            return null;

        ArrayList<Double> arl = new ArrayList<Double>();
        for (String s : tokens)
        {
            if (s == null || s.length() < 1)
                continue;

            double d;
            try
            {
                d = Double.parseDouble(s);
            }
            catch (NumberFormatException e)
            {
                Logging.logger().log(Level.SEVERE, "GeoRSS.NumberFormatException", s);
                continue;
            }

            arl.add(d);
        }

        return arl;
    }

    private static double getElevation(Node shapeNode, Iterable<Node> attrs)
    {
        double elevation = 0d;

        Node elevNode = findSiblingAttribute("elev", attrs, shapeNode);
        if (elevNode != null)
        {
            ArrayList<Double> ev = getDoubleValues(elevNode.getTextContent());
            if (ev != null && ev.size() > 0)
            {
                elevation = ev.get(0);
            }
            else
            {
                Logging.logger().log(Level.WARNING, "GeoRSS.MissingElementContent",  "elev");
            }
        }

        return elevation;
    }
}

⌨️ 快捷键说明

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