summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/epsdk-analytics/src/main/java/org/openecomp/portalsdk/analytics/gmap/map/NovaMap.java
blob: ba24506432de0ef1b5b8df8381cc26ef3a4a3366 (plain)
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*-
 * ================================================================================
 * eCOMP Portal SDK
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ================================================================================
 */
package org.openecomp.portalsdk.analytics.gmap.map;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashSet;

import javax.servlet.http.HttpServletRequest;

import org.openecomp.portalsdk.analytics.gmap.line.Line;
import org.openecomp.portalsdk.analytics.gmap.line.LineInfo;
import org.openecomp.portalsdk.analytics.gmap.map.layer.SwingLayer;
import org.openecomp.portalsdk.analytics.gmap.node.Node;
import org.openecomp.portalsdk.analytics.gmap.node.NodeInfo;

public class NovaMap {
	private static int[] shapeWidth;
	
	public static final Font TEXT_FONT = new Font("sans-serif", Font.BOLD, 12);
	public static final Font HEADER_FONT = new Font("sans-serif", Font.ITALIC+Font.BOLD, 12);
	
	private HashSet<String> showList;
	private ArrayList<SwingLayer> swingLayers;
	private AffineTransform transform;
	
	private Node node;
	private Line line;
	private ColorProperties colorProperties;
	
	private Rectangle2D defaultBoundary;
	
	private int zoomLevel;
	
	private String currentYearMonth;
	
	private String dataLoaded = "";
	
	/**
	 * size in screen pixel
	 */
	private Rectangle boundingBox;
	
	/**
	 * size in pixel web mercator projection
	 */
	private Rectangle2D mapArea;
	
	/**
	 * size in longitude latitude
	 */
	private Rectangle2D geoArea;
	
	public static double[] meter2pixel; 
	
	private boolean showLegend = false;
	
	static {
		initShapeWidth();
		initMeter2Pixel();
	}
	
  private static void initMeter2Pixel() {
  	meter2pixel = new double[MapConstant.ZOOM_MAX - MapConstant.ZOOM_MIN+1];
  	meter2pixel[0] = 156543.04/2;
    for(int i=1; i<meter2pixel.length; ++i)
        meter2pixel[i] = meter2pixel[i-1]/2;
  }
	
	private static void initShapeWidth() {
		// ZOOM_MAX+1 is added to below line because of ArrayIndexOutOfException. This is Suggested by Hendra Tuty. - Sundar
		shapeWidth = new int[MapConstant.ZOOM_MAX]; 
		int width = 0;
		for (int i = 0; i < shapeWidth.length; i++) {
			if (i < 5) {

			}
			else if (i == 5) {
				width = 4;
			}
			else if (i > 4 && i < 10) {
				width += 2;
			}
			else {
				width++;
			}

			shapeWidth[i] = width;
		}
	}
	
	public NovaMap() {
		boundingBox = new Rectangle();
		mapArea = new Rectangle2D.Double();
		geoArea = new Rectangle2D.Double();
		showList = new HashSet<String>();
		swingLayers = new ArrayList<SwingLayer>();
	}
	
	

	public int getBestZoomLevel(double Latitude1, double Longitude1,
			double Latitude2, double Longitude2,
			double height, double width) {
			
		if (height==0)
			height=700;
		if (width==0)
			width=1200;
		
		double  lat1 = Math.min(Latitude1, Latitude1);
		double  CosLat = Math.cos(Math.toRadians(lat1));
		double  Wmeter = getDistance(
				lat1, Longitude1,
				lat1, Longitude2)/CosLat;
		double  Hmeter = getDistance(
				Latitude1, Longitude1,
				Latitude2, Longitude1)/CosLat;

		int zoom = 0;
		if(Latitude1 == Latitude2 && Longitude1 == Longitude2) 
			zoom = 15;
		if (zoom <= 0) {
			for(;
			zoom < meter2pixel.length
			&& (width*meter2pixel[zoom]) > Wmeter
			&& (height*meter2pixel[zoom]) > Hmeter;
			++zoom) ;
		}

//		&& (1200*meter2pixel[zoom]) > Wmeter
//		&& (700*meter2pixel[zoom]) > Hmeter;

		return zoom + MapConstant.ZOOM_MIN-1;
	}

