/** * Converts from a string to a bin index. This takes in strings of exactly * the form output by {@link #toString()} - i.e., formatted with "[%d, %d]" */ public static BinIndex fromString (String string) { try { int a = string.indexOf('[')+1; int b = string.indexOf(',', a); int x = Integer.parseInt(string.substring(a, b).trim()); a = b+1; b = string.indexOf(']', a); int y = Integer.parseInt(string.substring(a+1, b).trim()); return new BinIndex(x, y); } catch (NumberFormatException e) { return null; } catch (IndexOutOfBoundsException e) { return null; } } }
@Override public TileAndBinIndices next () { int tileX = (int) Math.floor(_curBinX/_numXBins); int tileY = (int) Math.floor(_curBinY/_numYBins); TileIndex tile = new TileIndex(_level, tileX, tileY, _numXBins, _numYBins); int binX = _curBinX-_numXBins*tileX; int binY = _curBinY-_numYBins*tileY; BinIndex bin = new BinIndex(binX, _numYBins-1-binY); ++_curBinX; if (_curBinX > _maxBinX) { _curBinX = _minBinX; ++_curBinY; } return new TileAndBinIndices(tile, bin); }
@Override public BinIndex rootToBin (Point2D point, TileIndex tile) { Point2D tileMercator = rootToTileMercator(point, tile.getLevel()); return new BinIndex((int) Math.floor((tileMercator.getX()-tile.getX())*tile.getXBins()), tile.getYBins()-1-(int) Math.floor((tileMercator.getY()-tile.getY())*tile.getYBins())); }
@Override public BinIndex rootToBin (double x, double y, TileIndex tile) { Point2D tileMercator = rootToTileMercator(x, y, tile.getLevel()); return new BinIndex((int) Math.floor((tileMercator.getX()-tile.getX())*tile.getXBins()), tile.getYBins()-1-(int) Math.floor((tileMercator.getY()-tile.getY())*tile.getYBins())); }
@Override public BinIndex rootToBin (double x, double y, TileIndex tile) { long numDivs = 1L << tile.getLevel(); int binX = (int) Math.floor((numDivs*(x-_minX)*_recipDiffX - tile.getX())*tile.getXBins()); int binY = (int) Math.floor((numDivs*(y-_minY)*_recipDiffY - tile.getY())*tile.getYBins()); // int pow2 = 1 << tile.getLevel(); // double tileXSize = (_maxX-_minX)/pow2; // double tileYSize = (_maxY-_minY)/pow2; // // double xInTile = x-_minX - tile.getX()*tileXSize; // double yInTile = y-_minY - tile.getY()*tileYSize; // // int binX = (int) Math.floor(xInTile*tile.getXBins()/tileXSize); // int binY = (int) Math.floor(yInTile*tile.getYBins()/tileYSize); return new BinIndex(binX, tile.getYBins()-1-binY); }
@Test public void testBinComparison () { BinIndex b0 = new BinIndex(0, 0); BinIndex b1 = new BinIndex(1, 0); Assert.assertTrue(-1 == b0.compareTo(b1)); Assert.assertTrue(1 == b1.compareTo(b0)); b0 = new BinIndex(0, 0); b1 = new BinIndex(0, 1); Assert.assertTrue(-1 == b0.compareTo(b1)); Assert.assertTrue(1 == b1.compareTo(b0)); b0 = new BinIndex(0, 0); b1 = new BinIndex(0, 0); Assert.assertTrue(0 == b0.compareTo(b1)); Assert.assertTrue(0 == b1.compareTo(b0)); bins.add(new BinIndex(2, 1)); bins.add(new BinIndex(2, 0)); bins.add(new BinIndex(0, 1)); bins.add(new BinIndex(1, 1)); bins.add(new BinIndex(1, 2)); bins.add(new BinIndex(1, 0)); bins.add(new BinIndex(0, 2)); bins.add(new BinIndex(0, 0)); bins.add(new BinIndex(2, 2)); Assert.assertEquals(new BinIndex(0, 0), bins.get(0)); Assert.assertEquals(new BinIndex(1, 0), bins.get(1)); Assert.assertEquals(new BinIndex(2, 0), bins.get(2));
tileTop = (pow2 - tileY - 1) * yBins; BinIndex tileBin = new BinIndex(uniBinX - tileLeft, uniBinY - tileTop); TileIndex tile = new TileIndex(level, tileX, tileY, xBins, yBins);
static public JSONObject tileToJSON( AnnotationTile tile ) { JSONObject tileJSON = new JSONObject(); try { tileJSON.put("level", tile.getDefinition().getLevel() ); tileJSON.put("x", tile.getDefinition().getX() ); tileJSON.put("y", tile.getDefinition().getY() ); for (int i=0; i<tile.getDefinition().getXBins(); i++ ) { for (int j=0; j<tile.getDefinition().getYBins(); j++ ) { AnnotationBin bin = tile.getBin(i, j); if ( bin != null) { // add bin object to tile tileJSON.put( new BinIndex(i, j).toString(), binToJSON( bin ) ); } } } } catch ( Exception e ) { e.printStackTrace(); } return tileJSON; }
@Test public void testBoundingBoxDirection () { TileIndex tile = new TileIndex(5, 23, 17); BinIndex bin = new BinIndex(34, 78); Rectangle2D bounds = _mercator.getBinBounds(tile, bin); Assert.assertTrue(bounds.getMaxX() > bounds.getMinX()); Assert.assertTrue(bounds.getMaxY() > bounds.getMinY()); }
@Test public void testBoundingBoxes () { TileIndex tile = new TileIndex(10, 14, 6, 256, 256); Rectangle2D tileBounds = _mercator.getTileBounds(tile); BinIndex bll = new BinIndex(0, 255); BinIndex bur = new BinIndex(255, 0); Rectangle2D llBounds = _mercator.getBinBounds(tile, bll); Rectangle2D urBounds = _mercator.getBinBounds(tile, bur); Assert.assertEquals(llBounds.getMinX(), tileBounds.getMinX(), EPSILON); Assert.assertEquals(urBounds.getMaxX(), tileBounds.getMaxX(), EPSILON); Assert.assertEquals(llBounds.getMinY(), tileBounds.getMinY(), EPSILON); Assert.assertEquals(urBounds.getMaxY(), tileBounds.getMaxY(), EPSILON); }
@Test public void testSparseTileIterator () { SparseTileData<Integer> tile = new SparseTileData<>(new TileIndex(0, 0, 0)); tile.setBin(20, 2, 12); tile.setBin(10, 1, 3); tile.setBin(10, 5, 2); tile.setBin(120, 25, -2); tile.setBin(10, 4, 1); tile.setBin(120, 10, 10); tile.setBin(120, 9, 11); Iterator<Pair<BinIndex, Integer>> i = tile.getData(); Assert.assertEquals(new Pair<BinIndex, Integer>(new BinIndex(10, 1), 3), i.next()); Assert.assertEquals(new Pair<BinIndex, Integer>(new BinIndex(10, 4), 1), i.next()); Assert.assertEquals(new Pair<BinIndex, Integer>(new BinIndex(10, 5), 2), i.next()); Assert.assertEquals(new Pair<BinIndex, Integer>(new BinIndex(20, 2), 12), i.next()); Assert.assertEquals(new Pair<BinIndex, Integer>(new BinIndex(120, 9), 11), i.next()); Assert.assertEquals(new Pair<BinIndex, Integer>(new BinIndex(120, 10), 10), i.next()); Assert.assertEquals(new Pair<BinIndex, Integer>(new BinIndex(120, 25), -2), i.next()); Assert.assertFalse(i.hasNext()); }
private void testTile (TileIndex tile, int binX, int binY, int univX, int univY) { BinIndex tileBin = new BinIndex(binX, binY); BinIndex universalBin = new BinIndex(univX, univY); Assert.assertEquals(universalBin, TileIndex.tileBinIndexToUniversalBinIndex(tile, tileBin)); Assert.assertEquals(new TileAndBinIndices(tile, tileBin), TileIndex.universalBinIndexToTileBinIndex(tile, universalBin)); } @Test
private Point2D getBinCenter (TilePyramid binner, int level, int tileX, int tileY, int binX, int binY) { TileIndex tile = new TileIndex(level, tileX, tileY); BinIndex bin = new BinIndex(binX, binY); Rectangle2D area = binner.getBinBounds(tile, bin); return new Point2D.Double(area.getCenterX(), area.getCenterY()); }
for (int x=250; x<256; ++x) { for (int y=5; y>=0; --y) { expectedResults.add(new TileAndBinIndices(tile, new BinIndex(x, y))); for (int x=0; x<=5; ++x) { for (int y=5; y>=0; --y) { expectedResults.add(new TileAndBinIndices(tile, new BinIndex(x, y))); for (int x=250; x<256; ++x) { for (int y=255; y>=250; --y) { expectedResults.add(new TileAndBinIndices(tile, new BinIndex(x, y))); for (int x=0; x<=5; ++x) { for (int y=255; y>=250; --y) { expectedResults.add(new TileAndBinIndices(tile, new BinIndex(x, y)));
BinIndex bin = new BinIndex(127+x, 127+y); Rectangle2D bounds = mercator.getBinBounds(tile, bin); int reps = Math.max(0, 16-((x*x+y*y))); BinIndex xbin = new BinIndex(131+n, 127); BinIndex ybin = new BinIndex(127, 131+n); Rectangle2D xbounds = mercator.getBinBounds(tile, xbin); Rectangle2D ybounds = mercator.getBinBounds(tile, ybin);
/** * Translates from a bin relative to the root position of this tile, to a * bin relative to the root position of the entire data set. * * @param tile * The tile in which this bin falls * @param bin * The bin relative to the root position of this tile (with * coordinates [0 to getXBins(), 0 to getYBins()]) * @return The bin relative to the root position of the entire data set * (with coordinates [0 to getXBins()*2^level, 0 to * getYBins()*2^level]) */ public static BinIndex tileBinIndexToUniversalBinIndex (TileIndex tile, BinIndex bin) { // Tiles go from lower left to upper right // Bins go from upper left to lower right int pow2 = 1 << tile.getLevel(); int tileLeft, tileTop; int xBins = tile.getXBins(); tileLeft = tile.getX() * xBins; int yBins = tile.getYBins(); tileTop = (pow2 - tile.getY() - 1) * yBins; return new BinIndex(tileLeft + bin.getX(), tileTop + bin.getY()); }
@Test public void testBinStringification () { BinIndex b = new BinIndex(23, 41); String bs = b.toString(); Assert.assertEquals("[23, 41]", bs); BinIndex b2 = BinIndex.fromString(bs); Assert.assertEquals(b, b2); }
tileTop = (pow2 - tileY - 1) * yBins; BinIndex tileBin = new BinIndex(bin.getX() - tileLeft, bin.getY() - tileTop); TileIndex tile = new TileIndex(level, tileX, tileY, xBins, yBins);
List<Integer> expected = topTile.getBin(x, y); List<Integer> actual = null; BinIndex baseTopUBin = TileIndex.tileBinIndexToUniversalBinIndex(topIdx, new BinIndex(x, y)); BinIndex baseUBin = new BinIndex(baseTopUBin.getX() * lf, baseTopUBin.getY() * lf); TileIndex baseTile = new TileIndex(b, 0, 0); for (int rx = 0; rx < lf; ++rx) { for (int ry = 0; ry < lf; ++ry) { BinIndex bin = new BinIndex(baseUBin.getX() + rx, baseUBin.getY() + ry); TileAndBinIndices tbi = TileIndex.universalBinIndexToTileBinIndex(baseTile, bin); TileData<List<Integer>> subTile = allTiles.get(b).get(tbi.getTile());
@Test public void testRoundTripTransformation () { for (int level=0; level<4; ++level) { int pow2 = 1 << level; for (int x = 0; x<pow2; ++x) { for (int y=0; y<pow2; ++y) { TileIndex t0 = new TileIndex(level, x, y); Rectangle2D tileBounds = _mercator.getTileBounds(t0); TileIndex t1 = _mercator.rootToTile(getCenter(tileBounds), level); Assert.assertEquals(t0, t1); for (int dx=0; dx<t0.getXBins(); ++dx) { for (int dy=0; dy<t0.getYBins(); ++dy) { BinIndex b0 = new BinIndex(dx, dy); Rectangle2D binBounds = _mercator.getBinBounds(t0, b0); BinIndex b1 = _mercator.rootToBin(getCenter(binBounds), t0); Assert.assertEquals(b0, b1); } } } } } }