-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGeoGatewayServer.js
More file actions
918 lines (809 loc) · 33.1 KB
/
Copy pathGeoGatewayServer.js
File metadata and controls
918 lines (809 loc) · 33.1 KB
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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
/**
* This is the server-side JavaScript.
*/
//Call requires
var MongoClient=require('mongodb').MongoClient;
var test = require('assert');
var CollectionUtils=require('./collectionUtils').CollectionUtils;
var express=require('express');
var bodyParser=require('body-parser');
var http=require('http');
var exec=require('child_process').exec;
var execSync=require('child_process').execSync;
var spawn=require('child_process').spawn;
var path=require('path');
var fs=require('fs');
var multiparty=require('multiparty'); //Form multipart form uploads.
var util=require('util');
var mkdirp=require('mkdirp');
var RestClient=require('node-rest-client').Client;
var GridStore=require('mongodb').GridStore;
var Feed=require('feed');
var restClient=new RestClient();
//Place for uploading files.
var config=require('./config');
var geogatewayHomeDir=config.home; //"/Users/mpierce/GeoGateway/";
var baseDestDir=config.baseDestDir; //'html/userUploads/';
var projectBinDir=config.bin; //geogatewayHomeDir+"/bin/";
var baseUserProjectPath=geogatewayHomeDir+baseDestDir;
var uavsarSearchUrl=config.uavsarSearchUrl;
var wmsUrl=config.wmsUrl;
var wmscolorUrl = config.wmscolorUrl;
var losQueryUrl=config.losQueryUrl;
var altlosQueryUrl=config.altlosQueryUrl;
var sldserviceUrl = config.sldserviceUrl;
var uavsarratingUrl = config.uavsarratingUrl;
var gpsserviceUrl = config.gpsserviceUrl;
var seismicityplotUrl = config.seismicityplotUrl;
//Call or prepare constructors
var app=express();
var collectionUtils;
var gridStore;
//Use this to filter out empty data in the LOS query.
var tofind='""';
var regex=new RegExp(tofind,'g');
//Set up MongoClient
var url="mongodb://127.0.0.1:27017/geogatewaydb"; //This should not be hard coded?
MongoClient.connect(url, function(err, db) {
test.equal(null, err);
collectionUtils=new CollectionUtils(db);
});
app.set('port',process.env.PORT || 3000);
//Use serverOpts to set content types for static content.
var serverOpts= {
setHeaders: function (res,path,stat) {
// console.log("Path is "+path);
if(path.indexOf('.html')>0) {
res.setHeader('Content-Type','text/html');
}
else if(path.indexOf('.css')>0) {
res.setHeader('Content-Type','text/css');
}
else if(path.indexOf('.js')>0) {
//Note this may need to be changed to text/javascript
res.setHeader('Content-Type','application/x-javascript');
}
else {
// console.log('Sending text/plain');
res.setHeader('Content-Type','text/plain');
}
}
};
//We'll put HTML documents in the local "html" directory
app.use(express.static(__dirname+'/html',serverOpts));
//app.use(express.static(__dirname+'/html'));
app.use(bodyParser.json());
/**
* "Projects" family of functions are used to CRUD documents in a user's collection
*/
//Insert a new document. The document identifier will be generated by MongoDB.
app.post('/projects/:user', function(req,res){
//console.log(req.body);
//Create the collection if necessary
collectionUtils.createCollection(req.params.user, function(error, result){
if(error) {
console.error("Error:",error.stack);
res.status(400).send(error);
}
});
//Save the document to the collection.
collectionUtils.save(req.params.user, req.body, function(error,doc) {
handleResponse(error, doc, res);
});
});
//Update an existing document. This entirely replaces the ID'd document with the new document
app.put("/projects/:user/:documentId", function(req,res) {
collectionUtils.update(req.params.user,req.params.documentId, req.body, function(error, doc){
handleResponse(error, doc, res);
});
});
//Deletes a specific document.
app.delete("/projects/:user/:documentId", function(req, res) {
collectionUtils.delete(req.params.user, req.params.documentId, function(error, doc){
if(error) {
console.error(error.stack);
res.set('Content-Type','application/json');
res.status(400).send(error);
}
else {
res.set('Content-Type','application/json');
res.sendStatus(200);
}
});
});
//Gets the entire contents of a particular collection. This is returned as an array.
app.get('/projects/:user', function(req,res){
// console.log("Getting contents of collection for user "+req.params.user);
collectionUtils.findAll(req.params.user, function(error, obj) {
if(error) {
console.error(error.stack);
res.set('Content-Type','application/json');
res.status(400).send(error);
}
else {
res.set('Content-Type','application/json');
res.status(200).send(obj);
}
});
});
//Gets the document by its name.
app.get('/projects/:collection/:document', function(req,res){
collectionUtils.getById(req.params.collection, req.params.document,function(error,obj){
// handleResponse(error, obj, res);
if(error) {
console.error(error.stack);
res.set('Content-Type','application/json');
res.status(400).send(error);
}
else {
res.set('Content-Type','application/json');
res.status(200).send(obj);
}
});
});
//--------------------------------------------------
/**
* NOTECARDS family of functions. These perform CRUD operations for a user's
* collection of notecards.
*/
//Insert a new document. The document identifier will be generated by MongoDB.
app.post('/notecards/:user', function(req,res){
// console.log(req.body);
//Create the collection if necessary
collectionUtils.createCollection(req.params.user, function(error, result){
if(error) {
console.error("Error:",error.stack);
res.status(400).send(error);
}
});
//Save the document to the collection.
collectionUtils.save(req.params.user, req.body, function(error,doc) {
handleResponse(error, doc, res);
});
});
//Update an existing notecard. This entirely replaces the ID'd document with the new document
app.put("/notecards/:user/:documentId", function(req,res) {
collectionUtils.update(req.params.user,req.params.documentId, req.body, function(error, doc){
handleResponse(error, doc, res);
});
});
//Gets the entire contents of a particular collection. This is returned as an array.
app.get('/notecards/:user', function(req,res){
// console.log("Getting contents of collection for user "+req.params.user);
collectionUtils.findAll(req.params.user, function(error, obj) {
if(error) {
console.error(error.stack);
res.set('Content-Type','application/json');
res.status(400).send(error);
}
else {
res.set('Content-Type','application/json');
res.status(200).send(obj);
}
});
});
//Gets the document by its name.
app.get('/notecards/:collection/:document', function(req,res){
collectionUtils.getById(req.params.collection, req.params.document,function(error,obj){
// handleResponse(error, obj, res);
if(error) {
console.error(error.stack);
res.set('Content-Type','application/json');
res.status(400).send(error);
}
else {
res.set('Content-Type','application/json');
res.status(200).send(obj);
}
});
});
//Gets all matching documents
app.get('/notecards/:collection/:keyName/:searchString', function(req,res){
console.log("Query values",req.params.collection,req.params.keyName,req.params.searchString);
collectionUtils.searchFor(req.params.collection, req.params.keyName, req.params.searchString, function(error,obj){
// handleResponse(error, obj, res);
if(error) {
console.error(error.stack);
res.set('Content-Type','application/json');
res.status(400).send(error);
}
else {
res.set('Content-Type','application/json');
res.status(200).send(obj);
}
});
});
//--------------------------------------------------
/**
* Do uploads of supporting files.
*/
//This version just streams the data directly to a chose file.
app.post('/doUpload/:userName/:projectId',function(req,res){
var destDir=baseDestDir+"/"+req.params.userName+"/"+req.params.projectId+"/";
//console.log("File dest dir: "+destDir);
mkdirp.sync(destDir);
var form=new multiparty.Form();
//This listens for parts in the multipart request
form.on('part', function(part){
//If we get a file, then part.filename is not null.
if(part.filename!=null) {
var fullFileName=destDir+part.filename;
//part is a readableStream, so we can chunk it.
var out = fs.createWriteStream(fullFileName);
part.pipe(out);
//part.on('data', function(chunk) {
//Use appendFile since it creates the file if it doesn't already
//exist and appends since we are chunking.
// fs.appendFile(fullFileName, chunk,function(err){
// if(err) throw err;
// });
//});
//Keep going.
part.resume();
}
});
form.parse(req);
res.redirect('back');
});
//This version sets autoFiles to true, so the file will be written to a tmp dir chosen by
//the software. It then needs to be manually moved to the proper place. See
//https://github.com/andrewrk/node-multiparty.
app.post('doUpload2', function(req,res){
form.parse(req, function(err, fields, files){
res.writeHead(200,{'content-type':'text/plain'});
//console.log(util.inspect(files[0]));
//console.log(files[0].originalFilename);
//console.log(files[0].path);
//console.log(util.inspect(files[0].headers));
//Finally, report back
res.write('received upload:\n');
res.write('Received fields: '+util.inspect(fields));
res.write('\n\n');
res.end('Received files:'+util.inspect(files));
});
});
/**
* The Execute API family calls external processes
*/
//Runs the given executable in blocking (exec) mode. This assumes that the project has
//been correctly created, with input and output files specfiied. It will only execute
//things in the project's bin directory, but we need to watch for semicolons.
app.get('/execute/:exec/:collection/:documentId', function (req,res) {
collectionUtils.getById(req.params.collection, req.params.documentId,function(error,obj){
var theExec=projectBinDir+req.params.exec+" "+obj.projectInputFileName+" "+obj.projectOutputFileName;
//console.log("Execution path:"+theExec);
var baseWorkDirPath=baseUserProjectPath+obj.projectWorkDir;
//console.log("baseWorkDirPath:"+baseWorkDirPath);
exec(theExec, {"cwd":baseWorkDirPath},function(error, stdout, stderr){
if(error) {
console.error(error.stack);
res.status(400).send(error)
}
else {
fs.writeFileSync(baseWorkDirPath+"/"+obj.projectStandardOut,stdout);
fs.writeFileSync(baseWorkDirPath+"/"+obj.projectStandardError,stderr);
res.set('Content-Type','application/json');
res.sendStatus(200);
}
});
});
});
//This version uses exec and chains everything in a big if-else block.
app.get('/execute_disloc2/:exec/:collection/:documentId',function(req,res) {
collectionUtils.getById(req.params.collection, req.params.documentId,function(error,obj){
// console.log(obj);
// replace space in input file name
var newinputFileName = obj.projectInputFileName.replace(/\s/g,"\\ ")
var theExec=projectBinDir+"disloc"+" "+newinputFileName+" "+obj.projectOutputFileName;
var baseWorkDirPath=baseUserProjectPath+obj.projectWorkDir;
exec(theExec,{"cwd":baseWorkDirPath,"maxBuffer":500*1024},function(error,stdout,stderr) {
if (error!==null) {
console.error(error);
res.status(400).send(error);
}
else {
//Write the disloc output and error
//fs.writeFileSync(baseWorkDirPath+"/"+obj.projectStandardOut,stdout);
//fs.writeFileSync(baseWorkDirPath+"/"+obj.projectStandardError,stderr);
//Make the KML
theExec=projectBinDir+"disloc2kml"+" -i "+obj.projectOutputFileName+" -o "+obj.projectOutputKMLFileName;
exec(theExec,{"cwd":baseWorkDirPath},function(error,stdout,stderr){
if (error!==null){
console.error(error);
res.status(400).send(error);
}
else {
// run SARImage with the default parameter
theExec=projectBinDir+"SARImage"+" "+obj.projectOutputFileName+" "+obj.insarElevation + " " +obj.insarAzimuth+" "+obj.insarFrequency+" "+'""';
exec(theExec,{"cwd":baseWorkDirPath},function(error,stdout,stderr) {
if (error!==null){
console.error(error);
res.status(400).send(error);
}
else {
// run tilemap code
// run qsxy2tilt with the default parameter
// theExec=projectBinDir+"qsxy2tilt"+" -i "+obj.projectOutputFileName+" -o " + obj.projectOutputTiltCSVFileName;
// exec(theExec,{"cwd":baseWorkDirPath},function(error,stdout,stderr) {
// if (error!==null){
// console.error(error);
// //res.status(400).send(error);
// }
// else {
// // run tilemap vis code
// // run qsxy2tilt with the default parameter
// theExec=projectBinDir+"tiltmap_vis"+" -i " + obj.projectOutputTiltCSVFileName;
// exec(theExec,{"cwd":baseWorkDirPath},function(error,stdout,stderr){
// if (error!==null){
// console.error(error);
// //res.status(400).send(error);
// }
// else {
// zip the files for download
theExec= "zip -r " + obj.projectZipFileName + " .";
exec(theExec,{"cwd":baseWorkDirPath},function(error,stdout,stderr){
if (error!==null){
console.error(error);
res.status(400).send(error);
}
else {
//We are done.
res.set('Content-Type','application/json');
res.sendStatus(200);
}
});
}
// });
// }
//});
});
}
});
}
});
});
});
//Runs provided executable in non-blocking (spawn) mode. Should only run things project's
//bin directory. This version requires the command line arguments to be set separately in the
//project's entry (that is, the object retreived from the collection).
app.get('/spawn/:exec/:collection/:documentId', function(req, res) {
collectionUtils.getById(req.params.collection, req.params.documentId,function(error,obj){
//console.log(obj);
var theExec=req.params.exec;
// var simplexArgs=['-a','True',obj.projectInputFileName,obj.projectOutputFileName];
var theArgs=['-a','True']; //obj.cmdLineArgs;
theArgs.push(obj.projectInputFileName);
theArgs.push(obj.projectOutputFileName);
var baseWorkDirPath=baseUserProjectPath+obj.projectWorkDir;
//console.log("Base workdir:"+baseWorkDirPath);
var theProcess=spawn(theExec,theArgs,{'cwd':baseWorkDirPath,'env':{PATH:process.env.PATH+':'+projectBinDir}});
//Return to the clienb tub go ahead and keep running.
//This is a poor man's solution for now.
res.sendStatus(200);
theProcess.stdout.on('data', function (data) {
fs.writeFileSync(baseWorkDirPath+"/"+obj.projectStandardOut,data);
// console.log('Standard Out: "%s"', data);
});
theProcess.stderr.on('data', function (data) {
fs.writeFileSync(baseWorkDirPath+"/"+obj.projectStandardError,data);
//console.log('Standard Error: "%s"', data);
});
theProcess.on('error',function(err){
console.error("Got an error: "+err);
});
theProcess.on('close', function (exitCode) {
//TODO: This hard coded string depends on correct values both here and in the
//client-side controller code. Must fix.
//The project object may have been updated by other processes, so
//refresh.
//TODO: we need a way to update just one field of a object instead of the entire object.
collectionUtils.getById(req.params.collection, req.params.documentId,function(error,obj){
obj.status="Completed";
collectionUtils.update(req.params.collection,req.params.documentId,obj,function(error,doc){
if(error) {
console.error("Could not save the updated object");
}
else {
//console.log("Completed Project Metadata",doc);
}
});
});
// console.log('Exit code:', exitCode);
});
})
});
//Runs Simplex in blocking (exec) mode. This assumes that the project has been correctly created,
//with input and output files specfiied.
app.get('/execute/simplex/:collection/:documentId', function (req,res) {
collectionUtils.getById(req.params.collection, req.params.documentId,function(error,obj){
var simplexExec=projectBinDir+"simplex -a "+obj.projectInputFileName+" "+obj.projectOutputFileName;
//console.log("Execution path:"+simplexExec);
var baseWorkDirPath=baseUserProjectPath+obj.projectWorkDir;
//console.log("baseWorkDirPath:"+baseWorkDirPath);
exec(simplexExec, {"cwd":baseWorkDirPath},function(error, stdout, stderr){
if(error) {
console.error(error.stack);
res.status(400).send(error)
}
else {
// console.log('Standard Out:',stdout);
// console.log('Standard Err:', stderr);
fs.writeFileSync(baseWorkDirPath+"/"+obj.projectStandardOut,stdout);
fs.writeFileSync(baseWorkDirPath+"/"+obj.projectStandardError,stderr);
res.set('Content-Type','application/json');
res.sendStatus(200);
}
});
});
});
//Runs Simplex in non-blocking (spawn) mode.
app.get('/spawn/simplex/:collection/:documentId', function(req, res) {
collectionUtils.getById(req.params.collection, req.params.documentId,function(error,obj){
var simplexExec="simplex";
var simplexArgs=['-a','True',obj.projectInputFileName,obj.projectOutputFileName];
var baseWorkDirPath=baseUserProjectPath+obj.projectWorkDir;
//console.log("Base workdir:"+baseWorkDirPath);
var theProcess=spawn(simplexExec,simplexArgs,{'cwd':baseWorkDirPath,'env':{PATH:process.env.PATH+':'+projectBinDir}});
//Return to the clienb tub go ahead and keep running.
//This is a poor man's solution for now.
res.sendStatus(200);
theProcess.stdout.on('data', function (data) {
fs.writeFileSync(baseWorkDirPath+"/"+obj.projectStandardOut,data);
// console.log('Standard Out: "%s"', data);
});
theProcess.stderr.on('data', function (data) {
fs.writeFileSync(baseWorkDirPath+"/"+obj.projectStandardError,data);
// console.log('Standard Error: "%s"', data);
});
theProcess.on('error',function(err){
console.error("Got an error: "+err);
});
theProcess.on('close', function (exitCode) {
obj.status="Completed";
collectionUtils.update(req.params.collection,req.params.documentId,obj,function(error,doc){
if(error) {
console.log("Could not save the updated object");
}
else {
//console.log("State changed to completed");
//console.log(doc);
}
});
//console.log('Exit code:', exitCode);
});
})
});
//Runs the node.js execute function.
app.get('/execute/exec-test', function (req,res) {
exec('ls -l', function(error, stdout, stderr){
if(error) {
console.error(error.stack);
res.status(400).send(error)
}
else {
//console.log('Standard Out:',stdout);
//console.log('Standard Err:', stderr);
res.status(200).send("OK");
}
});
});
//Runs the node.js spawn function
app.get('/execute/spawn-test', function(req, res) {
var theProcess=spawn('ls', ['-l']);
//Note must use the %s formatter
theProcess.stdout.on('data', function (data) {
//console.log('Standard Out: "%s"', data);
});
theProcess.stderr.on('data', function (date) {
//console.log('Standard Error: "%s"', data);
});
theProcess.on('close', function (exitCode) {
//console.log('Exit code:', exitCode);
res.status(200).send('OK');
});
});
//--------------------------------------------------
/**
* These are GeoServer functions
*/
//The jQuery .get() sends the request parameters as a query ("?"), so we need to extract the
//value of the query from the request object.
app.get('/uavsar_flight_search/',function(req,res) {
//TODO: need to do a better job of constructing this URL
var queryStr=req.query.searchstring;
// var geoServerUrl='http://gf2.ucs.indiana.edu/quaketables/uavsar/search?searchstring=';
var geoServerUrl=uavsarSearchUrl+'searchstring=';
restClient.get(geoServerUrl+queryStr, function(data, response){
res.status(200).send(data);
}).on('error', function (err) {
console.log('something went wrong on the request', err.request.options);
});
});
app.get('/uavsar_query/',function(req,res){
//console.log("Query: ",req.query);
// var geoServerUrl='http://gf2.ucs.indiana.edu/quaketables/uavsar/search?geometry=';
var geoServerUrl=uavsarSearchUrl+'geometry=';
var queryStr=req.query.querystr;
//console.log(JSON.stringify(queryStr));
restClient.get(geoServerUrl+queryStr, function(data, response){
res.status(200).send(data);
}).on('error', function (err) {
console.log('something went wrong on the request', err.request.options);
});
});
// has_wms query, this is the temporary solution, shall be removed later
app.get('/has_wms/', function(req,res) {
var geoServerUrl;
if (req.query.server == 'coloring') {geoServerUrl = wmscolorUrl;}
else { geoServerUrl = wmsUrl;}
var wmsParams = [
"version=1.1.1",
"request=DescribeLayer",
"outputFormat=application/json",
"exceptions=application/json"
];
var layername = req.query.layername;
var queryUrl = geoServerUrl + wmsParams.join("&") + "&layers=" + "InSAR:" + layername;
//console.log(queryUrl);
restClient.get(queryUrl, function(data, response){
res.status(200).send(data);
}).on('error', function (err) {
console.log('something went wrong on the request', err.request.options);
});
});
// SLD service
app.get('/sldservice/', function(req,res) {
var queryUrl = sldserviceUrl;
// get area minmax for a image
if (req.query.service == "getminmax") {
queryUrl += "/getminmax?";
queryUrl += "image=" + req.query.image + "&extent=" + req.query.extent;
}
if (req.query.service == "sldgenerator") {
queryUrl += "/sldgenerator?";
queryUrl += "image=" + req.query.image + "&min=" + req.query.min + "&max=" + req.query.max + "&theme="+req.query.theme;
}
console.log(queryUrl);
restClient.get(queryUrl, function(data, response){
res.status(200).send(data);
}).on('error', function (err) {
console.log('something went wrong on the request', err.request.options);
});
});
// UAVSAR rating service
app.get('/uavsarrating/', function(req,res) {
var queryUrl = uavsarratingUrl;
//setrating
if (req.query.service == "setrating") {
queryUrl += "action=setrating";
queryUrl += "&uid=" + req.query.uid +"&dataname="+req.query.dataname;
queryUrl += "&user=" + req.query.user + "&rating=" + req.query.rating;
queryUrl += "&comments=" + req.query.comments;
};
//getcomments
if (req.query.service == "getcomments") {
queryUrl += "action=getcomments";
queryUrl += "&uid=" + req.query.uid;
};
//console.log(queryUrl);
restClient.get(queryUrl, function(data, response){
res.status(200).send(data);
}).on('error', function (err) {
console.log('something went wrong on the request', err.request.options);
});
});
app.get('/los_query/',function(req,res) {
// var base_url = 'http://gf1.ucs.indiana.edu/insartool/profile?image=InSAR:uid';
var base_url = losQueryUrl;
//var altpool=['10','26','258','1382','1434','1442'];
image_uid=req.query.image_uid;
// switch to new tools
//if (altpool.indexOf(image_uid) > -1) {base_url = altlosQueryUrl;};
//console.log(req.query.altlosflag);
if (req.query.altlosflag > 0) {base_url = altlosQueryUrl;};
image_name = req.query.image_name;
lat1=req.query.lat1;
lat2=req.query.lat2;
lng1=req.query.lng1;
lng2=req.query.lng2;
latlng1 = lng1 + ',' + lat1
latlng2 = lng2 + ',' + lat2
frmt=req.query.format;
resolution=req.query.resolution;
method=req.query.method;
azimuth=req.query.azimuth;
losLength=req.query.losLength;
//The below code for averaging needs to be validated.
average=null;
if(method=="average"){
average=req.query.average;
}
query_url= base_url + image_uid + '_unw' + '&point=' +
latlng1 + "," + latlng2 + '&format=' + frmt +
'&resolution=' + resolution + '&method=' + method;
if(average!=null) {
query_url+="&average="+average;
}
// console.log("Query URL: "+query_url);
// Make the call to GeoServer.
// GeoServer will return lines that should have the format "lon, lat, distance, value".
// We pass this directly back to the client.
// console.log(query_url);
restClient.get(query_url, function(data, response){
// console.log('response: %s',data);
data=data.toString();
data=data.replace(regex,'');
res.setHeader('Content-Type','text/csv');
res.setHeader('Content-Disposition','attachment;filename='+image_name+'.csv');
data = image_name+"\n"
+"start, "+lat1+", "+lng1+"\n"
+"end, "+lat2+", "+lng2+"\n"
+"azimuth, "+azimuth+"\n"
+"length, "+losLength+"\n"
+"Lon, Lat, Distance (km), Displacement, Elevation Angle\n"
+data;
// console.log(data);
res.status(200).send(data);
}).on('error', function (err) {
console.log('something went wrong on the request', err.request.options);
});
});
app.get('/gps_service/', function(req,res){
var base_url = gpsserviceUrl;
var queryUrl = base_url + req.query.data;
//console.log(queryUrl);
restClient.get(queryUrl, function(data, response){
//console.log(res.statusCode);
res.status(200).send(data);
}).on('error', function (err) {
console.log('something went wrong on the request', err.request.options);
});
});
app.get('/seismicity_plot/', function(req,res){
var base_url = seismicityplotUrl;
var queryUrl = base_url + req.query.data;
//console.log(queryUrl);
restClient.get(queryUrl, function(data, response){
//console.log(res.statusCode);
res.status(200).send(data);
}).on('error', function (err) {
console.log('something went wrong on the request', err.request.options);
});
});
app.get('/hgt_query/',function(req,res) {
// var base_url = 'http://gf1.ucs.indiana.edu/insartool/profile?image=InSAR:uid';
var base_url = losQueryUrl;
image_uid=req.query.image_uid;
lat1=req.query.lat1;
lat2=req.query.lat2;
lng1=req.query.lng1;
lng2=req.query.lng2;
latlng1 = lng1 + ',' + lat1
latlng2 = lng2 + ',' + lat2
frmt=req.query.format;
resolution=req.query.resolution;
method=req.query.method;
//The below code for averaging needs to be validated.
average=null;
if(method=="average"){
average=req.query.average;
}
query_url= base_url + image_uid + '_hgt' + '&point=' +
latlng1 + "," + latlng2 + '&format=' + frmt +
'&resolution=' + resolution + '&method=' + method;
if(average!=null) {
query_url+="&average="+average;
}
// console.log("Query URL: "+query_url);
restClient.get(query_url, function(data, response){
// console.log(data);
res.status(200).send(data);
}).on('error', function (err) {
console.log('something went wrong on the request', err.request.options);
});
});
/**
* This method returns all of the public projects for a specific user. Uses the 'feed' package.
*/
app.get('/rss/:collection',function(req,res) {
var feed=new Feed({
title: "GeoGateway Earthquake Model Feeds",
description: "Published models",
link: "http://geo-gateway.org",
image: "http://geo-gateway.org/images/logos/logo.png",
//Creative commons license.
author: {
name: req.params.collection
}
});
function getDescription(entry) {
//The following is specific to Disloc. We will need a way to generalize this.
//One way is to give more structure to the output to identify what should
//be publisehd.
var desc="Input File: "+makeAnchor(entry.projectInputFileNameUrl)+"<br>";
desc+="Output File: "+makeAnchor(entry.projectOutputFileNameUrl)+"<br>";
desc+="KML File: "+makeAnchor(entry.projectOutputKmlFileNameUrl)+"<br>";
desc+="SAR KML: "+makeAnchor(entry.projectOutputSARImageKmlFileNameUrl)+"<br>";
desc+="Tilt Map KML: "+makeAnchor(entry.projectOutputTiltCSVFileNameUrl)+"<br>";
desc+="Strain Map KML : "+makeAnchor(entry.projectOutputStrainMagFileNameUrl)+"<br>";
desc+="Project Zip: "+makeAnchor(entry.projectZipFileNameUrl)+"<br>";
return desc;
}
function makeAnchor(someUrl){
return "<a href='"+someUrl+"'>"+someUrl+"</a>";
}
//Get the items in the collection. This is returend as an array
collectionUtils.findAll(req.params.collection,function(error,obj) {
if(error) {
console.error(error);
}
else {
//The returned obj is an array with most recent entries last,
//so reverse the order.
for(var key=obj.length-1;key>=0;key--){
if(obj[key].permission=="Published") {
//TODO: need to format the description
feed.addItem({
title:obj[key].projectName,
description: getDescription(obj[key]), //JSON.stringify(obj[key]),
date: obj[key].creationTime,
author:{
name:req.params.collection
}
});
}
}
}
res.set('Content-Type','text/xml');
res.send(feed.render('rss-2.0'));
});
});
/**
* This method makes a query to http://www.ncedc.org/cgi-bin/catalog-search2.pl"
*/
app.get('/AnssCatalogService/',function(req,res) {
var base_url="https://earthquake.usgs.gov/fdsnws/event/1/query";
var amp="&"
// var format="format=geojson";
var format="format=kml";
var minmag="minmagnitude=5.0"
query_url=base_url+amp+format+amp+minmag;
restClient.get(query_url, function(data, response){
console.log(data.toString());
res.status(200).send(data);
});
});
//--------------------------------------------------
/**
* Local helper functions
*/
function myExec() {
};
function mySpawn() {
};
function handleResponse(error, obj, res) {
if(error) {
console.error(error.stack);
res.set('Content-Type','application/json');
res.status(400).send(error);
}
else {
//console.log(obj);
res.set('Content-Type','application/json');
res.status(200).send(obj);
}
};
//--------------------------------------------------
/**
*This is the catch-all for bad requests.
*/
app.use(function(req,res) {
res.status(404).send();
});
//--------------------------------------------------
/**
* Start the server
*/
http.createServer(app).listen(app.get('port'),function() {
console.log('Listening...');
});