mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-13 21:01:01 +08:00
Add conversion tests #631
This commit is contained in:
parent
223a65bb45
commit
dac1e399ef
@ -161,7 +161,7 @@ public class JGeometryConverter {
|
||||
}
|
||||
}
|
||||
|
||||
// to geoaltte
|
||||
// to geolatte
|
||||
|
||||
public static Geometry convert(JGeometry geometry) {
|
||||
switch (geometry.getType()) {
|
||||
|
||||
@ -22,6 +22,7 @@ import org.geolatte.geom.MultiLineString;
|
||||
import org.geolatte.geom.MultiPoint;
|
||||
import org.geolatte.geom.MultiPolygon;
|
||||
import org.geolatte.geom.Point;
|
||||
import org.geolatte.geom.PointCollectionFactory;
|
||||
import org.geolatte.geom.PointSequence;
|
||||
import org.geolatte.geom.PointSequenceBuilder;
|
||||
import org.geolatte.geom.PointSequenceBuilders;
|
||||
@ -66,13 +67,11 @@ public class PGgeometryConverter {
|
||||
private static org.postgis.Point convert(Point point) {
|
||||
org.postgis.Point pgPoint = new org.postgis.Point();
|
||||
pgPoint.srid = point.getSRID();
|
||||
pgPoint.dimension = point.getDimension();
|
||||
pgPoint.dimension = point.is3D() ? 3 : 2;
|
||||
pgPoint.haveMeasure = false;
|
||||
pgPoint.x = point.getX();
|
||||
if (pgPoint.dimension > 1) {
|
||||
pgPoint.y = point.getY();
|
||||
}
|
||||
if (pgPoint.dimension > 2) {
|
||||
pgPoint.y = point.getY();
|
||||
if (point.is3D()) {
|
||||
pgPoint.z = point.getZ();
|
||||
}
|
||||
if (point.isMeasured()) {
|
||||
@ -207,6 +206,9 @@ public class PGgeometryConverter {
|
||||
}
|
||||
|
||||
private static PointSequence convertPoints(org.postgis.Point[] points) {
|
||||
if (points.length == 0) {
|
||||
return PointCollectionFactory.createEmpty();
|
||||
}
|
||||
org.postgis.Point first = points[0];
|
||||
DimensionalFlag flag = DimensionalFlag.XY;
|
||||
if (first.dimension == 2 && first.haveMeasure) {
|
||||
|
||||
@ -0,0 +1,64 @@
|
||||
package com.mysema.query.sql.spatial;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.geolatte.geom.Geometry;
|
||||
import org.geolatte.geom.LineString;
|
||||
import org.geolatte.geom.MultiPoint;
|
||||
import org.geolatte.geom.Point;
|
||||
import org.geolatte.geom.PointSequence;
|
||||
import org.geolatte.geom.PointSequenceBuilder;
|
||||
import org.geolatte.geom.PointSequenceBuilders;
|
||||
import org.geolatte.geom.Points;
|
||||
import org.geolatte.geom.codec.Wkt;
|
||||
import org.geolatte.geom.crs.CrsId;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.mysema.query.Connections;
|
||||
|
||||
public abstract class AbstractConverterTest {
|
||||
|
||||
protected PointSequence createSequence(Point... points) {
|
||||
PointSequenceBuilder builder = PointSequenceBuilders.fixedSized(points.length, points[0].getDimensionalFlag());
|
||||
for (Point point : points) {
|
||||
builder.add(point);
|
||||
}
|
||||
return builder.toPointSequence();
|
||||
}
|
||||
|
||||
protected List<Geometry> getGeometries() {
|
||||
CrsId crs = CrsId.valueOf(1);
|
||||
List<Geometry> data = Lists.newArrayList();
|
||||
// points
|
||||
// data.add(Points.createEmpty());
|
||||
data.add(Points.create(1, 2));
|
||||
data.add(Points.create(1, 2, crs));
|
||||
data.add(Points.create3D(1, 2, 3));
|
||||
data.add(Points.create3D(1, 2, 3, crs));
|
||||
data.add(Points.createMeasured(1, 2, 3));
|
||||
data.add(Points.createMeasured(1, 2, 3, crs));
|
||||
// linestring
|
||||
data.add(LineString.createEmpty());
|
||||
for (int i = 0; i < 6; i++) {
|
||||
data.add(new LineString(createSequence((Point)data.get(i)), crs));
|
||||
}
|
||||
// polgyon
|
||||
// TODO
|
||||
// multipoint
|
||||
data.add(MultiPoint.createEmpty());
|
||||
for (int i = 0; i < 6; i++) {
|
||||
data.add(new MultiPoint(new Point[]{(Point)data.get(i)}));
|
||||
}
|
||||
// multilinestring
|
||||
// TODO
|
||||
// multipolygon
|
||||
// TODO
|
||||
|
||||
for (String wkt : Connections.getSpatialData().values()) {
|
||||
data.add(Wkt.fromWkt(wkt));
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package com.mysema.query.sql.spatial;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.geolatte.geom.Geometry;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PGgeometryConverterTest extends AbstractConverterTest {
|
||||
|
||||
@Test
|
||||
public void RoundTrip() {
|
||||
List<Geometry> geometries = getGeometries();
|
||||
for (Geometry geometry : geometries) {
|
||||
org.postgis.Geometry converted = PGgeometryConverter.convert(geometry);
|
||||
Geometry back = PGgeometryConverter.convert(converted);
|
||||
assertEquals(geometry, back);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -3,22 +3,15 @@ package com.mysema.query.sql.spatial;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.geolatte.geom.Geometry;
|
||||
import org.geolatte.geom.codec.Wkt;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.Connections;
|
||||
|
||||
public class SQLServerGeometryWriterTest {
|
||||
public class SQLServerGeometryWriterTest extends AbstractConverterTest {
|
||||
|
||||
@Test
|
||||
public void RoundTrip() throws IOException {
|
||||
Collection<String> wkt = Connections.getSpatialData().values();
|
||||
for (String geoWkt : wkt) {
|
||||
System.err.println(geoWkt);
|
||||
Geometry geometry = Wkt.fromWkt(geoWkt);
|
||||
for (Geometry geometry : getGeometries()) {
|
||||
byte[] bytes = new SQLServerGeometryWriter().write(geometry);
|
||||
Geometry geometry2 = new SQLServerGeometryReader().read(bytes);
|
||||
assertEquals(geometry, geometry2);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user