[postgis-users] Problem with Z coordinate using postgis-jts

Kevin Neufeld kneufeld at refractions.net
Wed Mar 5 13:22:01 PST 2008


Alternatively, don't use the postgis jar at all ... use a regular 
postgres driver and JTS.  This yields the results you are looking for.  
The only real difference here is the use of WKBReader instead of 
JtsBinaryParser.

    public static void main(String[] args) throws Throwable {
        Class.forName("org.postgresql.Driver").newInstance();
        String sql = "CREATE TABLE test (pk_0 int4 NOT NULL);";
        sql += "select 
AddGeometryColumn('test','the_geom','-1','GEOMETRY','2');";
        sql += "insert into test " + "values(3, "
                + "GeomFromText('LINESTRING(191232 243118,191108 
243242)',-1));";

        Connection c = DriverManager.getConnection(
            "jdbc:postgresql://turtle:9876/cwb/",
            "postgres",
            "postgres");

        Statement st = c.createStatement();
        try {
            st.execute("drop table test");
        } catch (SQLException e) {

        }
        st.execute(sql);

        ResultSet rs = st.executeQuery("select * from test");
        rs.next();
        String geom = rs.getString(2);
        WKBReader reader = new WKBReader();
        Geometry g = reader.read(WKBReader.hexToBytes(geom));
        Coordinate[] coords = g.getCoordinates();
        for (int i = 0; i < coords.length; i++) {
            System.out.println(coords[i]);
        }
    }

-- output is
(191232.0, 243118.0, NaN)
(191108.0, 243242.0, NaN)

-------------
Kevin Neufeld
Software Developer
Refractions Research Inc.
300-1207 Douglas St.
Victoria, B.C., V8W 2E7

Phone: (250) 383-3022
Email: kneufeld at refractions.net



Fernando González wrote:
> Thank you for the answer. When I execute this code:
>
>     public static void main(String[] args) throws Throwable {
>         Class.forName("org.postgresql.Driver").newInstance();
>         String sql = "CREATE TABLE test (pk_0 int4 NOT NULL);";
>         sql += "select 
> AddGeometryColumn('test','the_geom','-1','GEOMETRY','2');";
>         sql += "insert into test "
>                 + "values(3, "
>                 + "GeomFromText('LINESTRING(191232 243118,191108 
> 243242)',-1));";
>
>         Connection c = DriverManager
>                 .getConnection("jdbc:postgresql://127.0.0.1/gdms/test",
>                         "postgres", "postgres");
>         ((PGConnection) c)
>                 .addDataType("geometry", org.postgis.PGgeometry.class);
>         ((PGConnection) c).addDataType("box3d", 
> org.postgis.PGbox3d.class);
>
>         Statement st = c.createStatement();
>         try {
>             st.execute("drop table test");
>         } catch (SQLException e) {
>
>         }
>         st.execute(sql);
>
>         ResultSet rs = st.executeQuery("select * from test");
>         rs.next();
>         String geom = rs.getString(2);
>         JtsBinaryParser parser = new JtsBinaryParser();
>         Geometry g = parser.parse(geom);
>         Coordinate[] coords = g.getCoordinates();
>         for (Coordinate coordinate : coords) {
>             System.out.println(coordinate);
>         }
>     }
>
> I obtain this output:
> (191232.0, 243118.0, 0.0)
> (191108.0, 243242.0, 0.0)
>
> I think I should obtain
> (191232.0, 243118.0, NaN)
> (191108.0, 243242.0, NaN)
>
> am I wrong?
>
>
> Fernando.
>
> On Wed, Mar 5, 2008 at 12:14 PM, Mark Cave-Ayland 
> <mark.cave-ayland at siriusit.co.uk 
> <mailto:mark.cave-ayland at siriusit.co.uk>> wrote:
>
>     On Wednesday 05 March 2008 10:13:26 Fernando González wrote:
>     > Hi,
>     >
>     > I'm storing and reading some geometries from a postgis table.
>     The table is
>     > 2D. I'm using a jar I have compiled with the "make postgis_jts"
>     command. It
>     > works very well except for one thing. I write 2D JTS geometries,
>     this is
>     > with the z component equal to NaN, into a postgis table but when
>     I read
>     > them the Z coordinate is no longer equal to NaN but equal to 0.
>     is this a
>     > feature? a bug?
>     >
>     > I'm using this code to read the geometry. To store them I use
>     GeomFromText
>     > function and I specify only X-Y components for each coordinate
>     (I'm not
>     > specifying the Z coordinate):
>     >
>     > JtsBinaryParser parser = new JtsBinaryParser();
>     > String bytes = rs.getString(fieldId);
>     > Geometry geom = parser.parse(bytes);
>     >
>     > is it clear? I can write some code to reproduce the problem if
>     anyone is
>     > interested.
>     >
>     > Thanks in advance,
>     > Fernando
>
>
>     Hi Fernando,
>
>     Yes please. I may not be the person that eventually looks at this,
>     however a
>     reproducible test case is enormously helpful in cases like these.
>
>
>     ATB,
>
>     Mark.
>
>     --
>     Mark Cave-Ayland
>     Sirius Corporation - The Open Source Experts
>     http://www.siriusit.co.uk
>     T: +44 870 608 0063
>     _______________________________________________
>     postgis-users mailing list
>     postgis-users at postgis.refractions.net
>     <mailto:postgis-users at postgis.refractions.net>
>     http://postgis.refractions.net/mailman/listinfo/postgis-users
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users
>   



More information about the postgis-users mailing list