]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/save/MapGrouter.java
Version 20.4, May 2021
[GpsPrune.git] / src / tim / prune / save / MapGrouter.java
1 package tim.prune.save;
2
3 import tim.prune.data.DoubleRange;
4 import tim.prune.data.Track;
5 import tim.prune.data.TrackExtents;
6 import tim.prune.gui.map.MapSource;
7 import tim.prune.gui.map.MapTileManager;
8 import tim.prune.gui.map.TileConsumer;
9
10 import java.awt.Color;
11 import java.awt.Graphics;
12 import java.awt.Image;
13 import java.awt.image.BufferedImage;
14
15
16 /**
17  * Class to handle the sticking together (grouting) of map tiles
18  * to create a single map image for the current track
19  */
20 public class MapGrouter implements TileConsumer
21 {
22         /** The most recently produced image */
23         private GroutedImage _lastGroutedImage = null;
24
25         /**
26          * Clear the last image, it's not needed any more
27          */
28         public void clearMapImage() {
29                 _lastGroutedImage = null;
30         }
31
32         /**
33          * Grout the required map tiles together according to the track's extent
34          * @param inTrack track object
35          * @param inMapSource map source to use (may have one or two layers)
36          * @param inZoom selected zoom level
37          * @return grouted image, or null if no image could be created
38          */
39         public GroutedImage createMapImage(Track inTrack, MapSource inMapSource, int inZoom)
40         {
41                 return createMapImage(inTrack, inMapSource, inZoom, false);
42         }
43
44         /**
45          * Grout the required map tiles together according to the track's extent
46          * @param inTrack track object
47          * @param inMapSource map source to use (may have one or two layers)
48          * @param inZoom selected zoom level
49          * @param inDownload true to download tiles, false (by default) to just pull from disk
50          * @return grouted image, or null if no image could be created
51          */
52         public GroutedImage createMapImage(Track inTrack, MapSource inMapSource, int inZoom, boolean inDownload)
53         {
54                 // Get the extents of the track including a standard (10%) border around the data
55                 TrackExtents extents = new TrackExtents(inTrack);
56                 extents.applySquareBorder();
57                 DoubleRange xRange = extents.getXRange();
58                 DoubleRange yRange = extents.getYRange();
59
60                 // Work out which tiles are required
61                 final int zoomFactor = 1 << inZoom;
62                 final int minTileX = (int) (xRange.getMinimum() * zoomFactor);
63                 final int maxTileX = (int) (xRange.getMaximum() * zoomFactor);
64                 final int minTileY = (int) (yRange.getMinimum() * zoomFactor);
65                 final int maxTileY = (int) (yRange.getMaximum() * zoomFactor);
66
67                 // Work out how big the final image will be, create a BufferedImage
68                 final int pixCount = (int) (extents.getXRange().getRange() * zoomFactor * 256);
69                 if (pixCount < 2 || inZoom == 0) {return null;}
70                 BufferedImage resultImage = new BufferedImage(pixCount, pixCount, BufferedImage.TYPE_INT_RGB);
71                 Graphics g = resultImage.getGraphics();
72                 g.setColor(Color.WHITE);
73                 g.fillRect(0, 0, pixCount, pixCount);
74                 // Work out where to start drawing the tiles on the image
75                 int xOffset = (int) ((minTileX - xRange.getMinimum() * zoomFactor) * 256);
76
77                 // Make a map tile manager to load (or download) the tiles
78                 MapTileManager tileManager = new MapTileManager(this);
79                 tileManager.setMapSource(inMapSource);
80                 tileManager.enableTileDownloading(inDownload);
81                 tileManager.setReturnIncompleteImages();
82                 tileManager.setZoom(inZoom);
83
84                 int numTilesUsed = 0;
85                 int numTilesMissing = 0;
86
87                 // Loop over the tiles
88                 for (int x = minTileX; x <= maxTileX; x++)
89                 {
90                         int yOffset = (int) ((minTileY - yRange.getMinimum() * zoomFactor) * 256);
91                         for (int y = minTileY; y <= maxTileY; y++)
92                         {
93                                 for (int layer=0; layer < inMapSource.getNumLayers(); layer++)
94                                 {
95                                         Image tile = tileManager.getTile(layer, x, y, true);
96                                         // If we're downloading tiles, wait until the tile isn't null
97                                         int waitCount = 0;
98                                         while (tile == null && inDownload && waitCount < 3)
99                                         {
100                                                 // System.out.println("wait " + waitCount + " for tile to be not null");
101                                                 try {Thread.sleep(250);} catch (InterruptedException e) {}
102                                                 tile = tileManager.getTile(layer, x, y, false); // don't request another download
103                                                 waitCount++;
104                                         }
105                                         // See if there's a tile or not
106                                         if (tile != null)
107                                         {
108                                                 // Wait until tile is available (loaded asynchronously)
109                                                 while (tile.getWidth(null) < 0 && !inDownload)
110                                                 {
111                                                         try {
112                                                                 Thread.sleep(100);
113                                                         }
114                                                         catch (InterruptedException ie) {}
115                                                 }
116                                                 numTilesUsed++;
117                                                 g.drawImage(tile, xOffset, yOffset, null);
118                                         }
119                                         else
120                                         {
121                                                 // null tile, that means it's either not available or really slow to start downloading
122                                                 numTilesMissing++;
123                                         }
124                                 }
125                                 yOffset += 256;
126                         }
127                         xOffset += 256;
128                 }
129                 // Get rid of the image if it's empty
130                 if (numTilesUsed == 0) {
131                         resultImage = null;
132                 }
133                 // Store the xy limits in the GroutedImage to make it easier to draw on top
134                 GroutedImage result = new GroutedImage(resultImage, numTilesUsed, numTilesMissing);
135                 result.setXRange(xRange);
136                 result.setYRange(yRange);
137                 return result;
138         }
139
140         /**
141          * Get the grouted map image, using the previously-created one if available
142          * @param inTrack track object
143          * @param inMapSource map source to use (may have one or two layers)
144          * @param inZoom selected zoom level
145          * @return grouted image, or null if no image could be created
146          */
147         public synchronized GroutedImage getMapImage(Track inTrack, MapSource inMapSource, int inZoom)
148         {
149                 if (_lastGroutedImage == null) {
150                         _lastGroutedImage = createMapImage(inTrack, inMapSource, inZoom);
151                 }
152                 return _lastGroutedImage;
153         }
154
155         /**
156          * @param inTrack track object
157          * @param inZoom selected zoom level
158          * @return true if the image size is acceptable
159          */
160         public static boolean isZoomLevelOk(Track inTrack, int inZoom)
161         {
162                 // Get the extents of the track including a standard (10%) border around the data
163                 TrackExtents extents = new TrackExtents(inTrack);
164                 extents.applySquareBorder();
165
166                 // Work out how big the final image will be
167                 final int zoomFactor = 1 << inZoom;
168                 final int pixCount = (int) (extents.getXRange().getRange() * zoomFactor * 256);
169                 return pixCount > 2 && pixCount < 4000;
170         }
171
172         /** React to tiles being updated by the tile manager */
173         public void tilesUpdated(boolean inIsOk)
174         {
175                 // Doesn't need any action
176         }
177
178         /** React to cache problem */
179         public void reportCacheFailure()
180         {
181                 // Doesn't need any action
182         }
183 }