| | 1 | /** |
| | 2 | * This program is free software: you can redistribute it and/or modify |
| | 3 | * it under the terms of the GNU Lesser General Public License as published by |
| | 4 | * the Free Software Foundation, either version 3 of the License, or |
| | 5 | * (at your option) any later version. |
| | 6 | * |
| | 7 | * This program is distributed in the hope that it will be useful, |
| | 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| | 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| | 10 | * GNU General Public License for more details. |
| | 11 | * |
| | 12 | * You should have received a copy of the GNU Lesser General Public License |
| | 13 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| | 14 | * |
| | 15 | * @author Arne Kepp, The Open Planning Project, Copyright 2008 |
| | 16 | */ |
| | 17 | package org.geowebcache.service.mgmaps; |
| | 18 | |
| | 19 | import java.util.Map; |
| | 20 | |
| | 21 | import javax.servlet.http.HttpServletRequest; |
| | 22 | |
| | 23 | import org.apache.commons.logging.Log; |
| | 24 | import org.apache.commons.logging.LogFactory; |
| | 25 | import org.geowebcache.layer.SRS; |
| | 26 | import org.geowebcache.layer.TileLayer; |
| | 27 | import org.geowebcache.layer.TileRequest; |
| | 28 | import org.geowebcache.mime.MimeException; |
| | 29 | import org.geowebcache.mime.MimeType; |
| | 30 | import org.geowebcache.service.Service; |
| | 31 | import org.geowebcache.service.ServiceException; |
| | 32 | import org.geowebcache.service.ServiceRequest; |
| | 33 | import org.geowebcache.util.ServletUtils; |
| | 34 | |
| | 35 | /** |
| | 36 | * Class to convert from Google Maps coordinates into the internal |
| | 37 | * representation of a tile. |
| | 38 | */ |
| | 39 | public class MGMapsConverter extends Service { |
| | 40 | public static final String SERVICE_MGMAPS = "mgmaps"; |
| | 41 | |
| | 42 | private static Log log = LogFactory |
| | 43 | .getLog(org.geowebcache.service.mgmaps.MGMapsConverter.class); |
| | 44 | |
| | 45 | public MGMapsConverter() { |
| | 46 | super(SERVICE_MGMAPS); |
| | 47 | } |
| | 48 | |
| | 49 | public ServiceRequest getServiceRequest(HttpServletRequest request) throws ServiceException { |
| | 50 | return new ServiceRequest(super.getLayersParameter(request)); |
| | 51 | } |
| | 52 | |
| | 53 | public TileRequest getTileRequest(TileLayer tileLayer, HttpServletRequest request) |
| | 54 | throws ServiceException { |
| | 55 | Map params = request.getParameterMap(); |
| | 56 | |
| | 57 | String mimeType = ServletUtils.stringFromMap(params, "format"); |
| | 58 | String strZoom = ServletUtils.stringFromMap(params, "zoom"); |
| | 59 | String strX = ServletUtils.stringFromMap(params, "x"); |
| | 60 | String strY = ServletUtils.stringFromMap(params, "y"); |
| | 61 | |
| | 62 | int[] gridLoc = MGMapsConverter.convert(Integer.parseInt(strZoom), Integer |
| | 63 | .parseInt(strX), Integer.parseInt(strY)); |
| | 64 | |
| | 65 | SRS srs = new SRS(900913); |
| | 66 | |
| | 67 | MimeType mime = null; |
| | 68 | try { |
| | 69 | if(mimeType == null) { |
| | 70 | mimeType = "image/png"; |
| | 71 | } |
| | 72 | mime = MimeType.createFromMimeType(mimeType); |
| | 73 | } catch (MimeException me) { |
| | 74 | throw new ServiceException("Unable to determine requested format, " + mimeType); |
| | 75 | } |
| | 76 | return new TileRequest(gridLoc,mime,srs); |
| | 77 | } |
| | 78 | |
| | 79 | /** |
| | 80 | * Convert Google's tiling coordinates into an {x,y,x} |
| | 81 | * |
| | 82 | * see |
| | 83 | * http://code.google.com/apis/maps/documentation/overlays.html#Custom_Map_Types |
| | 84 | * Modified by JaakL for mgmaps zoom understanding |
| | 85 | * @param quadKey |
| | 86 | * @return |
| | 87 | */ |
| | 88 | public static int[] convert(int zoomLevel, int x, int y) { |
| | 89 | // Extent is the total number of tiles in y direction |
| | 90 | int newZoom=17-zoomLevel; |
| | 91 | int extent = (int) Math.pow(2, newZoom); |
| | 92 | |
| | 93 | if (x < 0 || x > extent - 1) { |
| | 94 | log.error("The X coordinate is not sane: " + x); |
| | 95 | return null; |
| | 96 | } |
| | 97 | |
| | 98 | if (y < 0 || y > extent - 1) { |
| | 99 | log.error("The Y coordinate is not sane: " + y); |
| | 100 | return null; |
| | 101 | } |
| | 102 | |
| | 103 | // xPos and yPos correspond to the top left hand corner |
| | 104 | int[] gridLoc = { x, extent - y - 1, newZoom}; |
| | 105 | |
| | 106 | return gridLoc; |
| | 107 | } |
| | 108 | } |