-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
378 lines (313 loc) · 10.8 KB
/
index.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
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
//get jquery and define global variables
var $ = giscloud.exposeJQuery();
var viewer, toolbar, author, map, layer, table, mapId, layerId, listOfObjects = [], listOfFestivalLocations = [], listOfFestivalTitles = [], listOfFestivalCoord = [], listOfFestivalUrl = [], listOfFestivalCity =[], listOfFestivalCountry = [];
//start my app
giscloud.ready(function(){
//show map (public map) and toolbar
viewer = new giscloud.Viewer("map", "256495");
toolbar = new giscloud.ui.Toolbar({
viewer: viewer,
container: "toolbar",
defaultTools: ["pan", "zoom", "full", "measure", "select"]
});
//click on feature -> start displayFeatures function
viewer.selectionChange (function (event) {
if (event.action === "add") {
giscloud.features.byId(event.feature.layerId, event.feature.featureId)
.done(displayFeatures);
}
});
//login part -> api key (e442091480d0f1a04621) is generated by Gis Cloud - you can use this one, or
//you can make your own application on Gis Cloud: My Account -> API access -> Applications
giscloud.oauth2.authorize("e442091480d0f1a04621", function() {
giscloud.oauth2.button("login");
});
//jquery click event -> submit input data
$('#save').click(function(){
//empty lists
listOfObjects = []; listOfFestivalLocations = []; listOfFestivalTitles = []; listOfFestivalCoord = []; listOfFestivalUrl = []; listOfFestivalCity =[]; listOfFestivalCountry = [];
//get input data
author = $('#author').val();
map = $('#mapName').val();
layer = $('#layerName').val();
table = $('#tableName').val();
//empty divisions for alerts and for showing saved data
$('#response1,#response2,#response3,#response4,#text,#featureData').empty();
//if input data exist -> start application
if(author,map,layer,table){
//if token not exist -> alert that you are not loged in
var token = giscloud.oauth2.token();
if (!token) {
$('#text').append('Please log in!');
return;
}
//if you are loged in -> start checkAuthor function
else {
checkAuthor();
}
}
//if input data not exist -> alert that you must fill every space
else {
if(!author){
$('#response1').append('*fill that space!');
}
if(!map){
$('#response2').append('*fill that space!');
}
if(!layer){
$('#response3').append('*fill that space!');
}
if(!table){
$('#response4').append('*fill that space!');
}
}
});
});
//function that check every artist and save specific data for his concerts
function checkAuthor() {
//api key for last.fm -> write it between quotes
var apiKey = '';
//get artist data
$.get('http://ws.audioscrobbler.com/2.0/?method=artist.getevents&artist='+author+'&api_key='+apiKey+'&format=json', function(data){
//define empty object and save artist data
var objects = {};
objects = data.events;
//save artist data in list
for(var i in objects){
listOfObjects.push(objects[i]);
}
//if value of sixth place in list is equal to zero -> artist is known but there is no data for his concerts
if(listOfObjects[6] == 0) {
$('#text').append('No data for this artist!');
$('#mapName,#layerName,#tableName').val('');
return;
}
//if there is no artist -> probably grammar error
if(!objects) {
$('#text').append('No artist in database!');
$('#mapName,#layerName,#tableName').val('');
return;
}
//if there is data for artist concerts
else {
//check length of list of artist data
var listLength = listOfObjects[0].length;
//if length is "undefined" then we have only one concert for that artist
if (typeof listLength == "undefined") {
//save specific data for that one concert
listOfFestivalTitles.push(listOfObjects[0].title);
listOfFestivalUrl.push(listOfObjects[0].url);
listOfFestivalLocations.push(listOfObjects[0].venue.location);
listOfFestivalCoord.push(listOfFestivalLocations[0]['geo:point']);
listOfFestivalCity.push(listOfFestivalLocations[0].city);
listOfFestivalCountry.push(listOfFestivalLocations[0].country);
//if that concert doesn't have informations about concert geolocation -> stop application and
//alert "no data for this author"
if (!listOfFestivalCoord[0]['geo:long']) {
$('#text').append('No data for this artist!');
$('#mapName,#layerName,#tableName').val('');
return;
}
}
//if length is defined we have more then one concert for artist
else {
//save specific data for all concerts
for(var i = 0; i < listOfObjects[0].length; i++){
listOfFestivalTitles.push(listOfObjects[0][i].title);
listOfFestivalUrl.push(listOfObjects[0][i].url);
listOfFestivalLocations.push(listOfObjects[0][i].venue.location);
listOfFestivalCoord.push(listOfFestivalLocations[i]['geo:point']);
listOfFestivalCity.push(listOfFestivalLocations[i].city);
listOfFestivalCountry.push(listOfFestivalLocations[i].country);
}
}
//start saveMap function
saveMap();
}
});
}
//function that defines and creates (saves) map for chosen artist
function saveMap() {
//map definition
var mapDef = {
name: map,
units: "meter"
};
//create map
giscloud.maps.create(mapDef)
//if fails -> alert
.fail(function () {
$("#text").append("Problems with saving your map name!");
return;
})
//if succeeds -> get new map id and start saveTable function
.done(function (newMapId) {
mapId = newMapId;
saveTable();
});
}
//function that defines and creates table for artist data
function saveTable() {
//clean table name and add time to table name (excludes same table name)
table = table.toLowerCase().replace(/(^[^a-z]+)|(\W+)/g, '_').substr(0, 50);
table += $.now();
//table definition
tableDef = {
name: table,
geometry: "point",
srid: 4326,
columns: {
"Country": { "type": "text" },
"City": { "type": "text" },
"Web_page": { "type": "text" },
"Name_of_concert": { "type": "text" }
}
}
//create table
giscloud.tables.create(tableDef)
//if fails -> alert
.fail(function () {
$("#text").append("Problems with saving your table name!");
return;
})
//if succeeds -> start saveLayer function
.done(function () {
saveLayer();
});
}
//function that defines and creates base and feature layer for artist data
function saveLayer() {
//base layer definition
var layerDef = {
map_id: mapId,
name: "MapQuest OSM",
source: { "type": "tile", "src": "mapquest-osm" },
type: "tile",
x_min: "-4500000.000000", x_max: "4500000.000000",
y_min: "-9000000.000000", y_max: "9000000.000000",
visible: true
};
// create basemap layer
(new giscloud.Layer(layerDef)).update()
//if fails -> alert
.fail(function () {
$("#text").append("Problems with saving your base layer!");
return;
})
//if succeeds -> define feature layer
.done(function () {
// now add the feature layer
layerDef.name = layer;
layerDef.type = "point";
layerDef.styles = [{ "symbol": { "type": "circle", "color":"9,195,0", "border":"0,0,204", "bw":"3", "size":"6" } }];
layerDef.source = {
"type": "pg",
"src": table
};
//create feature layer
(new giscloud.Layer(layerDef)).update()
//if fails -> alert
.fail(function () {
$("#text").append("Problems with saving your layer!");
return;
})
//if succeeds -> get new feature layer id and start saveFeature function
.done(function (newLayerId) {
layerId = newLayerId;
saveFeature();
});
});
}
//function that stores every feature with specific data in table and shows: map + base layer + layer with features
function saveFeature() {
//define variables and check length of list of artist data
var featureDef;
var listLength = listOfObjects[0].length;
//if length is "undefined" then we have only one concert for that artist
if (typeof listLength == "undefined") {
// prepare feature definition
featureDef = {
geometry: new giscloud.geometry.Point(listOfFestivalCoord[0]['geo:long'], listOfFestivalCoord[0]['geo:lat']).toOGC(),
data: {
"Country": listOfFestivalCountry[0],
"City": listOfFestivalCity[0],
"Web_page": listOfFestivalUrl[0],
"Name_of_concert": listOfFestivalTitles[0]
}
};
// create a new feature
giscloud.features.create(layerId, featureDef)
//if fails -> alert
.fail(function() {
$('#text').append('Problem with data saving!');
return;
})
//if succeeds -> show map with basemap layer and feature layer
.done(function() {
viewer.loadMap(mapId);
});
return;
}
//if length is defined we have more then one concert for artist
else {
//for loop -> for every concert
for(var i = 0; i < listLength; i++){
//if concert have informations about concert geolocation
if(listOfFestivalCoord[i]['geo:long'] && listOfFestivalCoord[i]['geo:lat']){
// prepare feature definition
featureDef = {
geometry: new giscloud.geometry.Point(listOfFestivalCoord[i]['geo:long'], listOfFestivalCoord[i]['geo:lat']).toOGC(),
data: {
"Country": listOfFestivalCountry[i],
"City": listOfFestivalCity[i],
"Web_page": listOfFestivalUrl[i],
"Name_of_concert": listOfFestivalTitles[i]
}
};
// create a new feature
giscloud.features.create(layerId, featureDef)
//if fails -> alert
.fail(function () {
$('#text').append('Problem with data saving!');
return;
})
//if succeeds -> show map with basemap layer and feature layer
.done(function () {
viewer.loadMap(mapId);
});
}
}
}
}
//function that displays data of feature in table below map
function displayFeatures (feature) {
//define variables and get feature data
var value, attr, data = feature.data;
//if feature data exists
if(data) {
//empty division with feature data
$('#featureData').empty();
//define counter for positioning on the page
var i = 0;
//for loop -> for every attribute in feature data
for(attr in data) {
//save attribute value
value = data[attr];
//define style for attribute -> display attribute in left side of division
var attrStyle = "width:50%;height:18px;margin:0px;padding:0px;top:"+i+"px;left:0px;position:absolute;font-size:50%;text-align:center;";
//define style for attribute value -> display attribute value in right side of division
var dataStyle = "width:50%;height:18px;margin:0px;padding:0px;top:"+i+"px;left:50%;position:absolute;font-size:50%;text-align:center;";
//define style for paragraph
var pStyle = "margin:0px; padding:0px; white-space:nowrap; width:100%; overflow:hidden; text-overflow:ellipsis;";
//if attribute is web page
if(attr === "web_page") {
//attribute value is hyperlink
value = '<a target="_blank" href="'+value+'">'+value+'</a>';
}
//display feature data
$('#featureData').append('<tr><td style="'+attrStyle+'"><p style="'+pStyle+'">'+attr+'</p></td><td style="'+dataStyle+'"><p style="'+pStyle+'">'+value+'</p></td></tr>');
//count new position
i = i+18;
}
}
}