Skip to content

Commit 1c89a77

Browse files
committed
improved CRF support in JTSFields
* moved 4326 enforcing to JTSUtil * added CRFTranslator to AbstractJTSField * added a test/example that makes a custom conversion from ETRS-TM35FIN to WGS84 (+1 squashed commit) Squashed commits: [35d4e89] improved CRF support in JTSFields * moved 4326 enforcing to JTSUtil * added CRFTranslator to AbstractJTSField * added a test/example that makes a custom conversion from ETRS-TM35FIN to WGS84
1 parent 962567b commit 1c89a77

File tree

7 files changed

+367
-46
lines changed

7 files changed

+367
-46
lines changed

pom.xml

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3838
<vaadin.version>7.1.8</vaadin.version>
3939
<g-leaflet-draw-version>0.4.3-SNAPSHOT</g-leaflet-draw-version>
40+
<geotools.version>10.2</geotools.version>
4041
</properties>
4142

4243
<build>
@@ -270,6 +271,11 @@
270271
<enabled>true</enabled>
271272
</snapshots>
272273
</repository>
274+
<repository>
275+
<id>osgeo</id>
276+
<name>Open Source Geospatial Foundation Repository</name>
277+
<url>http://download.osgeo.org/webdav/geotools/</url>
278+
</repository>
273279
<repository>
274280
<id>vaadin-snapshots</id>
275281
<url>http://oss.sonatype.org/content/repositories/vaadin-snapshots/</url>
@@ -302,6 +308,11 @@
302308
vaadin plugin -->
303309
<!-- <scope>provided</scope> -->
304310
</dependency>
311+
<dependency>
312+
<groupId>com.vividsolutions</groupId>
313+
<artifactId>jts</artifactId>
314+
<version>1.13</version>
315+
</dependency>
305316
<dependency>
306317
<groupId>com.vaadin</groupId>
307318
<artifactId>vaadin-server</artifactId>
@@ -314,6 +325,7 @@
314325
<version>${vaadin.version}</version>
315326
<scope>provided</scope>
316327
</dependency>
328+
317329
<dependency>
318330
<groupId>com.vaadin</groupId>
319331
<artifactId>vaadin-themes</artifactId>
@@ -326,11 +338,32 @@
326338
<version>7.2.2.v20101205</version>
327339
<scope>test</scope>
328340
</dependency>
329-
341+
342+
343+
330344
<dependency>
331-
<groupId>com.vividsolutions</groupId>
332-
<artifactId>jts</artifactId>
333-
<version>1.13</version>
345+
<groupId>org.geotools</groupId>
346+
<artifactId>gt-api</artifactId>
347+
<version>${geotools.version}</version>
348+
<scope>test</scope>
349+
</dependency>
350+
<dependency>
351+
<groupId>org.geotools</groupId>
352+
<artifactId>gt-referencing</artifactId>
353+
<version>${geotools.version}</version>
354+
<scope>test</scope>
355+
</dependency>
356+
<dependency>
357+
<groupId>org.geotools</groupId>
358+
<artifactId>gt-epsg-hsql</artifactId>
359+
<version>${geotools.version}</version>
360+
<scope>test</scope>
361+
</dependency>
362+
<dependency>
363+
<groupId>org.geotools</groupId>
364+
<artifactId>gt-epsg-extension</artifactId>
365+
<version>${geotools.version}</version>
366+
<scope>test</scope>
334367
</dependency>
335368

336369
<!-- If you have testbench licence, use that instead of raw selenium. Much

src/main/java/org/vaadin/addon/leaflet/util/AbstractJTSField.java

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.vaadin.addon.leaflet.LTileLayer;
55

66
import com.vaadin.client.ui.Field;
7-
import com.vaadin.data.Property;
87
import com.vaadin.ui.Component;
98
import com.vaadin.ui.CustomField;
109
import com.vividsolutions.jts.geom.Geometry;
@@ -53,20 +52,47 @@ public static void setDefaultConfigurator(Configurator configurator) {
5352
}
5453
defaultConfigurator = configurator;
5554
}
55+
56+
public interface CRFTranslator<T> {
5657

57-
protected LMap map = new LMap();
58+
T toPresentation(T geom);
59+
60+
T toModel(T geom);
61+
}
62+
63+
private static CRFTranslator<Geometry> defaultCRFTranslator = new CRFTranslator<Geometry>() {
64+
65+
@Override
66+
public Geometry toPresentation(Geometry geom) {
67+
assert geom.getSRID() == 0 || geom.getSRID() == 4326;
68+
return geom;
69+
}
70+
71+
@Override
72+
public Geometry toModel(Geometry geom) {
73+
assert geom.getSRID() == 0 || geom.getSRID() == 4326;
74+
return geom;
75+
}
76+
};
5877

