Search in sources :

Example 1 with Circle

use of ca.nrc.cadc.caom2.types.Circle in project caom2db by opencadc.

the class PostgreSQLGenerator method safeSetShape.

/**
 * Store list of points value in a double[] column.
 *
 * @param sb
 * @param ps
 * @param col
 * @param val
 * @throws SQLException
 */
@Override
protected void safeSetShape(StringBuilder sb, PreparedStatement ps, int col, Shape val) throws SQLException {
    if (val == null) {
        ps.setObject(col, null);
        if (sb != null) {
            sb.append("null,");
        }
        return;
    }
    log.debug("[safeSetShape] in: " + val);
    if (val instanceof Polygon) {
        Polygon poly = (Polygon) val;
        // 2 numbers per point
        Double[] dval = new Double[2 * poly.getPoints().size()];
        int i = 0;
        for (Point p : ((Polygon) val).getPoints()) {
            dval[i++] = p.cval1;
            dval[i++] = p.cval2;
        }
        java.sql.Array arr = ps.getConnection().createArrayOf("float8", dval);
        ps.setObject(col, arr);
        if (sb != null) {
            sb.append("[");
            for (double d : dval) {
                sb.append(d).append(",");
            }
            // replace last comma with closing ]
            sb.setCharAt(sb.length() - 1, ']');
        }
        return;
    }
    if (val instanceof Circle) {
        Circle circ = (Circle) val;
        Double[] dval = new Double[3];
        dval[0] = val.getCenter().cval1;
        dval[1] = val.getCenter().cval2;
        dval[2] = circ.getRadius();
        java.sql.Array arr = ps.getConnection().createArrayOf("float8", dval);
        ps.setObject(col, arr);
        if (sb != null) {
            sb.append("[");
            for (double d : dval) {
                sb.append(d).append(",");
            }
            // replace last comma with closing ]
            sb.setCharAt(sb.length() - 1, ']');
        }
    }
}
Also used : Circle(ca.nrc.cadc.caom2.types.Circle) Point(ca.nrc.cadc.caom2.types.Point) MultiPolygon(ca.nrc.cadc.caom2.types.MultiPolygon) Polygon(ca.nrc.cadc.caom2.types.Polygon) PgSpoint(ca.nrc.cadc.dali.postgresql.PgSpoint) Point(ca.nrc.cadc.caom2.types.Point)

Example 2 with Circle

use of ca.nrc.cadc.caom2.types.Circle in project caom2db by opencadc.

the class PostgreSQLGenerator method getCircle.

@Override
protected Circle getCircle(ResultSet rs, int col) throws SQLException {
    double[] coords = Util.getDoubleArray(rs, col);
    if (coords == null) {
        return null;
    }
    if (coords.length == 3) {
        double cval1 = coords[0];
        double cval2 = coords[1];
        double rad = coords[2];
        Circle ret = new Circle(new Point(cval1, cval2), rad);
        log.debug("[getCircle] " + ret);
        return ret;
    }
    throw new IllegalStateException("array length " + coords.length + " invalid for Circle");
}
Also used : Circle(ca.nrc.cadc.caom2.types.Circle) Point(ca.nrc.cadc.caom2.types.Point)

Example 3 with Circle

use of ca.nrc.cadc.caom2.types.Circle in project caom2db by opencadc.

the class PostgreSQLGenerator method generatePolygonApproximation.

ca.nrc.cadc.dali.Polygon generatePolygonApproximation(Circle val, int numVerts) {
    if (numVerts < 4) {
        throw new IllegalArgumentException("number of vertices in approximation too small (min: 4)");
    }
    CartesianTransform trans = CartesianTransform.getTransform(val);
    Point cen = trans.transform(val.getCenter());
    double phi = 2.0 * Math.PI / ((double) numVerts);
    // compute distance to vertices so that the edges are tangent and circle is
    // inside the polygon
    double vdist = val.getRadius() / Math.cos(phi / 2.0);
    // log.info("phi = " + phi + " vdist=" + vdist);
    CartesianTransform inv = trans.getInverseTransform();
    ca.nrc.cadc.dali.Polygon ret = new ca.nrc.cadc.dali.Polygon();
    for (int i = 0; i < numVerts; i++) {
        double x = cen.cval1 + vdist * Math.cos(i * phi);
        double y = cen.cval2 + vdist * Math.sin(i * phi);
        Point p = new Point(x, y);
        p = inv.transform(p);
        ret.getVertices().add(new ca.nrc.cadc.dali.Point(p.cval1, p.cval2));
    }
    return ret;
}
Also used : CartesianTransform(ca.nrc.cadc.caom2.types.CartesianTransform) Point(ca.nrc.cadc.caom2.types.Point) MultiPolygon(ca.nrc.cadc.caom2.types.MultiPolygon) Polygon(ca.nrc.cadc.caom2.types.Polygon) PgSpoint(ca.nrc.cadc.dali.postgresql.PgSpoint) Point(ca.nrc.cadc.caom2.types.Point)

Example 4 with Circle

use of ca.nrc.cadc.caom2.types.Circle in project caom2db by opencadc.

the class PostgreSQLGenerator method safeSetShapeAsPolygon.

/**
 * Store polygon value in an spoly column.
 *
 * @param sb
 * @param ps
 * @param col
 * @param val
 * @throws SQLException
 */
