@Test public void testCreateFromShape() { final int width = 2 * RADIUS; Shape shape = new Ellipse2D.Float(100, 200, width, width); KernelJAI shpKernel = KernelFactory.createFromShape( shape, null, KernelFactory.ValueType.BINARY, RADIUS, RADIUS, 1.0f); KernelJAI circleKernel = KernelFactory.createCircle(RADIUS); float[] shpData = shpKernel.getKernelData(); float[] circleData = circleKernel.getKernelData(); assertTrue(shpData.length == circleData.length); for (int i = 0; i < shpData.length; i++) { assertEquals(shpData[i], circleData[i], FTOL); } }
public static KernelJAI createFromShape(Shape shape, AffineTransform transform, ValueType type, int keyX, int keyY, float keyValue) { PathIterator iter = shape.getPathIterator(transform, 0.05); float[] buf = new float[6]; List<Coordinate> coords = CollectionFactory.list(); while (!iter.isDone()) { iter.currentSegment(buf); coords.add(new Coordinate(buf[0], buf[1])); iter.next(); } GeometryFactory gf = new GeometryFactory(); Coordinate[] coordsAr = coords.toArray(new Coordinate[coords.size()]); Geometry poly = gf.createPolygon(gf.createLinearRing(coordsAr), null); Envelope env = poly.getEnvelopeInternal(); int left = (int) Math.floor(env.getMinX()); int right = (int) Math.ceil(env.getMaxX()); int top = (int) Math.ceil(env.getMaxY()); int bottom = (int) Math.floor(env.getMinY()); int width = right - left + 1; int height = top - bottom + 1; float[] weights = new float[width * height]; int[] offset = new int[height]; for (int i = 0, o=0; i < offset.length; i++, o+=width) offset[i] = o; coords.clear(); double y = top; for (int iy = 0; iy < height; y--, iy++) { double x = left; for (int ix = 0; ix < width; x++, ix++) { coords.add(new Coordinate(x, y)); } } MultiPoint mp = gf.createMultiPoint(coords.toArray(new Coordinate[coords.size()])); Geometry inside = mp.intersection(poly.buffer(0.05, Math.max(width/2, 10))); final int n = inside.getNumGeometries(); double dmax = 0, dmax2 = 0; switch (type) { case COSINE: case EPANECHNIKOV: case GAUSSIAN: case QUARTIC: case TRIANGULAR: case TRIWEIGHT: for (int i = 0; i < n; i++) { Geometry g = inside.getGeometryN(i); Coordinate c = g.getCoordinate(); double d2 = Point2D.distanceSq(keyX, keyY, (int)c.x, (int)c.y); if (d2 > dmax2) { dmax2 = d2; } } dmax = Math.sqrt(dmax2); } double dist2 = 0; for (int i = 0; i < n; i++) { Geometry g = inside.getGeometryN(i); Coordinate c = g.getCoordinate(); int index = (int) c.x - left + offset[(int) c.y - bottom]; if (type != ValueType.BINARY) { dist2 = Point2D.distanceSq(keyX, keyY, (int)c.x, (int)c.y); } switch (type) { case BINARY: weights[index] = 1.0f; break; case COSINE: weights[index] = (float) (PI_ON_4 * Math.cos(PI_ON_2 * Math.sqrt(dist2) / dmax)); break; case DISTANCE: weights[index] = (float) Math.sqrt(dist2); break; case EPANECHNIKOV: weights[index] = (float) (3.0 * (1.0 - dist2 / dmax2) / 4.0); break; case GAUSSIAN: weights[index] = (float) (C_GAUSS * Math.exp(-0.5 * dist2 / dmax2)); break; case INVERSE_DISTANCE: weights[index] = (float) (1.0 / Math.sqrt(dist2)); break; case QUARTIC: double termq = 1.0 - dist2 / dmax2; weights[index] = (float) (C_QUARTIC * termq * termq); break; case TRIANGULAR: weights[index] = (float) (1.0 - Math.sqrt(dist2) / dmax); break; case TRIWEIGHT: double termt = 1.0 - dist2 / dmax2; weights[index] = (float) (C_TRIWEIGHT * termt * termt * termt); break; } } weights[keyX + offset[keyY]] = keyValue; return new KernelJAI(width, height, keyX, keyY, weights); }
KernelFactory { public static KernelJAI createFromShape(Shape shape, AffineTransform transform, ValueType type, int keyX, int keyY, float keyValue) { PathIterator iter = shape.getPathIterator(transform, 0.05); float[] buf = new float[6]; List<Coordinate> coords = CollectionFactory.list(); while (!iter.isDone()) { iter.currentSegment(buf); coords.add(new Coordinate(buf[0], buf[1])); iter.next(); } GeometryFactory gf = new GeometryFactory(); Coordinate[] coordsAr = coords.toArray(new Coordinate[coords.size()]); Geometry poly = gf.createPolygon(gf.createLinearRing(coordsAr), null); Envelope env = poly.getEnvelopeInternal(); int left = (int) Math.floor(env.getMinX()); int right = (int) Math.ceil(env.getMaxX()); int top = (int) Math.ceil(env.getMaxY()); int bottom = (int) Math.floor(env.getMinY()); int width = right - left + 1; int height = top - bottom + 1; float[] weights = new float[width * height]; int[] offset = new int[height]; for (int i = 0, o=0; i < offset.length; i++, o+=width) offset[i] = o; coords.clear(); double y = top; for (int iy = 0; iy < height; y--, iy++) { double x = left; for (int ix = 0; ix < width; x++, ix++) { coords.add(new Coordinate(x, y)); } } MultiPoint mp = gf.createMultiPoint(coords.toArray(new Coordinate[coords.size()])); Geometry inside = mp.intersection(poly.buffer(0.05, Math.max(width/2, 10))); final int n = inside.getNumGeometries(); double dmax = 0, dmax2 = 0; switch (type) { case COSINE: case EPANECHNIKOV: case GAUSSIAN: case QUARTIC: case TRIANGULAR: case TRIWEIGHT: for (int i = 0; i < n; i++) { Geometry g = inside.getGeometryN(i); Coordinate c = g.getCoordinate(); double d2 = Point2D.distanceSq(keyX, keyY, (int)c.x, (int)c.y); if (d2 > dmax2) { dmax2 = d2; } } dmax = Math.sqrt(dmax2); } double dist2 = 0; for (int i = 0; i < n; i++) { Geometry g = inside.getGeometryN(i); Coordinate c = g.getCoordinate(); int index = (int) c.x - left + offset[(int) c.y - bottom]; if (type != ValueType.BINARY) { dist2 = Point2D.distanceSq(keyX, keyY, (int)c.x, (int)c.y); } switch (type) { case BINARY: weights[index] = 1.0f; break; case COSINE: weights[index] = (float) (PI_ON_4 * Math.cos(PI_ON_2 * Math.sqrt(dist2) / dmax)); break; case DISTANCE: weights[index] = (float) Math.sqrt(dist2); break; case EPANECHNIKOV: weights[index] = (float) (3.0 * (1.0 - dist2 / dmax2) / 4.0); break; case GAUSSIAN: weights[index] = (float) (C_GAUSS * Math.exp(-0.5 * dist2 / dmax2)); break; case INVERSE_DISTANCE: weights[index] = (float) (1.0 / Math.sqrt(dist2)); break; case QUARTIC: double termq = 1.0 - dist2 / dmax2; weights[index] = (float) (C_QUARTIC * termq * termq); break; case TRIANGULAR: weights[index] = (float) (1.0 - Math.sqrt(dist2) / dmax); break; case TRIWEIGHT: double termt = 1.0 - dist2 / dmax2; weights[index] = (float) (C_TRIWEIGHT * termt * termt * termt); break; } } weights[keyX + offset[keyY]] = keyValue; return new KernelJAI(width, height, keyX, keyY, weights); } }
KernelFactory { public static KernelJAI createFromShape(Shape shape, AffineTransform transform, ValueType type, int keyX, int keyY, float keyValue) { PathIterator iter = shape.getPathIterator(transform, 0.05); float[] buf = new float[6]; List<Coordinate> coords = CollectionFactory.list(); while (!iter.isDone()) { iter.currentSegment(buf); coords.add(new Coordinate(buf[0], buf[1])); iter.next(); } GeometryFactory gf = new GeometryFactory(); Coordinate[] coordsAr = coords.toArray(new Coordinate[coords.size()]); Geometry poly = gf.createPolygon(gf.createLinearRing(coordsAr), null); Envelope env = poly.getEnvelopeInternal(); int left = (int) Math.floor(env.getMinX()); int right = (int) Math.ceil(env.getMaxX()); int top = (int) Math.ceil(env.getMaxY()); int bottom = (int) Math.floor(env.getMinY()); int width = right - left + 1; int height = top - bottom + 1; float[] weights = new float[width * height]; int[] offset = new int[height]; for (int i = 0, o=0; i < offset.length; i++, o+=width) offset[i] = o; coords.clear(); double y = top; for (int iy = 0; iy < height; y--, iy++) { double x = left; for (int ix = 0; ix < width; x++, ix++) { coords.add(new Coordinate(x, y)); } } MultiPoint mp = gf.createMultiPoint(coords.toArray(new Coordinate[coords.size()])); Geometry inside = mp.intersection(poly.buffer(0.05, Math.max(width/2, 10))); final int n = inside.getNumGeometries(); double dmax = 0, dmax2 = 0; switch (type) { case COSINE: case EPANECHNIKOV: case GAUSSIAN: case QUARTIC: case TRIANGULAR: case TRIWEIGHT: for (int i = 0; i < n; i++) { Geometry g = inside.getGeometryN(i); Coordinate c = g.getCoordinate(); double d2 = Point2D.distanceSq(keyX, keyY, (int)c.x, (int)c.y); if (d2 > dmax2) { dmax2 = d2; } } dmax = Math.sqrt(dmax2); } double dist2 = 0; for (int i = 0; i < n; i++) { Geometry g = inside.getGeometryN(i); Coordinate c = g.getCoordinate(); int index = (int) c.x - left + offset[(int) c.y - bottom]; if (type != ValueType.BINARY) { dist2 = Point2D.distanceSq(keyX, keyY, (int)c.x, (int)c.y); } switch (type) { case BINARY: weights[index] = 1.0f; break; case COSINE: weights[index] = (float) (PI_ON_4 * Math.cos(PI_ON_2 * Math.sqrt(dist2) / dmax)); break; case DISTANCE: weights[index] = (float) Math.sqrt(dist2); break; case EPANECHNIKOV: weights[index] = (float) (3.0 * (1.0 - dist2 / dmax2) / 4.0); break; case GAUSSIAN: weights[index] = (float) (C_GAUSS * Math.exp(-0.5 * dist2 / dmax2)); break; case INVERSE_DISTANCE: weights[index] = (float) (1.0 / Math.sqrt(dist2)); break; case QUARTIC: double termq = 1.0 - dist2 / dmax2; weights[index] = (float) (C_QUARTIC * termq * termq); break; case TRIANGULAR: weights[index] = (float) (1.0 - Math.sqrt(dist2) / dmax); break; case TRIWEIGHT: double termt = 1.0 - dist2 / dmax2; weights[index] = (float) (C_TRIWEIGHT * termt * termt * termt); break; } } weights[keyX + offset[keyY]] = keyValue; return new KernelJAI(width, height, keyX, keyY, weights); }  }
KernelFactory { public static KernelJAI createFromShape(Shape shape, AffineTransform transform, ValueType type, int keyX, int keyY, float keyValue) { PathIterator iter = shape.getPathIterator(transform, 0.05); float[] buf = new float[6]; List<Coordinate> coords = CollectionFactory.list(); while (!iter.isDone()) { iter.currentSegment(buf); coords.add(new Coordinate(buf[0], buf[1])); iter.next(); } GeometryFactory gf = new GeometryFactory(); Coordinate[] coordsAr = coords.toArray(new Coordinate[coords.size()]); Geometry poly = gf.createPolygon(gf.createLinearRing(coordsAr), null); Envelope env = poly.getEnvelopeInternal(); int left = (int) Math.floor(env.getMinX()); int right = (int) Math.ceil(env.getMaxX()); int top = (int) Math.ceil(env.getMaxY()); int bottom = (int) Math.floor(env.getMinY()); int width = right - left + 1; int height = top - bottom + 1; float[] weights = new float[width * height]; int[] offset = new int[height]; for (int i = 0, o=0; i < offset.length; i++, o+=width) offset[i] = o; coords.clear(); double y = top; for (int iy = 0; iy < height; y--, iy++) { double x = left; for (int ix = 0; ix < width; x++, ix++) { coords.add(new Coordinate(x, y)); } } MultiPoint mp = gf.createMultiPoint(coords.toArray(new Coordinate[coords.size()])); Geometry inside = mp.intersection(poly.buffer(0.05, Math.max(width/2, 10))); final int n = inside.getNumGeometries(); double dmax = 0, dmax2 = 0; switch (type) { case COSINE: case EPANECHNIKOV: case GAUSSIAN: case QUARTIC: case TRIANGULAR: case TRIWEIGHT: for (int i = 0; i < n; i++) { Geometry g = inside.getGeometryN(i); Coordinate c = g.getCoordinate(); double d2 = Point2D.distanceSq(keyX, keyY, (int)c.x, (int)c.y); if (d2 > dmax2) { dmax2 = d2; } } dmax = Math.sqrt(dmax2); } double dist2 = 0; for (int i = 0; i < n; i++) { Geometry g = inside.getGeometryN(i); Coordinate c = g.getCoordinate(); int index = (int) c.x - left + offset[(int) c.y - bottom]; if (type != ValueType.BINARY) { dist2 = Point2D.distanceSq(keyX, keyY, (int)c.x, (int)c.y); } switch (type) { case BINARY: weights[index] = 1.0f; break; case COSINE: weights[index] = (float) (PI_ON_4 * Math.cos(PI_ON_2 * Math.sqrt(dist2) / dmax)); break; case DISTANCE: weights[index] = (float) Math.sqrt(dist2); break; case EPANECHNIKOV: weights[index] = (float) (3.0 * (1.0 - dist2 / dmax2) / 4.0); break; case GAUSSIAN: weights[index] = (float) (C_GAUSS * Math.exp(-0.5 * dist2 / dmax2)); break; case INVERSE_DISTANCE: weights[index] = (float) (1.0 / Math.sqrt(dist2)); break; case QUARTIC: double termq = 1.0 - dist2 / dmax2; weights[index] = (float) (C_QUARTIC * termq * termq); break; case TRIANGULAR: weights[index] = (float) (1.0 - Math.sqrt(dist2) / dmax); break; case TRIWEIGHT: double termt = 1.0 - dist2 / dmax2; weights[index] = (float) (C_TRIWEIGHT * termt * termt * termt); break; } } weights[keyX + offset[keyY]] = keyValue; return new KernelJAI(width, height, keyX, keyY, weights); }  static KernelJAI createCircle(int radius); static KernelJAI createConstantCircle(int radius, float value); static KernelJAI createCircle(int radius, ValueType type, float centreValue); static KernelJAI createCircle(int radius, ValueType type); static KernelJAI createAnnulus(int outerRadius, int innerRadius, ValueType type, float centreValue); static KernelJAI createConstantAnnulus(int outerRadius, int innerRadius, float value); static KernelJAI createAnnulus(int outerRadius, int innerRadius, ValueType type); static KernelJAI createRectangle(int width, int height); static KernelJAI createConstantRectangle(int width, int height, float value); static KernelJAI createConstantRectangle(int width, int height,
            int keyX, int keyY,  float value); static KernelJAI createRectangle(
            int width, int height, ValueType type, int keyX, int keyY, float keyValue); static KernelJAI createRectangle(
            int width, int height, ValueType type, int keyX, int keyY); static KernelJAI createFromShape(Shape shape, AffineTransform transform, ValueType type, int keyX, int keyY, float keyValue); }
