01: /*
02: * Geotools2 - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2002-2006, Geotools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: */
17: package org.geotools.data.shapefile.shp.xml;
18:
19: import java.io.IOException;
20: import java.net.URL;
21: import java.net.URLConnection;
22:
23: import org.jdom.Document;
24: import org.jdom.Element;
25: import org.jdom.JDOMException;
26: import org.jdom.input.SAXBuilder;
27:
28: import com.vividsolutions.jts.geom.Envelope;
29:
30: public class ShpXmlFileReader {
31:
32: Document dom;
33:
34: /**
35: * Parse metadataFile (currently for bounding box information).
36: * <p>
37: *
38: * </p>
39: * @param metadatFileURL
40: * @throws JDOMException
41: * @throws IOException
42: */
43: public ShpXmlFileReader(URL metadatFileURL) throws JDOMException,
44: IOException {
45: SAXBuilder builder = new SAXBuilder(false);
46:
47: URLConnection connection = metadatFileURL.openConnection();
48:
49: dom = builder.build(connection.getInputStream());
50: }
51:
52: public Metadata parse() {
53: return parseMetadata(dom.getRootElement());
54: }
55:
56: protected Metadata parseMetadata(Element root) {
57: Metadata meta = new Metadata();
58: meta.setIdinfo(parseIdInfo(root.getChild("idinfo")));
59:
60: return meta;
61: }
62:
63: protected IdInfo parseIdInfo(Element element) {
64: IdInfo idInfo = new IdInfo();
65:
66: Element bounding = element.getChild("spdom").getChild(
67: "bounding");
68: idInfo.setBounding(parseBounding(bounding));
69:
70: Element lbounding = element.getChild("spdom").getChild(
71: "lbounding");
72: idInfo.setLbounding(parseBounding(lbounding));
73:
74: return idInfo;
75: }
76:
77: protected Envelope parseBounding(Element bounding) {
78: if (bounding == null)
79: return new Envelope();
80:
81: double minX = Double.parseDouble(bounding
82: .getChildText("westbc"));
83: double maxX = Double.parseDouble(bounding
84: .getChildText("eastbc"));
85: double minY = Double.parseDouble(bounding
86: .getChildText("southbc"));
87: double maxY = Double.parseDouble(bounding
88: .getChildText("northbc"));
89:
90: return new Envelope(minX, maxX, minY, maxY);
91: }
92:
93: }
|