1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.imagery;
|
---|
3 |
|
---|
4 | import java.util.function.Function;
|
---|
5 | import java.util.stream.IntStream;
|
---|
6 | import java.util.stream.Stream;
|
---|
7 |
|
---|
8 | import org.openstreetmap.gui.jmapviewer.TileXY;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * This is a rectangular range of tiles.
|
---|
12 | */
|
---|
13 | public class TileRange {
|
---|
14 | protected int minX;
|
---|
15 | protected int maxX;
|
---|
16 | protected int minY;
|
---|
17 | protected int maxY;
|
---|
18 | protected int zoom;
|
---|
19 |
|
---|
20 | protected TileRange() {
|
---|
21 | }
|
---|
22 |
|
---|
23 | protected TileRange(TileXY t1, TileXY t2, int zoom) {
|
---|
24 | minX = (int) Math.floor(Math.min(t1.getX(), t2.getX()));
|
---|
25 | minY = (int) Math.floor(Math.min(t1.getY(), t2.getY()));
|
---|
26 | maxX = (int) Math.ceil(Math.max(t1.getX(), t2.getX()));
|
---|
27 | maxY = (int) Math.ceil(Math.max(t1.getY(), t2.getY()));
|
---|
28 | this.zoom = zoom;
|
---|
29 | }
|
---|
30 |
|
---|
31 | protected double tilesSpanned() {
|
---|
32 | return Math.sqrt(1.0 * this.size());
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Returns size
|
---|
37 | * @return size
|
---|
38 | */
|
---|
39 | public int size() {
|
---|
40 | int xSpan = maxX - minX + 1;
|
---|
41 | int ySpan = maxY - minY + 1;
|
---|
42 | return xSpan * ySpan;
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Gets a stream of all tile positions in this set
|
---|
47 | * @return A stream of all positions
|
---|
48 | */
|
---|
49 | public Stream<TilePosition> tilePositions() {
|
---|
50 | if (zoom == 0) {
|
---|
51 | return Stream.empty();
|
---|
52 | } else {
|
---|
53 | return IntStream.rangeClosed(minX, maxX).mapToObj(
|
---|
54 | x -> IntStream.rangeClosed(minY, maxY).mapToObj(y -> new TilePosition(x, y, zoom))
|
---|
55 | ).flatMap(Function.identity());
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|