59-
private Configurator configurator;
60-
61-
private int srid = 4326;
62-
6378
/**
64-
* Set the EPSG Spatial Reference Identifier (SRID)
65-
* which defaults to 4326.
79+
* Sets the default CRFTranslator to convert values to and from presentation in WSG86 (EPSG:4326).
80+
*
81+
* @param configurator
6682
*/
67-
public void setSrid(int srid) {
68-
this.srid = srid;
69-
}
83+
@SuppressWarnings("unchecked")
84+
public static void setDefaultCRFTranslator(CRFTranslator<? extends Geometry> translator) {
85+
if (translator == null) {
86+
throw new IllegalArgumentException();
87+
}
88+
defaultCRFTranslator = (CRFTranslator<Geometry>) translator;
89+
}
90+
91+
protected LMap map = new LMap();
92+
93+
private Configurator configurator;
94+
95+
private CRFTranslator<T> cRFTranslator;
7096

7197
public AbstractJTSField() {
7298
super();
@@ -107,15 +133,7 @@ protected void setInternalValue(T newValue) {
107133
prepareEditing();
108134
}
109135
}
110-
111-
@Override
112-
public void setValue(T value) {
113-
if (value != null) {
114-
value.setSRID(srid);
115-
}
116-
super.setValue(value);
117-
}
118-
136+
119137
protected abstract void prepareEditing();
120138

121139
protected abstract void prepareDrawing();
@@ -128,4 +146,17 @@ public void setConfigurator(Configurator configurator) {
128146
this.configurator = configurator;
129147
}
130148

149+
@SuppressWarnings("unchecked")
150+
public CRFTranslator<T> getCRFTranslator() {
151+
if(cRFTranslator == null) {
152+
return (CRFTranslator<T>) defaultCRFTranslator;
153+
}
154+
return cRFTranslator;
155+
}
156+
157+
@SuppressWarnings("unchecked")
158+
public void setCRFTranslator(CRFTranslator<Geometry> cRFTranslator) {
159+
this.cRFTranslator = (CRFTranslator<T>) cRFTranslator;
160+
}
161+
131162
}

src/main/java/org/vaadin/addon/leaflet/util/JTSUtil.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121
import com.vividsolutions.jts.geom.MultiPoint;
2222
import com.vividsolutions.jts.geom.MultiPolygon;
2323
import com.vividsolutions.jts.geom.Polygon;
24+
import com.vividsolutions.jts.geom.PrecisionModel;
2425

