Add conversion of points #631

This commit is contained in:
Timo Westkämper 2014-02-03 23:16:58 +02:00
parent 24e898c4e9
commit 4bab9d056b
4 changed files with 44 additions and 18 deletions

View File

@ -15,14 +15,15 @@ package com.mysema.query.sql.postgres;
import org.geolatte.geom.Geometry;
import org.geolatte.geom.GeometryCollection;
import org.geolatte.geom.GeometryFactory;
import org.geolatte.geom.LineString;
import org.geolatte.geom.MultiLineString;
import org.geolatte.geom.MultiPoint;
import org.geolatte.geom.MultiPolygon;
import org.geolatte.geom.Point;
import org.geolatte.geom.Points;
import org.geolatte.geom.PolyHedralSurface;
import org.geolatte.geom.Polygon;
import org.geolatte.geom.crs.CrsId;
import org.postgis.LinearRing;
import org.postgis.PGgeometry;
@ -33,8 +34,6 @@ import org.postgis.PGgeometry;
*/
public class PGGeometryConverter {
private GeometryFactory geometryFactory;
public static PGgeometry convert(Geometry geometry) {
if (geometry instanceof Point) {
return convert((Point)geometry);
@ -58,8 +57,22 @@ public class PGGeometryConverter {
}
private static PGgeometry convert(Point point) {
point.get
return null; // TODO
org.postgis.Point pgPoint = new org.postgis.Point();
pgPoint.srid = point.getSRID();
pgPoint.dimension = point.getDimension();
pgPoint.haveMeasure = false;
pgPoint.x = point.getX();
if (pgPoint.dimension > 1) {
pgPoint.y = point.getY();
}
if (pgPoint.dimension > 2) {
pgPoint.z = point.getZ();
}
if (point.isMeasured()) {
pgPoint.m = point.getM();
pgPoint.haveMeasure = true;
}
return new PGgeometry(pgPoint);
}
private static PGgeometry convert(LineString lineString) {
@ -148,8 +161,24 @@ public class PGGeometryConverter {
}
private static Geometry convert(org.postgis.Point geometry) {
// TODO Auto-generated method stub
return null;
int d = geometry.dimension;
CrsId srid = CrsId.valueOf(geometry.srid);
if (!geometry.haveMeasure) {
switch (d) {
case 2:
return Points.create(geometry.x, geometry.y, srid);
case 3:
return Points.create3D(geometry.x, geometry.y, geometry.z, srid);
}
} else {
switch (d) {
case 2:
return Points.createMeasured(geometry.x, geometry.y, geometry.m, srid);
case 3:
return Points.create(geometry.x, geometry.y, geometry.z, geometry.m, srid);
}
}
throw new IllegalArgumentException(geometry.toString());
}
}

View File

@ -21,7 +21,7 @@ import java.sql.Types;
import javax.annotation.Nullable;
import org.geolatte.geom.Geometry;
import org.geolatte.geom.codec.Wkt;
import org.postgis.PGgeometry;
import com.mysema.query.sql.types.AbstractType;
@ -45,18 +45,14 @@ public class PGGeometryType extends AbstractType<Geometry> {
@Override
@Nullable
public Geometry getValue(ResultSet rs, int startIndex) throws SQLException {
String str = rs.getString(startIndex);
if (str != null) {
return Wkt.newWktDecoder(Wkt.Dialect.POSTGIS_EWKT_1).decode(str);
} else {
return null;
}
Object obj = rs.getObject(startIndex);
return obj != null ? PGGeometryConverter.convert((PGgeometry) obj) : null;
}
@Override
public void setValue(PreparedStatement st, int startIndex, Geometry value) throws SQLException {
String str = Wkt.newWktEncoder(Wkt.Dialect.POSTGIS_EWKT_1).encode(value);
st.setString(startIndex, str);
PGgeometry geometry = PGGeometryConverter.convert(value);
st.setObject(startIndex, geometry);
}
}

View File

@ -42,6 +42,7 @@ public class PostGISTemplates extends PostgresTemplates {
public PostGISTemplates(char escape, boolean quote) {
super(escape, quote);
addSpatialOps(true);
addCustomType(PGGeometryType.DEFAULT);
}
}

View File

@ -16,7 +16,7 @@ import org.junit.Test;
import com.google.common.collect.Lists;
import com.mysema.query.spatial.PointExpression;
import com.mysema.query.spatial.path.PointPath;
import com.mysema.query.sql.PostgresTemplates;
import com.mysema.query.sql.postgres.PostGISTemplates;
import com.mysema.query.sql.spatial.QShapes;
import com.mysema.query.sql.spatial.Shapes;
import com.mysema.query.types.ConstantImpl;
@ -30,7 +30,7 @@ public class SpatialBase extends AbstractBaseTest {
// Connections.initTeradata();
// Connections.setTemplates(TeradataTemplates.builder().newLineToSingleSpace().build());
Connections.initPostgres();
Connections.setTemplates(PostgresTemplates.builder().quote().newLineToSingleSpace().build());
Connections.setTemplates(PostGISTemplates.builder().quote().newLineToSingleSpace().build());
}
// TEMPORARY