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.Envelope;
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 bounding box from four minx, miny, maxx, maxy
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: ** @author Rob Atkinson, Social Change Online
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/BBOXAttributeIO.java $
044: * @version $Id: BBOXAttributeIO.java 17700 2006-01-22 23:30:39Z desruisseaux $
045: */
046: public class BBOXAttributeIO 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 minx = (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 miny = (Number) rs.getObject(position + 1);
068:
069: //+1 is safe, since the sql builder specifies the x and y columns
070: //right next to eachother.
071: Number maxx = (Number) rs.getObject(position + 2);
072:
073: //+1 is safe, since the sql builder specifies the x and y columns
074: //right next to eachother.
075: Number maxy = (Number) rs.getObject(position + 3);
076:
077: return gFactory.toGeometry(new Envelope(minx.doubleValue(),
078: maxx.doubleValue(), miny.doubleValue(), maxy
079: .doubleValue()));
080: } catch (SQLException e) {
081: throw new DataSourceException("Sql problem.", e);
082: }
083: }
084:
085: /**
086: * NOT YET IMPLEMENTED!!! (though probably shouldn't be too hard.
087: *
088: * @see org.geotools.data.jdbc.attributeio.AttributeIO#write(java.sql.ResultSet,
089: * int, java.lang.Object)
090: */
091: public void write(ResultSet rs, int position, Object value)
092: throws IOException {
093: try {
094: if (value == null) {
095: rs.updateNull(position);
096: } else {
097: rs.updateObject(position, value);
098: }
099: } catch (Exception e) {
100: throw new DataSourceException("Sql problem.", e);
101: }
102: }
103:
104: /**
105: * NOT YET IMPLEMENTED!!!
106: *
107: * @see org.geotools.data.jdbc.attributeio.AttributeIO#write(java.sql.PreparedStatement,
108: * int, java.lang.Object)
109: */
110: public void write(PreparedStatement ps, int position, Object value)
111: throws IOException {
112: try {
113: if (value == null) {
114: ps.setNull(position, Types.OTHER);
115: } else {
116: ps.setObject(position, value);
117: }
118: } catch (Exception e) {
119: throw new DataSourceException("Sql problem.", e);
120: }
121: }
122: }
|