	public static double getDistance(double Latitude1, double Longitude1,
			double Latitude2, double Longitude2) {
		Latitude1 = Math.toRadians(Latitude1);
		Longitude1 = Math.toRadians(Longitude1);
		Latitude2 = Math.toRadians(Latitude2);
		Longitude2 = Math.toRadians(Longitude2);

		final double R = 6371.0; // earth's mean radius in km
		double  dSinLat05  = Math.sin( (Latitude2 - Latitude1)/2 );
		double  dSinLong05 = Math.sin( (Longitude2 - Longitude1)/2 );
		double  a = dSinLat05 * dSinLat05 +
		Math.cos(Latitude1) * Math.cos(Latitude2) * dSinLong05 * dSinLong05;
		double  c = (0==a || 1==a)
		? 0
				: 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0-a));
		return R * c * 1000.0; //in meters
	}
	
	public Rectangle getBoundingBox() {
		return boundingBox;
	}
	
	public void setBoundingBox(int width, int height) {
		boundingBox.setSize(width, height);
	}
	
	public void setNode(Node node) {
		this.node = node;
	}
	
	public Node getNode() {
		return node;
	}
	
	public void setLine(Line line) {
		this.line = line;
	}
	
	public Line getLine() {
		return line;
	}
	
	public void setColorProperties(ColorProperties colorProperties) {
		this.colorProperties = colorProperties;
	}
	
	public ColorProperties getColorProperties() {
		return colorProperties;
	}
	
	public void setZoomLevel(int zoomLevel) {
		this.zoomLevel = zoomLevel;
	}
	
	public int getZoomLevel() {
		return zoomLevel;
	}
	
	public void addShowList(String type) {	
		showList.add(type.toUpperCase());
	}
	
	public void addShowList(String type, int number) {
		showList.add(type.toUpperCase() + ":" + number);
	}

	public void removeShowList(String type) {
		showList.remove(type.toUpperCase());
	}
	
	public void removeShowList(String type, int number) {
		showList.remove(type.toUpperCase() + ":" + number);
	}
	
	public void clearShowList() {
		showList.clear();
	}
	
	public HashSet getShowList() {
		return showList;
	}

	public boolean containsShowList(String type) {
		return showList.contains(type.toUpperCase());
	}
	
	public boolean containsShowList(String type, int number) {
		return showList.contains(type.toUpperCase() + ":" + number);
	}
	
	public int getShowListSize() {
		return showList.size();
	}
	
	public void addSwingLayer(SwingLayer swingLayer) {
		swingLayers.add(swingLayer);
	}
	
	public void removeSwingLayer(SwingLayer swingLayer) {
		swingLayers.remove(swingLayer);
	}
	
	public void clearSwingLayers() {
		swingLayers.clear();
	}
	
	public ArrayList<SwingLayer> getSwingLayers() {
		return swingLayers;
	}
	
	public int getShapeWidth() {
		return shapeWidth[getZoomLevel()>=22?21:(getZoomLevel()<=8 ? 8:getZoomLevel())];
	}
	
	public Point2D getPixelPos(double latitude, double longitude) {
		double sinLatitude = Math.sin(Math.toRadians(latitude));
		return new Point2D.Double(
				((longitude + 180.0) / 360.0) * 256.0 * (1<<zoomLevel),
				( 0.5 - Math.log((1.0+sinLatitude)/(1.0-sinLatitude))
						/(4.0*Math.PI) )*256.0*(1<<zoomLevel)
		);
	}
	
	private boolean checkTransform(Rectangle2D geoArea) {
		System.out.println("%%%%%%map.checkTransform start");
		if (!this.geoArea.equals(geoArea)) {
			Point2D point1 = getPixelPos(geoArea.getMinY(), geoArea.getMinX());
			Point2D point2 = getPixelPos(geoArea.getMaxY(), geoArea.getMaxX());
			mapArea.setRect(point1.getX(), point2.getY(),
					boundingBox.getWidth(), boundingBox.getHeight());
			this.geoArea.setRect(geoArea);
			resetTransform(boundingBox, mapArea);
			System.out.println("%%%%%%map.checkTransform end 1");
			return true;
		}
		
		System.out.println("%%%%%%map.checkTransform end 2");
		return false;
	}
	
	private void resetTransform(Rectangle boundingBox, Rectangle2D mapArea) {
		System.out.println("%%%%%%map.resetTransform start");
		if (mapArea == null || boundingBox.getWidth() == 0 || boundingBox.getHeight() == 0 ) {
			System.out.println("%%%%%%map.resetTransform end 1");
			return;
		}

		transform =  new AffineTransform(mapArea.getWidth() / boundingBox.getWidth(),
				0.0, 0.0, mapArea.getHeight() / boundingBox.getHeight(),
				mapArea.getMinX(), mapArea.getMinY());
		System.out.println("%%%%%%map.resetTransform end 2");		
	}
	
	protected AffineTransform getTransform() {
		if (transform != null) {
			return new AffineTransform(transform);
		}

		return null;
	}
	
	public Point2D getScreenPointFromPixel(double xPixel, double yPixel)  {
		try {
			return getTransform().inverseTransform(new Point2D.Double(xPixel, yPixel), null);
		} catch (NoninvertibleTransformException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}
	
	public Point2D getScreenPointFromLonLat(double longitude, double latitude) {
		Point2D point = getPixelPos(latitude, longitude);
		return getScreenPointFromPixel(point.getX(), point.getY());
	}
	
	public Point2D getLonLatFromPixel(int x1, int y1) {
		double x = (double) x1 / 256;
		double y = (double) y1 / 256;
		double lon      = -180; // x
		double lonWidth = 360; // width 360

		//double lat = -90;  // y
		//double latHeight = 180; // height 180
		double lat       = -1;
		double latHeight = 2;

		int tilesAtThisZoom = 1 << getZoomLevel();
		lonWidth  = 360.0 / tilesAtThisZoom;
		lon       = -180 + (x * lonWidth);
		latHeight = -2.0 / tilesAtThisZoom;
		lat       = 1 + (y * latHeight);
		
		// convert lat and latHeight to degrees in a transverse mercator projection
		// note that in fact the coordinates go from about -85 to +85 not -90 to 90!
		latHeight += lat;
		latHeight = (2 * Math.atan(Math.exp(Math.PI * latHeight))) - (Math.PI / 2);
		latHeight *= (180 / Math.PI);

		lat = (2 * Math.atan(Math.exp(Math.PI * lat))) - (Math.PI / 2);
		lat *= (180 / Math.PI);
		
		latHeight -= lat;

		if (lonWidth < 0) {
			lon      = lon + lonWidth;
			lonWidth = -lonWidth;
		}

		if (latHeight < 0) {
			lat       = lat + latHeight;
			latHeight = -latHeight;
		}

		return new Point2D.Double(lon, lat + latHeight);
	}
	
	public ArrayList getImage(final HttpServletRequest request, Rectangle2D geoArea) {
		Object showListArr[] = ((HashSet)getShowList()).toArray();	
		BufferedImage image = new BufferedImage(boundingBox.width, boundingBox.height, BufferedImage.TYPE_INT_ARGB);
		final Graphics2D g2d = image.createGraphics();
		// LEGEND INFO 
		BufferedImage legendImage = null;
		Graphics2D g2Legend = null;
		if(showLegend) {
			legendImage = new BufferedImage(boundingBox.width, (int)(20*showListArr.length) + 20, BufferedImage.TYPE_INT_ARGB);
			g2Legend = legendImage.createGraphics();
			g2Legend.setBackground(Color.WHITE);
		}

		checkTransform(geoArea);
		
		boolean shapefileLayerPainted = false;
		boolean swingLayerPainted = false;

		Object object = request.getAttribute("server_process_interrupted");
		if (object != null && ((Boolean) object)) {
			System.out.println("interrupted");
			g2d.dispose();
			return null;
		}

		for (SwingLayer layer : swingLayers) {
			swingLayerPainted = swingLayerPainted || layer.paintLayer(request, g2d, boundingBox, mapArea, g2Legend);
		}

		ArrayList imageArr = new ArrayList();
	    //if(showLegend) layer.paintLegend(g2Legend);	 
		
		g2d.dispose();
		if(showLegend)
			g2Legend.dispose();
		object = request.getAttribute("server_process_interrupted");
		
		if (object != null && ((Boolean) object)) {
			System.out.println("interrupted");
			return imageArr;
		}
		else if (!shapefileLayerPainted && !swingLayerPainted) {
			System.out.println("not painted");
			return imageArr;
		}
		
		imageArr.add(image);
		if(g2Legend!=null) {
			imageArr.add(legendImage);
		}
		return imageArr;
	}
	
	public Object singleLeftClick(double longitude, double latitude, Rectangle2D geoArea) {
		System.out.println("%%%%%%map.singleLeftClick start");
		System.out.println("%%%%%%map.singleLeftClick check transform start");
		checkTransform(geoArea);
		System.out.println("%%%%%%map.singleLeftClick check transform end");
		Point2D screenPoint = getScreenPointFromLonLat(longitude, latitude);
		System.out.println("%%%%%%map.singleLeftClick getting nodeExist array ");
		ArrayList<NodeInfo> existNodeInfo = node.nodeExist(screenPoint);
		
		if (existNodeInfo == null) {
			ArrayList<LineInfo> existLineInfo = line.lineExist(screenPoint);
			
			if (existLineInfo == null) {
				
			}
			else {
				System.out.println("%%%%%%map.singleLeftClick end 1");
				return existLineInfo;
			}
		}
		else {
//			if (existNodeInfo.size() == 1) {
//				NodeInfo nodeInfo = existNodeInfo.get(0);
//				node.getNodeCollection().clearSelectedNode();
//				node.getNodeCollection().addSelectedNode(nodeInfo.getID(), nodeInfo.getLegendID());
//				return getSelectedImage(geoArea);
//			}
//			else {
//				return existNodeInfo;
//			}
			
			System.out.println("%%%%%%map.singleLeftClick end 2");
			return existNodeInfo;
		}
		
		System.out.println("%%%%%%map.singleLeftClick end 3");
		return null;
	}

	public String getCurrentYearMonth() {
		return currentYearMonth;
	}

	public void setCurrentYearMonth(String currentYearMonth) {
		this.currentYearMonth = currentYearMonth;
	}

	public Rectangle2D getDefaultBoundary() {
		return defaultBoundary;
	}

	public void setDefaultBoundary(Rectangle2D defaultBoundary) {
		this.defaultBoundary = defaultBoundary;
	}
	
	public boolean isShowLegend() {
		return showLegend;
	}

	public void setShowLegend(boolean showLegend) {
		this.showLegend = showLegend;
	}

	public String getDataLoaded() {
		return dataLoaded;
	}

	public void setDataLoaded(String dataLoaded) {
		this.dataLoaded = dataLoaded;
	}
	
}