| | |
| | | * @author Markus Schaber |
| | | */ |
| | | |
| | | public class JtsGeometry extends PGobject |
| | | { |
| | | public class JtsGeometry extends PGobject { |
| | | /* JDK 1.5 Serialization */ |
| | | private static final long serialVersionUID = 0x100; |
| | | |
| | |
| | | /** |
| | | * Constructor called by JDBC drivers |
| | | */ |
| | | public JtsGeometry() |
| | | { |
| | | public JtsGeometry() { |
| | | setType("geometry"); |
| | | } |
| | | |
| | | public JtsGeometry(Geometry geom) |
| | | { |
| | | public JtsGeometry(Geometry geom) { |
| | | this(); |
| | | this.geom = geom; |
| | | } |
| | | |
| | | public JtsGeometry(String value) throws SQLException |
| | | { |
| | | public JtsGeometry(String value) throws SQLException { |
| | | this(); |
| | | setValue(value); |
| | | } |
| | | |
| | | public void setValue(String value) throws SQLException |
| | | { |
| | | public void setValue(String value) throws SQLException { |
| | | geom = geomFromString(value); |
| | | } |
| | | |
| | | public static Geometry geomFromString(String value) throws SQLException |
| | | { |
| | | try |
| | | { |
| | | public static Geometry geomFromString(String value) throws SQLException { |
| | | try { |
| | | value = value.trim(); |
| | | if (value.startsWith("00") || value.startsWith("01")) |
| | | { |
| | | if (value.startsWith("00") || value.startsWith("01")) { |
| | | return bp.parse(value); |
| | | } else |
| | | { |
| | | } else { |
| | | Geometry result; |
| | | // no srid := 0 in JTS world |
| | | int srid = 0; |
| | | // break up geometry into srid and wkt |
| | | if (value.startsWith("SRID=")) |
| | | { |
| | | if (value.startsWith("SRID=")) { |
| | | String[] temp = value.split(";"); |
| | | value = temp[1].trim(); |
| | | srid = Integer.parseInt(temp[0].substring(5)); |
| | |
| | | setSridRecurse(result, srid); |
| | | return result; |
| | | } |
| | | } catch (Exception E) |
| | | { |
| | | } 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) |
| | | { |
| | | public static void setSridRecurse(final Geometry geom, final int srid) { |
| | | geom.setSRID(srid); |
| | | if (geom instanceof GeometryCollection) |
| | | { |
| | | if (geom instanceof GeometryCollection) { |
| | | final int subcnt = geom.getNumGeometries(); |
| | | for (int i = 0; i < subcnt; i++) |
| | | { |
| | | for (int i = 0; i < subcnt; i++) { |
| | | setSridRecurse(geom.getGeometryN(i), srid); |
| | | } |
| | | } else if (geom instanceof Polygon) |
| | | { |
| | | } 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++) |
| | | { |
| | | for (int i = 0; i < subcnt; i++) { |
| | | poly.getInteriorRingN(i).setSRID(srid); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public Geometry getGeometry() |
| | | { |
| | | public Geometry getGeometry() { |
| | | return geom; |
| | | } |
| | | |
| | | public String toString() |
| | | { |
| | | public String toString() { |
| | | return geom.toString(); |
| | | } |
| | | |
| | | public String getValue() |
| | | { |
| | | public String getValue() { |
| | | return bw.writeHexed(getGeometry()); |
| | | } |
| | | |
| | | public Object clone() |
| | | { |
| | | public Object clone() { |
| | | JtsGeometry obj = new JtsGeometry(geom); |
| | | obj.setType(type); |
| | | return obj; |
| | | } |
| | | |
| | | public boolean equals(Object obj) |
| | | { |
| | | if ((obj != null) && (obj instanceof JtsGeometry)) |
| | | { |
| | | 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 |
| | | if (this.geom == other) { // handles identity as well as both |
| | | // ==null |
| | | return true; |
| | | } else if (this.geom != null && other != null) |
| | | { |
| | | } else if (this.geom != null && other != null) { |
| | | return other.equals(this.geom); |
| | | } |
| | | } |