KernelFactory { public static KernelJAI createFromShape(Shape shape, AffineTransform transform, ValueType type, int keyX, int keyY, float keyValue) { PathIterator iter = shape.getPathIterator(transform, 0.05); float[] buf = new float[6]; List<Coordinate> coords = CollectionFactory.list(); while (!iter.isDone()) { iter.currentSegment(buf); coords.add(new Coordinate(buf[0], buf[1])); iter.next(); } GeometryFactory gf = new GeometryFactory(); Coordinate[] coordsAr = coords.toArray(new Coordinate[coords.size()]); Geometry poly = gf.createPolygon(gf.createLinearRing(coordsAr), null); Envelope env = poly.getEnvelopeInternal(); int left = (int) Math.floor(env.getMinX()); int right = (int) Math.ceil(env.getMaxX()); int top = (int) Math.ceil(env.getMaxY()); int bottom = (int) Math.floor(env.getMinY()); int width = right - left + 1; int height = top - bottom + 1; float[] weights = new float[width * height]; int[] offset = new int[height]; for (int i = 0, o=0; i < offset.length; i++, o+=width) offset[i] = o; coords.clear(); double y = top; for (int iy = 0; iy < height; y--, iy++) { double x = left; for (int ix = 0; ix < width; x++, ix++) { coords.add(new Coordinate(x, y)); } } MultiPoint mp = gf.createMultiPoint(coords.toArray(new Coordinate[coords.size()])); Geometry inside = mp.intersection(poly.buffer(0.05, Math.max(width/2, 10))); final int n = inside.getNumGeometries(); double dmax = 0, dmax2 = 0; switch (type) { case COSINE: case EPANECHNIKOV: case GAUSSIAN: case QUARTIC: case TRIANGULAR: case TRIWEIGHT: for (int i = 0; i < n; i++) { Geometry g = inside.getGeometryN(i); Coordinate c = g.getCoordinate(); double d2 = Point2D.distanceSq(keyX, keyY, (int)c.x, (int)c.y); if (d2 > dmax2) { dmax2 = d2; } } dmax = Math.sqrt(dmax2); } double dist2 = 0; for (int i = 0; i < n; i++) { Geometry g = inside.getGeometryN(i); Coordinate c = g.getCoordinate(); int index = (int) c.x - left + offset[(int) c.y - bottom]; if (type != ValueType.BINARY) { dist2 = Point2D.distanceSq(keyX, keyY, (int)c.x, (int)c.y); } switch (type) { case BINARY: weights[index] = 1.0f; break; case COSINE: weights[index] = (float) (PI_ON_4 * Math.cos(PI_ON_2 * Math.sqrt(dist2) / dmax)); break; case DISTANCE: weights[index] = (float) Math.sqrt(dist2); break; case EPANECHNIKOV: weights[index] = (float) (3.0 * (1.0 - dist2 / dmax2) / 4.0); break; case GAUSSIAN: weights[index] = (float) (C_GAUSS * Math.exp(-0.5 * dist2 / dmax2)); break; case INVERSE_DISTANCE: weights[index] = (float) (1.0 / Math.sqrt(dist2)); break; case QUARTIC: double termq = 1.0 - dist2 / dmax2; weights[index] = (float) (C_QUARTIC * termq * termq); break; case TRIANGULAR: weights[index] = (float) (1.0 - Math.sqrt(dist2) / dmax); break; case TRIWEIGHT: double termt = 1.0 - dist2 / dmax2; weights[index] = (float) (C_TRIWEIGHT * termt * termt * termt); break; } } weights[keyX + offset[keyY]] = keyValue; return new KernelJAI(width, height, keyX, keyY, weights); }  static KernelJAI createCircle(int radius); static KernelJAI createConstantCircle(int radius, float value); static KernelJAI createCircle(int radius, ValueType type, float centreValue); static KernelJAI createCircle(int radius, ValueType type); static KernelJAI createAnnulus(int outerRadius, int innerRadius, ValueType type, float centreValue); static KernelJAI createConstantAnnulus(int outerRadius, int innerRadius, float value); static KernelJAI createAnnulus(int outerRadius, int innerRadius, ValueType type); static KernelJAI createRectangle(int width, int height); static KernelJAI createConstantRectangle(int width, int height, float value); static KernelJAI createConstantRectangle(int width, int height,
            int keyX, int keyY,  float value); static KernelJAI createRectangle(
            int width, int height, ValueType type, int keyX, int keyY, float keyValue); static KernelJAI createRectangle(
            int width, int height, ValueType type, int keyX, int keyY); static KernelJAI createFromShape(Shape shape, AffineTransform transform, ValueType type, int keyX, int keyY, float keyValue);  }