|
18 | 18 | import de.neufeld.gis.core.GisMap;
|
19 | 19 | import de.neufeld.gis.core.MapDataProvider;
|
20 | 20 | import de.neufeld.gis.core.MapDataProviderRegistry;
|
21 |
| -import de.neufeld.gis.core.MapDataUIProvider; |
22 | 21 | import de.neufeld.gis.core.SearchQuery;
|
23 | 22 | import de.neufeld.gis.core.SearchResult;
|
24 | 23 |
|
25 | 24 | public class OSMProvider implements MapDataProvider {
|
26 |
| - |
27 |
| - public static final String NAME="OSM_Provider"; |
28 |
| - |
29 |
| - private String url = "http://tile.openstreetmap.org/$path$.png"; |
30 |
| - private final double TILE_SIZE=256; |
31 |
| - private final double M_PER_PIXEL_ZOOM_0=156543.034; |
32 |
| - private final double M_PER_TILE_ZOOM_0=M_PER_PIXEL_ZOOM_0*TILE_SIZE; |
33 |
| - |
| 25 | + |
| 26 | + public static final String NAME = "OSM_Provider"; |
| 27 | + private final double TILE_SIZE = 256; |
| 28 | + private final double M_PER_PIXEL_ZOOM_0 = 156543.034; |
| 29 | + private final double M_PER_TILE_ZOOM_0 = M_PER_PIXEL_ZOOM_0 * TILE_SIZE; |
| 30 | + |
34 | 31 | private LogService logService;
|
35 | 32 | private MapDataProviderRegistry mapDataProviderRegistry;
|
36 | 33 |
|
@@ -63,95 +60,116 @@ public void shutdown() {
|
63 | 60 | @Override
|
64 | 61 | public Image draw(GisMap gisMap, IGraphics graphics,
|
65 | 62 | DrawParameter drawParameter) {
|
66 |
| - |
| 63 | + |
67 | 64 | ForkJoinPool forkJoinPool = new ForkJoinPool();
|
68 |
| - return forkJoinPool.invoke(new ImageTask(drawParameter,graphics)); |
| 65 | + return forkJoinPool.invoke(new ImageTask(gisMap, drawParameter, |
| 66 | + graphics)); |
69 | 67 | }
|
70 |
| - private class ImageTask extends RecursiveTask<Image>{ |
| 68 | + |
| 69 | + private class ImageTask extends RecursiveTask<Image> { |
71 | 70 | private DrawParameter drawParameter;
|
72 | 71 | private IGraphics graphics;
|
73 |
| - private ImageTask(DrawParameter drawParameter,IGraphics graphics){ |
74 |
| - this.drawParameter=drawParameter; |
75 |
| - this.graphics=graphics; |
| 72 | + private GisMap gisMap; |
| 73 | + |
| 74 | + private ImageTask(GisMap gisMap, DrawParameter drawParameter, |
| 75 | + IGraphics graphics) { |
| 76 | + this.drawParameter = drawParameter; |
| 77 | + this.graphics = graphics; |
| 78 | + this.gisMap = gisMap; |
76 | 79 | }
|
| 80 | + |
77 | 81 | @Override
|
78 | 82 | protected Image compute() {
|
79 |
| - Rectangle visibleArea=drawParameter.getVisibleArea().getBounds(); |
80 |
| - double numTiles=Math.pow(2, drawParameter.getZoomLevel()); |
81 |
| - double tileWidth=M_PER_TILE_ZOOM_0/numTiles; |
82 |
| - double currentX=visibleArea.getX()/tileWidth; |
83 |
| - double currentY=visibleArea.getY()/tileWidth; |
84 |
| - int tilesX=(int)Math.floor(currentX); |
85 |
| - int tilesY=(int)Math.floor(currentY); |
86 |
| - |
87 |
| - double offSetX=(tilesX-currentX)*TILE_SIZE; |
88 |
| - double offSetY=(tilesY-currentY)*TILE_SIZE; |
89 |
| - |
90 |
| - int numTilesX=(int)Math.ceil(visibleArea.getWidth()/tileWidth); |
91 |
| - int numTilesY=(int)Math.ceil(visibleArea.getHeight()/tileWidth); |
92 |
| - |
| 83 | + Rectangle visibleArea = drawParameter.getVisibleArea().getBounds(); |
| 84 | + double numTiles = Math.pow(2, drawParameter.getZoomLevel()); |
| 85 | + double tileWidth = M_PER_TILE_ZOOM_0 / numTiles; |
| 86 | + double currentX = visibleArea.getX() / tileWidth; |
| 87 | + double currentY = visibleArea.getY() / tileWidth; |
| 88 | + int tilesX = (int) Math.floor(currentX); |
| 89 | + int tilesY = (int) Math.floor(currentY); |
| 90 | + |
| 91 | + double offSetX = (tilesX - currentX) * TILE_SIZE; |
| 92 | + double offSetY = (tilesY - currentY) * TILE_SIZE; |
| 93 | + |
| 94 | + int numTilesX = (int) Math.ceil(visibleArea.getWidth() / tileWidth); |
| 95 | + int numTilesY = (int) Math |
| 96 | + .ceil(visibleArea.getHeight() / tileWidth); |
| 97 | + |
93 | 98 | List<RecursiveTask<ImageOffsetPair>> forks = new LinkedList<>();
|
94 |
| - |
95 |
| - for(int i=0;i<=numTilesX;i++){ |
96 |
| - for(int j=0;j<=numTilesY;j++){ |
97 |
| - ImageRetrieveTask task=new ImageRetrieveTask((int)drawParameter.getZoomLevel(),offSetX,offSetY,tilesX,tilesY,i,j); |
| 99 | + |
| 100 | + for (int i = 0; i <= numTilesX; i++) { |
| 101 | + for (int j = 0; j <= numTilesY; j++) { |
| 102 | + ImageRetrieveTask task = new ImageRetrieveTask( |
| 103 | + (String) gisMap.getProviderSpecificData(), |
| 104 | + (int) drawParameter.getZoomLevel(), offSetX, |
| 105 | + offSetY, tilesX, tilesY, i, j); |
98 | 106 | forks.add(task);
|
99 |
| - task.fork(); |
| 107 | + task.fork(); |
100 | 108 | }
|
101 | 109 | }
|
102 | 110 | for (RecursiveTask<ImageOffsetPair> task : forks) {
|
103 |
| - ImageOffsetPair pair=task.join(); |
| 111 | + ImageOffsetPair pair = task.join(); |
104 | 112 | graphics.pushState().translate(pair.offsetX, pair.offsetY);
|
105 |
| - |
| 113 | + |
106 | 114 | graphics.paint(pair.image);
|
107 | 115 | graphics.popState();
|
108 |
| - } |
109 |
| - |
110 |
| - |
| 116 | + } |
| 117 | + |
111 | 118 | return null;
|
112 | 119 | }
|
113 |
| - |
| 120 | + |
114 | 121 | }
|
115 |
| - |
116 |
| - private class ImageRetrieveTask extends RecursiveTask<ImageOffsetPair>{ |
| 122 | + |
| 123 | + private class ImageRetrieveTask extends RecursiveTask<ImageOffsetPair> { |
117 | 124 | final int zoomLevel;
|
118 |
| - final double offsetX;final double offsetY;final int tileX;final int tileY;final int moveX;final int moveY; |
119 |
| - public ImageRetrieveTask(int zoomLevel,double offsetX,double offsetY,int tileX,int tileY,int moveX,int moveY){ |
120 |
| - this.zoomLevel=zoomLevel; |
121 |
| - this.offsetX=offsetX; |
122 |
| - this.offsetY=offsetY; |
123 |
| - this.tileX=tileX; |
124 |
| - this.tileY=tileY; |
125 |
| - this.moveX=moveX; |
126 |
| - this.moveY=moveY; |
| 125 | + final double offsetX; |
| 126 | + final double offsetY; |
| 127 | + final int tileX; |
| 128 | + final int tileY; |
| 129 | + final int moveX; |
| 130 | + final int moveY; |
| 131 | + private String url; |
| 132 | + |
| 133 | + public ImageRetrieveTask(String url, int zoomLevel, double offsetX, |
| 134 | + double offsetY, int tileX, int tileY, int moveX, int moveY) { |
| 135 | + this.zoomLevel = zoomLevel; |
| 136 | + this.offsetX = offsetX; |
| 137 | + this.offsetY = offsetY; |
| 138 | + this.tileX = tileX; |
| 139 | + this.tileY = tileY; |
| 140 | + this.moveX = moveX; |
| 141 | + this.moveY = moveY; |
| 142 | + this.url = url; |
127 | 143 | }
|
128 |
| - |
| 144 | + |
129 | 145 | @Override
|
130 | 146 | protected ImageOffsetPair compute() {
|
131 | 147 |
|
132 |
| - ImageOffsetPair result=new ImageOffsetPair(); |
| 148 | + ImageOffsetPair result = new ImageOffsetPair(); |
133 | 149 | try {
|
134 |
| - String httpUrl=url.replace("$path$", zoomLevel+"/"+(tileX+moveX)+"/"+(tileY+moveY)); |
| 150 | + if (url.endsWith("/")) |
| 151 | + url = url.substring(0, url.length() - 2); |
| 152 | + String httpUrl = url + "/" + zoomLevel + "/" + (tileX + moveX) |
| 153 | + + "/" + (tileY + moveY) + ".png"; |
135 | 154 | URL imageUrl = new URL(httpUrl);
|
136 | 155 | Image image = new Image(ImageIO.read(imageUrl));
|
137 |
| - result.image=image; |
138 |
| - result.offsetX=offsetX+moveX*TILE_SIZE; |
139 |
| - result.offsetY=offsetY+moveY*TILE_SIZE; |
| 156 | + result.image = image; |
| 157 | + result.offsetX = offsetX + moveX * TILE_SIZE; |
| 158 | + result.offsetY = offsetY + moveY * TILE_SIZE; |
140 | 159 | } catch (IOException e) {
|
141 | 160 | logService.log(LogService.LOG_ERROR, e.getMessage(), e);
|
142 | 161 | }
|
143 | 162 | return result;
|
144 | 163 | }
|
145 |
| - |
| 164 | + |
146 | 165 | }
|
147 |
| - |
148 |
| - private class ImageOffsetPair{ |
| 166 | + |
| 167 | + private class ImageOffsetPair { |
149 | 168 | Image image;
|
150 | 169 | double offsetX;
|
151 | 170 | double offsetY;
|
152 | 171 | }
|
153 |
| - |
154 |
| - |
| 172 | + |
155 | 173 | @Override
|
156 | 174 | public SearchResult search(GisMap gisMap, SearchQuery searchQuery) {
|
157 | 175 | return null;
|
|
0 commit comments