@Override
protected void safeSetShapeAsPolygon(StringBuilder sb, PreparedStatement ps, int col, Shape val) throws SQLException {
    if (val == null) {
        ps.setObject(col, null);
        if (sb != null) {
            sb.append("null,");
        }
        return;
    }
    log.debug("[safeSetShapeAsPolygon] in: " + val);
    if (val instanceof Polygon) {
        Polygon vp = (Polygon) val;
        ca.nrc.cadc.dali.Polygon poly = new ca.nrc.cadc.dali.Polygon();
        for (Point p : vp.getPoints()) {
            poly.getVertices().add(new ca.nrc.cadc.dali.Point(p.cval1, p.cval2));
        }
        PgSpoly pgs = new PgSpoly();
        PGobject pgo = pgs.generatePolygon(poly);
        ps.setObject(col, pgo);
        if (sb != null) {
            sb.append(pgo.getValue());
            sb.append(",");
        }
        return;
    }
    if (val instanceof Circle) {
        Circle cv = (Circle) val;
        ca.nrc.cadc.dali.Polygon poly = generatePolygonApproximation(cv, 13);
        PgSpoly pgs = new PgSpoly();
        PGobject pgo = pgs.generatePolygon(poly);
        ps.setObject(col, pgo);
        if (sb != null) {
            sb.append(pgo.getValue());
            sb.append(",");
        }
    }
}
Also used : Circle(ca.nrc.cadc.caom2.types.Circle) PgSpoly(ca.nrc.cadc.dali.postgresql.PgSpoly) Point(ca.nrc.cadc.caom2.types.Point) MultiPolygon(ca.nrc.cadc.caom2.types.MultiPolygon) Polygon(ca.nrc.cadc.caom2.types.Polygon) PGobject(org.postgresql.util.PGobject)

Example 5 with Circle

use of ca.nrc.cadc.caom2.types.Circle in project caom2db by opencadc.

the class PostgreSQLGeneratorTest method testCircleToPolygonApproximatiom.

@Test
public void testCircleToPolygonApproximatiom() {
    try {
        Circle c = new Circle(new Point(12.0, 34.0), 1.0);
        double ca = c.getArea();
        double cs = c.getSize();
        for (int i = 4; i < 32; i += 2) {
            ca.nrc.cadc.dali.Polygon dpoly = gen.generatePolygonApproximation(c, i);
            List<Vertex> verts = new ArrayList<Vertex>();
            List<Point> points = new ArrayList<Point>();
            SegmentType t = SegmentType.MOVE;
            for (ca.nrc.cadc.dali.Point dp : dpoly.getVertices()) {
                points.add(new Point(dp.getLongitude(), dp.getLatitude()));
                verts.add(new Vertex(dp.getLongitude(), dp.getLatitude(), t));
                t = SegmentType.LINE;
            }
            verts.add(Vertex.CLOSE);
            MultiPolygon mp = new MultiPolygon(verts);
            Polygon poly = new Polygon(points, mp);
            double pa = poly.getArea();
            double ps = poly.getSize();
            double da = pa / ca;
            log.info("n=" + i + " poly: " + ps + " " + pa + " (" + da + ")");
        }
        log.info("circle: " + ca + " " + cs);
    } catch (Exception unexpected) {
        log.error("unexpected exception", unexpected);
        Assert.fail("unexpected exception: " + unexpected);
    }
}
Also used : Circle(ca.nrc.cadc.caom2.types.Circle) Vertex(ca.nrc.cadc.caom2.types.Vertex) ArrayList(java.util.ArrayList) Point(ca.nrc.cadc.caom2.types.Point) Point(ca.nrc.cadc.caom2.types.Point) SegmentType(ca.nrc.cadc.caom2.types.SegmentType) MultiPolygon(ca.nrc.cadc.caom2.types.MultiPolygon) MultiPolygon(ca.nrc.cadc.caom2.types.MultiPolygon) Polygon(ca.nrc.cadc.caom2.types.Polygon) Test(org.junit.Test)

Aggregations

Point (ca.nrc.cadc.caom2.types.Point)7 Circle (ca.nrc.cadc.caom2.types.Circle)6 MultiPolygon (ca.nrc.cadc.caom2.types.MultiPolygon)6 Polygon (ca.nrc.cadc.caom2.types.Polygon)6 Vertex (ca.nrc.cadc.caom2.types.Vertex)3 ObservationURI (ca.nrc.cadc.caom2.ObservationURI)2 PlaneURI (ca.nrc.cadc.caom2.PlaneURI)2 Interval (ca.nrc.cadc.caom2.types.Interval)2 SampledInterval (ca.nrc.cadc.caom2.types.SampledInterval)2 PgSpoint (ca.nrc.cadc.dali.postgresql.PgSpoint)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 Artifact (ca.nrc.cadc.caom2.Artifact)1 CustomAxis (ca.nrc.cadc.caom2.CustomAxis)1 DataQuality (ca.nrc.cadc.caom2.DataQuality)1 Energy (ca.nrc.cadc.caom2.Energy)1 EnergyBand (ca.nrc.cadc.caom2.EnergyBand)1 EnergyTransition (ca.nrc.cadc.caom2.EnergyTransition)1 Metrics (ca.nrc.cadc.caom2.Metrics)1 Observable (ca.nrc.cadc.caom2.Observable)1