/
routing.js
245 lines (219 loc) · 8.21 KB
/
routing.js
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
'use strict';
var gsc = gsc || {};
gsc.routing = (function() {
/**
* This module
* @exports gsc/routing
*/
var mod = {};
/**
* Create a new instance of a Route object
*
* @class - A class that represents a calculated route with a start and an end point
* @property {String} name - A descriptive name that the route will be saved under. Will also be used in the route list.
* @property {ol.geom.Point} startingPoint - A geojson object that defines a route
* @property {ol.geom.Point} endPoint - A geojson object that defines a route
* @property {Number} distance - A geojson object that defines a route
* @property {GeoJSON.FeatureCollection} segments - A geojson FeatureCollection object that defines the segments of the route
* @param {String} name - A descriptive name that the route will be saved under. Will also be used in the route list.
* @param {ol.geom.Point} startingPoint - The longitude/latitude pair that defines the start of the route
* @param {ol.geom.Point} endPoint - The longitude/latitude pair that defines the end point of the route
* @param {Number} distance - A geojson object that defines a route
* @param {GeoJSON.FeatureCollection} segments - A geojson FeatureCollection object that defines the segments of the route
* @example <caption>Create a new instance of the .Route object</caption>
* var point1 = new ol.geom.Point(1,1);
* var point2 = new ol.geom.Point(2,2);
* var route = new gsc.routing.Route('A to B', point1, point2, 112, routeGeoJson);
*/
mod.Route = function(name, startingPoint, endPoint, distance, segments) {
};
mod.Request = function(url) {
return jQuery.ajax({
method: 'GET',
url: url,
//dataType: 'application/json',
//crossDomain: true
});
};
/**
* Get a vector layer with displaying a route
*
* @param {double} lon1 - Longitiude of start point
* @param {double} lat1 - Latitude of the start point
* @param {double} lon2 - Longitude of the end point
* @param {double} lat2 - Latitude of the end point
* @returns {ol.layer.Vector} - The route as an OpenLayers layer
* @example <caption>Calculate a route</caption>
* gsc.routing.calculateRoute(2.8214,41.9794,2.8314,41.9694)
* .then(function(routeResponse) {
* var vectorLayer = routeResponse;
* // Do something with the vector layer - typically add it to an OpenLayers map
* }, function(err) {
* // An error occurred
* console.debug('Could not calculate route: ' + err);
* });
*/
mod.calculateRoute = function(lon1, lat1, lon2, lat2) {
var defaultStyles = {
'LineString': new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'green',
width: 5
})
}),
'MultiLineString': new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'green',
width: 5
})
})
};
var defaultStyleFunction = function(feature) {
return defaultStyles[feature.getGeometry().getType()];
};
var dfd = jQuery.Deferred();
var routingUrl = 'http://hub.geosmartcity.eu/' +
'GironaRouting/geo/RestService/getroute?';
routingUrl += ('x1=' + lon1 + '&y1=' + lat1 +
'&x2=' + lon2 + '&y2=' + lat2);
routingUrl += '&csurl=';
jQuery.getJSON('http://geosmartcity.avinet.no/proxy/index.php', {
csurl: 'http://hub.geosmartcity.eu/' +
'GironaRouting/geo/RestService/getroute',
x1: lon1,
y1: lat1,
x2: lon2,
y2: lat2
}).then(function(routeGeoJson) {
var readFeatures = new ol.format.GeoJSON()
.readFeatures(routeGeoJson, {
featureProjection: 'EPSG:3857',
dataProjection: 'EPSG:4326'
});
var routeLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: readFeatures
}),
style: defaultStyleFunction
});
dfd.resolve(routeLayer);
}, function(err) {
//console.log(err);
dfd.reject(false);
});
return dfd;
};
/**
* Alias for calculateRoute
* @deprecated
*/
mod.routing = mod.calculateRoute;
/**
* Persistently stores a route associated with the user id of the currently logged in user
*
* @param {gsc.routing.Route} route - The route to add
* @returns {gsc.Response}
* @example <caption>Save a route</caption>
* gsc.user.login() // All requests must take place inside a valid user session
* .then(function(success)) {
* var r = new gsc.routing.Route();
* gsc.routing.addRoute(r)
* .then(function(routeResponse) {
* var addedRoute = routeResponse.data;
* // Do something with the saved route
* }, function(err) {
* // Could not save the route
* console.debug('Could not save route: ' + err);
* });
* }, function(err) {
* // Could not login user
* console.debug('Could not authenticate user: ' + err);
* );
*/
mod.addRoute = function(route) {
return gsc.doPost('addRoute', {
route: route
});
};
/**
* Remove a stored route from the account of the currently logged in user
*
* @param {gsc.routing.Route} route - The route object to remove
* @returns {Promise.<gsc.Response>} - A gsc.routing.Route object wrapped in a gsc.Response
* @example <caption>Remove a saved route</caption>
* gsc.routing,getRouteList() // Load the existing routes
* .then(function(routeList) {
*
* // Pick a route to remove, must check if list has elements...
* var routeToRemove = routeList[0];
*
* gsc.routing.removeRoute(routeToRemove)
* .then(function(removeRouteResponse) {
* var removedRoute = removeRouteResponse.data;
* // Do something with the removed route
* }, function(err) {
* // Handle error
* console.debug('An error occurred while removing route: ' + err);
* });
* }, function(err) {
* // Handle error
* console.debug('User authentication failed: ' + err);
* });
*/
mod.removeRoute = function(route) {
return gsc.doPost('removeRoute', {
route: route
});
};
/**
* Retrieves a list of all stored routes for the currently logged in user
*
* @returns {gsc.Response} - An array of {@link gsc/storage.Route} objects wrapped in a standard
* {@link gsc.Response} web service response object
* @example <caption>Get list of the route saved for the current user</caption>
* gsc.routing.getRouteList()
* .then(function(routeListResponse) {
* var routeList = routeListResponse.data;
* // Do something with the route
* }, function(err) {
* // An error occurred, handle it
* console.debug('Err');
* });
*/
mod.getRouteList = function() {
return gsc.doPost('getRouteList');
};
/**
* Adds a widget on top of an Open Layers 3 map object
*
* @param {ol.Map} olMap - A pre-existing and initialized OpenLayers 3 Map object
* @example <caption>Add a route list widget to an existing OpenLayers map object</caption>
*
* var olMap = new ol.Map({
* // Map creation options
* });
*
* gsc.routing.savedRouteListWidget(olMap);
*
*/
mod.savedRouteListWidget = function(olMap) {
};
/**
* Display a calculated route on top of an existing OpenLayers3 map object instance
*
* @param {gsc.routing.Route} route - Route data object as returned by routing service
* @param {ol.Map} olMap - A pre-existing and initialized OpenLayers 3 Map object
* @example <caption>Add a route list widget to an existing OpenLayers map object</caption>
* // First instantiate a map (shown simplified here, for details, please refer to OpenLayers docs)
* var olMap = new ol.Map({
* // Map creation options
* });
*
* // Then simply add the widget on top of the map object using this static method
* gsc.routing.viewRouteWidget(olMap);
*
*/
mod.viewRouteWidget = function(route, olMap) {
};
return mod;
}());