Skip to content

Commit 94b44c4

Browse files
committed
rework box2d/3d: support SRID, streamline and cleanup, rework equals/hashCode implementations, use a epsilon for comparing coordinates, cleanup.
1 parent e54d885 commit 94b44c4

File tree

17 files changed

+189
-157
lines changed

17 files changed

+189
-157
lines changed

.classpath

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
3-
<classpathentry kind="src" output="target/classes" path="src/main/java">
3+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
44
<attributes>
5-
<attribute name="optional" value="true"/>
65
<attribute name="maven.pomderived" value="true"/>
76
</attributes>
87
</classpathentry>
9-
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
8+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
109
<attributes>
1110
<attribute name="maven.pomderived" value="true"/>
11+
<attribute name="test" value="true"/>
1212
</attributes>
1313
</classpathentry>
14-
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
14+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
1515
<attributes>
16-
<attribute name="optional" value="true"/>
1716
<attribute name="maven.pomderived" value="true"/>
18-
<attribute name="test" value="true"/>
1917
</attributes>
2018
</classpathentry>
21-
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
19+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
2220
<attributes>
2321
<attribute name="maven.pomderived" value="true"/>
24-
<attribute name="test" value="true"/>
22+
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
2523
</attributes>
2624
</classpathentry>
27-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
25+
<classpathentry kind="src" output="target/classes" path="src/main/java">
2826
<attributes>
27+
<attribute name="optional" value="true"/>
2928
<attribute name="maven.pomderived" value="true"/>
3029
</attributes>
3130
</classpathentry>
32-
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
31+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
3332
<attributes>
33+
<attribute name="optional" value="true"/>
3434
<attribute name="maven.pomderived" value="true"/>
35-
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
35+
<attribute name="test" value="true"/>
3636
</attributes>
3737
</classpathentry>
3838
<classpathentry kind="output" path="target/classes"/>
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
2-
2+
3+
34
<wb-module deploy-name="postgis-java-ng">
4-
5-
<wb-resource deploy-path="/" source-path="/src/main/java"/>
6-
5+
6+
77
<wb-resource deploy-path="/" source-path="/src/main/resources"/>
8-
9-
</wb-module>
8+
<wb-resource deploy-path="/" source-path="/src/main/java"/>
9+
1010

