001/*
002 * To change this template, choose Tools | Templates
003 * and open the template in the editor.
004 */
005
006package armyc2.c2sd.renderer.utilities;
007
008//import android.graphics.Point;
009import android.graphics.PointF;
010//import armyc2.c2sd.graphics2d.Point2D;
011//import armyc2.c2sd.graphics2d.Point2D;
012import armyc2.c2sd.graphics2d.*;
013/**
014 *
015 * @author michael.spinelli
016 */
017public class PointConversion implements IPointConversion {
018
019    double _scale = 0;
020    int _pixelWidth = 0;
021    int _PixelHeight = 0;
022    double _geoTop = 0;
023    double _geoLeft = 0;
024    double _geoBottom = 0;
025    double _geoRight = 0;
026
027    //pixels to geo
028    //double _geoMultiplierX = 0;
029    //double _geoMultiplierY = 0;
030    //geo to pixels
031    double _pixelMultiplierX = 0;
032    double _pixelMultiplierY = 0;
033    boolean _normalize = true;
034    public void set_normalize(boolean value)
035    {
036        _normalize=value;
037    }
038
039    public PointConversion(int pixelWidth, int pixelHeight,
040                            double geoTop, double geoLeft,
041                            double geoBottom, double geoRight)
042    {/*
043            _pixelWidth = pixelWidth;
044            _PixelHeight = pixelHeight;
045            _geoTop = geoTop;
046            _geoLeft = geoLeft;
047            _geoBottom = geoBottom;
048            _geoRight = geoRight;*/
049
050            UpdateExtents(pixelWidth, pixelHeight, geoTop, geoLeft, geoBottom, geoRight);
051    }
052
053    public void UpdateExtents(int pixelWidth, int pixelHeight,
054                            double geoTop, double geoLeft,
055                            double geoBottom, double geoRight)
056    {
057            _pixelWidth = pixelWidth;
058            _PixelHeight = pixelHeight;
059            _geoTop = geoTop;
060            _geoLeft = geoLeft;
061            _geoBottom = geoBottom;
062            _geoRight = geoRight;
063
064            //_geoMultiplierX = ((double)_pixelWidth) / (_geoRight - _geoLeft) ;
065            //_geoMultiplierY = ((double)_PixelHeight) / (_geoTop - _geoBottom) ;
066
067            _pixelMultiplierX = (_geoRight - _geoLeft) / ((double)_pixelWidth) ;
068            _pixelMultiplierY = (_geoTop - _geoBottom) / ((double)_PixelHeight) ;   
069            
070            if(_geoRight-_geoLeft < -180)
071            {
072                _pixelMultiplierX = (_geoRight - _geoLeft + 360) / ((double)_pixelWidth) ;                
073            }
074            if(_geoRight-_geoLeft > 180)
075            {
076                _pixelMultiplierX = (360 - (_geoRight - _geoLeft) ) / ((double)_pixelWidth) ;                
077            }
078            if(_geoTop < _geoBottom)            
079                _pixelMultiplierY = -Math.abs(_pixelMultiplierY);
080            else
081                _pixelMultiplierY = Math.abs(_pixelMultiplierY);
082            
083//            if(_geoRight < _geoLeft)            
084//                _pixelMultiplierX = -Math.abs(_pixelMultiplierX);
085//            else
086//                _pixelMultiplierX = Math.abs(_pixelMultiplierX);
087            //end section
088
089        //for 2d
090        //40.075m meters is the circumference of the earth
091        //distanceInMeters=(distanceInDegrees/360)*40.075m meters
092        double distanceInDegrees=Math.abs(_geoRight - _geoLeft);
093        if(Math.abs(_geoRight - _geoLeft)>180)
094            distanceInDegrees=Math.abs(distanceInDegrees-360);
095        double distanceInMeters=(distanceInDegrees/360)*(40.075*1000000);
096        double scale = (_pixelWidth / distanceInMeters) * (1.0 / 96.0) * (1.0 / 39.37);
097        _scale = 1.0 / scale;
098
099    }
100
101    @Override
102    public PointF PixelsToGeo(PointF pixel)
103    {
104        PointF coords = new PointF();
105
106        coords.x = (float)(pixel.x * _pixelMultiplierX + _geoLeft); //xMultiplier;
107        coords.y = (float)(_geoTop - (pixel.y * _pixelMultiplierY));
108        
109        //diagnostic 12-18-12
110        if(coords.x > 180)
111            coords.x -= 360;
112        if(coords.x < -180)
113            coords.x += 360;
114        //end section
115        
116        return coords;
117    }
118
119    @Override
120    public PointF GeoToPixels(PointF coord)
121    {
122        PointF pixel = new PointF();
123
124        //double xMultiplier = _pixelMultiplierX;//(_geoRight - _geoLeft) / ((double)_pixelWidth) ;
125        //double yMultiplier = _pixelMultiplierY;//(_geoTop - _geoBottom) / ((double)_PixelHeight) ;
126        double temp;
127
128        //temp = ((coord.x  - _geoLeft) / _pixelMultiplierX);//xMultiplier);
129        double calcValue=coord.x  - _geoLeft;
130        if(_normalize)
131        {
132            if(calcValue<-180)
133                calcValue+=360;
134            else if(calcValue>180)
135                calcValue-=360;
136        }
137        temp = (calcValue / _pixelMultiplierX);//xMultiplier);
138
139        pixel.x = (float)temp;
140
141        temp = ((_geoTop - coord.y) / _pixelMultiplierY);//yMultiplier);
142        pixel.y = (float)temp;
143                
144        return pixel;
145    }
146    
147//      @Override
148//      public Point2D PixelsToGeo(Point pixel) {
149//              Point2D coords = new Point2D.Double();
150//      
151//      double x = (float)(pixel.x * _pixelMultiplierX + _geoLeft); //xMultiplier;
152//        double y = (float)(_geoTop - (pixel.y * _pixelMultiplierY));
153//        
154//        //diagnostic 12-18-12
155//        if(x > 180)
156//            x -= 360;
157//        if(x < -180)
158//            x += 360;
159//        //end section
160//        
161//        coords.setLocation(x, y);
162//        
163//        return coords;
164//      }
165
166//      @Override
167//      public Point GeoToPixels(Point2D coord) {
168//              Point pixel = new Point();
169//              int x = 0;
170//              int y = 0;
171//        double temp;
172//
173//        temp = ((coord.getX()  - _geoLeft) / _pixelMultiplierX);//xMultiplier);
174//
175//        x = (int)temp;
176//
177//        temp = ((_geoTop - coord.getY()) / _pixelMultiplierY);//yMultiplier);
178//        y = (int)temp;
179//                
180//        pixel.x = x;
181//        pixel.y = y;
182//        return pixel;
183//      }
184
185        @Override
186        public Point2D PixelsToGeo(Point2D pixel) {
187                
188                Point2D coords = new Point2D.Double();
189        
190        double x = (float)(pixel.getX() * _pixelMultiplierX + _geoLeft); //xMultiplier;
191        double y = (float)(_geoTop - (pixel.getY() * _pixelMultiplierY));
192        
193        //diagnostic 12-18-12
194        if(x > 180)
195            x -= 360;
196        if(x < -180)
197            x += 360;
198        //end section
199        
200        coords.setLocation(x, y);
201        
202        return coords;
203        }
204
205        //@Override
206        public Point2D GeoToPixels(Point2D coord) {
207                
208                Point2D pixel = new Point2D.Double();
209                double x = 0;
210                double y = 0;
211        double temp;
212
213        //temp = ((coord.getX()  - _geoLeft) / _pixelMultiplierX);//xMultiplier);
214        double calcValue=coord.getX()  - _geoLeft;
215        if(_normalize)
216        {
217            if(calcValue<-180)
218                calcValue+=360;
219            else if(calcValue>180)
220                calcValue-=360;
221        }
222        temp = (calcValue / _pixelMultiplierX);//xMultiplier);
223
224        x = temp;
225
226        temp = ((_geoTop - coord.getY()) / _pixelMultiplierY);//yMultiplier);
227        y = temp;
228                
229        pixel.setLocation(x,y);
230        return pixel;
231        }
232
233
234    public int getPixelWidth()
235    {
236        return _pixelWidth;
237    }
238
239    public int getPixelHeight()
240    {
241        return _PixelHeight;
242    }
243
244    public double getUpperLat()
245    {
246        return _geoTop;
247    }
248
249    public double getLowerLat()
250    {
251        return _geoBottom;
252    }
253
254    public double getLeftLon()
255    {
256        return _geoLeft;
257    }
258
259    public double getRightLon()
260    {
261        return _geoRight;
262    }
263
264
265
266
267
268}