/* * TileFactoryInfo.java * * Created on June 26, 2006, 10:47 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package org.jdesktop.swingx.mapviewer; import java.awt.geom.Point2D; /** * A TileFactoryInfo encapsulates all information specific to a map server. This * includes everything from the url to load the map tiles from to the size and * depth of the tiles. Theoretically any map server can be used by installing a * customized TileFactoryInfo. Currently * * @author joshy */ @Deprecated public class TileFactoryInfo { private final int minimumZoomLevel; private final int maximumZoomLevel; private final int totalMapZoom; // the size of each tile (assumes they are square) private int tileSize = 256; /* * The number of tiles wide at each zoom level */ private final int[] mapWidthInTilesAtZoom; /** * An array of coordinates in <em>pixels</em> that indicates the center in * the world map for the given zoom level. */ private final Point2D[] mapCenterInPixelsAtZoom;// = new Point2D.Double[18]; /** * An array of doubles that contain the number of pixels per degree of * longitude at a give zoom level. */ private final double[] longitudeDegreeWidthInPixels; /** * An array of doubles that contain the number of radians per degree of * longitude at a given zoom level (where longitudeRadianWidthInPixels[0] is * the most zoomed out) */ private final double[] longitudeRadianWidthInPixels; /** * The base url for loading tiles from (and its alternatives). */ protected String[] baseURLs; private final String xparam; private final String yparam; private final String zparam; private boolean xr2l = true; private boolean yt2b = true; private int defaultZoomLevel; /** A name for this info. */ private final String name; /** * Creates a new instance of TileFactoryInfo. Note that TileFactoryInfo * should be considered invariate, meaning that subclasses should ensure all * of the properties stay the same after the class is constructed. Returning * different values of getTileSize() for example is considered an error and * may result in unexpected behavior. * * @param minimumZoomLevel The minimum zoom level * @param maximumZoomLevel the maximum zoom level * @param totalMapZoom the top zoom level, essentially the height of the * pyramid * @param tileSize the size of the tiles in pixels (must be square) * @param xr2l if the x goes r to l (is this backwards?) * @param yt2b if the y goes top to bottom * @param xparam the x parameter for the tile url * @param yparam the y parameter for the tile url * @param zparam the z parameter for the tile url * @param baseURLs the base url for grabbing tiles (and alternative URLs) */ /* * @param xr2l true if tile x is measured from the far left of the map to * the far right, or else false if based on the center line. * * @param yt2b true if tile y is measured from the top (north pole) to the * bottom (south pole) or else false if based on the equator. */ public TileFactoryInfo(int minimumZoomLevel, int maximumZoomLevel, int totalMapZoom, int tileSize, boolean xr2l, boolean yt2b, String xparam, String yparam, String zparam, String... baseURLs) { this("name not provided", minimumZoomLevel, maximumZoomLevel, totalMapZoom, tileSize, xr2l, yt2b, xparam, yparam, zparam, baseURLs); } /** * Creates a new instance of TileFactoryInfo. Note that TileFactoryInfo * should be considered invariate, meaning that subclasses should ensure all * of the properties stay the same after the class is constructed. Returning * different values of getTileSize() for example is considered an error and * may result in unexpected behavior. * * @param name A name to identify this information. * @param minimumZoomLevel The minimum zoom level * @param maximumZoomLevel the maximum zoom level * @param totalMapZoom the top zoom level, essentially the height of the * pyramid * @param tileSize the size of the tiles in pixels (must be square) * @param xr2l if the x goes r to l (is this backwards?) * @param yt2b if the y goes top to bottom * @param xparam the x parameter for the tile url * @param yparam the y parameter for the tile url * @param zparam the z parameter for the tile url * @param baseURLs the base url for grabbing tiles (and alternative URLs) */ /* * @param xr2l true if tile x is measured from the far left of the map to * the far right, or else false if based on the center line. * * @param yt2b true if tile y is measured from the top (north pole) to the * bottom (south pole) or else false if based on the equator. */ public TileFactoryInfo(String name, int minimumZoomLevel, int maximumZoomLevel, int totalMapZoom, int tileSize, boolean xr2l, boolean yt2b, String xparam, String yparam, String zparam, String... baseURLs) { this.name = name; this.minimumZoomLevel = minimumZoomLevel; this.maximumZoomLevel = maximumZoomLevel; this.totalMapZoom = totalMapZoom; this.baseURLs = baseURLs; this.xparam = xparam; this.yparam = yparam; this.zparam = zparam; this.setXr2l(xr2l); this.setYt2b(yt2b); this.tileSize = tileSize; // init the num tiles wide int tilesize = this.getTileSize(0); longitudeDegreeWidthInPixels = new double[totalMapZoom + 1]; longitudeRadianWidthInPixels = new double[totalMapZoom + 1]; mapCenterInPixelsAtZoom = new Point2D.Double[totalMapZoom + 1]; mapWidthInTilesAtZoom = new int[totalMapZoom + 1]; // for each zoom level for (int z = totalMapZoom; z >= 0; --z) { // how wide is each degree of longitude in pixels longitudeDegreeWidthInPixels[z] = tilesize / 360.0; // how wide is each radian of longitude in pixels longitudeRadianWidthInPixels[z] = tilesize / (2.0 * Math.PI); int t2 = tilesize / 2; mapCenterInPixelsAtZoom[z] = new Point2D.Double(t2, t2); mapWidthInTilesAtZoom[z] = tilesize / this.getTileSize(0); tilesize *= 2; } } /** * Get the minimum zoom level * * @return the minimum zoom level */ public int getMinimumZoomLevel() { return minimumZoomLevel; } // public void setMinimumZoomLevel(int minimumZoomLevel) { // this.minimumZoomLevel = minimumZoomLevel; // } /** * Get the maximum zoom level * * @return the maximum zoom level */ public int getMaximumZoomLevel() { return maximumZoomLevel; } // // public void setMaximumZoomLevel(int maximumZoomLevel) { // this.maximumZoomLevel = maximumZoomLevel; // } /** * Get the top zoom level * * @return the top zoom level */ public int getTotalMapZoom() { return totalMapZoom; } /* * public void setTotalMapZoom(int totalMapZoom) { this.totalMapZoom = * totalMapZoom; } */ /** * Get the with of a tile at the given zoom level * * @param zoom the zoom level * * @return the tile width in pixels */ public int getMapWidthInTilesAtZoom(int zoom) { return mapWidthInTilesAtZoom[zoom]; } /** * Get the center position of the map at the given zoom level * * @param zoom the zoom level * * @return the center position in pixels */ public Point2D getMapCenterInPixelsAtZoom(int zoom) { return mapCenterInPixelsAtZoom[zoom]; } /** * Returns the tile url (and its alternatives) for the specified tile at the * specified zoom level. By default it will generate a tile url using the * base url and parameters specified in the constructor. Thus if * * <PRE> * <CODE>baseURl = http://www.myserver.com/maps?version=0.1 * xparam = x * yparam = y * zparam = z * tilepoint = [1,2] * zoom level = 3 * </CODE> * </PRE> * * then the resulting url would be: * * <pre> * <code>http://www.myserver.com/maps?version=0.1&x=1&y=2&z=3</code> * </pre> * * * Note that the URL can be a <CODE>file:</CODE> url. * * @param zoom the zoom level * @param x the tile x ordinate * @param y the tile y ordinate * @return a valid URL to load the tile */ public String[] getTileUrls(int x, int y, int zoom) { // System.out.println("getting tile at zoom: " + zoom); // System.out.println("map width at zoom = " + // getMapWidthInTilesAtZoom(zoom)); String ypart = "&" + yparam + "=" + y; // System.out.println("ypart = " + ypart); if (!yt2b) { int tilemax = getMapWidthInTilesAtZoom(zoom); // int y = tilePoint.getY(); ypart = "&" + yparam + "=" + (tilemax / 2 - y - 1); } // System.out.println("new ypart = " + ypart); String[] result = new String[baseURLs.length]; for (int i = 0; i < baseURLs.length; ++i) { String url = baseURLs[i] + "&" + xparam + "=" + x + ypart + // "&" + yparam + "=" + tilePoint.getY() + "&" + zparam + "=" + zoom; result[i] = url; } return result; } /** * Get the tile size. * * @param zoom the zoom level * * @return the tile size */ public int getTileSize(int zoom) { return tileSize; } /** * Get the width of a longitude degree at the given zoom level * * @param zoom the zoom level * * @return the width in pixels */ public double getLongitudeDegreeWidthInPixels(int zoom) { return longitudeDegreeWidthInPixels[zoom]; } /** * Get the width of a longitude radian at the given zoom level * * @param zoom the zoom level * * @return the width in pixels */ public double getLongitudeRadianWidthInPixels(int zoom) { return longitudeRadianWidthInPixels[zoom]; } /** * A property indicating if the X coordinates of tiles go from right to left * or left to right. * * @return if the x ordinates go from right to left */ public boolean isXr2l() { return xr2l; } /** * A property indicating if the X coordinates of tiles go from right to left * or left to right. * * @param xr2l if the x ordinates go from right to left */ public void setXr2l(boolean xr2l) { this.xr2l = xr2l; } /** * A property indicating if the Y coordinates of tiles go from right to left * or left to right. * * @return if the y ordinates go from top to bottom */ public boolean isYt2b() { return yt2b; } /** * A property indicating if the Y coordinates of tiles go from right to left * or left to right. * * @param yt2b if the y ordinates go from top to bottom */ public void setYt2b(boolean yt2b) { this.yt2b = yt2b; } /** * Get the default zoom level * * @return the default zoom level */ public int getDefaultZoomLevel() { return defaultZoomLevel; } /** * Set the default zoom level * * @param defaultZoomLevel the default zoom level */ public void setDefaultZoomLevel(int defaultZoomLevel) { this.defaultZoomLevel = defaultZoomLevel; } /** * The name of this info. * * @return Returns the name of this info class for debugging or GUI widgets. */ public String getName() { return name; } }