-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathGeoFenceRenderer.java
175 lines (151 loc) · 6.59 KB
/
GeoFenceRenderer.java
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
/*
* Copyright 2015 Vitaly Litvak (vitavaque@gmail.com)
*
* 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.traccar.web.client.view;
import org.gwtopenmaps.openlayers.client.Style;
import org.gwtopenmaps.openlayers.client.feature.VectorFeature;
import org.gwtopenmaps.openlayers.client.geometry.*;
import org.gwtopenmaps.openlayers.client.layer.Vector;
import org.gwtopenmaps.openlayers.client.util.JSObject;
import org.traccar.web.client.GeoFenceDrawing;
import org.traccar.web.shared.model.GeoFence;
import org.traccar.web.shared.model.LonLat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GeoFenceRenderer {
private final MapView mapView;
private final Map<Long, GeoFenceDrawing> drawings = new HashMap<>();
public GeoFenceRenderer(MapView mapView) {
this.mapView = mapView;
}
protected Vector getVectorLayer() {
return mapView.getGeofenceLayer();
}
public void drawGeoFence(GeoFence geoFence, boolean drawTitle) {
switch (geoFence.getType()) {
case CIRCLE:
drawCircle(geoFence, drawTitle);
break;
case POLYGON:
drawPolygon(geoFence, drawTitle);
break;
case LINE:
drawLine(geoFence, drawTitle);
break;
}
}
public void removeGeoFence(GeoFence geoFence) {
GeoFenceDrawing drawing = getDrawing(geoFence);
if (drawing != null) {
getVectorLayer().removeFeature(drawing.getShape());
getVectorLayer().removeFeature(drawing.getTitle());
drawings.remove(geoFence.getId());
}
}
private void drawCircle(GeoFence circle, boolean drawTitle) {
LonLat center = circle.points().get(0);
Polygon circleShape = Polygon.createRegularPolygon(mapView.createPoint(center.lon, center.lat), applyMercatorScale(center.lat, circle.getRadius()), 100, 0f);
Style st = new Style();
st.setFillOpacity(0.3);
st.setStrokeWidth(1.5);
st.setStrokeOpacity(0.8);
st.setStrokeColor('#' + circle.getColor());
st.setFillColor('#' + circle.getColor());
VectorFeature drawing = new VectorFeature(circleShape, st);
getVectorLayer().addFeature(drawing);
VectorFeature title = drawName(circle.getName(), mapView.createPoint(center.lon, center.lat));
drawings.put(circle.getId(), new GeoFenceDrawing(drawing, title));
if (drawTitle) {
getVectorLayer().addFeature(title);
}
}
private void drawPolygon(GeoFence polygon, boolean drawTitle) {
List<LonLat> lonLats = polygon.points();
Point[] points = new Point[lonLats.size()];
int i = 0;
for (LonLat lonLat : lonLats) {
points[i++] = mapView.createPoint(lonLat.lon, lonLat.lat);
}
Polygon polygonShape = new Polygon(new LinearRing[] { new LinearRing(points) });
Style st = new Style();
st.setFillOpacity(0.3);
st.setStrokeWidth(1.5);
st.setStrokeOpacity(0.8);
st.setStrokeColor('#' + polygon.getColor());
st.setFillColor('#' + polygon.getColor());
VectorFeature drawing = new VectorFeature(polygonShape, st);
getVectorLayer().addFeature(drawing);
Point center = getCollectionCentroid(polygonShape);
VectorFeature title = drawName(polygon.getName(), center);
drawings.put(polygon.getId(), new GeoFenceDrawing(drawing, title));
if (drawTitle) {
getVectorLayer().addFeature(title);
}
}
private void drawLine(GeoFence line, boolean drawTitle) {
List<LonLat> lonLats = line.points();
Point[] linePoints = new Point[lonLats.size()];
int i = 0;
for (LonLat lonLat : lonLats) {
linePoints[i++] = mapView.createPoint(lonLat.lon, lonLat.lat);
}
LineString lineString = new LineString(linePoints);
VectorFeature lineFeature = new VectorFeature(lineString);
lineFeature.getAttributes().setAttribute("widthInMeters", applyMercatorScale(lonLats.get(0).lat, line.getRadius()));
lineFeature.getAttributes().setAttribute("lineColor", '#' + line.getColor());
getVectorLayer().addFeature(lineFeature);
VectorFeature title = drawName(line.getName(), getCollectionCentroid(lineString));
drawings.put(line.getId(), new GeoFenceDrawing(lineFeature, title));
if (drawTitle) {
getVectorLayer().addFeature(title);
}
}
/**
* Applies scale to specified radius in meters so it is precisely drawn on map
*
* @see <a href="http://osdir.com/ml/openlayers-users-gis/2012-09/msg00034.html">http://osdir.com/ml/openlayers-users-gis/2012-09/msg00034.html</a>
* @see <a href="http://osdir.com/ml/openlayers-users-gis/2012-09/msg00036.html">http://osdir.com/ml/openlayers-users-gis/2012-09/msg00036.html</a>
*/
private float applyMercatorScale(double latitude, float radius) {
return (float) (radius / Math.cos(latitude * Math.PI / 180));
}
private VectorFeature drawName(String name, Point point) {
org.gwtopenmaps.openlayers.client.Style st = new org.gwtopenmaps.openlayers.client.Style();
st.setLabel(name);
st.setLabelAlign("cb");
st.setFontColor("#FF9B30");
st.setFontSize("14");
st.setFill(false);
st.setStroke(false);
return new VectorFeature(point, st);
}
private static Point getCollectionCentroid(Collection collection) {
JSObject jsPoint = getCollectionCentroid(collection.getJSObject());
return Point.narrowToPoint(jsPoint);
}
public static native JSObject getCollectionCentroid(JSObject collection) /*-{
return collection.getCentroid(false);
}-*/;
public GeoFenceDrawing getDrawing(GeoFence geoFence) {
return drawings.get(geoFence.getId());
}
public void selectGeoFence(GeoFence geoFence) {
GeoFenceDrawing drawing = getDrawing(geoFence);
if (drawing != null) {
mapView.getMap().zoomToExtent(drawing.getShape().getGeometry().getBounds());
}
}
}