01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.data.mysql;
17:
18: import java.io.IOException;
19:
20: import org.geotools.data.FeatureReader;
21: import org.geotools.data.jdbc.JDBCTextFeatureWriter;
22: import org.geotools.data.jdbc.QueryData;
23:
24: import com.vividsolutions.jts.geom.Geometry;
25: import com.vividsolutions.jts.geom.GeometryCollection;
26: import com.vividsolutions.jts.geom.LineString;
27: import com.vividsolutions.jts.geom.MultiLineString;
28: import com.vividsolutions.jts.geom.MultiPoint;
29: import com.vividsolutions.jts.geom.MultiPolygon;
30: import com.vividsolutions.jts.geom.Point;
31: import com.vividsolutions.jts.geom.Polygon;
32: import com.vividsolutions.jts.io.WKTWriter;
33:
34: /**
35: * Feature writer handling specific geometric function from MySQL 4.1
36: *
37: * TODO This ought to handle MySQL 4.1's geometric datatypes, but it does not work.
38: * This is because 4.1 sends geometric data in a different packet format than other
39: * datatypes, and because of this the MySQL driver does not allow ResultSet objects
40: * with geometric data to be updatable. I have not found anything about this MySQL
41: * bug in the MySQL bug database, so I will add a new bug there. In the meantime,
42: * this package should work fine for writing non-geometric data.
43: * @author Gary Sheppard garysheppard@psu.edu
44:
45: *
46: * @author wolf
47: * @author Gary Sheppard garysheppard@psu.edu
48: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/mysql/src/main/java/org/geotools/data/mysql/MySQLFeatureWriter.java $
49: */
50: public class MySQLFeatureWriter extends JDBCTextFeatureWriter {
51: private static WKTWriter geometryWriter = new WKTWriter();
52:
53: public MySQLFeatureWriter(FeatureReader fReader, QueryData queryData)
54: throws IOException {
55: super (fReader, queryData);
56: }
57:
58: /**
59: * @see org.geotools.data.jdbc.JDBCTextFeatureWriter#getGeometryInsertText(com.vividsolutions.jts.geom.Geometry,
60: * int)
61: */
62: protected String getGeometryInsertText(Geometry geom, int srid) {
63: if (geom == null) {
64: return "NULL";
65: }
66:
67: String geoText = geometryWriter.write(geom);
68: String sql = null;
69:
70: if (GeometryCollection.class.isAssignableFrom(geom.getClass())) {
71: if (MultiPoint.class.isAssignableFrom(geom.getClass())) {
72: sql = "MultiPointFromText";
73: } else if (MultiLineString.class.isAssignableFrom(geom
74: .getClass())) {
75: sql = "MultiLineStringFromText";
76: } else if (MultiPolygon.class.isAssignableFrom(geom
77: .getClass())) {
78: sql = "MultiPolygonFromText";
79: } else {
80: sql = "GeometryCollectionFromText";
81: }
82: } else {
83: if (Point.class.isAssignableFrom(geom.getClass())) {
84: sql = "PointFromText";
85: } else if (LineString.class.isAssignableFrom(geom
86: .getClass())) {
87: sql = "LineStringFromText";
88: } else if (Polygon.class.isAssignableFrom(geom.getClass())) {
89: sql = "PolygonFromText";
90: } else {
91: sql = "GeometryFromText";
92: }
93: }
94:
95: sql += ("('" + geoText + "', " + srid + ")");
96:
97: return sql;
98: }
99: }
|