-
Notifications
You must be signed in to change notification settings - Fork 0
/
arcgis-server.js
688 lines (559 loc) · 24.4 KB
/
arcgis-server.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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
(function () {
var myConnector = tableau.makeConnector();
//Holds the schemas of table data to be delivered to Tableau
var _tableSchemas = [];
//Connection Info
var _api_params = {
url_type: '', //One of root, query, geojson
base_url: '',
token: '',
username: '',
services: [],
folders: [],
layers: [],
schema_properties: [],
protocol: 'https', //or http
https_proxy_url: "http://localhost:8889/", //This one forwards requests to HTTPS endpoints only
http_proxy_url: "http://localhost:8890/", //This one forwards requests to HTTP only
json_format_querystirng: 'f=json',
getRootListURL: function(folder) {
if(folder) return this.root_list_url = this.proxy_url + this.base_url + "services/" + folder
return this.root_list_url = this.proxy_url + this.base_url;
},
service_url_pattern: "services/{servicename}/{servicetype}",
getServiceURL: function(serviceObj) {
return this.proxy_url + this.base_url + this.service_url_pattern.replace("{servicename}", serviceObj.name).replace("{servicetype}", serviceObj.type);
},
getProxyURL: function(url){
return this.proxy_url + url;
},
setProtocol: function(p){
if(p == "https"){
this.proxy_url = this.https_proxy_url;
}
else if(p == "http"){
this.proxy_url = this.http_proxy_url;
}
},
formatForJSON: function(url){
if(url.indexOf("?") > -1){
return url + "&" + this.json_format_querystirng;
}
else{
return url + "?" + this.json_format_querystirng;
}
},
query_geojson_pattern: "/query?where=1%3D1&outFields=*&outSR=4326&f=json",
queryLayerFormatForJSON: function(url){
//Will format a layer's endpoint to add on the default query '/query?where=1%3D1&f=geojson' (brings back all records in geojson format)
//If a query is passed in preserve its querystring options, and make sure we've got the minimum covered so it'll work properly
if(url.indexOf("query?") > -1){
//It's got some stuff after the ?
var root = url.split("?")[0];
var qs = url.split("?")[1]; //just the last part
var qs_arr = qs.split("&");
//Check for the required items
if(qs.indexOf('where=') == -1){
//not there. Add it
qs_arr.push('where=1%3D1')
}
if(qs.indexOf('outFields=') == -1){
//not there. Add it
qs_arr.push('outFields=*')
}
if(qs.indexOf('outSR=') == -1){
//not there. Add it
qs_arr.push('outSR=4326')
}
if(qs.indexOf('f=json') == -1){
//not there. Add it
qs_arr.push('f=json')
}
//join 'em and send it out
return root + "?" + qs_arr.join("&");
}
return url + this.query_geojson_pattern;
}
};
myConnector.getSchema = function (schemaCallback) {
//This is the main entry point. When reusing the same connector for different URLs, make sure to reset all of the globals
//The serialized info from the interactive page magically appears here in the tableau.connectionData property
var user_info = JSON.parse(tableau.connectionData);
_api_params.username = user_info.username;
_api_params.password = user_info.password;
var url = user_info.server_url;
//Which protocol has been entered into the box?
if(url.indexOf("https://") > -1){
_api_params.setProtocol("https");
}
else if(url.indexOf("http://") > -1){
_api_params.setProtocol("http");
}
//replace http:// or https:// when we're using the proxy for dev
url = url.replace("http://", "").replace("https://", "");
//Depending on what's entered in the textbox, decide how to handle it
_api_params.url_type = parseURLForType(url);
//Option 1 is an ArcGIS Server Root URL /arcgis/rest/
//Option 2 is a fulll path to a query output: https://services1.arcgis.com/1KAnay3h8WGQTI9I/arcgis/rest/services/oregon_state/FeatureServer/0/query?outFields=*&where=1%3D1
//Option 3 is a URL to a geojson file
if (_api_params.url_type == 'root'){
_api_params.base_url = url;
//Given the server url, go find all of the available datasources
getSchemasFromRoot(function(){
schemaCallback(_tableSchemas);
});
}
else if(_api_params.url_type == 'query'){
_api_params.layers.push({url: _api_params.getProxyURL(url)});
getLayerSchemas(function(){
schemaCallback(_tableSchemas);
});
}
else if (_api_params.url_type == 'geojson'){
//https://opendata.arcgis.com/datasets/c0881d0e88dc411a8b4bbe6b86526041_0.geojson
_api_params.layers.push({url: _api_params.getProxyURL(url)});
getLayerSchemasFromGeoJSON(function(){
schemaCallback(_tableSchemas);
});
}
};
myConnector.getData = function (table, doneCallback) {
getLayerRecords(table, function(){
tableau.log("Finished Getting Layer Records, about to call donecallback();");
doneCallback();
})
};
tableau.registerConnector(myConnector);
//Gets the expected schema per table. We'll have to flatten the JSON response a little to shoehorn it into this structure
function getSchemasFromRoot(callback) {
//Dynamically crawl the rest endpoint and see which resources have the Query capability, then generate a list and find out the field types to build the schema.
discoverServices(function(){
//Services have been stored in _api_params.services array.
//We may also have picked up some folders, which need to be explored. Do that now.
discoverFolderizedServices(function(){
//Now find out which ones have Layers with query endpoints and gather those up.
discoverQueryableLayerEndpoints(function(){
//Now, we have a bunch of layers(tables), each with different schemas. Build up the table array.
getLayerSchemas(function(){
callback();
})
})
})
})
}
function discoverServices(callback){
var url = _api_params.getRootListURL();
//Then try to list styles and populate the dropdown
$.getJSON(_api_params.formatForJSON(url), function (resp, txtstatus, jqxhr) {
var server = resp;
//Iterate over the JSON object
//Should see currentVersion, folders, services
//Focus on root level services for now. Find the query endpoints for layers.
if (server && server.services){
for (var i = 0, len = server.services.length; i < len; i++) {
if(server.services[i].type == 'MapServer'){
//Bingo! This is an endpoint that has queryable layers (probably). We want to expore it more.
_api_params.services.push(server.services[i]);
}
}
}
//Do folders too - folders hold services in an organized manner
if (server && server.folders){
for (var i = 0, len = server.folders.length; i < len; i++) {
_api_params.folders.push(server.folders[i]);
}
}
//Mission Accomplished
callback();
});
}
function discoverFolderizedServices(callback){
var folder= _api_params.folders.pop();
var url = '';
if (folder) {
url = _api_params.getRootListURL(folder);
}
else {
//No more folders left. Exit. TODO: Add Error msg to callback if we hit it.
callback();
return;
}
//Get the list of services for the given folder
$.getJSON(_api_params.formatForJSON(url), function (resp, txtstatus, jqxhr) {
var server = resp;
//Iterate over the JSON object
//Should see currentVersion, folders, services
if (server && server.services){
for (var i = 0, len = server.services.length; i < len; i++) {
if(server.services[i].type == 'MapServer'){
//Bingo! This is an endpoint that has queryable layers (probably). We want to expore it more.
_api_params.services.push(server.services[i]);
}
}
}
//If there are more folders, go back in and explore them.
if (_api_params.folders.length > 0) {
//Mission Accomplished
discoverFolderizedServices(callback);
}
else {
//Otherwise, get the heck out of here!
callback();
return;
}
});
}
//Gathers all of the the layer objects for services that have Query capabilities enabled
function discoverQueryableLayerEndpoints(callback){
var service= _api_params.services.pop();
var url = '';
if (service) {
url = _api_params.getServiceURL(service);
}
else {
//No more services left. Exit. TODO: Add Error msg to callback if we hit it.
callback();
return;
}
//Then try to get the list of layers from this service
$.getJSON(_api_params.formatForJSON(url), function (resp, txtstatus, jqxhr) {
var serviceResponse = resp;
// Iterate over the JSON object
//Should see currentVersion, serviceDescription, mapName, description, copyrightText, supportsDynamicLayers, layers, tables, spatialReference, singleFusedMapCache, initialExtent, fullExtent, minScale, maxScale, units, supportedImageFormatTypes, documentInfo, capabilities (Query is there!), supportedQueryFormats, exportTilesAllowed, maxRecordCount (for paging), maxImageHeight, maxImageWidth, supportedExtensions
//TODO: Note layers can have sub layers. If that's so, then you'll need to go back and find out what they are with another query.
//Skip for now.
//Find out if we can query these joints
if (serviceResponse && serviceResponse.capabilities){
if(serviceResponse.capabilities.indexOf("Query") > -1){
//We can query these layers. Add them to a list
if (serviceResponse && serviceResponse.layers){
for (var i = 0, len = serviceResponse.layers.length; i < len; i++) {
if(_api_params.layers.length > 50) {
callback();
return;
}
serviceResponse.layers[i].url = url + "/" + serviceResponse.layers[i].id; //take the service url defined above and stick the layer id to the end. That'll be the layer query url we'll use later to grab the schema.
_api_params.layers.push(serviceResponse.layers[i])
}
}
}
}
//Experimental - See if there are any WMTS endpoints here as well
if(serviceResponse && serviceResponse.singleFusedMapCache && serviceResponse.singleFusedMapCache == true){
//If there's a singleFusedMapCache, then there should be a WMTS endpoint (I think).
//http://gis3.nve.no/arcgis/rest/services/wmts/Bratthet/MapServer/
if(serviceResponse.spatialReference){
var srid = serviceResponse.spatialReference.latestWkid || serviceResponse.spatialReference.wkid;
if(srid == '3857' || srid == '102100'){
//We're good. If it's any other srid, we can't use it.
}
}
}
//If there are more services with layers, keep digging.
if (_api_params.services.length > 0) {
//Mission Accomplished
discoverQueryableLayerEndpoints(callback);
}
else {
callback();
return;
}
});
}
//Get info about each layer/table
function getLayerSchemas(callback) {
var layer = _api_params.layers.pop();
var url = '';
if (layer) {
url = layer.url;
}
else {
//No more styles left. Exit. TODO: Add Error msg to callback if we hit it.
callback();
return;
}
$.getJSON(_api_params.formatForJSON(url), function (resp, txtstatus, jqxhr) {
var summary = resp;
var columns = [];
var spatialColumnName = "";
// Iterate over the JSON object and coerce the schema into a format that Tableau expects (flat)
//Get out of here if this is a polyline. Tableau doesn't know anything about that for now.
if (summary.geometryType != 'esriGeometryPolyline'){
if (summary && summary.fields) {
//Iterate over the field inforamtion and build up the schema definition
for (var i = 0, len = summary.fields.length; i < len; i++) {
layer = summary.fields[i];
columns.push({
id: formatColumnNames(layer.name),
alias: layer.alias,
dataType: getTableauColumnType(layer.type),
});
if(layer.type === "esriFieldTypeGeometry"){
spatialColumnName = layer.name;
}
}
}
//See if we captured a spatial column. If not, add it to the schema with the default 'Geometry' name. Like from GeoJSON.
if (spatialColumnName == ""){
columns.push({
id: "Geometry",
alias: "Geometry",
dataType: getTableauColumnType("esriFieldTypeGeometry"),
})
spatialColumnName = "Geometry";
}
//Add the layer/table name to the master table list
//Only add table if column length is > 0
//If we're reading directly from a query endpoint, there is no summary.name. You have to pull it from the URL. Ugh.
var table_name = "";
if(_api_params.url_type == "root"){
table_name = summary.name;
}
else if(_api_params.url_type == "query"){
table_name = findServiceNameFromURL(url);
}
else if(_api_params.url_type == "geojson"){
//No table name (for display in the data pane in Tableau)
}
if(columns.length > 0){
_tableSchemas.push({
id: formatColumnNames(table_name),
alias: table_name,
columns: columns
})
var schema_properties = {
query_url: _api_params.queryLayerFormatForJSON(url),
spatial_column: spatialColumnName
}
_api_params.schema_properties[formatColumnNames(table_name)] = schema_properties;
}
}
//If there are more layers, go get 'em
if (_api_params.layers.length > 0) {
getLayerSchemas(callback);
}
else {
callback();
return;
}
});
}
//Get info about the geojson schema
function getLayerSchemasFromGeoJSON(callback) {
var layer = _api_params.layers.pop();
var url = '';
if (layer) {
url = layer.url;
}
else {
//No more styles left. Exit. TODO: Add Error msg to callback if we hit it.
callback();
return;
}
$.getJSON(url, function (resp, txtstatus, jqxhr) {
var summary = resp;
var columns = [];
var spatialColumnName = "";
// Iterate over the JSON object and coerce the schema into a format that Tableau expects (flat)
//Get out of here if this is a polyline. Tableau doesn't know anything about that as for now.
if (summary.features[0].type.toLowerCase() != 'polyline' && summary.features[0].type.toLowerCase() != 'multilinestring'){
var fields = Object.keys(summary.features[0].properties);
for (var i = 0, len = fields.length; i < len; i++) {
columns.push({
id: formatColumnNames(fields[i]), //names and aliases have to be alphanumeric only. No spaces or characters.
alias: fields[i],
dataType: getTableauColumnTypeFromGeoJSON(typeof(summary.features[0].properties[fields[i]]))
});
}
//Add spatial column to the schema with the default 'Geometry' name.
columns.push({
id: "Geometry",
alias: "Geometry",
dataType: getTableauColumnTypeFromGeoJSON("geometry")
})
spatialColumnName = "Geometry";
//Add the layer/table name to the master table list. Just call it GeoJSON
var table_name = "GeoJSON";
if(columns.length > 0){
_tableSchemas.push({
id: table_name,
alias: table_name,
columns: columns
})
var schema_properties = {
query_url: url,
spatial_column: spatialColumnName
}
_api_params.schema_properties[formatColumnNames(table_name)] = schema_properties;
}
}
//Any more layers? Go get 'em
if (_api_params.layers.length > 0) {
getLayerSchemas(callback);
}
else {
callback();
return;
}
});
}
//Query REST endpoint for a given layer
function getLayerRecords(tableObj, callback) {
var url = '';
var schema_properties;
if (tableObj && tableObj.tableInfo) {
schema_properties = _api_params.schema_properties[tableObj.tableInfo.id];
url = schema_properties.query_url;
}
else {
//No more styles left. Exit. TODO: Add Error msg to callback if we hit it.
callback();
return;
}
$.getJSON(url, function (resp, txtstatus, jqxhr) {
var geojson = "";
if(_api_params.url_type == 'root' || _api_params.url_type == 'query'){
geojson = arcgisToGeoJSON(resp); //Convert esriJSON to geojson.
}
else if(_api_params.url_type == 'geojson'){
geojson = resp; //It's already geojson. No need to convert.
}
var summary = geojson;
var features = summary.features;
var rows = [];
// Iterate over the JSON object and coerce the schema into a format that Tableau expects (flat)
if (features && features.length > 0 && tableObj) {
//Grab a list of the properties
var properties = features[0].properties;
for (var i = 0, len = features.length; i < len; i++) {
var row_builder = {};
for (var property_name in properties){
row_builder[formatColumnNames(property_name)] = features[i].properties[(property_name)];
}
//Grab geometry
if(features[i].geometry){
row_builder[schema_properties.spatial_column] = features[i].geometry;
}
//Commit row
rows.push(row_builder);
}
}
//Only add the row if it isn't empty
if(rows.length > 0){
tableObj.appendRows(rows);
}
//TODO: Determine if we need to grab more features with a followup call. ArcGIS Server limits queries to 1000 results by default, though this is configurable by the server administrator. Different versions of ArcGIS Server have paging capability, where you can specify a starting record and the number of records to fetch, but not all. The trick is to first query the endpoint and get back all of the ObjectIDs (there is no limit on this query). Then you sort the IDs and do your own paging. Ugh.
//if (_api_params.layers.length > 0) {
// getLayerSchemas(callback);
//}
//else {
callback();
return;
//}
});
}
//Return root, query or geojson
function parseURLForType(url){
//If the URl ends in arcgis/rest/, then let's assume it's of type 'root'. //TODO: Many ArcGIS Server instances follow this rule, but some don't (Admins can configure the URL structure to be different than the default.)
if(url.endsWith("arcgis/rest/") === true){
return 'root';
}
else if(url.indexOf("arcgis/rest/") > -1 && url.indexOf("FeatureServer") > -1 && url.indexOf("query?") > -1){
return 'query';
}
else if (url.endsWith(".geojson") === true){
return 'geojson';
}
else{
return 'root';
}
}
//for a query type URL, we can grab the name from the URL. It's after rest/services
function findServiceNameFromURL(url){
var s = url.split("/");
return s[s.indexOf("services") + 1]; //Get the value at index of the array that holds the token immediately after /services/
}
//Clean up layer names that are used as table names for Tableau. Only allow alphanumeric table names.
function formatColumnNames(input){
return input.replace(/[^a-z0-9]/gi,'');
}
function getTableauColumnType(esriType){
//esriFieldTypeBlob, esriFieldTypeRaster not supported, 'cause it doesn't map to a Tableau type.
//TODO: Make this fail gracefully if you can't find the right match, or if somehting changes, or if there is no type passed in.
var map = {
"esriFieldTypeOID": tableau.dataTypeEnum.int,
"esriFieldTypeInteger": tableau.dataTypeEnum.int,
"esriFieldTypeSmallInteger": tableau.dataTypeEnum.int,
"esriFieldTypeDouble": tableau.dataTypeEnum.float,
"esriFieldTypeSingle": tableau.dataTypeEnum.float,
"esriFieldTypeString": tableau.dataTypeEnum.string,
"esriFieldTypeDate": tableau.dataTypeEnum.date,
"esriFieldTypeGeometry": tableau.dataTypeEnum.geometry,
"esriFieldTypeGUID": tableau.dataTypeEnum.string,
"esriFieldTypeGlobalID": tableau.dataTypeEnum.string,
"esriFieldTypeXML": tableau.dataTypeEnum.string
}
if(map[esriType]){
return map[esriType];
}
else{
console.log("ESRI Type " + esriType + " not found. Add it.")
return tableau.dataTypeEnum.string;
}
}
function getTableauColumnTypeFromGeoJSON(jsType){
//using javascript typeof to get bool, number, string
//TODO: Make this fail gracefully if you can't find the right match, or if somehting changes, or if there is no type passed in.
var map = {
"number": tableau.dataTypeEnum.float,
"string": tableau.dataTypeEnum.string,
"bool": tableau.dataTypeEnum.string,
"geometry": tableau.dataTypeEnum.geometry
}
if(map[jsType]){
return map[jsType];
}
else{
console.log("Javascript Type " + jsType + " not found. Add it.")
return tableau.dataTypeEnum.string;
}
}
$(document).ready(function () {
$('#submitButton').click(function () {
//Gather up what tables the user wanted
var user_info = {
username: "",
password: "",
server_url: ""
}
//TODO: Validate that username and token are populated
if ($('#txtServerURL').val() != '') {
user_info.server_url = $('#txtServerURL').val();
}
else {
//Display message to fill in username
return;
}
//TODO: Validate that username and token are populated
if ($('#txtUsername').val() != '') {
user_info.username = $('#txtUsername').val();
}
else {
//Display message to fill in username
return;
}
//Validate that user has entered username and API Token
if ($('#txtAPIToken').val() != '') {
user_info.token = $('#txtAPIToken').val();
}
else {
//Display message to fill in username
return;
}
//Before the interactive page goes away (it will when we 'submit' in a sec.), stash the user's token and username (as a string) in the talbeau.connectionData property to pass it over to the so-called 'Gather data' phase, where we'll actually pull the schema and data.
tableau.connectionData = JSON.stringify(user_info);
tableau.connectionName = 'ESRI ArcGIS Server';
tableau.submit();
});
});
})();