use of org.voltdb.types.GeographyValue in project voltdb by VoltDB.
the class TestGeographyValueQueries method testGeographySize.
public void testGeographySize() throws Exception {
Client client = getClient();
// Make sure that we can resize a GEOGRAPHY column in a populated table if
// we decide that we want to insert a polygon that is larger
// than the column's current size.
String wktFourVerts = "POLYGON ((1.0 1.0, -1.0 1.0, -1.0 -1.0, 1.0 -1.0, 1.0 1.0))";
GeographyValue gv = GeographyValue.fromWKT(wktFourVerts);
assertEquals(179, gv.getLengthInBytes());
VoltTable vt = client.callProcedure("tiny_polygon.Insert", 0, gv).getResults()[0];
validateTableOfScalarLongs(vt, new long[] { 1 });
String wktFiveVerts = "POLYGON ((" + "1.0 1.0, " + "-1.0 1.0, " + "-1.0 -1.0, " + "1.0 -1.0, " + "0.0 0.0, " + "1.0 1.0))";
gv = GeographyValue.fromWKT(wktFiveVerts);
assertEquals(203, gv.getLengthInBytes());
verifyProcFails(client, "The size 203 of the value exceeds the size of the GEOGRAPHY column \\(179 bytes\\)", "tiny_polygon.Insert", 1, gv);
ClientResponse cr = client.callProcedure("@AdHoc", "alter table tiny_polygon alter column poly geography(203);");
assertEquals(ClientResponse.SUCCESS, cr.getStatus());
vt = client.callProcedure("tiny_polygon.Insert", 1, gv).getResults()[0];
validateTableOfScalarLongs(vt, new long[] { 1 });
validateTableColumnOfScalarVarchar(client, "select asText(poly) from tiny_polygon order by id", new String[] { wktFourVerts, wktFiveVerts });
// Restore catalog changes:
cr = client.callProcedure("@AdHoc", "truncate table tiny_polygon;");
assertEquals(ClientResponse.SUCCESS, cr.getStatus());
cr = client.callProcedure("@AdHoc", "alter table tiny_polygon alter column poly geography(179) not null;");
assertEquals(ClientResponse.SUCCESS, cr.getStatus());
}
use of org.voltdb.types.GeographyValue in project voltdb by VoltDB.
the class TestSQLTypesSuite method comparisonHelper.
/** Utility to compare two instances of a VoltType for equality */
@SuppressWarnings({ "incomplete-switch" })
private boolean comparisonHelper(final Object lhs, final Object rhs, final VoltType vt) {
switch(vt) {
case TINYINT:
final Byte b1 = (Byte) lhs;
final Byte b2 = (Byte) rhs;
// System.out.println("\tComparing " + b1 + " == " + b2);
return b1.byteValue() == b2.byteValue();
case SMALLINT:
final Short s1 = (Short) lhs;
final Short s2 = (Short) rhs;
// System.out.println("\tComparing " + s1 + " == " + s2);
return s1.shortValue() == s2.shortValue();
case INTEGER:
final Integer i1 = (Integer) lhs;
final Integer i2 = (Integer) rhs;
// System.out.println("\tComparing " + i1 + " == " + i2);
return i1.intValue() == i2.intValue();
case BIGINT:
final Long l1 = (Long) lhs;
final Long l2 = (Long) rhs;
// System.out.println("\tComparing " + l1 + " == " + l2);
return l1.longValue() == l2.longValue();
case FLOAT:
final Double d1 = (Double) lhs;
final Double d2 = (Double) rhs;
// Handle the screwy null double value (isn't quite min double)
if (((d1 == VoltType.NULL_FLOAT) && (d2 <= d1)) || ((d2 == VoltType.NULL_FLOAT) && (d1 <= d2))) {
return true;
}
return (Math.abs(d1 - d2) < 0.0000000001);
case STRING:
// System.out.println("\tComparing " + lhs + " == " + rhs);
if ((lhs == null || lhs == VoltType.NULL_STRING_OR_VARBINARY) && (rhs == null || rhs == VoltType.NULL_STRING_OR_VARBINARY)) {
return true;
}
return ((String) lhs).equals(rhs);
case VARBINARY:
boolean lhsnull = (lhs == null || lhs == VoltType.NULL_STRING_OR_VARBINARY);
boolean rhsnull = (rhs == null || rhs == VoltType.NULL_STRING_OR_VARBINARY);
if (lhsnull && rhsnull)
return true;
if (lhsnull != rhsnull)
return false;
// assume neither is null from here
String lhs2 = null;
String rhs2 = null;
if (lhs instanceof byte[])
lhs2 = Encoder.hexEncode((byte[]) lhs);
else
lhs2 = (String) lhs;
if (rhs instanceof byte[])
rhs2 = Encoder.hexEncode((byte[]) rhs);
else
rhs2 = (String) rhs;
return lhs2.equalsIgnoreCase(rhs2);
case TIMESTAMP:
// System.out.println("\tComparing " + lhs + " == " + rhs);
if ((lhs == null || lhs == VoltType.NULL_TIMESTAMP) && (rhs == null || rhs == VoltType.NULL_TIMESTAMP)) {
return true;
}
return ((TimestampType) lhs).equals(rhs);
case DECIMAL:
// System.out.println("\tComparing " + lhs + " == " + rhs);
if ((lhs == null || lhs == VoltType.NULL_DECIMAL) && (rhs == null || rhs == VoltType.NULL_DECIMAL)) {
return true;
}
return ((BigDecimal) lhs).equals(rhs);
case GEOGRAPHY_POINT:
{
if ((lhs == VoltType.NULL_POINT || lhs == null) && (rhs == VoltType.NULL_POINT || rhs == null)) {
return true;
}
GeographyPointValue gpvLhs = (GeographyPointValue) lhs;
GeographyPointValue gpvRhs = (GeographyPointValue) rhs;
return gpvLhs.equals(gpvRhs);
}
case GEOGRAPHY:
{
if ((lhs == VoltType.NULL_GEOGRAPHY || lhs == null) && (rhs == VoltType.NULL_GEOGRAPHY || rhs == null)) {
return true;
}
GeographyValue gvLhs = (GeographyValue) lhs;
GeographyValue gvRhs = (GeographyValue) rhs;
return gvLhs.equals(gvRhs);
}
default:
throw new IllegalArgumentException("Unknown type in comparisonHelper");
}
}
use of org.voltdb.types.GeographyValue in project voltdb by VoltDB.
the class TestPolygonFactory method makeRegularConvexPolygons.
/**
* Create many regular convex polygons. In returnValue.get(n).get(k) we put an
* (n+3)-sided polygon with the given center and start vertex, with hole size
* equal to k/numHoleSizes. If k == 0 there is no hole. Note that k ranges between
* 0 and 4, so k/NumHoleSizes ranges between 0 and (1-1/numHoleSizes). If k == 0
* there is no hole.
*
* @return
*/
private static List<List<GeographyValue>> makeRegularConvexPolygons(GeographyPointValue firstCenter, GeographyPointValue firstFirstVertex, int minNumberVertices, int maxNumberVertices, int numHoleSizes, double xmul, double ymul) {
List<List<GeographyValue>> answer = new ArrayList<List<GeographyValue>>();
for (int numVertices = minNumberVertices; numVertices <= maxNumberVertices; numVertices += 1) {
int idx = numVertices - minNumberVertices;
List<GeographyValue> oneSize = new ArrayList<GeographyValue>();
// The x coordinate is humHoleSizes*idx.
GeographyPointValue sCenter = firstCenter.add(x.mul(numHoleSizes * idx));
for (int hidx = 0; hidx < numHoleSizes; hidx += 1) {
// The y coordinate is ymul * hidx.
GeographyPointValue offset = sCenter.add(y.mul(ymul * hidx));
GeographyPointValue center = firstCenter.add(offset);
GeographyPointValue firstVertex = firstFirstVertex.add(offset);
oneSize.add(PolygonFactory.CreateRegularConvex(center, firstVertex, numVertices, (hidx + 0.0) / numHoleSizes));
}
answer.add(oneSize);
}
return answer;
}
use of org.voltdb.types.GeographyValue in project voltdb by VoltDB.
the class TestPolygonFactory method main.
/**
* This main routine is useful for manual testing. The idea is that one
* runs this routine and WKT polygons are printed. These can be displayed
* with qgis or some other display tool.
*
* @param arg
*/
public static void main(String[] args) {
boolean doStars = false;
boolean doRegs = false;
int minVerts = 3;
int maxVerts = 12;
int numIRs = 5;
int numHoles = 5;
double xmul = 3.0;
double ymul = 3.0;
for (int arg = 0; arg < args.length; arg += 1) {
if (args[arg].equals("--stars")) {
doStars = true;
} else if (args[arg].equals("--reg")) {
doRegs = true;
} else if (args[arg].equals("--minVerts")) {
minVerts = getIntArg(args, ++arg, "--minVerts expects one integer parameters");
} else if (args[arg].equals("--maxVerts")) {
maxVerts = getIntArg(args, ++arg, "--maxVerts expects one integer parameters");
} else if (args[arg].equals("--numHoles")) {
numHoles = getIntArg(args, ++arg, "--numHoles expects one integer parameter");
} else if (args[arg].equals("--numIRs")) {
numIRs = getIntArg(args, ++arg, "--numIRs expects one integer parameter");
} else if (args[arg].equals("--xmul")) {
xmul = getDoubleArg(args, ++arg, "--xmul expects one double parameter");
} else if (args[arg].equals("--ymul")) {
ymul = getDoubleArg(args, ++arg, "--ymul expects one double parameter");
} else {
System.err.printf("Unknown command line parameter \"%s\"\n", args[arg]);
System.exit(100);
}
}
GeographyPointValue center = origin.add(x.mul(10).add(y.mul(10)));
GeographyPointValue firstVertex = center.add(x.mul(CENTER_SHRINK * xmul).add(y.mul(CENTER_SHRINK * ymul)));
if (doRegs) {
List<List<GeographyValue>> polys = makeRegularConvexPolygons(center, firstVertex, minVerts, maxVerts, numHoles, xmul, ymul);
for (int nsides = 0; nsides < polys.size(); nsides += 1) {
for (int holeSize = 0; holeSize < 5; holeSize += 1) {
System.out.printf("%s\n", formatWKT(polys.get(nsides).get(holeSize).toString()));
}
}
}
if (doStars) {
GeographyPointValue scenter = center;
GeographyPointValue sfirstVertex = firstVertex;
List<List<List<GeographyValue>>> stars = makeStarPolygons(scenter, sfirstVertex, minVerts, maxVerts, numIRs, numHoles, xmul, ymul);
for (int nsides = 0; nsides < stars.size(); nsides += 1) {
List<List<GeographyValue>> oneSize = stars.get(nsides);
for (int innerRadiusIdx = 0; innerRadiusIdx < oneSize.size(); innerRadiusIdx += 1) {
List<GeographyValue> oneInnerRadius = oneSize.get(innerRadiusIdx);
for (int holeSizeIdx = 0; holeSizeIdx < oneInnerRadius.size(); holeSizeIdx += 1) {
GeographyValue oneStar = oneInnerRadius.get(holeSizeIdx);
System.out.printf("%s\n", formatWKT(oneStar.toString()));
}
}
}
}
}
use of org.voltdb.types.GeographyValue in project voltdb by VoltDB.
the class TestPolygonFactory method testRegularConvexPolygon.
public void testRegularConvexPolygon() throws Exception {
// Test a triangle.
GeographyValue pt3 = PolygonFactory.CreateRegularConvex(origin, y.mul(20.0), 3, 0);
String triangle = "POLYGON ((0.0 20.0, -17.320508075689 -10.0, 17.320508075689 -10.0, 0.0 20.0))";
assertEquals(triangle, pt3.toString());
// Test a square.
GeographyValue pt4 = PolygonFactory.CreateRegularConvex(origin, y.mul(20).add(x.mul(20)), 4, 0);
String square = "POLYGON ((20.0 20.0, -20.0 20.0, -20.0 -20.0, 20.0 -20.0, 20.0 20.0))";
assertEquals(square, pt4.toString());
GeographyPointValue offset = x.mul(20).add(y.mul(20));
GeographyValue pt4plus = pt4.add(offset);
String squareOff = "POLYGON ((40.0 40.0, 0.0 40.0, 0.0 0.0, 40.0 0.0, 40.0 40.0))";
assertEquals(squareOff, pt4plus.toString());
// For n = 3 to 10, generate a regular polygon with n points
// centered at the origin starting at the given start vertex.
GeographyPointValue startVertex = x.add(y);
for (int npts = 3; npts < 10; npts += 1) {
GeographyValue regularConvex = PolygonFactory.CreateRegularConvex(origin, startVertex, npts, 0.0);
List<List<GeographyPointValue>> loops = regularConvex.getRings();
assertEquals(1, loops.size());
List<GeographyPointValue> loop = loops.get(0);
assertEquals(npts + 1, loop.size());
regularConvex = PolygonFactory.CreateRegularConvex(origin, startVertex, npts, 0.5);
loops = regularConvex.getRings();
assertEquals(2, loops.size());
assertEquals(npts + 1, loops.get(0).size());
assertEquals(npts + 1, loops.get(1).size());
}
}
Aggregations