+ if (enabled) {
+
+ int level = MapScalingTransform.zoomLevel(ot);
+
+// double smallerViewboxDimension = viewbox.getWidth() < viewbox.getHeight() ? viewbox.getWidth() * MAP_SCALE : viewbox.getHeight() * MAP_SCALE;
+// int level = 0;
+// double tileSize = 360 * MAP_SCALE*2.5;
+// while (level < MAX_TILE_LEVEL) {
+// double ratio = smallerViewboxDimension / tileSize;
+// if (ratio >= 0.85) {
+// break;
+// }
+// tileSize *= 0.5;
+// level++;
+// }
+ /*
+ * To convert y-coordinates to map coordinates in ruler, use:
+ * double val = (y-offsetY)/scaleY;
+ * val = Math.toDegrees(Math.atan(Math.sinh(Math.toRadians(val))));
+ * String str = formatValue(val);
+ */
+
+ double minx = Math.min(180, Math.max(viewbox.getMinX(), -180));
+ double maxx = Math.min(180, Math.max(viewbox.getMaxX(), -180));
+
+ double miny = Math.min(360, Math.max(viewbox.getMinY()+180, 0));
+ double maxy = Math.min(360, Math.max(viewbox.getMaxY()+180, 0));
+ //System.err.println("minx " + minx + " maxx " + maxx + " miny " + miny + " maxy " + maxy);
+ g2d.setTransform(new AffineTransform());
+ g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
+
+ int levels = (1 << level);
+ //System.err.println("level " + level);
+ //System.err.println("levels " + levels);
+ // http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
+ int left = (int)Math.floor( (minx + 180) / 360 * (1<<level) );
+ int right = (int)Math.floor( (maxx + 180) / 360 * (1<<level) );
+ int top = (int)Math.floor(miny / 360 * (1<<level));// (int)Math.floor( (1 - Math.log(Math.tan(Math.toRadians(miny)) + 1 / Math.cos(Math.toRadians(miny))) / Math.PI) / 2 * (1<<level) );
+ int bottom = (int)Math.floor(maxy / 360 * (1<<level));// (int)Math.floor( (1 - Math.log(Math.tan(Math.toRadians(maxy)) + 1 / Math.cos(Math.toRadians(maxy))) / Math.PI) / 2 * (1<<level) );
+ //System.err.println("left " + left + " right " + right + " bottom " + bottom + " top " + top);
+ double tsx = 360 / (double)levels; // Size of tile on zoom level
+ //System.err.println("tileSize " + tsx);
+ for (int tx = left; tx <= right; tx++) {
+ if (tx < 0 || tx >= levels)
+ continue;
+ for (int ty = top; ty <= bottom; ty++) {
+ if (ty < 0 || ty >= levels)
+ continue;
+ TileKey tile = new TileKey(level, tx, ty);
+ double y = (double) ty - (double) levels / 2; // In level 0 we have only one tile
+
+ paintTile(tileCache, g2d, tr, tile, tx * tsx - 180, y * tsx, tsx);
+ }