| 1 | /* |
|---|
| 2 | * Copyright (C) 2009 Camptocamp |
|---|
| 3 | * |
|---|
| 4 | * This file is part of MapFish Print |
|---|
| 5 | * |
|---|
| 6 | * MapFish Print is free software: you can redistribute it and/or modify |
|---|
| 7 | * it under the terms of the GNU General Public License as published by |
|---|
| 8 | * the Free Software Foundation, either version 3 of the License, or |
|---|
| 9 | * (at your option) any later version. |
|---|
| 10 | * |
|---|
| 11 | * MapFish Print is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with MapFish Print. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | package org.mapfish.print.map.readers; |
|---|
| 21 | |
|---|
| 22 | import org.mapfish.print.RenderingContext; |
|---|
| 23 | import org.mapfish.print.Transformer; |
|---|
| 24 | import org.mapfish.print.map.renderers.TileRenderer; |
|---|
| 25 | import org.mapfish.print.utils.PJsonArray; |
|---|
| 26 | import org.mapfish.print.utils.PJsonObject; |
|---|
| 27 | |
|---|
| 28 | import java.io.UnsupportedEncodingException; |
|---|
| 29 | import java.net.URI; |
|---|
| 30 | import java.net.URISyntaxException; |
|---|
| 31 | import java.util.List; |
|---|
| 32 | import java.util.Map; |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * Support for the protocol using directly the content of a WMTS REST structure. |
|---|
| 36 | */ |
|---|
| 37 | public class WMTSMapReader extends TileableMapReader { |
|---|
| 38 | protected final String layer; |
|---|
| 39 | @SuppressWarnings("unused") |
|---|
| 40 | private final float opacity; |
|---|
| 41 | private final String version; |
|---|
| 42 | private final String requestEncoding; |
|---|
| 43 | private final PJsonArray tileOrigin; |
|---|
| 44 | private final String style; |
|---|
| 45 | private final PJsonArray dimensions; |
|---|
| 46 | private final PJsonObject dimensionsParams; |
|---|
| 47 | private final String matrixSet; |
|---|
| 48 | private final int zoomOffset; |
|---|
| 49 | private final PJsonArray matrixIds; |
|---|
| 50 | private final String formatSuffix; |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | private WMTSMapReader(String layer, RenderingContext context, PJsonObject params) { |
|---|
| 54 | super(context, params); |
|---|
| 55 | this.layer = layer; |
|---|
| 56 | PJsonArray maxExtent = params.optJSONArray("tileFullExtent", params.getJSONArray("maxExtent")); |
|---|
| 57 | PJsonArray tileSize = params.getJSONArray("tileSize"); |
|---|
| 58 | opacity = params.optFloat("opacity", 1.0F); |
|---|
| 59 | version = params.getString("version"); |
|---|
| 60 | requestEncoding = params.getString("requestEncoding"); |
|---|
| 61 | // Optional (but mandatory until matrixIds is supported) |
|---|
| 62 | tileOrigin = params.getJSONArray("tileOrigin"); |
|---|
| 63 | style = params.getString("style"); |
|---|
| 64 | // Optional |
|---|
| 65 | dimensions = params.optJSONArray("dimensions"); |
|---|
| 66 | // Optional |
|---|
| 67 | dimensionsParams = params.optJSONObject("params"); |
|---|
| 68 | matrixSet = params.getString("matrixSet"); |
|---|
| 69 | // Optional (but mandatory until matrixIds is supported) |
|---|
| 70 | zoomOffset = params.getInt("zoomOffset"); |
|---|
| 71 | // Optional |
|---|
| 72 | matrixIds = params.optJSONArray("matrixIds"); |
|---|
| 73 | if (matrixIds != null) { |
|---|
| 74 | throw new RuntimeException("matrixIds are not supported for now. Use zoomOffset and tileOrigin instead. Patch welcome."); |
|---|
| 75 | } |
|---|
| 76 | formatSuffix = params.getString("formatSuffix"); |
|---|
| 77 | |
|---|
| 78 | tileCacheLayerInfo = new WMTSLayerInfo(params.getJSONArray("resolutions"), tileSize.getInt(0), tileSize.getInt(1), maxExtent.getFloat(0), maxExtent.getFloat(1), maxExtent.getFloat(2), maxExtent.getFloat(3), formatSuffix); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | protected TileRenderer.Format getFormat() { |
|---|
| 82 | return TileRenderer.Format.BITMAP; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | protected void addCommonQueryParams(Map<String, List<String>> result, Transformer transformer, String srs, boolean first) { |
|---|
| 86 | //not much query params for this protocol... |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | protected URI getTileUri(URI commonUri, Transformer transformer, float minGeoX, float minGeoY, float maxGeoX, float maxGeoY, long w, long h) throws URISyntaxException, UnsupportedEncodingException { |
|---|
| 90 | float targetResolution = (maxGeoX - minGeoX) / w; |
|---|
| 91 | WMTSLayerInfo.ResolutionInfo resolution = tileCacheLayerInfo.getNearestResolution(targetResolution); |
|---|
| 92 | |
|---|
| 93 | int col = (int) Math.round(Math.floor(((maxGeoX + minGeoX)/2-tileOrigin.getFloat(0)) / (resolution.value * w))); |
|---|
| 94 | int row = (int) Math.round(Math.floor((tileOrigin.getFloat(1)-(maxGeoY + minGeoY)/2) / (resolution.value * h))); |
|---|
| 95 | |
|---|
| 96 | StringBuilder path = new StringBuilder(); |
|---|
| 97 | if (!commonUri.getPath().endsWith("/")) { |
|---|
| 98 | path.append('/'); |
|---|
| 99 | } |
|---|
| 100 | if (requestEncoding.compareTo("REST") == 0) { |
|---|
| 101 | path.append(version); |
|---|
| 102 | path.append('/').append(layer); |
|---|
| 103 | path.append('/').append(style); |
|---|
| 104 | // Add dimensions |
|---|
| 105 | if (dimensions != null) { |
|---|
| 106 | for (int i = 0; i< dimensions.size(); i++) { |
|---|
| 107 | path.append('/').append(dimensionsParams.getString(dimensions.getString(i))); |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | path.append('/').append(matrixSet); |
|---|
| 111 | path.append('/').append(resolution.index + zoomOffset); |
|---|
| 112 | path.append('/').append(row); |
|---|
| 113 | path.append('/').append(col); |
|---|
| 114 | |
|---|
| 115 | path.append('.').append(tileCacheLayerInfo.getExtension()); |
|---|
| 116 | |
|---|
| 117 | return new URI(commonUri.getScheme(), commonUri.getUserInfo(), commonUri.getHost(), commonUri.getPort(), commonUri.getPath() + path, commonUri.getQuery(), commonUri.getFragment()); |
|---|
| 118 | } else { |
|---|
| 119 | throw new RuntimeException("Only WMTS REST structure is supported"); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | protected static void create(List<MapReader> target, RenderingContext context, PJsonObject params) { |
|---|
| 124 | String layer = params.getString("layer"); |
|---|
| 125 | target.add(new WMTSMapReader(layer, context, params)); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | public boolean testMerge(MapReader other) { |
|---|
| 129 | return false; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | public boolean canMerge(MapReader other) { |
|---|
| 133 | return false; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | public String toString() { |
|---|
| 137 | return layer; |
|---|
| 138 | } |
|---|
| 139 | } |
|---|