11+
</wb-module>
12+
13+
1114
</project-modules>

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ It is originally based on [postgis-java](https://github.com/postgis/postgis-java
1616
**Project goals and improvements:**
1717
* Support for geometries containing arcs like `CIRCULARSTRING` or `CURVEPOLYGON`
1818
* Support for PostGIS [geography datatype](https://postgis.net/docs/using_postgis_dbmanagement.html#PostGIS_Geography)
19+
* Extended support for bounding boxes, i.e. box2/box3 PostGIS data types (as PGbox2d/PGbox3d).
1920
* Use generic Java types where possible and simplify/streamline API
2021
* Clean up code to basically only work on [WKB](https://en.wikipedia.org/wiki/Well-known_text#Well-known_binary)/EWKB implementations to reduce code duplication and focus on the actual database format
2122
* Support for the latest PostgreSQL and PostGIS versions
@@ -37,6 +38,8 @@ It is originally based on [postgis-java](https://github.com/postgis/postgis-java
3738
* MultiPolygon
3839
* MultiSurface
3940
* GeometryCollection
41+
* [box2d](https://postgis.net/docs/box2d_type.html)
42+
* [box3d](https://postgis.net/docs/box3d_type.html)
4043

4144
## How to use it ##
4245
There is a Maven artifact in the official Maven repository, so just add this to your Maven POM:
@@ -45,11 +48,11 @@ There is a Maven artifact in the official Maven repository, so just add this to
4548
<dependency>
4649
<groupId>io.github.sebasbaumh</groupId>
4750
<artifactId>postgis-java-ng</artifactId>
48-
<version>22.0.1</version>
51+
<version>22.1.0</version>
4952
</dependency>
5053
```
5154

52-
The version reflects the year of the release, e.g. `22.0.0` is a version released in 2022.
55+
The version reflects the year of the release, e.g. `22.1.0` is a version released in 2022.
5356

5457
The API differs a bit from [postgis-java](https://github.com/postgis/postgis-java) with the main point being a different namespace (`io.github.sebasbaumh.postgis`) as publishing a project to Maven Central requires to own that namespace.
5558
In addition the class structure is a bit different (see below) to support arc geometries and reduce boilerplate code, but you should be able to adapt to it easily.
@@ -80,4 +83,16 @@ Or add the following Maven build parameters to the launch configuration in eclip
8083
|`testJdbcUsername`|`unittest`|
8184
|`testJdbcPassword`|`CHANGEME`|
8285

86+
If it works, you will see this line in the build output:
87+
88+
```
89+
Tests are running with a database
90+
```
91+
92+
else
93+
94+
```
95+
Tests are running without a database
96+
```
97+
8398
*There are also local tests contained in the project, so you are still able to test most parts without specifying a PostgreSQL server. And the test console output will show if tests were run with or without a database.*

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<groupId>io.github.sebasbaumh</groupId>
44
<artifactId>postgis-java-ng</artifactId>
55
<!-- version for release -->
6-
<version>22.0.2-SNAPSHOT</version>
6+
<version>22.1.0-SNAPSHOT</version>
77
<packaging>jar</packaging>
88
<name>PostGIS Java bindings</name>
99
<description>This project contains Java bindings for using PostGIS geometries coming from a PostgreSQL database.</description>

src/main/java/io/github/sebasbaumh/postgis/CompoundCurve.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ public void close()
130130
@Override
131131
public boolean equals(@Nullable Object other)
132132
{
133-
// check parent
134-
if (super.equals(other) && (other instanceof CompoundCurve))
133+
// check type and parent
134+
if ((other instanceof CompoundCurve) && super.equals(other))
135135
{
136136
CompoundCurve cother = (CompoundCurve) other;
137137
return PostGisUtil.equalsIterable(this.subgeoms, cother.subgeoms);
@@ -216,7 +216,7 @@ public Point getStartPoint()
216216
@Override
217217
public int hashCode()
218218
{
219-
return subgeoms.hashCode();
219+
return 31 * super.hashCode() + subgeoms.hashCode();
220220
}
221221

222222
/*

src/main/java/io/github/sebasbaumh/postgis/Geometry.java

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ public boolean equals(@Nullable Object obj)
117117
{
118118
return false;
119119
}
120-
// check all properties specific to this instance
120+
// check all properties specific to this instance, rest is checked by subclasses
121121
Geometry other = (Geometry) obj;
122-
return other.getClass().equals(this.getClass()) && (this.type == other.type) && (this.srid == other.srid);
122+
return (this.type == other.type) && (this.srid == other.srid);
123123
}
124124

125125
/**
@@ -128,18 +128,6 @@ public boolean equals(@Nullable Object obj)
128128
*/
129129
public abstract Iterable<Point> getCoordinates();
130130

131-
/**
132-
* Queries the number of geometric dimensions of this geometry. This does not include measures, as opposed to the
133-
* server.
134-
* @deprecated use {@link #is3d()} instead
135-
* @return The dimensionality (eg, 2D or 3D) of this geometry.
136-
*/
137-
@Deprecated
138-
public int getDimension()
139-
{
140-
return is3d() ? 3 : 2;
141-
}
142-
143131
/**
144132
* Gets the number of coordinates of this {@link Geometry}.
145133
* @return number of coordinates
@@ -192,17 +180,6 @@ public int hashCode()
192180
*/
193181
public abstract boolean isEmpty();
194182

195-
/**
196-
* Return the number of Points of the geometry
197-
* @return number of points in the geometry
198-
* @deprecated use {@link #getNumberOfCoordinates()} instead
199-
*/
200-
@Deprecated
201-
public final int numPoints()
202-
{
203-
return this.getNumberOfCoordinates();
204-
}
205-
206183
/**
207184
* Recursively sets the srid on this geometry and all contained subgeometries
208185
* @param srid the SRID for this geometry

src/main/java/io/github/sebasbaumh/postgis/LineString.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ public void close()
140140
@Override
141141
public boolean equals(@Nullable Object other)
142142
{
143-
// check parent
144-
if (super.equals(other) && (other instanceof LineString))
143+
// check type and parent
144+
if ((other instanceof LineString) && super.equals(other))
145145
{
146146
LineString ls = (LineString) other;
147147
// check all points

src/main/java/io/github/sebasbaumh/postgis/MultiGeometry.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public boolean checkConsistency()
7979
@Override
8080
public boolean equals(@Nullable Object other)
8181
{
82-
// check parent
83-
if (super.equals(other) && (other instanceof MultiGeometry<?>))
82+
// check type and parent
83+
if ((other instanceof MultiGeometry<?>) && super.equals(other))
8484
{
8585
MultiGeometry<?> cother = (MultiGeometry<?>) other;
8686
return PostGisUtil.equalsIterable(this.subgeoms, cother.subgeoms);
@@ -133,7 +133,7 @@ public int getNumberOfCoordinates()
133133
@Override
134134
public int hashCode()
135135
{
136-
return subgeoms.hashCode();
136+
return 31 * super.hashCode() + subgeoms.hashCode();
137137
}
138138

139139
/*

src/main/java/io/github/sebasbaumh/postgis/PGbox2d.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@
2929

3030
import java.sql.SQLException;
3131

32+
import org.postgresql.util.PGobject;
33+
3234
/**
3335
* BOX2D representing the maximum extents of the geometry.
3436
* @author Sebastian Baumhekel
3537
*/
3638
public class PGbox2d extends PGboxbase
3739
{
40+
/**
41+
* Type of the {@link PGobject}.
42+
*/
43+
private static final String PG_TYPE = "box2d";
3844
/* JDK 1.5 Serialization */
3945
private static final long serialVersionUID = 0x100;
4046

@@ -43,7 +49,7 @@ public class PGbox2d extends PGboxbase
4349
*/
4450
public PGbox2d()
4551
{
46-
super();
52+
super(PG_TYPE);
4753
}
4854

4955
/**
@@ -53,7 +59,7 @@ public PGbox2d()
5359
*/
5460
public PGbox2d(Point llb, Point urt)
5561
{
56-
super(llb, urt);
62+
super(PG_TYPE, llb, urt);
5763
}
5864

5965
/**
@@ -63,13 +69,13 @@ public PGbox2d(Point llb, Point urt)
6369
*/
6470
public PGbox2d(String value) throws SQLException
6571
{
66-
super(value);
72+
super(PG_TYPE, value);
6773
}
6874

6975
@Override
70-
public String getPGtype()
76+
public PGbox2d clone()
7177
{
72-
return "box2d";
78+
return new PGbox2d(llb.copy(), urt.copy());
7379
}
7480

7581
@Override
@@ -79,9 +85,9 @@ public String getPrefix()
7985
}
8086

8187
@Override
82-
protected PGboxbase newInstance()
88+
public boolean is3d()
8389
{
84-
return new PGbox2d();
90+
return false;
8591
}
8692

8793
@Override

src/main/java/io/github/sebasbaumh/postgis/PGbox3d.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,18 @@
3030

3131
import java.sql.SQLException;
3232

33+
import org.postgresql.util.PGobject;
34+
3335
/**
3436
* BOX3D representing the maximum extents of the geometry.
3537
* @author Sebastian Baumhekel
3638
*/
3739
public class PGbox3d extends PGboxbase
3840
{
41+
/**
42+
* Type of the {@link PGobject}.
43+
*/
44+
private static final String PG_TYPE = "box3d";
3945
/* JDK 1.5 Serialization */
4046
private static final long serialVersionUID = 0x100;
4147

@@ -44,7 +50,7 @@ public class PGbox3d extends PGboxbase
4450
*/
4551
public PGbox3d()
4652
{
47-
super();
53+
super(PG_TYPE);
4854
}
4955

5056
/**
@@ -54,7 +60,7 @@ public PGbox3d()
5460
*/
5561
public PGbox3d(Point llb, Point urt)
5662
{
57-
super(llb, urt);
63+
super(PG_TYPE, llb, urt);
5864
}
5965

6066
/**
@@ -64,13 +70,13 @@ public PGbox3d(Point llb, Point urt)
6470
*/
6571
public PGbox3d(String value) throws SQLException
6672
{
67-
super(value);
73+
super(PG_TYPE, value);
6874
}
6975

7076
@Override
71-
public String getPGtype()
77+
public PGbox3d clone()
7278
{
73-
return "box3d";
79+
return new PGbox3d(llb.copy(), urt.copy());
7480
}
7581

7682
@Override
@@ -80,8 +86,18 @@ public String getPrefix()
8086
}
8187

8288
@Override
83-
protected PGboxbase newInstance()
89+
public boolean is3d()
8490
{
85-
return new PGbox3d();
91+
return true;
8692
}
93+
94+
/**
95+
* Gets this {@link Point} as a 2d object.
96+
* @return {@link Point}
97+
*/
98+
public PGbox2d to2d()
99+
{
100+
return new PGbox2d(llb.to2d(), urt.to2d());
101+
}
102+
87103
}

0 commit comments

Comments
 (0)