Search in sources :

Example 1 with Polygon

use of ca.nrc.cadc.caom2.types.Polygon 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 Polygon

use of ca.nrc.cadc.caom2.types.Polygon 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 3 with Polygon

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

the class PostgreSQLGenerator method safeSetSubIntervalList.

/**
 * Store a list of intervals in a polygon column.
 *
 * @param sb
 * @param ps
 * @param col
 * @param subs
 * @throws SQLException
 */
@Override
protected void safeSetSubIntervalList(StringBuilder sb, PreparedStatement ps, int col, List<Interval> subs) throws SQLException {
    if (subs == null || subs.isEmpty()) {
        ps.setObject(col, null);
        if (sb != null) {
            sb.append("null,");
        }
    } else {
        log.debug("[safeSetSubIntervalList] in: " + subs.size() + " Intervals");
        ca.nrc.cadc.dali.DoubleInterval[] dis = new ca.nrc.cadc.dali.DoubleInterval[subs.size()];
        int i = 0;
        for (Interval si : subs) {
            dis[i++] = new ca.nrc.cadc.dali.DoubleInterval(si.getLower(), si.getUpper());
        }
        PgInterval pgi = new PgInterval();
        PGpolygon poly = pgi.generatePolygon2D(dis);
        ps.setObject(col, poly);
        if (sb != null) {
            sb.append(poly.getValue());
            sb.append(",");
        }
    }
}
Also used : PGpolygon(org.postgresql.geometric.PGpolygon) PgInterval(ca.nrc.cadc.dali.postgresql.PgInterval) PgSpoint(ca.nrc.cadc.dali.postgresql.PgSpoint) Point(ca.nrc.cadc.caom2.types.Point) PgInterval(ca.nrc.cadc.dali.postgresql.PgInterval) Interval(ca.nrc.cadc.caom2.types.Interval) SampledInterval(ca.nrc.cadc.caom2.types.SampledInterval)

Example 4 with Polygon

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

the class ObservationValidator method doit.

