| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
package org.locationtech.jts.io.gml2; |
| 13 |
|
| 14 |
import java.io.IOException; |
| 15 |
import java.io.Reader; |
| 16 |
import java.io.StringReader; |
| 17 |
|
| 18 |
import javax.xml.parsers.ParserConfigurationException; |
| 19 |
import javax.xml.parsers.SAXParser; |
| 20 |
import javax.xml.parsers.SAXParserFactory; |
| 21 |
|
| 22 |
import org.locationtech.jts.geom.Geometry; |
| 23 |
import org.locationtech.jts.geom.GeometryCollection; |
| 24 |
import org.locationtech.jts.geom.GeometryFactory; |
| 25 |
import org.locationtech.jts.geom.PrecisionModel; |
| 26 |
import org.xml.sax.InputSource; |
| 27 |
import org.xml.sax.SAXException; |
| 28 |
import org.xml.sax.helpers.DefaultHandler; |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Reads a GML2 geometry from an XML fragment into a {@link Geometry}. |
| 33 |
* <p> |
| 34 |
* An example of the GML2 format handled is: |
| 35 |
* <pre> |
| 36 |
* <LineString> |
| 37 |
* <coordinates> |
| 38 |
* 24824.045318333192,38536.15071012041 |
| 39 |
* 26157.378651666528,37567.42733944659 26666.666,36000.0 |
| 40 |
* 26157.378651666528,34432.57266055341 |
| 41 |
* 24824.045318333192,33463.84928987959 |
| 42 |
* 23175.954681666804,33463.84928987959 |
| 43 |
* 21842.621348333472,34432.57266055341 21333.333,36000.0 |
| 44 |
* 21842.621348333472,37567.42733944659 |
| 45 |
* 23175.954681666808,38536.15071012041 |
| 46 |
* </coordinates> |
| 47 |
* </LineString> |
| 48 |
* </pre> |
| 49 |
* |
| 50 |
* The reader ignores namespace prefixes, |
| 51 |
* and disables both the validation and namespace options on the <tt>SAXParser</tt>. |
| 52 |
* This class requires the presence of a SAX Parser available via the |
| 53 |
* {@link javax.xml.parsers.SAXParserFactory#newInstance()} |
| 54 |
* method. |
| 55 |
* <p> |
| 56 |
* A specification of the GML XML format |
| 57 |
* can be found at the OGC web site: <a href='http://www.opengeospatial.org/'>http://www.opengeospatial.org/</a>. |
| 58 |
* <p> |
| 59 |
* It is the caller's responsibility to ensure that the supplied {@link PrecisionModel} |
| 60 |
* matches the precision of the incoming data. |
| 61 |
* If a lower precision for the data is required, a subsequent |
| 62 |
* process must be run on the data to reduce its precision. |
| 63 |
* <p> |
| 64 |
* To parse and build geometry directly from a SAX stream, see {@link GMLHandler}. |
| 65 |
* |
| 66 |
* @author David Zwiers, Vivid Solutions. |
| 67 |
* |
| 68 |
* @see GMLHandler |
| 69 |
*/ |
| 70 |
public class GMLReader |
| 71 |
{ |
| 72 |
|
| 73 |
/** |
| 74 |
* Reads a GML2 Geometry from a <tt>String</tt> into a single {@link Geometry} |
| 75 |
* |
| 76 |
* If a collection of geometries is found, a {@link GeometryCollection} is returned. |
| 77 |
* |
| 78 |
* @param gml The GML String to parse |
| 79 |
* @param geometryFactory When null, a default will be used. |
| 80 |
* @return the resulting JTS Geometry |
| 81 |
* |
| 82 |
* @throws ParserConfigurationException |
| 83 |
* @throws IOException |
| 84 |
* @throws SAXException |
| 85 |
* |
| 86 |
* @see #read(Reader, GeometryFactory) |
| 87 |
*/ |
| 88 |
public Geometry read(String gml, GeometryFactory geometryFactory) throws SAXException, IOException, ParserConfigurationException{ |
| 89 |
return read(new StringReader(gml),geometryFactory); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Reads a GML2 Geometry from a {@link Reader} into a single {@link Geometry} |
| 94 |
* |
| 95 |
* If a collection of Geometries is found, a {@link GeometryCollection} is returned. |
| 96 |
* |
| 97 |
* @param reader The input source |
| 98 |
* @param geometryFactory When null, a default will be used. |
| 99 |
* @return The resulting JTS Geometry |
| 100 |
* @throws SAXException |
| 101 |
* @throws IOException |
| 102 |
*/ |
| 103 |
public Geometry read(Reader reader, GeometryFactory geometryFactory) throws SAXException, IOException, ParserConfigurationException{ |
| 104 |
SAXParserFactory fact = SAXParserFactory.newInstance(); |
| 105 |
|
| 106 |
fact.setNamespaceAware(false); |
| 107 |
fact.setValidating(false); |
| 108 |
|
| 109 |
SAXParser parser = fact.newSAXParser(); |
| 110 |
|
| 111 |
if(geometryFactory == null) |
| 112 |
geometryFactory = new GeometryFactory(); |
| 113 |
|
| 114 |
GMLHandler gh = new GMLHandler(geometryFactory,null); |
| 115 |
parser.parse(new InputSource(reader), (DefaultHandler)gh); |
| 116 |
|
| 117 |
return gh.getGeometry(); |
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
|