001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002, 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: */
017: package org.geotools.data.geometryless.attributeio;
018:
019: import java.io.IOException;
020: import java.sql.PreparedStatement;
021: import java.sql.ResultSet;
022: import java.sql.SQLException;
023: import java.sql.Types;
024:
025: import org.geotools.data.DataSourceException;
026: import org.geotools.data.jdbc.attributeio.AttributeIO;
027:
028: import com.vividsolutions.jts.geom.Coordinate;
029: import com.vividsolutions.jts.geom.GeometryFactory;
030:
031: /**
032: * WARNING - this may mess up if the geometry is not the last attribute in the
033: * schema. Some one please test this and remove this/fix it.
034: *
035: * <p>
036: * This is a very specific AttributeIO to deal with a point from two x, y
037: * columns. This probably should be generalized, into a more generic scheme
038: * to map multiple columns into one object, either geometry or non-geometry
039: * columns. Hopefully we can revisit this relatively soon.
040: * </p>
041: *
042: * @author Chris Holmes, TOPP
043: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/geometryless/src/main/java/org/geotools/data/geometryless/attributeio/PointXYAttributeIO.java $
044: * @version $Id: PointXYAttributeIO.java 17700 2006-01-22 23:30:39Z desruisseaux $
045: */
046: public class PointXYAttributeIO implements AttributeIO {
047: //TODO: I beleive we have a geotools specific geometry factory, or will
048: //have one soon, that uses fancy coordinate sequences, but I'm not sure
049: //where it is. It should probably be used here.
050: private static GeometryFactory gFactory = new GeometryFactory();
051:
052: /**
053: * @see org.geotools.data.jdbc.attributeio.AttributeIO#read(java.sql.ResultSet,
054: * int)
055: */
056: public Object read(ResultSet rs, int position) throws IOException {
057: try {
058: //casting to a Number is safe, since we checked during the
059: //buildAttributeType method to make sure the column type was
060: //a number. Would probably be a bit faster to use straight
061: //java primitives, but then we would have to hard code which
062: //type we could deal with, or else make the user specify.
063: Number x = (Number) rs.getObject(position);
064:
065: //+1 is safe, since the sql builder specifies the x and y columns
066: //right next to eachother.
067: Number y = (Number) rs.getObject(position + 1);
068:
069: Coordinate coord = new Coordinate(x.doubleValue(), y
070: .doubleValue());
071:
072: return gFactory.createPoint(coord);
073: } catch (SQLException e) {
074: throw new DataSourceException("Sql problem.", e);
075: }
076: }
077:
078: /**
079: * NOT YET IMPLEMENTED!!! (though probably shouldn't be too hard.
080: *
081: * @see org.geotools.data.jdbc.attributeio.AttributeIO#write(java.sql.ResultSet,
082: * int, java.lang.Object)
083: */
084: public void write(ResultSet rs, int position, Object value)
085: throws IOException {
086: try {
087: if (value == null) {
088: rs.updateNull(position);
089: } else {
090: rs.updateObject(position, value);
091: }
092: } catch (Exception e) {
093: throw new DataSourceException("Sql problem.", e);
094: }
095: }
096:
097: /**
098: * NOT YET IMPLEMENTED!!!
099: *
100: * @see org.geotools.data.jdbc.attributeio.AttributeIO#write(java.sql.PreparedStatement,
101: * int, java.lang.Object)
102: */
103: public void write(PreparedStatement ps, int position, Object value)
104: throws IOException {
105: try {
106: if (value == null) {
107: ps.setNull(position, Types.OTHER);
108: } else {
109: ps.setObject(position, value);
110: }
111: } catch (Exception e) {
112: throw new DataSourceException("Sql problem.", e);
113: }
114: }
115: }
|