|
1 | 1 | package com.baeldung.hibernate; |
2 | 2 |
|
3 | 3 | import com.baeldung.hibernate.pojo.PointEntity; |
| 4 | +import com.baeldung.hibernate.pojo.PolygonEntity; |
| 5 | +import com.vividsolutions.jts.geom.Coordinate; |
4 | 6 | import com.vividsolutions.jts.geom.Geometry; |
5 | 7 | import com.vividsolutions.jts.geom.Point; |
| 8 | +import com.vividsolutions.jts.geom.Polygon; |
6 | 9 | import com.vividsolutions.jts.io.ParseException; |
7 | 10 | import com.vividsolutions.jts.io.WKTReader; |
| 11 | +import com.vividsolutions.jts.util.GeometricShapeFactory; |
8 | 12 | import org.hibernate.Session; |
9 | 13 | import org.hibernate.Transaction; |
10 | 14 | import org.junit.After; |
|
13 | 17 |
|
14 | 18 | import javax.persistence.Query; |
15 | 19 | import java.io.IOException; |
| 20 | +import java.util.List; |
16 | 21 |
|
17 | 22 | import static org.assertj.core.api.Assertions.assertThat; |
18 | 23 | import static org.junit.Assert.assertEquals; |
@@ -82,16 +87,58 @@ public void shouldSelectAllPointsWithinPolygon() throws ParseException { |
82 | 87 | .containsOnly("POINT (1 1)", "POINT (1 2)", "POINT (3 4)"); |
83 | 88 | } |
84 | 89 |
|
| 90 | + @Test |
| 91 | + public void shouldSelectAllPointsWithinRadius() throws ParseException { |
| 92 | + insertPoint("POINT (1 1)"); |
| 93 | + insertPoint("POINT (1 2)"); |
| 94 | + insertPoint("POINT (3 4)"); |
| 95 | + insertPoint("POINT (5 6)"); |
| 96 | + |
| 97 | + Query query = session.createQuery("select p from PointEntity p where within(p.point, :circle) = true", |
| 98 | + PointEntity.class); |
| 99 | + query.setParameter("circle", createCircle(0.0, 0.0, 5)); |
| 100 | + |
| 101 | + assertThat(query.getResultList().stream().map(p -> ((PointEntity) p).getPoint().toString())) |
| 102 | + .containsOnly("POINT (1 1)", "POINT (1 2)"); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void shouldSelectAdjacentPolygons() throws ParseException { |
| 107 | + insertPolygon("POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0))"); |
| 108 | + insertPolygon("POLYGON ((3 0, 3 5, 8 5, 8 0, 3 0))"); |
| 109 | + insertPolygon("POLYGON ((2 2, 3 1, 2 5, 4 3, 3 3, 2 2))"); |
| 110 | + |
| 111 | + Query query = session.createQuery("select p from PolygonEntity p where touches(p.polygon, :polygon) = true", |
| 112 | + PolygonEntity.class); |
| 113 | + query.setParameter("polygon", wktToGeometry("POLYGON ((5 5, 5 10, 10 10, 10 5, 5 5))")); |
| 114 | + assertThat(query.getResultList().stream().map(p -> ((PolygonEntity) p).getPolygon().toString())) |
| 115 | + .containsOnly("POLYGON ((0 0, 0 5, 5 5, 5 0, 0 0))", "POLYGON ((3 0, 3 5, 8 5, 8 0, 3 0))"); |
| 116 | + } |
| 117 | + |
85 | 118 | private void insertPoint(String point) throws ParseException { |
86 | 119 | PointEntity entity = new PointEntity(); |
87 | 120 | entity.setPoint((Point) wktToGeometry(point)); |
88 | 121 | session.persist(entity); |
89 | 122 | } |
90 | 123 |
|
| 124 | + private void insertPolygon(String polygon) throws ParseException { |
| 125 | + PolygonEntity entity = new PolygonEntity(); |
| 126 | + entity.setPolygon((Polygon) wktToGeometry(polygon)); |
| 127 | + session.persist(entity); |
| 128 | + } |
| 129 | + |
91 | 130 | private Geometry wktToGeometry(String wellKnownText) throws ParseException { |
92 | 131 | WKTReader fromText = new WKTReader(); |
93 | 132 | Geometry geom = null; |
94 | 133 | geom = fromText.read(wellKnownText); |
95 | 134 | return geom; |
96 | 135 | } |
| 136 | + |
| 137 | + private static Geometry createCircle(double x, double y, double radius) { |
| 138 | + GeometricShapeFactory shapeFactory = new GeometricShapeFactory(); |
| 139 | + shapeFactory.setNumPoints(32); |
| 140 | + shapeFactory.setCentre(new Coordinate(x, y)); |
| 141 | + shapeFactory.setSize(radius * 2); |
| 142 | + return shapeFactory.createCircle(); |
| 143 | + } |
97 | 144 | } |
0 commit comments