Changeset 8710


Ignore:
Timestamp:
Jan 9, 2012 3:01:44 AM (5 years ago)
Author:
strk
Message:

Update Java components to support SRID<=0 as unknown SRID (#1221)

This is first patch by Maria Arias de Reyna taking on maintainance
of the Java components. It includes tweaks in README file and
a new maven based build system. It also includes some indenting
changes.

Location:
trunk/java/jdbc
Files:
3 added
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/java/jdbc/README

    r2465 r8710  
    6969* How do I build it? *
    7070
    71 You need a recent pgjdbc driver jar, see the download link from above.
    72 It is currently tested and supported with 7.2, 7.3, 7.4, 8.0 and 8.1 pgjdbc
    73 releases. Those are the same PostgreSQL releases that are supported by the
    74 PostGIS server-side C code, and apart from 7.2, the same as currently
    75 supported by PostgreSQL server developers.
    76 
    77 The current PostGIS jdbc release is also reported to run (but not compile)
    78 against the postgresql jdbc7.1-1.2.jar, but this is not supported, as well as
    79 current PostGIS backend itsself does not support 7.1 servers any more.
    80 
    81 Note that your pgjdbc driver version does not constrain the PostgreSQL
     71There are older make files with which you can try to build, but maven is recommended,
     72as it handles dependencies on a better and cleaner way.
     73
     74You have to install maven on your computer to build it. To install maven you can try
     75to search on your software repositories or read the documentation:
     76http://maven.apache.org/download.html
     77
     78To compile your postgis driver, go to the jdbc folder and execute the console
     79command "mvn package". This should create a postgis jar on the target folder
     80inside the jdbc folder.
     81
     82Note that your postgis driver version does not constrain the PostgreSQL
    8283server version. As the JDBC drivers are downwards compatible against older
    8384servers, and PostgreSQL servers typically accept older clients, you can
     
    8687newest stable pgjdbc build that is documented to work against your
    8788server release.
    88 
    89 Make shure the pgjdbc driver is available in your Java CLASSPATH,
    90 either by setting the environment variable, or by editing the
    91 Makefile.
    92 
    93 A "make jar" then compiles the code and creates two jar files. The
    94 "postgis.jar" is for normal usage and deployment, the
    95 "postgis_debug.jar" additionally includes the source code, for
    96 debugging purposes.
    97 
    9889
    9990* It is called jdbc2 - does it work with jdbc3, too? *
  • trunk/java/jdbc/jtssrc/org/postgis/jts/JtsBinaryParser.java

    r8481 r8710  
    103103
    104104        if (haveS) {
    105             int newsrid = Geometry.parseSRID(data.getInt());
     105            int newsrid = org.postgis.Geometry.parseSRID(data.getInt());
    106106            if (inheritSrid && newsrid != srid) {
    107107                throw new IllegalArgumentException("Inconsistent srids in complex geometry: " + srid + ", " + newsrid);
     
    110110            }
    111111        } else if (!inheritSrid) {
    112             srid = Geometry.UNKNOWN_SRID;
     112            srid = org.postgis.Geometry.UNKNOWN_SRID;
    113113        }
    114114       
  • trunk/java/jdbc/src/examples/TestServer.java

    r1622 r8710  
    3434public class TestServer {
    3535
    36     public static void main(String[] args) {
    37         if (args.length != 4 && args.length != 3) {
    38             System.err.println("Usage: java examples/TestServer dburl user pass [tablename]");
    39             System.err.println();
    40             System.err.println("dburl has the following format:");
    41             System.err.println("jdbc:postgresql://HOST:PORT/DATABASENAME");
    42             System.err.println("tablename is 'jdbc_test' by default.");
    43             System.exit(1);
    44         }
     36        public static void main(String[] args) {
     37                if (args.length != 4 && args.length != 3) {
     38                        System.err
     39                                        .println("Usage: java examples/TestServer dburl user pass [tablename]");
     40                        System.err.println();
     41                        System.err.println("dburl has the following format:");
     42                        System.err.println("jdbc:postgresql://HOST:PORT/DATABASENAME");
     43                        System.err.println("tablename is 'jdbc_test' by default.");
     44                        System.exit(1);
     45                }
    4546
    46         Connection conn;
     47                Connection conn;
    4748
    48         String dburl = args[0];
    49         String dbuser = args[1];
    50         String dbpass = args[2];
     49                String dburl = args[0];
     50                String dbuser = args[1];
     51                String dbpass = args[2];
    5152
    52         String dbtable = "jdbc_test";
     53                String dbtable = "jdbc_test";
    5354
    54         String dropSQL = "drop table " + dbtable;
    55         String createSQL = "create table " + dbtable + " (geom geometry, id int4)";
    56         String insertPointSQL = "insert into " + dbtable + " values ('POINT (10 10 10)',1)";
    57         String insertPolygonSQL = "insert into " + dbtable
    58                 + " values ('POLYGON ((0 0 0,0 10 0,10 10 0,10 0 0,0 0 0))',2)";
     55                String dropSQL = "drop table " + dbtable;
     56                String createSQL = "create table " + dbtable
     57                                + " (geom geometry, id int4)";
     58                String insertPointSQL = "insert into " + dbtable
     59                                + " values ('POINT (10 10 10)',1)";
     60                String insertPolygonSQL = "insert into " + dbtable
     61                                + " values ('POLYGON ((0 0 0,0 10 0,10 10 0,10 0 0,0 0 0))',2)";
    5962
    60         try {
     63                try {
    6164
    62             System.out.println("Creating JDBC connection...");
    63             Class.forName("org.postgresql.Driver");
    64             conn = DriverManager.getConnection(dburl, dbuser, dbpass);
    65             System.out.println("Adding geometric type entries...");
    66             /*
    67              * magic trickery to be pgjdbc 7.2 compatible
    68              *
    69              * This works due to the late binding of data types in most java
    70              * VMs. As this is more a demo source than a real-world app, we can
    71              * risk breaking on exotic VMs here. Real-world apps usually do not
    72              * suffer from this problem as they do not have to support such a
    73              * wide range of different pgjdbc releases, or they can use the
    74              * approach from org.postgis.DriverWrapper (which we do not use here
    75              * intentionally to have a test for the other ways to do it).
    76              */
    77             if (conn.getClass().getName().equals("org.postgresql.jdbc2.Connection")) {
    78                 ((org.postgresql.Connection) conn).addDataType("geometry", "org.postgis.PGgeometry");
    79                 ((org.postgresql.Connection) conn).addDataType("box3d", "org.postgis.PGbox3d");
    80             } else {
    81                 ((org.postgresql.PGConnection) conn).addDataType("geometry",
    82                         "org.postgis.PGgeometry");
    83                 ((org.postgresql.PGConnection) conn).addDataType("box3d", "org.postgis.PGbox3d");
    84             }
    85             Statement s = conn.createStatement();
    86             System.out.println("Creating table with geometric types...");
    87             // table might not yet exist
    88             try {
    89                 s.execute(dropSQL);
    90             } catch (Exception e) {
    91                 System.out.println("Error dropping old table: " + e.getMessage());
    92             }
    93             s.execute(createSQL);
    94             System.out.println("Inserting point...");
    95             s.execute(insertPointSQL);
    96             System.out.println("Inserting polygon...");
    97             s.execute(insertPolygonSQL);
    98             System.out.println("Done.");
    99             s = conn.createStatement();
    100             System.out.println("Querying table...");
    101             ResultSet r = s.executeQuery("select asText(geom),id from " + dbtable);
    102             while (r.next()) {
    103                 Object obj = r.getObject(1);
    104                 int id = r.getInt(2);
    105                 System.out.println("Row " + id + ":");
    106                 System.out.println(obj.toString());
    107             }
    108             s.close();
    109             conn.close();
    110         } catch (Exception e) {
    111             System.err.println("Aborted due to error:");
    112             e.printStackTrace();
    113             System.exit(1);
    114         }
    115     }
     65                        System.out.println("Creating JDBC connection...");
     66                        Class.forName("org.postgresql.Driver");
     67                        conn = DriverManager.getConnection(dburl, dbuser, dbpass);
     68                        System.out.println("Adding geometric type entries...");
     69
     70                        ((org.postgresql.PGConnection) conn).addDataType("geometry",
     71                                        org.postgis.PGgeometry.class);
     72                        ((org.postgresql.PGConnection) conn).addDataType("box3d",
     73                                        org.postgis.PGbox3d.class);
     74
     75                        Statement s = conn.createStatement();
     76                        System.out.println("Creating table with geometric types...");
     77                        // table might not yet exist
     78                        try {
     79                                s.execute(dropSQL);
     80                        } catch (Exception e) {
     81                                System.out.println("Error dropping old table: "
     82                                                + e.getMessage());
     83                        }
     84                        s.execute(createSQL);
     85                        System.out.println("Inserting point...");
     86                        s.execute(insertPointSQL);
     87                        System.out.println("Inserting polygon...");
     88                        s.execute(insertPolygonSQL);
     89                        System.out.println("Done.");
     90                        s = conn.createStatement();
     91                        System.out.println("Querying table...");
     92                        ResultSet r = s.executeQuery("select asText(geom),id from "
     93                                        + dbtable);
     94                        while (r.next()) {
     95                                Object obj = r.getObject(1);
     96                                int id = r.getInt(2);
     97                                System.out.println("Row " + id + ":");
     98                                System.out.println(obj.toString());
     99                        }
     100                        s.close();
     101                        conn.close();
     102                } catch (Exception e) {
     103                        System.err.println("Aborted due to error:");
     104                        e.printStackTrace();
     105                        System.exit(1);
     106                }
     107        }
    116108}
  • trunk/java/jdbc/src/org/postgis/DriverWrapper.java

    r2570 r8710  
    225225     * @throws SQLException
    226226     */
    227     public static void addGISTypes72(org.postgresql.Connection pgconn) throws SQLException {
     227    public static void addGISTypes72(org.postgresql.PGConnection pgconn) throws SQLException {
    228228        loadTypesAdder("72").addGT((Connection) pgconn, false);
    229229    }
     
    265265    /** addGISTypes for V7.3 and V7.4 pgjdbc */
    266266    protected static final class TypesAdder74 extends TypesAdder {
    267         public void addGeometries(Connection conn) {
    268             PGConnection pgconn = (PGConnection) conn;
    269             pgconn.addDataType("geometry", "org.postgis.PGgeometry");
    270         }
    271 
    272         public void addBoxen(Connection conn) {
    273             PGConnection pgconn = (PGConnection) conn;
    274             pgconn.addDataType("box3d", "org.postgis.PGbox3d");
    275             pgconn.addDataType("box2d", "org.postgis.PGbox2d");
    276         }
    277 
    278         public void addBinaryGeometries(Connection conn) {
    279             PGConnection pgconn = (PGConnection) conn;
    280             pgconn.addDataType("geometry", "org.postgis.PGgeometryLW");
     267        public void addGeometries(Connection conn) throws SQLException {
     268            PGConnection pgconn = (PGConnection) conn;
     269            pgconn.addDataType("geometry", org.postgis.PGgeometry.class);
     270        }
     271
     272        public void addBoxen(Connection conn) throws SQLException {
     273            PGConnection pgconn = (PGConnection) conn;
     274            pgconn.addDataType("box3d", org.postgis.PGbox3d.class);
     275            pgconn.addDataType("box2d", org.postgis.PGbox2d.class);
     276        }
     277
     278        public void addBinaryGeometries(Connection conn) throws SQLException {
     279            PGConnection pgconn = (PGConnection) conn;
     280            pgconn.addDataType("geometry", org.postgis.PGgeometryLW.class);
    281281        }
    282282    }
     
    284284    /** addGISTypes for V7.2 pgjdbc */
    285285    protected static class TypesAdder72 extends TypesAdder {
    286         public void addGeometries(Connection conn) {
    287             org.postgresql.Connection pgconn = (org.postgresql.Connection) conn;
    288             pgconn.addDataType("geometry", "org.postgis.PGgeometry");
    289         }
    290 
    291         public void addBoxen(Connection conn) {
    292             org.postgresql.Connection pgconn = (org.postgresql.Connection) conn;
    293             pgconn.addDataType("box3d", "org.postgis.PGbox3d");
    294             pgconn.addDataType("box2d", "org.postgis.PGbox2d");
    295         }
    296 
    297         public void addBinaryGeometries(Connection conn) {
    298             org.postgresql.Connection pgconn = (org.postgresql.Connection) conn;
    299             pgconn.addDataType("geometry", "org.postgis.PGgeometryLW");
     286        public void addGeometries(Connection conn) throws SQLException {
     287            org.postgresql.PGConnection pgconn = (org.postgresql.PGConnection) conn;
     288            pgconn.addDataType("geometry", org.postgis.PGgeometry.class);
     289        }
     290
     291                public void addBoxen(Connection conn) throws SQLException {
     292            org.postgresql.PGConnection pgconn = (org.postgresql.PGConnection) conn;
     293            pgconn.addDataType("box3d", org.postgis.PGbox3d.class);
     294            pgconn.addDataType("box2d", org.postgis.PGbox2d.class);
     295        }
     296
     297        public void addBinaryGeometries(Connection conn) throws SQLException {
     298            org.postgresql.PGConnection pgconn = (org.postgresql.PGConnection) conn;
     299            pgconn.addDataType("geometry", org.postgis.PGgeometryLW.class);
    300300        }
    301301    }
  • trunk/java/jdbc/src/org/postgis/Geometry.java

    r8487 r8710  
    3131/** The base class of all geometries */
    3232public abstract class Geometry implements Serializable {
    33     /* JDK 1.5 Serialization */
    34     private static final long serialVersionUID = 0x100;
    35 
    36     // OpenGIS Geometry types as defined in the OGC WKB Spec
    37     // (May we replace this with an ENUM as soon as JDK 1.5
    38     // has gained widespread usage?)
    39 
    40     /** Fake type for linear ring */
    41     public static final int LINEARRING = 0;
    42     /**
    43      * The OGIS geometry type number for points.
    44      */
    45     public static final int POINT = 1;
    46 
    47     /**
    48      * The OGIS geometry type number for lines.
    49      */
    50     public static final int LINESTRING = 2;
    51 
    52     /**
    53      * The OGIS geometry type number for polygons.
    54      */
    55     public static final int POLYGON = 3;
    56 
    57     /**
    58      * The OGIS geometry type number for aggregate points.
    59      */
    60     public static final int MULTIPOINT = 4;
    61 
    62     /**
    63      * The OGIS geometry type number for aggregate lines.
    64      */
    65     public static final int MULTILINESTRING = 5;
    66 
    67     /**
    68      * The OGIS geometry type number for aggregate polygons.
    69      */
    70     public static final int MULTIPOLYGON = 6;
    71 
    72     /**
    73      * The OGIS geometry type number for feature collections.
    74      */
    75     public static final int GEOMETRYCOLLECTION = 7;
    76 
    77     public static final String[] ALLTYPES = new String[]{
    78         "", // internally used LinearRing does not have any text in front of it
    79         "POINT",
    80         "LINESTRING",
    81         "POLYGON",
    82         "MULTIPOINT",
    83         "MULTILINESTRING",
    84         "MULTIPOLYGON",
    85         "GEOMETRYCOLLECTION"};
    86 
    87     /**
    88      * The Text representations of the geometry types
    89      */
    90     public static String getTypeString(int type) {
    91         if (type >= 0 && type <= 7) {
    92             return ALLTYPES[type];
    93         } else {
    94             throw new IllegalArgumentException("Unknown Geometry type" + type);
    95         }
    96     }
    97 
    98     // Properties common to all geometries
    99     /**
    100      * The dimensionality of this feature (2,3)
    101      */
    102     public int dimension;
    103 
    104     /**
    105      * Do we have a measure (4th dimension)
    106      */
    107     public boolean haveMeasure = false;
    108 
    109     /**
    110      * The OGIS geometry type of this feature. this is final as it never
    111      * changes, it is bound to the subclass of the instance.
    112      */
    113     public final int type;
    114 
    115     /**
    116      * Official UNKNOWN srid value
    117      */
    118     public final int UNKNOWN_SRID = 0;
    119 
    120     /**
    121      * The spacial reference system id of this geometry, default is no srid
    122      */
    123     public int srid = UNKNOWN_SRID;
    124 
    125     /**
    126      * Parse a SRID value, anything <= 0 is unknown
    127      */
    128     public static int parseSRID(int srid) {
    129         if ( srid < 0 ) {
    130             /* TODO: raise a warning ? */
    131             srid = 0;
    132         }
    133         return srid;
    134     }
    135 
    136     /**
    137      * Constructor for subclasses
    138      *
    139      * @param type has to be given by all subclasses.
    140      */
    141     protected Geometry(int type) {
    142         this.type = type;
    143     }
    144 
    145     /**
    146      * java.lang.Object hashCode implementation
    147      */
    148     public int hashCode() {
    149         return dimension | (type * 4) | (srid * 32);
    150     }
    151 
    152     /**
    153      * java.lang.Object equals implementation
    154      */
    155     public boolean equals(Object other) {
    156         return (other != null) && (other instanceof Geometry) && equals((Geometry) other);
    157     }
    158 
    159     /**
    160      * geometry specific equals implementation - only defined for non-null
    161      * values
    162      */
    163     public boolean equals(Geometry other) {
    164         return (other != null)
    165                 && (this.dimension == other.dimension)
    166                 && (this.type == other.type)
    167                 && (this.srid == other.srid)
    168                 && (this.haveMeasure == other.haveMeasure)
    169                 && other.getClass().equals(this.getClass())
    170                 && this.equalsintern(other);
    171     }
    172 
    173     /**
    174      * Whether test coordinates for geometry - subclass specific code
    175      *
    176      * Implementors can assume that dimensin, type, srid and haveMeasure are
    177      * equal, other != null and other is the same subclass.
    178      */
    179     protected abstract boolean equalsintern(Geometry other);
    180 
    181     /**
    182      * Return the number of Points of the geometry
    183      */
    184     public abstract int numPoints();
    185 
    186     /**
    187      * Get the nth Point of the geometry
    188      *
    189      * @param n the index of the point, from 0 to numPoints()-1;
    190      * @throws ArrayIndexOutOfBoundsException in case of an emtpy geometry or
    191      *             bad index.
    192      */
    193     public abstract Point getPoint(int n);
    194 
    195     /**
    196      * Same as getPoint(0);
    197      */
    198     public abstract Point getFirstPoint();
    199 
    200     /**
    201      * Same as getPoint(numPoints()-1);
    202      */
    203     public abstract Point getLastPoint();
    204 
    205     /**
    206      * The OGIS geometry type number of this geometry.
    207      */
    208     public int getType() {
    209         return this.type;
    210     }
    211 
    212     /**
    213      * Return the Type as String
    214      */
    215     public String getTypeString() {
    216         return getTypeString(this.type);
    217     }
    218 
    219     /** Returns whether we have a measure */
    220     public boolean isMeasured() {
    221         return haveMeasure;
    222     }
    223 
    224     /**
    225      * Queries the number of geometric dimensions of this geometry. This does
    226      * not include measures, as opposed to the server.
    227      *
    228      * @return The dimensionality (eg, 2D or 3D) of this geometry.
    229      */
    230     public int getDimension() {
    231         return this.dimension;
    232     }
    233 
    234     /**
    235      * The OGIS geometry type number of this geometry.
    236      */
    237     public int getSrid() {
    238         return this.srid;
    239     }
    240 
    241     /**
    242      * Recursively sets the srid on this geometry and all contained
    243      * subgeometries
    244      */
    245     public void setSrid(int srid) {
    246         this.srid = srid;
    247     }
    248 
    249     public String toString() {
    250         StringBuffer sb = new StringBuffer();
    251         if (srid != UNKNOWN_SRID) {
    252             sb.append("SRID=");
    253             sb.append(srid);
    254             sb.append(';');
    255         }
    256         outerWKT(sb, true);
    257         return sb.toString();
    258     }
    259 
    260     /**
    261      * Render the WKT version of this Geometry (without SRID) into the given
    262      * StringBuffer.
    263      */
    264     public void outerWKT(StringBuffer sb, boolean putM) {
    265         sb.append(getTypeString());
    266         if (putM && haveMeasure && dimension == 2) {
    267             sb.append('M');
    268         }
    269         mediumWKT(sb);
    270     }
    271 
    272     public final void outerWKT(StringBuffer sb) {
    273         outerWKT(sb, true);
    274     }
    275 
    276     /**
    277      * Render the WKT without the type name, but including the brackets into the
    278      * StringBuffer
    279      */
    280     protected void mediumWKT(StringBuffer sb) {
    281         sb.append('(');
    282         innerWKT(sb);
    283         sb.append(')');
    284     }
    285 
    286     /**
    287      * Render the "inner" part of the WKT (inside the brackets) into the
    288      * StringBuffer.
    289      */
    290     protected abstract void innerWKT(StringBuffer SB);
    291 
    292     /**
    293      * backwards compatibility method
    294      */
    295     public String getValue() {
    296         StringBuffer sb = new StringBuffer();
    297         mediumWKT(sb);
    298         return sb.toString();
    299     }
    300 
    301     /**
    302      * Do some internal consistency checks on the geometry.
    303      *
    304      * Currently, all Geometries must have a valid dimension (2 or 3) and a
    305      * valid type. 2-dimensional Points must have Z=0.0, as well as non-measured
    306      * Points must have m=0.0. Composed geometries must have all equal SRID,
    307      * dimensionality and measures, as well as that they do not contain NULL or
    308      * inconsistent subgeometries.
    309      *
    310      * BinaryParser and WKTParser should only generate consistent geometries.
    311      * BinaryWriter may produce invalid results on inconsistent geometries.
    312      *
    313      * @return true if all checks are passed.
    314      */
    315     public boolean checkConsistency() {
    316         return (dimension >= 2 && dimension <= 3) && (type >= 0 && type <= 7);
    317     }
    318 
    319     /**
    320      * Splits the SRID=4711; part of a EWKT rep if present and sets the srid.
    321      *
    322      * @return value without the SRID=4711; part
    323      */
    324     protected String initSRID(String value) {
    325         value = value.trim();
    326         if (value.startsWith("SRID=")) {
    327             int index = value.indexOf(';', 5); // sridprefix length is 5
    328             if (index == -1) {
    329                 throw new IllegalArgumentException(
    330                         "Error parsing Geometry - SRID not delimited with ';' ");
    331             } else {
    332                 this.srid = Integer.parseInt(value.substring(5, index));
    333                 return value.substring(index + 1).trim();
    334             }
    335         } else {
    336             return value;
    337         }
    338     }
     33        /* JDK 1.5 Serialization */
     34        private static final long serialVersionUID = 0x100;
     35
     36        // OpenGIS Geometry types as defined in the OGC WKB Spec
     37        // (May we replace this with an ENUM as soon as JDK 1.5
     38        // has gained widespread usage?)
     39
     40        /** Fake type for linear ring */
     41        public static final int LINEARRING = 0;
     42        /**
     43         * The OGIS geometry type number for points.
     44         */
     45        public static final int POINT = 1;
     46
     47        /**
     48         * The OGIS geometry type number for lines.
     49         */
     50        public static final int LINESTRING = 2;
     51
     52        /**
     53         * The OGIS geometry type number for polygons.
     54         */
     55        public static final int POLYGON = 3;
     56
     57        /**
     58         * The OGIS geometry type number for aggregate points.
     59         */
     60        public static final int MULTIPOINT = 4;
     61
     62        /**
     63         * The OGIS geometry type number for aggregate lines.
     64         */
     65        public static final int MULTILINESTRING = 5;
     66
     67        /**
     68         * The OGIS geometry type number for aggregate polygons.
     69         */
     70        public static final int MULTIPOLYGON = 6;
     71
     72        /**
     73         * The OGIS geometry type number for feature collections.
     74         */
     75        public static final int GEOMETRYCOLLECTION = 7;
     76
     77        public static final String[] ALLTYPES = new String[] {
     78                        "", // internally used LinearRing does not have any text in front of
     79                                // it
     80                        "POINT", "LINESTRING", "POLYGON", "MULTIPOINT", "MULTILINESTRING",
     81                        "MULTIPOLYGON", "GEOMETRYCOLLECTION" };
     82
     83        /**
     84         * The Text representations of the geometry types
     85         */
     86        public static String getTypeString(int type) {
     87                if (type >= 0 && type <= 7) {
     88                        return ALLTYPES[type];
     89                } else {
     90                        throw new IllegalArgumentException("Unknown Geometry type" + type);
     91                }
     92        }
     93
     94        // Properties common to all geometries
     95        /**
     96         * The dimensionality of this feature (2,3)
     97         */
     98        public int dimension;
     99
     100        /**
     101         * Do we have a measure (4th dimension)
     102         */
     103        public boolean haveMeasure = false;
     104
     105        /**
     106         * The OGIS geometry type of this feature. this is final as it never
     107         * changes, it is bound to the subclass of the instance.
     108         */
     109        public final int type;
     110
     111        /**
     112         * Official UNKNOWN srid value
     113         */
     114        public final static int UNKNOWN_SRID = 0;
     115
     116        /**
     117         * The spacial reference system id of this geometry, default is no srid
     118         */
     119        public int srid = UNKNOWN_SRID;
     120
     121        /**
     122         * Parse a SRID value, anything <= 0 is unknown
     123         */
     124        public static int parseSRID(int srid) {
     125                if (srid < 0) {
     126                        /* TODO: raise a warning ? */
     127                        srid = 0;
     128                }
     129                return srid;
     130        }
     131
     132        /**
     133         * Constructor for subclasses
     134         *
     135         * @param type
     136         *            has to be given by all subclasses.
     137         */
     138        protected Geometry(int type) {
     139                this.type = type;
     140        }
     141
     142        /**
     143         * java.lang.Object hashCode implementation
     144         */
     145        public int hashCode() {
     146                return dimension | (type * 4) | (srid * 32);
     147        }
     148
     149        /**
     150         * java.lang.Object equals implementation
     151         */
     152        public boolean equals(Object other) {
     153                return (other != null) && (other instanceof Geometry)
     154                                && equals((Geometry) other);
     155        }
     156
     157        /**
     158         * geometry specific equals implementation - only defined for non-null
     159         * values
     160         */
     161        public boolean equals(Geometry other) {
     162                return (other != null) && (this.dimension == other.dimension)
     163                                && (this.type == other.type) && (this.srid == other.srid)
     164                                && (this.haveMeasure == other.haveMeasure)
     165                                && other.getClass().equals(this.getClass())
     166                                && this.equalsintern(other);
     167        }
     168
     169        /**
     170         * Whether test coordinates for geometry - subclass specific code
     171         *
     172         * Implementors can assume that dimensin, type, srid and haveMeasure are
     173         * equal, other != null and other is the same subclass.
     174         */
     175        protected abstract boolean equalsintern(Geometry other);
     176
     177        /**
     178         * Return the number of Points of the geometry
     179         */
     180        public abstract int numPoints();
     181
     182        /**
     183         * Get the nth Point of the geometry
     184         *
     185         * @param n
     186         *            the index of the point, from 0 to numPoints()-1;
     187         * @throws ArrayIndexOutOfBoundsException
     188         *             in case of an emtpy geometry or bad index.
     189         */
     190        public abstract Point getPoint(int n);
     191
     192        /**
     193         * Same as getPoint(0);
     194         */
     195        public abstract Point getFirstPoint();
     196
     197        /**
     198         * Same as getPoint(numPoints()-1);
     199         */
     200        public abstract Point getLastPoint();
     201
     202        /**
     203         * The OGIS geometry type number of this geometry.
     204         */
     205        public int getType() {
     206                return this.type;
     207        }
     208
     209        /**
     210         * Return the Type as String
     211         */
     212        public String getTypeString() {
     213                return getTypeString(this.type);
     214        }
     215
     216        /** Returns whether we have a measure */
     217        public boolean isMeasured() {
     218                return haveMeasure;
     219        }
     220
     221        /**
     222         * Queries the number of geometric dimensions of this geometry. This does
     223         * not include measures, as opposed to the server.
     224         *
     225         * @return The dimensionality (eg, 2D or 3D) of this geometry.
     226         */
     227        public int getDimension() {
     228                return this.dimension;
     229        }
     230
     231        /**
     232         * The OGIS geometry type number of this geometry.
     233         */
     234        public int getSrid() {
     235                return this.srid;
     236        }
     237
     238        /**
     239         * Recursively sets the srid on this geometry and all contained
     240         * subgeometries
     241         */
     242        public void setSrid(int srid) {
     243                this.srid = srid;
     244        }
     245
     246        public String toString() {
     247                StringBuffer sb = new StringBuffer();
     248                if (srid != UNKNOWN_SRID) {
     249                        sb.append("SRID=");
     250                        sb.append(srid);
     251                        sb.append(';');
     252                }
     253                outerWKT(sb, true);
     254                return sb.toString();
     255        }
     256
     257        /**
     258         * Render the WKT version of this Geometry (without SRID) into the given
     259         * StringBuffer.
     260         */
     261        public void outerWKT(StringBuffer sb, boolean putM) {
     262                sb.append(getTypeString());
     263                if (putM && haveMeasure && dimension == 2) {
     264                        sb.append('M');
     265                }
     266                mediumWKT(sb);
     267        }
     268
     269        public final void outerWKT(StringBuffer sb) {
     270                outerWKT(sb, true);
     271        }
     272
     273        /**
     274         * Render the WKT without the type name, but including the brackets into the
     275         * StringBuffer
     276         */
     277        protected void mediumWKT(StringBuffer sb) {
     278                sb.append('(');
     279                innerWKT(sb);
     280                sb.append(')');
     281        }
     282
     283        /**
     284         * Render the "inner" part of the WKT (inside the brackets) into the
     285         * StringBuffer.
     286         */
     287        protected abstract void innerWKT(StringBuffer SB);
     288
     289        /**
     290         * backwards compatibility method
     291         */
     292        public String getValue() {
     293                StringBuffer sb = new StringBuffer();
     294                mediumWKT(sb);
     295                return sb.toString();
     296        }
     297
     298        /**
     299         * Do some internal consistency checks on the geometry.
     300         *
     301         * Currently, all Geometries must have a valid dimension (2 or 3) and a
     302         * valid type. 2-dimensional Points must have Z=0.0, as well as non-measured
     303         * Points must have m=0.0. Composed geometries must have all equal SRID,
     304         * dimensionality and measures, as well as that they do not contain NULL or
     305         * inconsistent subgeometries.
     306         *
     307         * BinaryParser and WKTParser should only generate consistent geometries.
     308         * BinaryWriter may produce invalid results on inconsistent geometries.
     309         *
     310         * @return true if all checks are passed.
     311         */
     312        public boolean checkConsistency() {
     313                return (dimension >= 2 && dimension <= 3) && (type >= 0 && type <= 7);
     314        }
     315
     316        /**
     317         * Splits the SRID=4711; part of a EWKT rep if present and sets the srid.
     318         *
     319         * @return value without the SRID=4711; part
     320         */
     321        protected String initSRID(String value) {
     322                value = value.trim();
     323                if (value.startsWith("SRID=")) {
     324                        int index = value.indexOf(';', 5); // sridprefix length is 5
     325                        if (index == -1) {
     326                                throw new IllegalArgumentException(
     327                                                "Error parsing Geometry - SRID not delimited with ';' ");
     328                        } else {
     329                                this.srid = Integer.parseInt(value.substring(5, index));
     330                                return value.substring(index + 1).trim();
     331                        }
     332                } else {
     333                        return value;
     334                }
     335        }
    339336}
  • trunk/java/jdbc/src/org/postgis/Version.java

    r2512 r8710  
    3030/** Corresponds to the appropriate PostGIS that carried this source */
    3131public class Version {
    32     /** We read our version information from this ressource... */
     32    /** We read our version information from this resource... */
    3333    private static final String RESSOURCENAME = "org/postgis/version.properties";
    3434
  • trunk/java/jdbc/src/org/postgis/binary/BinaryParser.java

    r8481 r8710  
    139139        Geometry result = result1;
    140140
    141         if (srid != Geometry.UNKONWN_SRID) {
     141        if (srid != Geometry.UNKNOWN_SRID) {
    142142            result.setSrid(srid);
    143143        }
  • trunk/java/jdbc/src/org/postgis/java2d/ShapeBinaryParser.java

    r8481 r8710  
    2626import java.awt.geom.GeneralPath;
    2727
     28import org.postgis.Geometry;
    2829import org.postgis.binary.ByteGetter;
    2930import org.postgis.binary.ValueGetter;
  • trunk/java/jdbc/todo.txt

    r2491 r8710  
    44- even more Testing, especialy against different postgis,
    55  pgjdbc and postgresql releases.
     6 
     7- Use JUnit for testing (maven)
     8
     9- Unify the build of app java projects -> maven
    610
    711- Handling of length() - esp. with modifying the geometries
     
    2327- Rework the BinaryParser/BinaryWriter to work on SQLInput/SQLOutput
    2428  instances, as well as reworking ValueGetter/ValueSetter to implment those interfaces.
    25 
    26 - Unify the build of app java projects
Note: See TracChangeset for help on using the changeset viewer.