]> granicus.if.org Git - postgis/commitdiff
Fixed all deprecation warnings in JTS code by using proper GeometryFactory instances...
authorMarkus Schaber <markus@schabi.de>
Thu, 20 Oct 2005 15:58:52 +0000 (15:58 +0000)
committerMarkus Schaber <markus@schabi.de>
Thu, 20 Oct 2005 15:58:52 +0000 (15:58 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@1977 b70326c6-7e19-0410-871a-916f4a2858ee

jdbc2/jtssrc/org/postgis/jts/JtsBinaryParser.java
jdbc2/jtssrc/org/postgis/jts/JtsBinaryWriter.java
jdbc2/jtssrc/org/postgis/jts/JtsGeometry.java
jdbc2/jtssrc/org/postgis/jts/JtsGisWrapper.java
jdbc2/jtssrc/org/postgis/jts/JtsWrapper.java

index 4818d9f6d9bfc40e67c6ac38a3bc94606ca9d3fd..afaea214e7163853fcc7f6a18487d135ff83a88f 100644 (file)
  */
 package org.postgis.jts;
 
-import com.vividsolutions.jts.geom.Coordinate;
-import com.vividsolutions.jts.geom.CoordinateSequence;
-import com.vividsolutions.jts.geom.Geometry;
-import com.vividsolutions.jts.geom.GeometryCollection;
-import com.vividsolutions.jts.geom.GeometryFactory;
-import com.vividsolutions.jts.geom.LineString;
-import com.vividsolutions.jts.geom.LinearRing;
-import com.vividsolutions.jts.geom.MultiLineString;
-import com.vividsolutions.jts.geom.MultiPoint;
-import com.vividsolutions.jts.geom.MultiPolygon;
-import com.vividsolutions.jts.geom.Point;
-import com.vividsolutions.jts.geom.Polygon;
-import com.vividsolutions.jts.geom.impl.PackedCoordinateSequence;
-
 import org.postgis.binary.ByteGetter;
 import org.postgis.binary.ValueGetter;
 import org.postgis.binary.ByteGetter.BinaryByteGetter;
 import org.postgis.binary.ByteGetter.StringByteGetter;
 
+import com.vividsolutions.jts.geom.*;
+import com.vividsolutions.jts.geom.impl.PackedCoordinateSequence;
+
 /**
  * Parse binary representation of geometries. Currently, only text rep (hexed)
  * implementation is tested.
@@ -55,10 +44,9 @@ import org.postgis.binary.ByteGetter.StringByteGetter;
  * 2^28 coordinates (8 bytes each).
  * 
  * @author Markus Schaber, markus.schaber@logix-tt.com
- *  
+ * 
  */
 public class JtsBinaryParser {
-    protected final GeometryFactory geofac = new GeometryFactory();
 
     /**
      * Get the appropriate ValueGetter for my endianness
@@ -101,6 +89,11 @@ public class JtsBinaryParser {
 
     /** Parse a geometry starting at offset. */
     protected Geometry parseGeometry(ValueGetter data) {
+        return parseGeometry(data, null);
+    }
+
+    /** Parse with a known geometry factory */
+    protected Geometry parseGeometry(ValueGetter data, GeometryFactory geofac) {
         byte endian = data.getByte(); // skip and test endian flag
         if (endian != data.endian) {
             throw new IllegalArgumentException("Endian inconsistency!");
@@ -113,48 +106,50 @@ public class JtsBinaryParser {
         boolean haveM = (typeword & 0x40000000) != 0;
         boolean haveS = (typeword & 0x20000000) != 0;
 
-        int srid = -1;
+        // srid defaults to 0 in jts
+        int srid = 0;
 
         if (haveS) {
             srid = data.getInt();
         }
-        
-        Geometry result1;
+
+        if (geofac == null) {
+            geofac = new GeometryFactory(JtsGeometry.prec, srid, JtsGeometry.csfac);
+        } else if (geofac.getSRID() != srid) {
+            throw new IllegalArgumentException("Inconsistent srids in complex geometry: " + srid
+                    + ", " + geofac.getSRID());
+        }
+        Geometry result;
         switch (realtype) {
-        case org.postgis.Geometry.POINT :
-            result1 = parsePoint(data, haveZ, haveM);
-            break;
-        case org.postgis.Geometry.LINESTRING :
-            result1 = parseLineString(data, haveZ, haveM);
-            break;
-        case org.postgis.Geometry.POLYGON :
-            result1 = parsePolygon(data, haveZ, haveM);
-            break;
-        case org.postgis.Geometry.MULTIPOINT :
-            result1 = parseMultiPoint(data);
-            break;
-        case org.postgis.Geometry.MULTILINESTRING :
-            result1 = parseMultiLineString(data);
-            break;
-        case org.postgis.Geometry.MULTIPOLYGON :
-            result1 = parseMultiPolygon(data);
-            break;
-        case org.postgis.Geometry.GEOMETRYCOLLECTION :
-            result1 = parseCollection(data);
-            break;
-        default :
-            throw new IllegalArgumentException("Unknown Geometry Type!");
+            case org.postgis.Geometry.POINT :
+                result = parsePoint(data, haveZ, haveM, geofac);
+                break;
+            case org.postgis.Geometry.LINESTRING :
+                result = parseLineString(data, haveZ, haveM, geofac);
+                break;
+            case org.postgis.Geometry.POLYGON :
+                result = parsePolygon(data, haveZ, haveM, geofac);
+                break;
+            case org.postgis.Geometry.MULTIPOINT :
+                result = parseMultiPoint(data, geofac);
+                break;
+            case org.postgis.Geometry.MULTILINESTRING :
+                result = parseMultiLineString(data, geofac);
+                break;
+            case org.postgis.Geometry.MULTIPOLYGON :
+                result = parseMultiPolygon(data, geofac);
+                break;
+            case org.postgis.Geometry.GEOMETRYCOLLECTION :
+                result = parseCollection(data, geofac);
+                break;
+            default :
+                throw new IllegalArgumentException("Unknown Geometry Type!");
         }
 
-        Geometry result = result1;
-
-        if (haveS) {
-            result.setSRID(srid);
-        }
         return result;
     }
 
-    private Point parsePoint(ValueGetter data, boolean haveZ, boolean haveM) {
+    private Point parsePoint(ValueGetter data, boolean haveZ, boolean haveM, GeometryFactory geofac) {
         double X = data.getDouble();
         double Y = data.getDouble();
         Point result;
@@ -202,45 +197,48 @@ public class JtsBinaryParser {
         return cs;
     }
 
-    private MultiPoint parseMultiPoint(ValueGetter data) {
+    private MultiPoint parseMultiPoint(ValueGetter data, GeometryFactory geofac) {
         Point[] points = new Point[data.getInt()];
         parseGeometryArray(data, points);
         return geofac.createMultiPoint(points);
     }
 
-    private LineString parseLineString(ValueGetter data, boolean haveZ, boolean haveM) {
+    private LineString parseLineString(ValueGetter data, boolean haveZ, boolean haveM,
+            GeometryFactory geofac) {
         return geofac.createLineString(parseCS(data, haveZ, haveM));
     }
 
-    private LinearRing parseLinearRing(ValueGetter data, boolean haveZ, boolean haveM) {
+    private LinearRing parseLinearRing(ValueGetter data, boolean haveZ, boolean haveM,
+            GeometryFactory geofac) {
         return geofac.createLinearRing(parseCS(data, haveZ, haveM));
     }
 
-    private Polygon parsePolygon(ValueGetter data, boolean haveZ, boolean haveM) {
+    private Polygon parsePolygon(ValueGetter data, boolean haveZ, boolean haveM,
+            GeometryFactory geofac) {
         int holecount = data.getInt() - 1;
         LinearRing[] rings = new LinearRing[holecount];
-        LinearRing shell = parseLinearRing(data, haveZ, haveM);
+        LinearRing shell = parseLinearRing(data, haveZ, haveM, geofac);
         for (int i = 0; i < holecount; i++) {
-            rings[i] = parseLinearRing(data, haveZ, haveM);
+            rings[i] = parseLinearRing(data, haveZ, haveM, geofac);
         }
         return geofac.createPolygon(shell, rings);
     }
 
-    private MultiLineString parseMultiLineString(ValueGetter data) {
+    private MultiLineString parseMultiLineString(ValueGetter data, GeometryFactory geofac) {
         int count = data.getInt();
         LineString[] strings = new LineString[count];
         parseGeometryArray(data, strings);
         return geofac.createMultiLineString(strings);
     }
 
-    private MultiPolygon parseMultiPolygon(ValueGetter data) {
+    private MultiPolygon parseMultiPolygon(ValueGetter data, GeometryFactory geofac) {
         int count = data.getInt();
         Polygon[] polys = new Polygon[count];
         parseGeometryArray(data, polys);
         return geofac.createMultiPolygon(polys);
     }
 
-    private GeometryCollection parseCollection(ValueGetter data) {
+    private GeometryCollection parseCollection(ValueGetter data, GeometryFactory geofac) {
         int count = data.getInt();
         Geometry[] geoms = new Geometry[count];
         parseGeometryArray(data, geoms);
index 0f64a897bfd172d6a5aa4c92f31fbd88b20fc287..ac12beda4848f7c724e36c671665091cfaa1127d 100644 (file)
@@ -137,7 +137,7 @@ public class JtsBinaryWriter {
         dest.setInt(typeword);
 
         if (checkSrid(geom)) {
-            dest.setInt(geom.getSRID());
+            dest.setInt(geom.getFactory().getSRID());
         }
 
         switch (plaintype) {
@@ -294,7 +294,7 @@ public class JtsBinaryWriter {
     }
 
     private boolean checkSrid(Geometry geom) {
-        final int srid = geom.getSRID();
+        final int srid = geom.getFactory().getSRID();
         // SRID is default 0 with jts geometries
         final boolean result = srid != -1 && srid != 0;
         return result;
index fd068a56769f79a38c100dddd8da227b4760a8de..d143d79a512e483b26ad67fef8eaa020eee8821e 100644 (file)
 
 package org.postgis.jts;
 
-import com.vividsolutions.jts.geom.Geometry;
-import com.vividsolutions.jts.io.WKTReader;
+import java.sql.SQLException;
 
 import org.postgresql.util.PGobject;
 
-import java.sql.SQLException;
+import com.vividsolutions.jts.geom.CoordinateSequenceFactory;
+import com.vividsolutions.jts.geom.Geometry;
+import com.vividsolutions.jts.geom.GeometryFactory;
+import com.vividsolutions.jts.geom.PrecisionModel;
+import com.vividsolutions.jts.geom.impl.PackedCoordinateSequenceFactory;
+import com.vividsolutions.jts.io.WKTReader;
 
 /**
  * JTS Geometry SQL wrapper. Supports PostGIS 1.x (lwgeom hexwkb) for writing
@@ -45,9 +49,10 @@ public class JtsGeometry extends PGobject {
 
     Geometry geom;
 
-    final static WKTReader reader = new WKTReader();
     final static JtsBinaryParser bp = new JtsBinaryParser();
     final static JtsBinaryWriter bw = new JtsBinaryWriter();
+    final static PrecisionModel prec = new PrecisionModel();
+    final static CoordinateSequenceFactory csfac = PackedCoordinateSequenceFactory.DOUBLE_FACTORY;
 
     /** Constructor called by JDBC drivers */
     public JtsGeometry() {
@@ -82,8 +87,9 @@ public class JtsGeometry extends PGobject {
                     value = temp[1].trim();
                     srid = Integer.parseInt(temp[0].substring(5));
                 }
+                final GeometryFactory factory = new GeometryFactory(prec, srid, csfac);
+                final WKTReader reader = new WKTReader(factory);
                 result = reader.read(value);
-                result.setSRID(srid);
                 return result;
             }
         } catch (Exception E) {
index bee777489f5525ae11497234f9741beda32d136e..a67ae70ccbfc298008c5c6613ca8c775ee1e21e2 100644 (file)
@@ -86,13 +86,13 @@ public class JtsGisWrapper extends Driver {
 
     /**
      * adds the JTS/PostGIS Data types to a PG Connection.
+     * 
+     * @throws SQLException
      */
-    public static void addGISTypes(PGConnection pgconn) {
-        pgconn.addDataType("geometry", "org.postgis.jts.JtsGeometry");
-        pgconn.addDataType("box3d", "org.postgis.PGbox3d");
-        //pgconn.addDataType("geometry",
-        // org.postgis.jts.JtsGeometry.class);
-        //pgconn.addDataType("box3d", org.postgis.PGbox3d.class);
+    public static void addGISTypes(PGConnection pgconn) throws SQLException {
+        pgconn.addDataType("geometry", org.postgis.jts.JtsGeometry.class);
+        pgconn.addDataType("box3d", org.postgis.PGbox3d.class);
+        pgconn.addDataType("box2d", org.postgis.PGbox2d.class);
     }
 
     /**
index 29544dff50a42f17fa2757314d772abfa2bfa3de..e83dc6e9c227e6cac04c62a586b7f6b38c717b9e 100644 (file)
@@ -93,15 +93,10 @@ public class JtsWrapper extends Driver {
 
     /**
      * adds the JTS/PostGIS Data types to a PG Connection.
+     * @throws SQLException 
      */
-    public static void addGISTypes(PGConnection pgconn) {
-        // Add data type up to PostgreSQL jdbc driver V7.4
-        pgconn.addDataType("geometry", "org.postgis.jts.JtsGeometry");
-
-        // Use the following for PostgreSQL jdbc driver V8.0 or newer
-        // The above way still works, but is deprecated.
-        // pgconn.addDataType("geometry",
-        // org.postgis.jts.JtsGeometry.class);
+    public static void addGISTypes(PGConnection pgconn) throws SQLException {
+        pgconn.addDataType("geometry", org.postgis.jts.JtsGeometry.class);
     }
 
     /**