Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libRemoteServices / src / org / gvsig / remoteclient / wmts / struct / WMTSTileMatrix.java @ 34313

History | View | Annotate | Download (7.58 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program 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 this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.remoteclient.wmts.struct;
23

    
24
import java.awt.geom.Rectangle2D;
25
import java.io.IOException;
26
import java.util.ArrayList;
27

    
28
import org.kxml2.io.KXmlParser;
29
import org.xmlpull.v1.XmlPullParserException;
30

    
31
/**
32
 * Description of a tile matrix
33
 * @author Nacho Brodin (nachobrodin@gmail.com)
34
 */
35
public abstract class WMTSTileMatrix extends WMTSBaseStruct {
36
    private double                      scaleDenominator    = 0;
37
    private double[]                    topLeftCorner       = null;
38
    private int                         tileWidth           = 0;
39
    private int                         tileHeight          = 0;
40
    private long                        matrixWidth         = 0;
41
    private long                        matrixHeight        = 0;
42
    private static final double         MTS_X_GRADO         = 111319.490793274;
43

    
44
    public class Tile {
45
            public int       wPx, hPx;
46
            public int       row, col;
47
            public double    ulx, uly, lrx, lry;
48
            
49
            public Tile(int wPx, int hPx, int row, int col, double ulx, double uly, double lrx, double lry) {
50
                    this.row = row;
51
                    this.col = col;
52
                    this.ulx = ulx;
53
                    this.uly = uly;
54
                    this.lrx = lrx;
55
                    this.lry = lry;
56
                    this.wPx = wPx;
57
                    this.hPx = hPx;
58
            }
59
    }
60
    
61
    /**
62
     * Parses this service
63
     * @param parser
64
     * @param content
65
     * @throws IOException
66
     * @throws XmlPullParserException
67
     */
68
    @SuppressWarnings("unchecked")
69
        public abstract void parse(KXmlParser parser, ArrayList list) throws IOException, XmlPullParserException; 
70
        
71
    
72
        public double getScaleDenominator() {
73
                return scaleDenominator;
74
        }
75

    
76
        public void setScaleDenominator(double scaleDenominator) {
77
                this.scaleDenominator = scaleDenominator;
78
        }
79

    
80
        public int getTileWidth() {
81
                return tileWidth;
82
        }
83

    
84
        public void setTileWidth(int tileWidth) {
85
                this.tileWidth = tileWidth;
86
        }
87

    
88
        public int getTileHeight() {
89
                return tileHeight;
90
        }
91

    
92
        public void setTileHeight(int tileHeight) {
93
                this.tileHeight = tileHeight;
94
        }
95

    
96
        public long getMatrixWidth() {
97
                return matrixWidth;
98
        }
99

    
100
        public void setMatrixWidth(long matrixWidth) {
101
                this.matrixWidth = matrixWidth;
102
        }
103

    
104
        public long getMatrixHeight() {
105
                return matrixHeight;
106
        }
107

    
108
        public void setMatrixHeight(long matrixHeight) {
109
                this.matrixHeight = matrixHeight;
110
        }
111

    
112
        public double[] getTopLeftCorner() {
113
                if(topLeftCorner == null)
114
                        topLeftCorner = new double[2];
115
                return topLeftCorner;
116
        }
117
        
118
    public void parse(KXmlParser parser) throws IOException, XmlPullParserException {
119
            
120
    }
121
    
122
    /**
123
     * This function will check if the request coordinates intersects with the tiles in the matrix. If a tile
124
     * intersects then this will be added to the list.  
125
     */
126
    @SuppressWarnings("unchecked")
127
        public ArrayList intersects(boolean projected, WMTSTileMatrixLimits tileMatrixLimits, Rectangle2D request, Rectangle2D extentLayer) {
128
            double widthMtsTile = 0;
129
            double heightMtsTile = 0;
130
            if(!projected) {
131
                    widthMtsTile = (scaleDenominator * tileWidth * 0.28) / (MTS_X_GRADO * 1000);
132
                    heightMtsTile = (scaleDenominator * tileHeight * 0.28) / (MTS_X_GRADO * 1000);
133
            } else {
134
                    widthMtsTile = (scaleDenominator * tileWidth * 0.28) / 1000;
135
                    heightMtsTile = (scaleDenominator * tileHeight * 0.28) / 1000;
136
            }
137

    
138
                double ulx, uly, lrx, lry;
139
            ArrayList list = new ArrayList();
140
            Rectangle2D r = new Rectangle2D.Double(0, 0, 0, 0);
141

    
142
            //Recorre la matriz de tiles calculando las coordenadas de cada tile
143
            //Para cada tile comprueba si intersecta con el ?rea seleccionada si es as?
144
            //mete el tile en la lista para devolverlo como resultado
145
            
146
            //cambiar esto por un conversor entre coordenadas pixel y reales
147
            
148
            int i = 0, j = 0;
149
            double initX = topLeftCorner[1] + (widthMtsTile * tileMatrixLimits.getMinTileCol());
150
            double initY = topLeftCorner[0] - (heightMtsTile * (tileMatrixLimits.getMinTileRow()));
151
            
152
            for (int row = tileMatrixLimits.getMinTileRow(); row <= tileMatrixLimits.getMaxTileRow(); row++) {
153
                    uly = initY - (heightMtsTile * i);
154
                        lry = uly - heightMtsTile;
155
                        j = 0;
156
                        for (int col = tileMatrixLimits.getMinTileCol(); col <= tileMatrixLimits.getMaxTileCol(); col++) {
157
                                ulx = initX + (widthMtsTile * j);
158
                                lrx = ulx + widthMtsTile;
159
                                r.setRect(Math.min(ulx, lrx), Math.min(uly, lry), Math.abs(ulx - lrx), Math.abs(uly - lry));
160
                                if(request.intersects(r)) {
161
                                        list.add(new Tile(tileWidth, tileHeight, row, col, ulx, uly, lrx, lry));
162
                                }
163
                                j ++;
164
                        }
165
                        i ++;
166
                }
167
            
168
            return list;
169
    }
170
    
171
    /**
172
     * This function will check if the request coordinates intersects with the tiles in the matrix. If a tile
173
     * intersects then this will be added to the list.  
174
     */
175
    @SuppressWarnings("unchecked")
176
        public ArrayList intersects(boolean projected, Rectangle2D request, Rectangle2D extentLayer) {
177
            double widthMtsTile = 0;
178
            double heightMtsTile = 0;
179
            if(!projected) {
180
                    widthMtsTile = (scaleDenominator * tileWidth * 0.28) / (MTS_X_GRADO * 1000);
181
                    heightMtsTile = (scaleDenominator * tileHeight * 0.28) / (MTS_X_GRADO * 1000);
182
            } else {
183
                    widthMtsTile = (scaleDenominator * tileWidth * 0.28) / 1000;
184
                    heightMtsTile = (scaleDenominator * tileHeight * 0.28) / 1000;
185
            }
186

    
187
                double ulx, uly, lrx, lry;
188
            ArrayList list = new ArrayList();
189
            Rectangle2D r = new Rectangle2D.Double(0, 0, 0, 0);
190

    
191
            //Recorre la matriz de tiles calculando las coordenadas de cada tile
192
            //Para cada tile comprueba si intersecta con el ?rea seleccionada si es as?
193
            //mete el tile en la lista para devolverlo como resultado
194
            
195
            int i = 0, j = 0;
196
            double initX = topLeftCorner[1];
197
            double initY = topLeftCorner[0];
198
            
199
            //cambiar esto por un conversor entre coordenadas pixel y reales
200
            for (int row = 0; row < matrixHeight; row++) {
201
                    uly = initY - (heightMtsTile * i);
202
                        lry = uly - heightMtsTile;
203
                        j = 0;
204
                        for (int col = 0; col < matrixWidth; col++) {
205
                                ulx = initX + (widthMtsTile * j);
206
                                lrx = ulx + widthMtsTile;
207
                                r.setRect(Math.min(ulx, lrx), Math.min(uly, lry), Math.abs(ulx - lrx), Math.abs(uly - lry));
208
                                if(request.intersects(r)) {
209
                                        list.add(new Tile(tileWidth, tileHeight, row, col, ulx, uly, lrx, lry));
210
                                }
211
                                j ++;
212
                        }
213
                        i ++;
214
                }
215
            
216
            return list;
217
    }
218
        
219
        public void print() {
220
                System.out.println("   *****WMTSTileMatrix******");
221
                System.out.println("   scaleDenominator:" + getScaleDenominator());
222
                if(topLeftCorner != null)
223
                        System.out.println("   topLeftCorner:" + topLeftCorner[0] + ", " + topLeftCorner[1]);
224
                System.out.println("   tileWidth:" + getTileWidth());
225
                System.out.println("   tileHeight:" + getTileHeight());
226
                System.out.println("   matrixWidth:" + getMatrixWidth());
227
                System.out.println("   matrixHeight:" + getMatrixHeight());
228
        }
229
}