001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.data.postgis.attributeio;
017:
018: import java.io.IOException;
019: import java.sql.PreparedStatement;
020: import java.sql.ResultSet;
021: import java.sql.SQLException;
022: import java.sql.Types;
023:
024: import org.geotools.data.DataSourceException;
025: import org.geotools.data.jdbc.attributeio.AttributeIO;
026:
027: import com.vividsolutions.jts.geom.Geometry;
028: import com.vividsolutions.jts.io.ParseException;
029: import com.vividsolutions.jts.io.WKTReader;
030: import com.vividsolutions.jts.io.WKTWriter;
031:
032: /**
033: * An attribute IO object that can read and write geometries encoded into
034: * EWKT format.
035: *
036: * @since @2.4.1 Read/Write EWKT
037: * @author jgarnett
038: * @source $URL: http://svn.geotools.org/geotools/branches/2.4.x/modules/library/jdbc/src/main/java/org/geotools/data/jdbc/attributeio/WKTAttributeIO.java $
039: */
040: public class EWKTAttributeIO implements AttributeIO {
041: WKTReader reader;
042: WKTWriter writer;
043:
044: /**
045: * Lazily initialize the WKTReader
046: */
047: private WKTReader getWKTReader() {
048: if (reader == null) {
049: reader = new WKTReader();
050: }
051: return reader;
052: }
053:
054: /**
055: * Lazily initialize the WKTWriter
056: *
057: */
058: private WKTWriter getWKTWriter() {
059: if (writer == null) {
060: writer = new WKTWriter();
061: }
062:
063: return writer;
064: }
065:
066: /**
067: * @see org.geotools.data.jdbc.attributeio.AttributeIO#read(java.sql.ResultSet,
068: * int)
069: */
070: public Object read(ResultSet rs, int position) throws IOException {
071: try {
072: String wkt = rs.getString(position);
073: if (wkt == null || wkt.equals("")) {
074: return null;
075: }
076: int srid = 0; // unknown!
077: // we must be in EWKT mode
078: int split = wkt.indexOf(";");
079: srid = Integer.parseInt(wkt.substring(5, split));
080: wkt = wkt.substring(split + 1);
081: Geometry geom = getWKTReader().read(wkt);
082: geom.setSRID(srid);
083: return geom;
084:
085: } catch (SQLException e) {
086: throw new DataSourceException("Sql reading problem", e);
087: } catch (ParseException e) {
088: throw new DataSourceException("Could not parse WKT", e);
089: }
090: }
091:
092: /**
093: * @see org.geotools.data.jdbc.attributeio.AttributeIO#write(java.sql.ResultSet,
094: * int, java.lang.Object)
095: */
096: public void write(ResultSet rs, int position, Object value)
097: throws IOException {
098: try {
099: if (value == null) {
100: rs.updateNull(position);
101: } else {
102: Geometry g = (Geometry) value;
103: String wkt = getWKTWriter().write(g);
104: String ewkt = "SRID=" + g.getSRID() + ";" + wkt;
105: rs.updateString(position, ewkt);
106: }
107: } catch (Exception e) {
108: throw new DataSourceException("Sql writing problem", e);
109: }
110: }
111:
112: /**
113: * @see org.geotools.data.jdbc.attributeio.AttributeIO#write(java.sql.ResultSet,
114: * int, java.lang.Object)
115: */
116: public void write(PreparedStatement ps, int position, Object value)
117: throws IOException {
118: try {
119: if (value == null) {
120: ps.setNull(position, Types.VARCHAR);
121: } else {
122: Geometry g = (Geometry) value;
123: String wkt = getWKTWriter().write(g);
124: String ewkt = "SRID=" + g.getSRID() + ";" + wkt;
125: ps.setString(position, ewkt);
126: }
127: } catch (Exception e) {
128: throw new DataSourceException("Sql writing problem", e);
129: }
130: }
131: }
|