server.js 9.34 KB
Newer Older
Joe TS Dell's avatar
upload  
Joe TS Dell committed
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
(function () {
    "use strict";
    const express = require('express');
    const boxIntersect = require('box-intersect');
    const bodyParser = require('body-parser');
    const compression = require('compression');
    const app = express();
    const http = require('http');
    const useragent = require('useragent');

    const port = 80;
    app.use(compression());
    app.use(function (req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        next();
    });
    app.use(bodyParser.json()); // for parsing application/json
    app.use(bodyParser.urlencoded({
        extended: true
    }));
    app.use(express.static(__dirname));

    const geovolumes_server_url = "http://localhost"
    // const geovolumes_server_url = "https://steinbeis-3dps.eu/3DGeoVolumes"
    // GeoVolume - A function to replace the geovolumes_server_url
    var replace_server_url = function (const_json) {
        var input_str = JSON.stringify(const_json)
        input_str = input_str.replace(/host_url/g, geovolumes_server_url);
        var input_output = JSON.parse(input_str)
        return input_output;
    }

    // GeoVolume Check if bbox intersection
    // This version only check 2D !!!!
    var validate_bbox = function (collections_input, req) {
        if (typeof req.query.bbox == "string") {

            try {
                // user request with bbox
                // var collections_json = JSON.parse(collections_input);
                var collections_json = collections_input;
                var req_query_bbox = req.query.bbox;
                var bbox = JSON.parse("[" + req_query_bbox + "]");
                // remove the req-height if exist
                if (config.server_full_log) {
                    console.log(`bbox lenght: ${bbox.lenght}`)
                }
                if (bbox.length == 6) {
                    bbox = [bbox[0], bbox[1], bbox[3], bbox[4]]
                } else if (bbox.lenght == 0 || typeof bbox.lenght == 'undefined') {
                    return collections_input
                }
                // prepare result array
                var resultJSON = {}
                resultJSON["links"] = collections_json["links"]
                resultJSON["collections"] = [] // prepare empty collection

                for (let index = 0; index < collections_json["collections"].length; index++) {
                    var collection_bbox_minx = collections_json["collections"][index].extent.spatial.bbox[0][0]
                    var collection_bbox_miny = collections_json["collections"][index].extent.spatial.bbox[0][1]
                    var collection_bbox_maxx = collections_json["collections"][index].extent.spatial.bbox[0][3]
                    var collection_bbox_maxy = collections_json["collections"][index].extent.spatial.bbox[0][4]
                    var bbox_collection = [];
                    bbox_collection.push(collection_bbox_minx)
                    bbox_collection.push(collection_bbox_miny)
                    bbox_collection.push(collection_bbox_maxx)
                    bbox_collection.push(collection_bbox_maxy)
                    var boxes = [bbox, bbox_collection];
                    var overlap = boxIntersect(boxes);
                    if (overlap == '') {} else {
                        resultJSON["collections"].push(collections_json["collections"][index])
                    }
                    if (index == collections_json["collections"].length - 1) {
                        // Return only the last loop
                        return resultJSON
                    }
                }

            } catch (error) {
                console.log(`Error in the BBOX function validate_bbox() :`)
                console.log(error)
                return collections_input
            }

        } else {
            // user request without bbox
            return collections_input
        }
    }

    app.get('/', function (req, res) {
        var landingpage_json = require('./3DGeoVolumes/landingpage.json')
        var landingpage_output = replace_server_url(landingpage_json)
        try {
            var agent = useragent.parse(req.headers['user-agent']);
            var agentString = agent.toString()
            if (req.query.f === "json" || req.query.format === "json" || agentString.includes("Other")) {
                res.json(landingpage_output);
            } else {
                // browser
                res.render('geovolumes/landing.ejs', {
                    landingpage_output
                })
            }
        } catch (error) {
            console.log(error)
        }

    })

    // GeoVolume - conformance
    const conformance_json = require('./3DGeoVolumes/conformance.json')
    app.get('/conformance', function (req, res) {
        res.json(conformance_json);
    })

    // GeoVolume - Collections
    app.get('/collections', function (req, res) {
        try {
            var collection_resource = require('./3DGeoVolumes/collections/collections.json')
            var collection_resource_updated = replace_server_url(collection_resource)
            var collection_resource_updated_bbox = validate_bbox(collection_resource_updated, req)

            // check user-agent
            var agent = useragent.parse(req.headers['user-agent']);
            var agentString = agent.toString()
            console.log(`request agent: ${agentString}`)
            if (req.query.f === "json" || req.query.format === "json" || agentString.includes("Other")) {
                res.json(collection_resource_updated_bbox);
            } else {
                // browser
                res.render('geovolumes/collections.ejs', {
                    collection_resource_updated_bbox
                })
            }
        } catch (error) {
            console.log(error)
        }

    })

    app.get('/collections/:collectionsId', function (req, res) {
        try {
JOE XMG's avatar
JOE XMG committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
            var collection_resource = require('./3DGeoVolumes/collections/collections.json')
            var collection_resource_updated = replace_server_url(collection_resource)
            var selected_collection_byID = []
            for (let index = 0; index < collection_resource_updated.collections.length; index++) {
                if (collection_resource_updated.collections[index]["id"] == req.params.collectionsId) {
                    selected_collection_byID.push(collection_resource_updated.collections[index])
                }
            }
            var selected_collection = {
                "links": [
                  {
                    "rel": "self",
                    "href": `host_url/collections/${req.params.collectionsId}`,
                    "type": "application/json",
                    "title": `OGC API - 3D GeoVolumes collections of ${req.params.collectionsId}`
                  }
                ],
                "collections": selected_collection_byID
            }
            selected_collection = replace_server_url(selected_collection)
            res.json(selected_collection);
Joe TS Dell's avatar
upload  
Joe TS Dell committed
166
        } catch (error) {
JOE XMG's avatar
JOE XMG committed
167
168
            console.log(error)
            res.send("internal error")
Joe TS Dell's avatar
upload  
Joe TS Dell committed
169
170
        }
    })
JOE XMG's avatar
update    
JOE XMG committed
171
172
173
174
175
176
177
178
    // request 3D Tiles
    app.get('/collections/:collectionsId/3dtiles', function (req, res) {
        try {
                var collections_3dtiles_json = require(`./3DGeoVolumes/collections/${req.params.collectionsId}/3dtiles/tileset.json`)
                var collections_3dtiles_output = replace_server_url(collections_3dtiles_json)
                res.json(collections_3dtiles_output);
        } catch (error) {
            console.log(error)
JOE XMG's avatar
JOE XMG committed
179
            res.send("internal error")
JOE XMG's avatar
update    
JOE XMG committed
180
181
182
        }
    })

Joe TS Dell's avatar
upload  
Joe TS Dell committed
183

Joe TS Dell's avatar
update    
Joe TS Dell committed
184
185
186
187
188
189
190
191
    app.get('/collections/:collectionsId/:containerId', function (req, res) {
        try {
            var collection_json = require(`./3DGeoVolumes/collections/${req.params.collectionsId}/${req.params.containerId}/${req.params.containerId}.json`)
            res.json(collection_json);
        } catch (error) {
            res.send("internal error at /3DGeoVolumes/collections/:collectionsId/:containerId")
        }
    })
JOE XMG's avatar
update    
JOE XMG committed
192
    app.get('/collections/:collectionsId/:containerId/3dtiles', function (req, res) {
Joe TS Dell's avatar
upload  
Joe TS Dell committed
193
        try {
JOE XMG's avatar
update    
JOE XMG committed
194
                var collections_3dtiles_json = require(`./3DGeoVolumes/collections/${req.params.collectionsId}/${req.params.containerId}/tileset.json`)
Joe TS Dell's avatar
update    
Joe TS Dell committed
195
196
                var collections_3dtiles_output = replace_server_url(collections_3dtiles_json)
                res.json(collections_3dtiles_output);
Joe TS Dell's avatar
upload  
Joe TS Dell committed
197
        } catch (error) {
JOE XMG's avatar
update    
JOE XMG committed
198
199
            console.log(error)
            res.send("internal error")
Joe TS Dell's avatar
upload  
Joe TS Dell committed
200
201
        }
    })
Joe TS Dell's avatar
update    
Joe TS Dell committed
202
    
Joe TS Dell's avatar
update    
Joe TS Dell committed
203
    app.get('/i3s', function (req, res) {
Joe TS Dell's avatar
update    
Joe TS Dell committed
204
205
206
        if (req.query.i3s_resource_url) {
            res.render('arcgisclient.ejs', 
                req.query
Joe TS Dell's avatar
update    
Joe TS Dell committed
207
208
            )
        } else {
Joe TS Dell's avatar
update    
Joe TS Dell committed
209
            res.send("Error: no i3s query parameter specified")
Joe TS Dell's avatar
update    
Joe TS Dell committed
210
211
        }
    })
Joe TS Dell's avatar
update    
Joe TS Dell committed
212
213
214
215
216
217
218
219
220
221
222
223
    app.get('/3dtiles', function (req, res) {
        if (req.query.url_3dtiles_json) {
            res.render('cesiumclient.ejs', 
                req.query
            )
        } else {
            res.send("Error: no url_3dtiles_json parameter specified")
        }
    })
    app.use(express.static("static"));
    app.use(express.static("3DGeoVolumes"));

Joe TS Dell's avatar
upload  
Joe TS Dell committed
224
225
226
227
228
229
    const httpServer = http.createServer(app);
    httpServer.listen(port, () => {
        console.log(`HTTP Server running on port ${port}`);
        console.log(`server at http://localhost:${port}/`);
    })
})();