01: package com.vividsolutions.jump.datastore.postgis;
02:
03: import java.sql.*;
04: import java.io.*;
05: import com.vividsolutions.jump.feature.*;
06: import com.vividsolutions.jts.geom.*;
07: import com.vividsolutions.jts.io.*;
08:
09: import org.postgresql.*;
10: import com.vividsolutions.jump.datastore.*;
11: import com.vividsolutions.jump.datastore.jdbc.*;
12:
13: /**
14: *
15: */
16: public class PostgisValueConverterFactory {
17: // should lazily init these
18: private final ValueConverter WKT_GEOMETRY_MAPPER = new WKTGeometryValueConverter();
19: private final ValueConverter WKB_GEOMETRY_MAPPER = new WKBGeometryValueConverter();
20:
21: private final Connection conn;
22: private final WKBReader wkbReader = new WKBReader();
23: private final WKTReader wktReader = new WKTReader();
24:
25: public PostgisValueConverterFactory(Connection conn) {
26: this .conn = conn;
27: }
28:
29: public ValueConverter getConverter(ResultSetMetaData rsm,
30: int columnIndex) throws SQLException {
31: String classname = rsm.getColumnClassName(columnIndex);
32: String dbTypeName = rsm.getColumnTypeName(columnIndex);
33:
34: // MD - this is slow - is there a better way?
35: if (dbTypeName.equalsIgnoreCase("geometry"))
36: // WKB is now the normal way to store geometry in PostGIS [mmichaud 2007-05-13]
37: return WKB_GEOMETRY_MAPPER;
38:
39: if (dbTypeName.equalsIgnoreCase("bytea"))
40: return WKB_GEOMETRY_MAPPER;
41:
42: // handle the standard types
43: ValueConverter stdConverter = ValueConverterFactory
44: .getConverter(rsm, columnIndex);
45: if (stdConverter != null)
46: return stdConverter;
47:
48: // default - can always show it as a string!
49: return ValueConverterFactory.STRING_MAPPER;
50: }
51:
52: class WKTGeometryValueConverter implements ValueConverter {
53: public AttributeType getType() {
54: return AttributeType.GEOMETRY;
55: }
56:
57: public Object getValue(ResultSet rs, int columnIndex)
58: throws IOException, SQLException, ParseException {
59: Object valObj = rs.getObject(columnIndex);
60: String s = valObj.toString();
61: Geometry geom = wktReader.read(s);
62: return geom;
63: }
64: }
65:
66: class WKBGeometryValueConverter implements ValueConverter {
67: public AttributeType getType() {
68: return AttributeType.GEOMETRY;
69: }
70:
71: public Object getValue(ResultSet rs, int columnIndex)
72: throws IOException, SQLException, ParseException {
73: //Object obj = rs.getObject(columnIndex);
74: //byte[] bytes = (byte[]) obj;
75: byte[] bytes = rs.getBytes(columnIndex);
76: Geometry geom = wkbReader.read(bytes);
77: return geom;
78: }
79: }
80: }
|