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, ']');
}
}
}
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");
}
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;
}
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(",");
}
}
}
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);
}
}
Aggregations