Linear.java
001 /*
002
003 Linear 线性投影
004
005 PACKAGE: cma.common.projection
006 FILENAME: Linear.java
007 LANGUAGE: Java2 v1.4
008 ORIGINAL: none
009 DESCRIPTION:
010 CREATE: 2007-07-08 13:22:39
011 UPDATE:
012 AUTHOR: 刘泽军 (BJ0773@gmail.com)
013
014 */
015
016 package cma.common.projection;
017
018 import java.io.*;
019 import java.awt.*;
020 import java.awt.geom.*;
021 import java.util.*;
022 import java.lang.Math.*;
023
024 public class Linear {
025
026 //中心经纬度(0.0<=longitude<=360, -90.0<=latitude<=90.0)
027 private Point2D.Double center;
028
029 //中心经纬度对应的屏幕坐标
030 private Point place;
031
032 //缩放比例(非0正值,经向纬向可以不同)
033 private Point2D.Double scale;
034
035 /**
036 * 功能:
037 * 构造函数
038 * 参数:
039 * 无(使用缺省值)
040 * 返回值:
041 * 无
042 */
043 public Linear () {
044 reset ( 109.40 , 24.35 , 640 , 480 , 111.0 , 106.5 ) ;
045 }
046
047 /**
048 * 功能:
049 * 构造函数
050 * 参数:
051 * lon,lat - 中心经纬度,
052 * px,py - 中心经纬度对应的屏幕坐标
053 * sc - 缩放系数(经向纬向相同)
054 * 返回值:
055 * 无
056 */
057 public Linear ( double lon, double lat, int px, int py, double sc ) {
058 reset ( lon, lat, px, py, sc, sc ) ;
059 }
060
061 /**
062 * 功能:
063 * 构造函数
064 * 参数:
065 * lon,lat - 中心经纬度,
066 * px,py - 中心经纬度对应的屏幕坐标
067 * sx,sy - 缩放系数
068 * 返回值:
069 * 无
070 */
071 public Linear ( double lon, double lat, int px, int py, double sx, double sy ) {
072 reset ( lon, lat, px, py, sx, sy ) ;
073 }
074
075 /**
076 * 功能:
077 * 重置参数
078 * 参数:
079 * lon,lat - 中心经纬度,
080 * px,py - 中心经纬度对应的屏幕坐标
081 * sx,sy - 缩放系数
082 * 返回值:
083 * 无
084 */
085 public void reset ( double lon, double lat, int px, int py, double sx, double sy ) {
086 center = new Point2D.Double (
087 lon < 0.0 ? 0.0 : lon > 360.0 ? 360.0 : lon,
088 lat < - 90.0 ? - 90.0 : lat > 90.0 ? 90.0 : lat
089 ) ;
090 place = new Point ( px, py ) ;
091 scale = new Point2D.Double ( sx<= 0.0 ? 1.0 :sx, sy<= 0.0 ? 1.0 :sy ) ;
092 }
093
094 /**
095 * 功能:
096 * 获得中心经纬度
097 * 参数:
098 * 无
099 * 返回值:
100 * 中心经纬度
101 */
102 public Point2D.Double getCenter () {
103 return ( center ) ;
104 }
105
106 /**
107 * 功能:
108 * 获得中心经纬度对应的屏幕坐标
109 * 参数:
110 * 无
111 * 返回值:
112 * 中心经纬度对应的屏幕坐标
113 */
114 public Point getPlace () {
115 return ( place ) ;
116 }
117
118 /**
119 * 功能:
120 * 获得缩放比例
121 * 参数:
122 * 无
123 * 返回值:
124 * 缩放比例
125 */
126 public Point2D.Double getScale () {
127 return ( scale ) ;
128 }
129
130 /**
131 * 功能:
132 * 获得屏幕坐标
133 * 参数:
134 * lon - 经度
135 * lat - 纬度
136 * 返回值:
137 * 屏幕坐标
138 */
139 public Point getPosition ( double lon, double lat ) {
140 return (
141 new Point (
142 place.x + ( int )(( lon - center.x ) * scale.x ) ,
143 place.y + ( int )(( center.y - lat ) * scale.y )
144 )
145 ) ;
146 }
147
148 /**
149 * 功能:
150 * 获得屏幕坐标对应的经纬度
151 * 参数:
152 * x - 屏幕水平坐标
153 * y - 屏幕垂直坐标
154 * 返回值:
155 * 对应的经纬度
156 */
157 public Point2D.Double getCoordinate ( int x, int y ) {
158 return (
159 new Point2D.Double (
160 center.x + ( x - place.x ) / scale.x,
161 center.y + ( y - place.y ) / scale.y
162 )
163 ) ;
164 }
165
166 /**
167 * 功能:
168 * 画经线、纬线
169 * 参数:
170 * g - 图形设备
171 * f - 字体
172 * c - 画线颜色
173 * inc_lon - 经线间隔
174 * inc_lat - 纬线间隔
175 * 返回值:
176 * 无
177 */
178 public void drawGridLine ( Graphics2D g, Font f, Color c, int inc_lon, int inc_lat ) {
179
180 Color saveColor = g.getColor () ;
181 Font saveFont = g.getFont () ;
182 g.setColor ( c ) ;
183 g.setFont ( null ==f?f: new Font ( "Times New Roman" , Font.PLAIN, 12 )) ;
184 FontMetrics fm = g.getFontMetrics () ;
185 String text;
186 byte tmpByte [] ;
187 int bytesWidth, bytesHeight = fm.getHeight () ;;
188 Point pos1, pos2;
189 if ( inc_lon > 0 ) {
190 for ( double lon= 0.0 ;lon<= 360.0 ;lon=lon+inc_lon ) {
191 pos1 = this .getPosition ( lon, - 90.0 ) ;
192 pos2 = this .getPosition ( lon, 90.0 ) ;
193 text = String.valueOf ( lon ) ;
194 tmpByte = text.getBytes () ;
195 bytesWidth = fm.bytesWidth ( tmpByte, 0 , tmpByte.length ) ;
196 if ( 0.0 == Math.IEEEremainder ( lon, 10.0 ) ) {
197 g.drawString ( text, -bytesWidth/ 2 , bytesHeight/ 2 ) ;
198 }
199 g.drawLine ( pos1.x, pos1.y, pos2.x, pos2.y ) ;
200 }
201 }
202 if ( inc_lat > 0 ) {
203 for ( double lat=- 90.0 ;lat<= 90.0 ;lat=lat+inc_lat ) {
204 pos1 = this .getPosition ( 0.0 , lat ) ;
205 pos2 = this .getPosition ( 360.0 , lat ) ;
206 text = String.valueOf ( lat ) ;
207 tmpByte = text.getBytes () ;
208 bytesWidth = fm.bytesWidth ( tmpByte, 0 , tmpByte.length ) ;
209 if ( 0.0 == Math.IEEEremainder ( lat, 30.0 ) ) {
210 g.drawString ( text, -bytesWidth/ 2 , bytesHeight/ 2 ) ;
211 }
212 g.drawLine ( pos1.x, pos1.y, pos2.x, pos2.y ) ;
213 }
214 }
215 g.setFont ( saveFont ) ;
216 g.setColor ( saveColor ) ;
217 }
218 }