2526
/**
2627
* Helper methods to convert between JTS geometry types and v-leaflet objects.
27-
*
28+
* <p>
29+
* The CRF is expected to be WGS84 (~EPSG:4326 ~ GPS coordinates) in both
30+
* directions.
2831
*/
2932
public class JTSUtil {
3033

@@ -189,7 +192,7 @@ public static org.vaadin.addon.leaflet.shared.Point[] toLeafletPointArray(
189192

190193
public static LineString toLineString(
191194
org.vaadin.addon.leaflet.shared.Point[] points) {
192-
GeometryFactory factory = new GeometryFactory();
195+
GeometryFactory factory = getGeometryFactory();
193196
Coordinate[] coordinates = new Coordinate[points.length];
194197
for (int i = 0; i < coordinates.length; i++) {
195198
Point p = points[i];
@@ -210,12 +213,10 @@ public static LinearRing toLinearRing(LPolygon polygon) {
210213

211214
public static Polygon toPolygon(LPolygon polygon) {
212215
Point[] points = polygon.getPoints();
213-
return new Polygon(toLinearRing(points), null, new GeometryFactory());
216+
return getGeometryFactory().createPolygon(toLinearRing(points));
214217
}
215218

216219
private static LinearRing toLinearRing(Point[] points) {
217-
GeometryFactory factory = new GeometryFactory();
218-
219220
boolean closed = points[0].equals(points[points.length - 1]);
220221

221222
Coordinate[] coordinates = new Coordinate[points.length
@@ -227,12 +228,18 @@ private static LinearRing toLinearRing(Point[] points) {
227228
if (!closed) {
228229
coordinates[coordinates.length - 1] = coordinates[0];
229230
}
230-
return factory.createLinearRing(coordinates);
231+
return getGeometryFactory().createLinearRing(coordinates);
232+
}
233+
234+
private static GeometryFactory getGeometryFactory() {
235+
GeometryFactory factory = new GeometryFactory(new PrecisionModel(),
236+
4326);
237+
return factory;
231238
}
232239

233240
public static com.vividsolutions.jts.geom.Point toPoint(
234241
org.vaadin.addon.leaflet.shared.Point p) {
235-
com.vividsolutions.jts.geom.Point point = new GeometryFactory()
242+
com.vividsolutions.jts.geom.Point point = getGeometryFactory()
236243
.createPoint(new Coordinate(p.getLon(), p.getLat()));
237244
return point;
238245
}

src/main/java/org/vaadin/addon/leaflet/util/LineStringField.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class LineStringField extends AbstractJTSField<LineString> {
1818

1919
public LineStringField() {
2020
}
21-
21+
2222
public LineStringField(String caption) {
2323
this();
2424
setCaption(caption);
@@ -30,18 +30,20 @@ public Class<? extends LineString> getType() {
3030
}
3131

3232
protected void prepareEditing() {
33-
if(lPolyline == null ) {
33+
if (lPolyline == null) {
3434
lPolyline = new LPolyline();
3535
map.addLayer(lPolyline);
3636
}
37-
Point[] lPointArray = JTSUtil.toLeafletPointArray(getInternalValue());
37+
Point[] lPointArray = JTSUtil.toLeafletPointArray(getCRFTranslator()
38+
.toPresentation(getInternalValue()));
3839
lPolyline.setPoints(lPointArray);
3940
LEditing editing = new LEditing(lPolyline);
4041
editing.addFeatureModifiedListener(new FeatureModifiedListener() {
4142

4243
@Override
4344
public void featureModified(FeatureModifiedEvent event) {
44-
setValue(JTSUtil.toLineString(lPolyline));
45+
setValue(getCRFTranslator().toModel(
46+
JTSUtil.toLineString(lPolyline)));
4547
}
4648
});
4749
map.zoomToExtent(new Bounds(lPolyline.getPoints()));
@@ -50,10 +52,12 @@ public void featureModified(FeatureModifiedEvent event) {
5052
protected void prepareDrawing() {
5153
LDrawPolyline drawPolyline = new LDrawPolyline(map);
5254
drawPolyline.addFeatureDrawnListener(new FeatureDrawnListener() {
53-
55+
5456
@Override
5557
public void featureDrawn(FeatureDrawnEvent event) {
56-
setValue(JTSUtil.toLineString((LPolyline)event.getDrawnFeature()));
58+
setValue(getCRFTranslator().toModel(
59+
JTSUtil.toLineString((LPolyline) event
60+
.getDrawnFeature())));
5761
}
5862
});
5963

src/main/java/org/vaadin/addon/leaflet/util/LinearRingField.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ protected void prepareEditing() {
3434
lPolygon = new LPolygon();
3535
map.addLayer(lPolygon);
3636
}
37-
Point[] lPointArray = JTSUtil.toLeafletPointArray(getInternalValue());
37+
Point[] lPointArray = JTSUtil.toLeafletPointArray(getCRFTranslator()
38+
.toPresentation(getInternalValue()));
3839
lPolygon.setPoints(lPointArray);
3940
LEditing editing = new LEditing(lPolygon);
4041
editing.addFeatureModifiedListener(new FeatureModifiedListener() {
4142

4243
@Override
4344
public void featureModified(FeatureModifiedEvent event) {
44-
setValue(JTSUtil.toLinearRing(lPolygon));
45+
setValue(getCRFTranslator().toModel(
46+
JTSUtil.toLinearRing(lPolygon)));
4547
}
4648
});
4749
map.zoomToExtent(new Bounds(lPolygon.getPoints()));
@@ -56,8 +58,10 @@ public void featureDrawn(FeatureDrawnEvent event) {
5658
// TODO fill Vaadin bug report: exception from here has horrible
5759
// stack trace (non informative), even more horrible than the
5860
// usual that has some irrelevant stuff in front
59-
setValue(JTSUtil.toLinearRing((LPolygon) event
60-
.getDrawnFeature()));
61+
setValue(getCRFTranslator()
62+
.toModel(
63+
JTSUtil.toLinearRing((LPolygon) event
64+
.getDrawnFeature())));
6165
}
6266
});
6367

src/main/java/org/vaadin/addon/leaflet/util/PointField.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ public class PointField extends AbstractJTSField<Point> {
1414
private LMarker marker;
1515

1616
public PointField() {
17-
17+
1818
}
1919
public PointField(String caption) {
2020
this();
2121
setCaption(caption);
2222
}
23-
23+
2424

2525
@Override
2626
public Class<? extends Point> getType() {
@@ -29,12 +29,14 @@ public Class<? extends Point> getType() {
2929

3030
protected void prepareEditing() {
3131
if (marker == null) {
32-
marker = new LMarker(JTSUtil.toLeafletPoint(getInternalValue()));
32+
marker = new LMarker(JTSUtil.toLeafletPoint(getCRFTranslator()
33+
.toPresentation(getInternalValue())));
3334
marker.addDragEndListener(new DragEndListener() {
3435

3536
@Override
3637
public void dragEnd(DragEndEvent event) {
37-
setValue(JTSUtil.toPoint(marker));
38+
setValue(getCRFTranslator()
39+
.toModel(JTSUtil.toPoint(marker)));
3840
}
3941
});
4042
map.addLayer(marker);
@@ -48,7 +50,8 @@ protected void prepareDrawing() {
4850

4951
@Override
5052
public void featureDrawn(FeatureDrawnEvent event) {
51-
setValue(JTSUtil.toPoint((LMarker) event.getDrawnFeature()));
53+
setValue(getCRFTranslator().toModel(
54+
JTSUtil.toPoint((LMarker) event.getDrawnFeature())));
5255
}
5356
});
5457

0 commit comments

Comments
 (0)