Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap / src / es / prodevelop / gvsig / mobile / fmap / driver / raster / osm / OpenStreetMapUtils.java @ 21606

History | View | Annotate | Download (4.2 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Prodevelop and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *   Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *   +34 963862235
28
 *   gvsig@gva.es
29
 *   http://www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Prodevelop Integraci?n de Tecnolog?as SL
34
 *   Conde Salvatierra de ?lava , 34-10
35
 *   46004 Valencia
36
 *   Spain
37
 *
38
 *   +34 963 510 612
39
 *   +34 963 510 968
40
 *   gis@prodevelop.es
41
 *   http://www.prodevelop.es
42
 *
43
 *    or
44
 *
45
 *   Instituto de Rob?tica
46
 *   Apartado de correos 2085
47
 *   46071 Valencia
48
 *   (Spain)
49
 *   
50
 *   +34 963 543 577
51
 *   jjordan@robotica.uv.es
52
 *   http://robotica.uv.es
53
 *   
54
 */
55

    
56
package es.prodevelop.gvsig.mobile.fmap.driver.raster.osm;
57

    
58
import java.awt.geom.Point2D;
59

    
60
public class OpenStreetMapUtils {
61
        
62
        public static double SEMI_EQUATOR_LENGTH = 180.0;
63
        public static final boolean TILE_STRATEGY_RESIZE_DOWN = true;
64
        
65
        public static final int OSM_LAYER_MAPNIK = 0;
66
        public static final int OSM_LAYER_OSMARENDER = 1;
67
        
68
        public static final String OSM_LAYER_MAPNIK_SUBFOLDER = "mnik";
69
        public static final String OSM_LAYER_OSMARENDER_SUBFOLDER = "osma";
70

    
71
        public static OpenStreetMapTile getTileFor(Point2D p, int d, int lyr) {
72
                
73
                Point2D _p = normalize(p);
74

    
75
                double left = _p.getX() + SEMI_EQUATOR_LENGTH;
76
                double top = -_p.getY() + SEMI_EQUATOR_LENGTH;
77
                
78
                double tile_w = 2 * SEMI_EQUATOR_LENGTH / Math.pow(2.0, d);
79
                int max_ind = Math.round((float) Math.pow(2.0, d));
80
                
81
                int left_ind = ((int) Math.floor(left / tile_w)) % max_ind;
82
                int top_ind = ((int) Math.floor(top / tile_w)) % max_ind;
83
                
84
                OpenStreetMapTile tile = new OpenStreetMapTile(left_ind, top_ind, d, lyr);
85
                
86
                return tile;
87
        }
88
        
89
        public static int[] getDepthAndTileSize(
90
                        double world_dist, int pixels, boolean st) {
91
                
92
                int[] resp = new int[2];
93
                
94
                double dist_per_pixel = world_dist / pixels;
95
                int d = 0;
96
                while (dist_per_pixel < (2.0 * SEMI_EQUATOR_LENGTH / 256.0)) {
97
                        d++;
98
                        dist_per_pixel = 2 * dist_per_pixel;
99
                }
100
                
101
                resp[0] = d;
102
                
103
                if (!st) {
104
                        resp[0]--;
105
                        if (resp[0] < 0) resp[0] = 0;
106
                }
107
                
108
                dist_per_pixel = 2.0 * SEMI_EQUATOR_LENGTH / dist_per_pixel;
109
                resp[1] = Math.round((float) dist_per_pixel);
110
                
111
                if (!st) {
112
                        resp[1] = 2 * resp[1];
113
                }
114

    
115
                return resp;
116
        }
117
        
118
        public static Point2D normalize(Point2D p) {
119
                
120
                if ((p.getX() >= -SEMI_EQUATOR_LENGTH) && (p.getX() < SEMI_EQUATOR_LENGTH)
121
                                && (p.getY() > -SEMI_EQUATOR_LENGTH) && (p.getY() <= SEMI_EQUATOR_LENGTH)) {
122
                        return p;
123
                }
124

    
125
                double tx = 0;
126
                double ty = 0;
127
                double x = 0, y = 0;
128
                if (p.getX() > 0) {
129
                        tx = 1 + Math.floor((p.getX() - SEMI_EQUATOR_LENGTH) / (2 * SEMI_EQUATOR_LENGTH));
130
                        x = p.getX() - (tx * (2 * SEMI_EQUATOR_LENGTH));
131
                } else {
132
                        tx = -(1 + Math.floor((-p.getX() - SEMI_EQUATOR_LENGTH) / (2 * SEMI_EQUATOR_LENGTH)));
133
                        x = p.getX() - (tx * (2 * SEMI_EQUATOR_LENGTH));
134
                }
135
                
136
                if (p.getY() > 0) {
137
                        ty = 1 + Math.floor((p.getY() - SEMI_EQUATOR_LENGTH) / (2 * SEMI_EQUATOR_LENGTH));
138
                        y = p.getY() - (ty * (2 * SEMI_EQUATOR_LENGTH));
139
                } else {
140
                        ty = -(1 + Math.floor((-p.getY() - SEMI_EQUATOR_LENGTH) / (2 * SEMI_EQUATOR_LENGTH)));
141
                        y = p.getY() - (ty * (2 * SEMI_EQUATOR_LENGTH));
142
                }
143
                
144
                return new Point2D.Double(x, y);
145
        }
146
        
147
        public static double normalizeOffset(double d) {
148
                
149
                if (Math.abs(Math.abs(d) - (2 * OpenStreetMapUtils.SEMI_EQUATOR_LENGTH))
150
                        <
151
                        (0.000001 * OpenStreetMapUtils.SEMI_EQUATOR_LENGTH)) return 0;
152
                return d;
153
        }
154
        
155
        
156
        
157

    
158
}