forked from geodmms/xdgnjobs

?? ?
2011-10-24 1cc0f32f7fefa2821a052f823e735bdb14193b93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * JtsGeometry.java
 *
 * Wrapper for PostgreSQL JDBC driver to allow transparent reading and writing
 * of JTS geometries
 *
 * (C) 2005 Markus Schaber, markus.schaber@logix-tt.com
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or visit the web at
 * http://www.gnu.org.
 *
 * $Id$
 */
 
package com.ximple.eofms.util.postjts;
 
import java.sql.SQLException;
 
import org.postgresql.util.PGobject;
 
import com.vividsolutions.jts.geom.CoordinateSequenceFactory;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Polygon;
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
 * and both PostGIS 0.x (EWKT) and 1.x (lwgeom hexwkb) for reading.
 *
 * @author Markus Schaber
 */
 
public class JtsGeometry extends PGobject {
    /* JDK 1.5 Serialization */
    private static final long serialVersionUID = 0x100;
 
    Geometry geom;
 
    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;
 
    final static GeometryFactory geofac = new GeometryFactory(prec, 0, csfac);
 
    static final WKTReader reader = new WKTReader(geofac);
 
    /**
     * Constructor called by JDBC drivers
     */
    public JtsGeometry() {
        setType("geometry");
    }
 
    public JtsGeometry(Geometry geom) {
        this();
        this.geom = geom;
    }
 
    public JtsGeometry(String value) throws SQLException {
        this();
        setValue(value);
    }
 
    public void setValue(String value) throws SQLException {
        geom = geomFromString(value);
    }
 
    public static Geometry geomFromString(String value) throws SQLException {
        try {
            value = value.trim();
            if (value.startsWith("00") || value.startsWith("01")) {
                return bp.parse(value);
            } else {
                Geometry result;
                // no srid := 0 in JTS world
                int srid = 0;
                // break up geometry into srid and wkt
                if (value.startsWith("SRID=")) {
                    String[] temp = value.split(";");
                    value = temp[1].trim();
                    srid = Integer.parseInt(temp[0].substring(5));
                }
 
                result = reader.read(value);
                setSridRecurse(result, srid);
                return result;
            }
        } catch (Exception E) {
            E.printStackTrace();
            throw new SQLException("Error parsing SQL data:" + E);
        }
    }
 
    /**
     * Recursively set a srid for the geometry and all subgeometries
     */
    public static void setSridRecurse(final Geometry geom, final int srid) {
        geom.setSRID(srid);
        if (geom instanceof GeometryCollection) {
            final int subcnt = geom.getNumGeometries();
            for (int i = 0; i < subcnt; i++) {
                setSridRecurse(geom.getGeometryN(i), srid);
            }
        } else if (geom instanceof Polygon) {
            Polygon poly = (Polygon) geom;
            poly.getExteriorRing().setSRID(srid);
            final int subcnt = poly.getNumInteriorRing();
            for (int i = 0; i < subcnt; i++) {
                poly.getInteriorRingN(i).setSRID(srid);
            }
        }
    }
 
    public Geometry getGeometry() {
        return geom;
    }
 
    public String toString() {
        return geom.toString();
    }
 
    public String getValue() {
        return bw.writeHexed(getGeometry());
    }
 
    public Object clone() {
        JtsGeometry obj = new JtsGeometry(geom);
        obj.setType(type);
        return obj;
    }
 
    public boolean equals(Object obj) {
        if ((obj != null) && (obj instanceof JtsGeometry)) {
            Geometry other = ((JtsGeometry) obj).geom;
            if (this.geom == other) { // handles identity as well as both
                // ==null
                return true;
            } else if (this.geom != null && other != null) {
                return other.equals(this.geom);
            }
        }
        return false;
    }
}