private Progress doit() {
    Progress ret = new Progress();
    long t = System.currentTimeMillis();
    long timeState = -1;
    long timeQuery = -1;
    long timeTransaction = -1;
    try {
        // hint
        System.gc();
        t = System.currentTimeMillis();
        timeState = System.currentTimeMillis() - t;
        t = System.currentTimeMillis();
        log.info("getObservationList: " + src.getIdentifier());
        List<ObservationState> tmpSrcState = null;
        if (srcObservationDAO != null) {
            tmpSrcState = srcObservationDAO.getObservationList(src.getCollection(), null, null, null);
        } else if (srcObservationService != null) {
            tmpSrcState = srcObservationService.getObservationList(src.getCollection(), null, null, null);
        } else {
            throw new RuntimeException("BUG: both srcObservationDAO and srcObservationService are null");
        }
        log.info("found: " + tmpSrcState.size());
        Set<ObservationState> srcState = new TreeSet<>(compStateUri);
        srcState.addAll(tmpSrcState);
        // GC
        tmpSrcState = null;
        log.info("source set: " + srcState.size());
        log.info("getObservationList: " + dest.getIdentifier());
        List<ObservationState> tmpDstState = destObservationDAO.getObservationList(dest.getCollection(), null, null, null);
        log.info("found: " + tmpDstState.size());
        Set<ObservationState> dstState = new TreeSet<>(compStateUri);
        dstState.addAll(tmpDstState);
        // GC
        tmpDstState = null;
        log.info("destination set: " + dstState.size());
        Set<ObservationStateError> errlist = calculateErroneousObservationStates(srcState, dstState);
        log.info("discrepancies found: " + errlist.size());
        timeQuery = System.currentTimeMillis() - t;
        t = System.currentTimeMillis();
        List<SkippedWrapperURI<ObservationStateError>> entityListSrc = wrap(errlist);
        ret.found = srcState.size();
        ListIterator<SkippedWrapperURI<ObservationStateError>> iter = entityListSrc.listIterator();
        while (iter.hasNext()) {
            SkippedWrapperURI<ObservationStateError> ow = iter.next();
            ObservationStateError o = ow.entity;
            // allow garbage collection during loop
            iter.remove();
            String skipMsg = null;
            try {
                if (!dryrun) {
                    if (o != null) {
                        skipMsg = o.toString();
                        try {
                            log.debug("starting HarvestSkipURI transaction");
                            boolean putSkip = true;
                            HarvestSkipURI skip = harvestSkip.get(source, cname, o.getObs().getURI().getURI());
                            Date tryAfter = o.getObs().maxLastModified;
                            if (skip == null) {
                                skip = new HarvestSkipURI(source, cname, o.getObs().getURI().getURI(), tryAfter, skipMsg);
                                ret.added++;
                            } else {
                                // avoid lastModified update for no change
                                putSkip = false;
                                ret.known++;
                            }
                            if (destObservationDAO.getTransactionManager().isOpen()) {
                                throw new RuntimeException("BUG: found open trasnaction at start of next observation");
                            }
                            log.debug("starting transaction");
                            destObservationDAO.getTransactionManager().startTransaction();
                            // track the fail
                            if (putSkip) {
                                log.info("put: " + skip);
                                harvestSkip.put(skip);
                            } else {
                                log.info("known: " + skip);
                            }
                        } catch (Throwable oops) {
                            log.warn("failed to insert HarvestSkipURI", oops);
                            destObservationDAO.getTransactionManager().rollbackTransaction();
                            log.warn("rollback HarvestSkipURI: OK");
                        }
                    }
                    log.debug("committing transaction");
                    destObservationDAO.getTransactionManager().commitTransaction();
                    log.debug("commit: OK");
                }
                ret.mismatch++;
            } catch (Throwable oops) {
                String str = oops.toString();
                if (oops instanceof Error) {
                    log.error("FATAL - probably installation or environment", oops);
                } else if (oops instanceof NullPointerException) {
                    log.error("BUG", oops);
                } else if (oops instanceof BadSqlGrammarException) {
                    log.error("BUG", oops);
                    BadSqlGrammarException bad = (BadSqlGrammarException) oops;
                    SQLException sex1 = bad.getSQLException();
                    if (sex1 != null) {
                        log.error("CAUSE", sex1);
                        SQLException sex2 = sex1.getNextException();
                        log.error("NEXT CAUSE", sex2);
                    }
                } else if (oops instanceof DataAccessResourceFailureException) {
                    log.error("SEVERE PROBLEM - probably out of space in database", oops);
                } else if (oops instanceof DataIntegrityViolationException && str.contains("duplicate key value violates unique constraint \"i_observationuri\"")) {
                    log.error("CONTENT PROBLEM - duplicate observation: " + " " + o.getObs().getURI().getURI().toASCIIString());
                } else if (oops instanceof UncategorizedSQLException) {
                    if (str.contains("spherepoly_from_array")) {
                        log.error("UNDETECTED illegal polygon: " + o.getObs().getURI().getURI());
                    } else {
                        log.error("unexpected exception", oops);
                    }
                } else if (oops instanceof IllegalArgumentException && str.contains("CaomValidator") && str.contains("keywords")) {
                    log.error("CONTENT PROBLEM - invalid keywords: " + " " + o.getObs().getURI().getURI().toASCIIString());
                } else {
                    log.error("unexpected exception", oops);
                }
            }
        }
    } finally {
        timeTransaction = System.currentTimeMillis() - t;
        log.debug("time to get HarvestState: " + timeState + "ms");
        log.debug("time to run ObservationListQuery: " + timeQuery + "ms");
        log.debug("time to run transactions: " + timeTransaction + "ms");
    }
    return ret;
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) TreeSet(java.util.TreeSet) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) HarvestSkipURI(ca.nrc.cadc.caom2.harvester.state.HarvestSkipURI) Date(java.util.Date) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) ObservationState(ca.nrc.cadc.caom2.ObservationState)

Example 5 with Polygon

use of ca.nrc.cadc.caom2.types.Polygon 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)

Aggregations

Point (ca.nrc.cadc.caom2.types.Point)7 MultiPolygon (ca.nrc.cadc.caom2.types.MultiPolygon)6 Polygon (ca.nrc.cadc.caom2.types.Polygon)6 Circle (ca.nrc.cadc.caom2.types.Circle)5 ObservationURI (ca.nrc.cadc.caom2.ObservationURI)3 Interval (ca.nrc.cadc.caom2.types.Interval)3 SampledInterval (ca.nrc.cadc.caom2.types.SampledInterval)3 Vertex (ca.nrc.cadc.caom2.types.Vertex)3 PgSpoint (ca.nrc.cadc.dali.postgresql.PgSpoint)3 Artifact (ca.nrc.cadc.caom2.Artifact)2 ObservationState (ca.nrc.cadc.caom2.ObservationState)2 Plane (ca.nrc.cadc.caom2.Plane)2 PlaneURI (ca.nrc.cadc.caom2.PlaneURI)2 HarvestSkipURI (ca.nrc.cadc.caom2.harvester.state.HarvestSkipURI)2 URI (java.net.URI)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 CustomAxis (ca.nrc.cadc.caom2.CustomAxis)1 DataQuality (ca.nrc.cadc.caom2.DataQuality)1