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 Guangxi Institude of Meteorology and Disaster-reducing Research(GIMDR)
015 REFERENCE:
016
017 COMPILE: javac Coordinate.java Linear.java
018 */
019
020 package cma.common.projection;
021
022 import java.io.*;
023 import java.awt.*;
024 import java.awt.geom.*;
025 import java.util.*;
026 import java.lang.Math.*;
027 import java.text.DecimalFormat;
028
029 public class Linear extends Coordinate {
030
031 /**
032 * 功能:
033 * 重置参数
034 * 参数:
035 * lon,lat - 中心经纬度,
036 * px,py - 中心经纬度对应的屏幕坐标
037 * sx,sy - 缩放系数
038 * 返回值:
039 * 无
040 */
041 public void reset(double lon, double lat, int px, int py, double sc, double sx, double sy) {
042 type = Coordinate.LINEAR;
043 center = new Point2D.Double(
044 lon < 0.0 ? 0.0 : lon > 360.0 ? 360.0 : lon,
045 lat < -90.0 ? -90.0 : lat > 90.0 ? 90.0 : lat
046 );
047 place = new Point(px, py);
048 scaleXY = new Point2D.Double(sx==0.0?1.0:Math.abs(sx), sy==0.0?1.0:Math.abs(sy));
049 scale = Math.abs(sc);
050 scaleOriginal = scale;
051 offset = new Point(0, 0);//未用
052 }
053
054 /**
055 * 功能:
056 * 构造函数
057 * 参数:
058 * 无(使用缺省值)
059 * 返回值:
060 * 无
061 */
062 public Linear() {
063 reset(109.40, 24.35, 640, 480, 1.0, 10.0, 10.0);
064 }
065
066 /**
067 * 功能:
068 * 构造函数
069 * 参数:
070 * lon,lat - 中心经纬度,
071 * px,py - 中心经纬度对应的屏幕坐标
072 * sc - 缩放系数
073 * 返回值:
074 * 无
075 */
076 public Linear(double lon, double lat, int px, int py, double sc) {
077 reset(lon, lat, px, py, sc, 10.0*sc, 10.0*sc);
078 }
079
080 /**
081 * 功能:
082 * 构造函数
083 * 参数:
084 * lon,lat - 中心经纬度,
085 * px,py - 中心经纬度对应的屏幕坐标
086 * sx,sy - 缩放比例
087 * 返回值:
088 * 无
089 */
090 public Linear(double lon, double lat, int px, int py, double sx, double sy) {
091 reset(lon, lat, px, py, 1.0, sx, sy);
092 }
093
094 /**
095 * 功能:
096 * 重置参数
097 * 参数:
098 * lon,lat - 中心经纬度,
099 * px,py - 中心经纬度对应的屏幕坐标
100 * sx,sy - 缩放系数
101 * 返回值:
102 * 无
103 */
104 public void reset(double lon, double lat, int px, int py, double sx, double sy) {
105 reset(lon, lat, px, py, 1.0, sx, sy);
106 }
107
108 /**
109 * 功能:
110 * 获得屏幕坐标
111 * 参数:
112 * lon - 经度
113 * lat - 纬度
114 * 返回值:
115 * 屏幕坐标
116 */
117 public Point getPosition(double lon, double lat) {
118 return(
119 new Point(
120 place.x + (int)(0.5 + (lon - center.x) * scale * scaleXY.x),
121 place.y + (int)(0.5 + (center.y - lat) * scale * scaleXY.y)
122 )
123 );
124 }
125
126 /**
127 * 功能:
128 * 获得屏幕坐标对应的经纬度
129 * 参数:
130 * x - 屏幕水平坐标
131 * y - 屏幕垂直坐标
132 * 返回值:
133 * 对应的经纬度
134 */
135 public Point2D.Double getCoordinate(int x, int y) {
136 return(
137 new Point2D.Double(
138 center.x + (x - place.x) / scale / scaleXY.x,
139 center.y + (place.y - y) / scale / scaleXY.y
140 )
141 );
142 }
143
144 /**
145 * 功能:
146 * 画经线、纬线
147 * 参数:
148 * g - 图形设备
149 * f - 字体
150 * c - 画线颜色
151 * inc_lon - 经线间隔
152 * inc_lat - 纬线间隔
153 * 返回值:
154 * 无
155 */
156 public void drawGridLine(Graphics2D g, Font f, Color c, int inc_lon, int inc_lat) {
157
158 DecimalFormat df = new DecimalFormat("0.#");
159 Color saveColor = g.getColor();
160 Font saveFont = g.getFont();
161 g.setColor(c);
162 g.setFont(null==f?f:new Font("Times New Roman", Font.PLAIN, 12));
163 FontMetrics fm = g.getFontMetrics();
164 String text;
165 byte tmpByte[];
166 int bytesWidth, bytesHeight = fm.getHeight();;
167 Point pos1, pos2;
168 if( inc_lon > 0 ) {
169 for(double lon=0.0;lon<=360.0;lon=lon+inc_lon) {
170 if( 180.0 == lon ) {
171 for(double lat=-90.0;lat<=90.0;lat=lat+inc_lat) {
172 text = df.format(lat);
173 tmpByte = text.getBytes();
174 bytesWidth = fm.bytesWidth(tmpByte, 0, tmpByte.length);
175 pos1 = this.getPosition(lon, lat);
176 g.drawString(text, pos1.x-bytesWidth/2, pos1.y+bytesHeight/3);
177 }
178 }
179 pos1 = this.getPosition(lon, -90.0);
180 pos2 = this.getPosition(lon, 90.0);
181 g.drawLine(pos1.x, pos1.y, pos2.x, pos2.y);
182 }
183 }
184 if( inc_lat > 0 ) {
185 for(double lat=-90.0;lat<=90.0;lat=lat+inc_lat) {
186 if( 0.0 == lat ) {
187 for(double lon=0.0;lon<=360.0;lon=lon+inc_lon) {
188 text = df.format(lon);
189 tmpByte = text.getBytes();
190 bytesWidth = fm.bytesWidth(tmpByte, 0, tmpByte.length);
191 pos1 = this.getPosition(lon, lat);
192 g.drawString(text, pos1.x-bytesWidth/2, pos1.y+bytesHeight/3);
193 }
194 }
195 pos1 = this.getPosition( 0.0, lat);
196 pos2 = this.getPosition(360.0, lat);
197 g.drawLine(pos1.x, pos1.y, pos2.x, pos2.y);
198 }
199 }
200 g.setFont(saveFont);
201 g.setColor(saveColor);
202 }
203 }