~ubuntu-branches/ubuntu/feisty/postgis/feisty

« back to all changes in this revision

Viewing changes to jdbc2/src/org/postgis/PGboxbase.java

  • Committer: Bazaar Package Importer
  • Author(s): Alex Bodnaru
  • Date: 2005-05-05 10:02:45 UTC
  • Revision ID: james.westby@ubuntu.com-20050505100245-3005l6jn1jwvpsrw
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * PGboxbase.java
 
3
 * 
 
4
 * PostGIS extension for PostgreSQL JDBC driver - bounding box model
 
5
 * 
 
6
 * 
 
7
 * (C) 2004 Paul Ramsey, pramsey@refractions.net
 
8
 * 
 
9
 * (C) 2005 Markus Schaber, markus@schabi.de
 
10
 * 
 
11
 * This library is free software; you can redistribute it and/or modify it under
 
12
 * the terms of the GNU Lesser General Public License as published by the Free
 
13
 * Software Foundation, either version 2.1 of the License.
 
14
 * 
 
15
 * This library is distributed in the hope that it will be useful, but WITHOUT
 
16
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
17
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 
18
 * details.
 
19
 * 
 
20
 * You should have received a copy of the GNU Lesser General Public License
 
21
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 
22
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or visit the web at
 
23
 * http://www.gnu.org.
 
24
 * 
 
25
 * $Id: PGboxbase.java,v 1.6 2005/03/30 15:24:40 mschaber Exp $
 
26
 */
 
27
 
 
28
package org.postgis;
 
29
 
 
30
import org.postgresql.util.PGobject;
 
31
import org.postgresql.util.PGtokenizer;
 
32
 
 
33
import java.sql.SQLException;
 
34
 
 
35
/*
 
36
 * Updates Oct 2002 - data members made private - getLLB() and getURT() methods
 
37
 * added
 
38
 */
 
39
 
 
40
public abstract class PGboxbase extends PGobject {
 
41
 
 
42
    /**
 
43
     * The lower left bottom corner of the box.
 
44
     */
 
45
    protected Point llb;
 
46
 
 
47
    /**
 
48
     * The upper right top corner of the box.
 
49
     */
 
50
    protected Point urt;
 
51
 
 
52
    /**
 
53
     * The Prefix we have in WKT rep.
 
54
     * 
 
55
     * I use an abstract method here so we do not need to replicate the String
 
56
     * object in every instance.
 
57
     *  
 
58
     */
 
59
    public abstract String getPrefix();
 
60
 
 
61
    /**
 
62
     * The Postgres type we have (same construct as getPrefix())
 
63
     */
 
64
    public abstract String getPGtype();
 
65
 
 
66
    public PGboxbase() {
 
67
        this.setType(getPGtype());
 
68
    }
 
69
 
 
70
    public PGboxbase(Point llb, Point urt) {
 
71
        this();
 
72
        this.llb = llb;
 
73
        this.urt = urt;
 
74
    }
 
75
 
 
76
    public PGboxbase(String value) throws SQLException {
 
77
        this();
 
78
        setValue(value);
 
79
    }
 
80
 
 
81
    public void setValue(String value) throws SQLException {
 
82
        int srid = -1;
 
83
        value = value.trim();
 
84
        if (value.startsWith("SRID=")) {
 
85
            String[] temp = PGgeometry.splitSRID(value);
 
86
            value = temp[1].trim();
 
87
            srid = Integer.parseInt(temp[0].substring(5));
 
88
        }
 
89
        String myPrefix = getPrefix();
 
90
        if (value.startsWith(myPrefix)) {
 
91
            value = value.substring(myPrefix.length()).trim();
 
92
        }
 
93
        PGtokenizer t = new PGtokenizer(PGtokenizer.removePara(value), ',');
 
94
        llb = new Point(t.getToken(0));
 
95
        urt = new Point(t.getToken(1));
 
96
        if (srid != -1) {
 
97
            llb.setSrid(srid);
 
98
            urt.setSrid(srid);
 
99
        }
 
100
    }
 
101
 
 
102
    public String getValue() {
 
103
        StringBuffer sb = new StringBuffer();
 
104
        outerWKT(sb);
 
105
        return sb.toString();
 
106
    }
 
107
 
 
108
    private void outerWKT(StringBuffer sb) {
 
109
        sb.append(getPrefix());
 
110
        sb.append('(');
 
111
        llb.innerWKT(sb);
 
112
        sb.append(',');
 
113
        urt.innerWKT(sb);
 
114
        sb.append(')');
 
115
    }
 
116
 
 
117
    /**
 
118
     * Unlike geometries, toString() does _not_ contain the srid, as server-side
 
119
     * PostGIS cannot parse this.
 
120
     */
 
121
    public String toString() {
 
122
        return getValue();
 
123
    }
 
124
 
 
125
    /** Returns the lower left bottom corner of the box as a Point object */
 
126
    public Point getLLB() {
 
127
        return llb;
 
128
    }
 
129
 
 
130
    /** Returns the upper right top corner of the box as a Point object */
 
131
    public Point getURT() {
 
132
        return urt;
 
133
    }
 
134
 
 
135
    public boolean equals(Object other) {
 
136
        if (other instanceof PGboxbase) {
 
137
            PGboxbase otherbox = (PGboxbase) other;
 
138
            return (compareLazyDim(this.llb, otherbox.llb) && compareLazyDim(this.urt, otherbox.urt));
 
139
        }
 
140
        return false;
 
141
    }
 
142
 
 
143
    /**
 
144
     * Compare two coordinates with lazy dimension checking.
 
145
     * 
 
146
     * As the Server always returns Box3D with three dimensions, z==0 equals
 
147
     * dimensions==2
 
148
     *  
 
149
     */
 
150
    protected static boolean compareLazyDim(Point first, Point second) {
 
151
        return first.x == second.x
 
152
                && first.y == second.y
 
153
                && (((first.dimension == 2 || first.z == 0.0) && (second.dimension == 2 || second.z == 0)) || (first.z == second.z));
 
154
    }
 
155
 
 
156
    public Object clone() {
 
157
        PGboxbase obj = newInstance();
 
158
        obj.llb = this.llb;
 
159
        obj.urt = this.urt;
 
160
        obj.setType(type);
 
161
        return obj;
 
162
    }
 
163
 
 
164
    /**
 
165
     * We could have used this.getClass().newInstance() here, but this forces us
 
166
     * dealing with InstantiationException and IllegalAccessException. Due to
 
167
     * the PGObject.clone() brokennes that does not allow clone() to throw
 
168
     * CloneNotSupportedException, we cannot even pass this exceptions down to
 
169
     * callers in a sane way.
 
170
     */
 
171
    protected abstract PGboxbase newInstance();
 
172
}
 
 
b'\\ No newline at end of file'