Search in sources :

Example 31 with GeographyValue

use of org.voltdb.types.GeographyValue in project voltdb by VoltDB.

the class PolygonFactory method CreateStar.

/**
     * Create a star-shaped polygon with an optional hole.  This polygon will have
     * numPointsInStar outer points, and numPointsInStar inner points.  For each
     * outer point, OP, distance(center, OP) = distance(center, firstVertex).  For
     * each inner point, IP, distance(center, IP) = ratioOfPointLength*distance(center, firstVertex).
     *
     * If sizeOfHole is positive, then there is a hole with inner and outer vertices, as
     * with the exterior shell.  For each hole exterior point, HEP,
     *     distance(center, HEP) = sizeOfHole*distance(center, firstVertex).  For each
     * hole interior point, HIP,
     *     distance(center, HIP) = sizeOfHole*rationOfPointLength*distance(center, firstVertex).
     * So, the hole is equal to the exterior shell scaled by the number sizeOfHole.
     *
     * Note that this polygon will be symmetric around any line through the center and
     * an outer or inner point.  Consequently, the centroid of the generated polygon
     * must be the center.
     *
     * @param center The center of the polygon.
     * @param firstVertex The first vertex.
     * @param numPointsInStar The number of exterior points in the star.
     * @param ratioOfPointLength The outer/inner scale factor.  This must be in the range (0,1].
     * @param sizeOfHole The scale factor for the hole.  This must be in the range [0,1).
     * @return
     * @throws IllegalArgumentException
     */
public static GeographyValue CreateStar(GeographyPointValue center, GeographyPointValue firstVertex, int numPointsInStar, // pass in 1.0 for a polygon with twice the number of vertices, or .1 for a very jagged star
double ratioOfPointLength, double sizeOfHole) throws IllegalArgumentException {
    // I don't think a 1 or 2 pointed star is possible.
    if (numPointsInStar < 3) {
        throw new IllegalArgumentException("Star polygons must have 3 or more points.");
    }
    if (sizeOfHole < 0 || 1.0 <= sizeOfHole) {
        throw new IllegalArgumentException("Star polygon hole size must be in the range [0.0, 1.0)");
    }
    if (ratioOfPointLength <= 0 || 1.0 < ratioOfPointLength) {
        throw new IllegalArgumentException("Star polygon external/internal radius ration must be in the range (0.0, 1.0]");
    }
    // We will add the nth point at angle n*phi.  We want to
    // add points in a counter clockwise order, so phi must be
    // a positive angle.  We will have twice as many vertices
    // as points.
    double phi = 360.0 / (2 * numPointsInStar);
    GeographyPointValue innerFirstVertex = firstVertex.scale(center, ratioOfPointLength);
    GeographyPointValue holeFirstVertex = null;
    GeographyPointValue innerHoleFirstVertex = null;
    // Calculate hole radii.
    if (sizeOfHole > 0) {
        holeFirstVertex = firstVertex.scale(center, sizeOfHole);
        innerHoleFirstVertex = firstVertex.scale(center, sizeOfHole * ratioOfPointLength);
    }
    // The outer and inner radii go here. We will
    // index into this array using the last bit of
    // an integer loop index.
    GeographyPointValue[] firstVertices = { firstVertex, innerFirstVertex };
    // We use the same trick here.
    GeographyPointValue[] holeFirstVertices = { holeFirstVertex, innerHoleFirstVertex };
    //
    // We have to add all shells in counter clockwise order, and all
    // holes in clockwise order.  This amounts to rotating the shell
    // generator vector by phi and the hole generator vector by -phi.
    //
    List<GeographyPointValue> outerLoop = new ArrayList<GeographyPointValue>();
    List<GeographyPointValue> holeLoop = null;
    if (sizeOfHole > 0) {
        holeLoop = new ArrayList<GeographyPointValue>();
    }
    for (int idx = 0; idx < 2 * numPointsInStar; idx += 1) {
        GeographyPointValue vert = null;
        GeographyPointValue holeVert = null;
        // Even vertices are the points.
        // Odd vertices are the valleys, if that's the right
        // term.
        vert = firstVertices[idx % 2];
        holeVert = holeFirstVertices[idx % 2];
        outerLoop.add(vert.rotate(idx * phi, center));
        if (sizeOfHole > 0) {
            holeLoop.add(holeVert.rotate(-(idx * phi), center));
        }
    }
    outerLoop.add(outerLoop.get(0));
    if (sizeOfHole > 0) {
        holeLoop.add(holeLoop.get(0));
    }
    List<List<GeographyPointValue>> loops = new ArrayList<List<GeographyPointValue>>();
    loops.add(outerLoop);
    if (sizeOfHole > 0) {
        loops.add(holeLoop);
    }
    return new GeographyValue(loops);
}
Also used : GeographyValue(org.voltdb.types.GeographyValue) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) GeographyPointValue(org.voltdb.types.GeographyPointValue)

