-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRasterMap.java
More file actions
307 lines (227 loc) · 9.65 KB
/
RasterMap.java
File metadata and controls
307 lines (227 loc) · 9.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package javaxt.cartography;
import java.io.*;
import java.awt.image.*;
import java.awt.*;
import javax.imageio.*;
import javax.imageio.stream.*;
import javaxt.geospatial.geometry.Point;
//******************************************************************************
//** CreateMap Class - By Peter Borissow
//******************************************************************************
/**
* Enter class description
*
******************************************************************************/
public class RasterMap {
private double ULx = 0;
private double ULy = 0;
private double resX = 1;
private double resY = 1;
private BufferedImage bufferedImage = null;
Graphics2D g2d = null;
//**************************************************************************
//** Creates a new instance of CreateMap
//**************************************************************************
public RasterMap(double minX, double minY,
double maxX, double maxY,
int width, int height){
//Create BufferedImage
this.bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//Initialize Variables
init(minX, minY, maxX, maxY, bufferedImage);
//Set default background and line color
g2d.setColor(Color.WHITE);
g2d.fillRect(0,0,width,height);
g2d.setColor(Color.BLACK);
}
//**************************************************************************
//** Creates a new instance of CreateMap
//**************************************************************************
public RasterMap(double minX, double minY,
double maxX, double maxY,
BufferedImage bufferedImage){
this.bufferedImage = bufferedImage;
//Initialize Variables
init(minX, minY, maxX, maxY, bufferedImage);
}
//**************************************************************************
//** Init
//**************************************************************************
private void init(double minX, double minY, double maxX, double maxY,
BufferedImage bufferedImage){
//Validate Coordinates
if (validate(minX, minY, maxX, maxY)==false) return; //error?
//Get Width/Height
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
//Update min/max coordinates
minX = x(minX);
minY = y(minY);
maxX = x(maxX);
maxY = y(maxY);
//Update Local Variables using updated values
this.ULx = minX;
this.ULy = maxY;
//Compute pixelsPerDeg
this.resX = ((double) width) / (maxX-minX);
this.resY = ((double) height) / (minY-maxY);//(maxY-minY);
//System.out.println("pixelsPerDeg = " + resX + ", " + resY);
//Instantiate 2D Drawing Class
this.g2d = bufferedImage.createGraphics();
g2d.setColor(Color.BLACK);
}
//**************************************************************************
//** Validate
//**************************************************************************
/** Used to validate coordinates used to invoke this class */
private boolean validate(double minX, double minY, double maxX, double maxY){
if (minX > maxX || minY > maxY) return false;
if (minX < -180 || maxX < -180 || maxX > 180 || minX > 180) return false;
if (minY < -90 || maxY < -90 || maxY > 90 || minY > 90) return false;
return true;
}
//**************************************************************************
//** X
//**************************************************************************
/** Used to convert longitude to pixel coordinates */
private double x(double pt){
pt += 180;
double x = (pt - ULx) * resX;
//System.out.println("X = " + x);
return x;
}
private double x(String pt){
return x(cdbl(pt));
}
//**************************************************************************
//** Y
//**************************************************************************
/** Used to convert latitude to pixel coordinates */
private double y(double pt){
//System.out.print("Y = " + pt + " (");
pt = -pt;
if (pt<=0) pt = 90 + -pt;
else pt = 90 - pt;
pt = 180-pt;
//System.out.print(pt + ")" + " (");
double y = (pt - ULy) * resY;
if (cint(y)==0 || cint(y)==-0) y = 0;
//else y = -y;
//System.out.println(y + ")");
return y;
}
private double y(String pt){
return y(cdbl(pt));
}
public void AddGraticule(double SpacingInDegrees){
int spacing = 15; //degrees
for (int i=0; i<=360; i++){
int x = -(180-i);
Insert(x + ",90 " + x + ",-90",null);
i+=spacing-1;
}
for (int i=0; i<=90; i++){
int y = i;
Insert("-180," + y + " 180," + y,null);
Insert("-180," + -y + " 180," + -y,null);
i+=spacing-1;
}
}
//**************************************************************************
//** Insert
//**************************************************************************
/** Used to insert a "feature" into the map */
public void Insert(Point[] Coordinates, Style style){
}
//**************************************************************************
//** Insert
//**************************************************************************
/** Used to insert a "feature" into the map */
public void Insert(String Coordinates, Style style){
String cs = ",";
String ts = " ";
Insert(Coordinates, style, cs, ts);
}
public void Insert(String Coordinates, Style style, String cs, String ts){
if (Coordinates==null) return;
Coordinates = Coordinates.trim();
String[] coords = Coordinates.split(ts);
int numPoints = coords.length;
int[] x = new int[numPoints];
int[] y = new int[numPoints];
for (int i=0; i<numPoints; i++){
String[] pt = coords[i].split(cs);
x[i] = cint(x(pt[0]));
y[i] = cint(y(pt[1]));
}
if (coords.length==1){
g2d.fillOval(cint(x(x[0])), cint(y(y[0])), 5, 5);
}
else{
if (coords[0].equals(coords[coords.length-1])){
g2d.drawPolygon(x, y, numPoints);
}
else{
g2d.drawPolyline(x, y, numPoints);
}
}
}
//**************************************************************************
//** getBufferedImage
//**************************************************************************
public BufferedImage getBufferedImage(double minX, double minY,
double maxX, double maxY,
int width, int height){
minX = x(minX);
minY = y(minY);
maxX = x(maxX);
maxY = y(maxY);
int x = cint(minX);
int y = cint(maxY);
int w = cint(maxX)-x;
int h = cint(minY)-y;
//Crop the Map
BufferedImage croppedImage = bufferedImage.getSubimage(x,y,w,h);
return croppedImage;
/*
//Resize Cropped Map
java.awt.Image scaledImage = croppedImage.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);
BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = outputImage.createGraphics();
g2d.drawImage(scaledImage, 0, 0, null);
g2d.dispose();
//Return Output
return outputImage;
*/
}
//**************************************************************************
//** Save
//**************************************************************************
/** Used to save the map to an image file */
public void Save(String PathToFile){
try{
//g2d.dispose();
File File = new File(PathToFile);
File.getParentFile().mkdirs();
String FileName = File.getName();
String FileExt = FileName.substring(FileName.lastIndexOf(".")+1,FileName.length());
RenderedImage rendImage = bufferedImage;
ImageIO.write(rendImage,FileExt,File);
}
catch (Exception e){
//System.out.println(e.toString());
}
}
public Style getStyle(){
return new Style(g2d);
}
public BufferedImage getImage(){
return bufferedImage;
}
//**************************************************************************
//** String Conversions
//**************************************************************************
private int cint(String str){return Integer.valueOf(str).intValue(); }
private int cint(Double d){ return (int)Math.round(d); }
private double cdbl(String str){return Double.valueOf(str).doubleValue(); }
}