Example 32 with GeographyValue

use of org.voltdb.types.GeographyValue in project voltdb by VoltDB.

the class VoltTableUtil method toCSVWriter.

public static void toCSVWriter(CSVWriter csv, VoltTable vt, List<VoltType> columnTypes) throws IOException {
    final SimpleDateFormat sdf = m_sdf.get();
    String[] fields = new String[vt.getColumnCount()];
    while (vt.advanceRow()) {
        for (int ii = 0; ii < vt.getColumnCount(); ii++) {
            final VoltType type = columnTypes.get(ii);
            if (type == VoltType.BIGINT || type == VoltType.INTEGER || type == VoltType.SMALLINT || type == VoltType.TINYINT) {
                final long value = vt.getLong(ii);
                if (vt.wasNull()) {
                    fields[ii] = Constants.CSV_NULL;
                } else {
                    fields[ii] = Long.toString(value);
                }
            } else if (type == VoltType.FLOAT) {
                final double value = vt.getDouble(ii);
                if (vt.wasNull()) {
                    fields[ii] = Constants.CSV_NULL;
                } else {
                    fields[ii] = Double.toString(value);
                }
            } else if (type == VoltType.DECIMAL) {
                final BigDecimal bd = vt.getDecimalAsBigDecimal(ii);
                if (vt.wasNull()) {
                    fields[ii] = Constants.CSV_NULL;
                } else {
                    fields[ii] = bd.toString();
                }
            } else if (type == VoltType.STRING) {
                final String str = vt.getString(ii);
                if (vt.wasNull()) {
                    fields[ii] = Constants.CSV_NULL;
                } else {
                    fields[ii] = str;
                }
            } else if (type == VoltType.TIMESTAMP) {
                final TimestampType timestamp = vt.getTimestampAsTimestamp(ii);
                if (vt.wasNull()) {
                    fields[ii] = Constants.CSV_NULL;
                } else {
                    fields[ii] = sdf.format(timestamp.asApproximateJavaDate());
                    fields[ii] += String.format("%03d", timestamp.getUSec());
                }
            } else if (type == VoltType.VARBINARY) {
                byte[] bytes = vt.getVarbinary(ii);
                if (vt.wasNull()) {
                    fields[ii] = Constants.CSV_NULL;
                } else {
                    fields[ii] = Encoder.hexEncode(bytes);
                }
            } else if (type == VoltType.GEOGRAPHY_POINT) {
                final GeographyPointValue pt = vt.getGeographyPointValue(ii);
                if (vt.wasNull()) {
                    fields[ii] = Constants.CSV_NULL;
                } else {
                    fields[ii] = pt.toString();
                }
            } else if (type == VoltType.GEOGRAPHY) {
                final GeographyValue gv = vt.getGeographyValue(ii);
                if (vt.wasNull()) {
                    fields[ii] = Constants.CSV_NULL;
                } else {
                    fields[ii] = gv.toString();
                }
            }
        }
        csv.writeNext(fields);
    }
    csv.flush();
}
Also used : GeographyValue(org.voltdb.types.GeographyValue) VoltType(org.voltdb.VoltType) TimestampType(org.voltdb.types.TimestampType) SimpleDateFormat(java.text.SimpleDateFormat) GeographyPointValue(org.voltdb.types.GeographyPointValue) BigDecimal(java.math.BigDecimal)

Example 33 with GeographyValue

use of org.voltdb.types.GeographyValue in project voltdb by VoltDB.

the class TestVoltTable method testFormattedString.

public void testFormattedString() throws JSONException, IOException {
    // Set the default timezone since we're using a timestamp type.  Eliminate test flakeyness.
    VoltDB.setDefaultTimezone();
    VoltTable table = new VoltTable(new ColumnInfo("tinyint", VoltType.TINYINT), new ColumnInfo("smallint", VoltType.SMALLINT), new ColumnInfo("integer", VoltType.INTEGER), new ColumnInfo("bigint", VoltType.BIGINT), new ColumnInfo("float", VoltType.FLOAT), new ColumnInfo("string", VoltType.STRING), new ColumnInfo("varbinary", VoltType.VARBINARY), new ColumnInfo("timestamp", VoltType.TIMESTAMP), new ColumnInfo("decimal", VoltType.DECIMAL));
    // add a row of nulls the hard way
    table.addRow(VoltType.NULL_TINYINT, VoltType.NULL_SMALLINT, VoltType.NULL_INTEGER, VoltType.NULL_BIGINT, VoltType.NULL_FLOAT, VoltType.NULL_STRING_OR_VARBINARY, VoltType.NULL_STRING_OR_VARBINARY, VoltType.NULL_TIMESTAMP, VoltType.NULL_DECIMAL);
    // add a row of nulls the easy way
    table.addRow(null, null, null, null, null, null, null, null, null);
    // add a row of actual data.  Hard-code the timestamp so that we can compare deterministically
    table.addRow(123, 12345, 1234567, 12345678901L, 1.234567, "aabbcc", new byte[] { 10, 26, 10 }, new TimestampType(99), new BigDecimal("123.45"));
    String formattedOutput = table.toFormattedString();
    String expectedOutput = "tinyint  smallint  integer  bigint       float     string  varbinary  timestamp                   decimal          \n" + "-------- --------- -------- ------------ --------- ------- ---------- --------------------------- -----------------\n" + "    NULL      NULL     NULL         NULL      NULL NULL    NULL       NULL                                     NULL\n" + "    NULL      NULL     NULL         NULL      NULL NULL    NULL       NULL                                     NULL\n" + "     123     12345  1234567  12345678901  1.234567 aabbcc  0A1A0A     1970-01-01 00:00:00.000099   123.450000000000\n";
    assertExpectedOutput(expectedOutput, formattedOutput);
    // fetch formatted output without column names
    formattedOutput = table.toFormattedString(false);
    expectedOutput = "    NULL      NULL     NULL         NULL      NULL NULL    NULL       NULL                                     NULL\n" + "    NULL      NULL     NULL         NULL      NULL NULL    NULL       NULL                                     NULL\n" + "     123     12345  1234567  12345678901  1.234567 aabbcc  0A1A0A     1970-01-01 00:00:00.000099   123.450000000000\n";
    assertExpectedOutput(expectedOutput, formattedOutput);
    table = new VoltTable(new ColumnInfo("bigint", VoltType.BIGINT), new ColumnInfo("geography", VoltType.GEOGRAPHY), new ColumnInfo("geography_point", VoltType.GEOGRAPHY_POINT), new ColumnInfo("timestamp", VoltType.TIMESTAMP));
    table.addRow(VoltType.NULL_BIGINT, VoltType.NULL_GEOGRAPHY, VoltType.NULL_POINT, VoltType.NULL_TIMESTAMP);
    table.addRow(null, null, null, null);
    table.addRow(123456789, new GeographyValue("POLYGON (( 1.1  9.9, " + "-9.1  9.9, " + "-9.1 -9.9, " + " 9.1 -9.9, " + " 1.1  9.9))"), new GeographyPointValue(-179.0, -89.9), new TimestampType(-1));
    formattedOutput = table.toFormattedString();
    expectedOutput = "bigint     geography                                                    geography_point       timestamp                  \n" + "---------- ------------------------------------------------------------ --------------------- ---------------------------\n" + "      NULL NULL                                                         NULL                  NULL                       \n" + "      NULL NULL                                                         NULL                  NULL                       \n" + " 123456789 POLYGON ((1.1 9.9, -9.1 9.9, -9.1 -9.9, 9.1 -9.9, 1.1 9.9))  POINT (-179.0 -89.9)  1969-12-31 23:59:59.999999 \n";
    assertExpectedOutput(expectedOutput, formattedOutput);
    // row with a polygon that max's output column width for geopgraphy column
    table.addRow(1234567890, new GeographyValue("POLYGON (( 179.1  89.9, " + "-179.1  89.9, " + "-179.1 -89.9, " + " 179.1 -89.9, " + " 179.1  89.9))"), new GeographyPointValue(-179.0, -89.9), new TimestampType(0));
    formattedOutput = table.toFormattedString();
    expectedOutput = "bigint      geography                                                                   geography_point       timestamp                  \n" + "----------- --------------------------------------------------------------------------- --------------------- ---------------------------\n" + "       NULL NULL                                                                        NULL                  NULL                       \n" + "       NULL NULL                                                                        NULL                  NULL                       \n" + "  123456789 POLYGON ((1.1 9.9, -9.1 9.9, -9.1 -9.9, 9.1 -9.9, 1.1 9.9))                 POINT (-179.0 -89.9)  1969-12-31 23:59:59.999999 \n" + " 1234567890 POLYGON ((179.1 89.9, -179.1 89.9, -179.1 -89.9, 179.1 -89.9, 179.1 89.9))  POINT (-179.0 -89.9)  1970-01-01 00:00:00.000000 \n";
    assertExpectedOutput(expectedOutput, formattedOutput);
    // row with a polygon that goes beyond max aligned display limit for polygon. This will result in
    // other columns following to appear further away from their original column
    table.addRow(12345678901L, new GeographyValue("POLYGON (( 179.12  89.9, " + "-179.12  89.9, " + "-179.1  -89.9, " + " 179.1  -89.9, " + "   0     0," + "  1.123  1.11," + " 179.12  89.9))"), new GeographyPointValue(0, 0), new TimestampType(99));
    formattedOutput = table.toFormattedString();
    expectedOutput = "bigint       geography                                                                   geography_point       timestamp                  \n" + "------------ --------------------------------------------------------------------------- --------------------- ---------------------------\n" + "        NULL NULL                                                                        NULL                  NULL                       \n" + "        NULL NULL                                                                        NULL                  NULL                       \n" + "   123456789 POLYGON ((1.1 9.9, -9.1 9.9, -9.1 -9.9, 9.1 -9.9, 1.1 9.9))                 POINT (-179.0 -89.9)  1969-12-31 23:59:59.999999 \n" + "  1234567890 POLYGON ((179.1 89.9, -179.1 89.9, -179.1 -89.9, 179.1 -89.9, 179.1 89.9))  POINT (-179.0 -89.9)  1970-01-01 00:00:00.000000 \n" + " 12345678901 POLYGON ((179.12 89.9, -179.12 89.9, -179.1 -89.9, 179.1 -89.9, 0.0 0.0, 1.123 1.11, 179.12 89.9)) POINT (0.0 0.0)       1970-01-01 00:00:00.000099 \n";
    assertExpectedOutput(expectedOutput, formattedOutput);
    // test the final one without column names
    formattedOutput = table.toFormattedString(false);
    expectedOutput = "        NULL NULL                                                                        NULL                  NULL                       \n" + "        NULL NULL                                                                        NULL                  NULL                       \n" + "   123456789 POLYGON ((1.1 9.9, -9.1 9.9, -9.1 -9.9, 9.1 -9.9, 1.1 9.9))                 POINT (-179.0 -89.9)  1969-12-31 23:59:59.999999 \n" + "  1234567890 POLYGON ((179.1 89.9, -179.1 89.9, -179.1 -89.9, 179.1 -89.9, 179.1 89.9))  POINT (-179.0 -89.9)  1970-01-01 00:00:00.000000 \n" + " 12345678901 POLYGON ((179.12 89.9, -179.12 89.9, -179.1 -89.9, 179.1 -89.9, 0.0 0.0, 1.123 1.11, 179.12 89.9)) POINT (0.0 0.0)       1970-01-01 00:00:00.000099 \n";
    assertExpectedOutput(expectedOutput, formattedOutput);
}
Also used : GeographyValue(org.voltdb.types.GeographyValue) ColumnInfo(org.voltdb.VoltTable.ColumnInfo) TimestampType(org.voltdb.types.TimestampType) GeographyPointValue(org.voltdb.types.GeographyPointValue) BigDecimal(java.math.BigDecimal)

Aggregations

GeographyValue (org.voltdb.types.GeographyValue)33 GeographyPointValue (org.voltdb.types.GeographyPointValue)23 TimestampType (org.voltdb.types.TimestampType)11 BigDecimal (java.math.BigDecimal)10 VoltTable (org.voltdb.VoltTable)10 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Client (org.voltdb.client.Client)6 IOException (java.io.IOException)5 JSONString (org.json_voltpatches.JSONString)4 VoltType (org.voltdb.VoltType)3 ByteBuffer (java.nio.ByteBuffer)2 JSONObject (org.json_voltpatches.JSONObject)2 BBContainer (org.voltcore.utils.DBBPool.BBContainer)2 ColumnInfo (org.voltdb.VoltTable.ColumnInfo)2 NullCallback (org.voltdb.client.NullCallback)2 File (java.io.File)1 BigInteger (java.math.BigInteger)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1