annotate pkg/controllers/cross.go @ 648:cf62cb84fa23

Cross sections: Added a naive O(N^2) algorithm to join the neighbored line segments.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 13 Sep 2018 16:54:52 +0200
parents 794592cad75a
children 7aeacd7f150b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
633
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
1 package controllers
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
2
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
3 import (
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
4 "database/sql"
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
5 "net/http"
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
6
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
7 "gemma.intevation.de/gemma/pkg/models"
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
8 )
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
9
643
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
10 // TODO: This is hardcoded on SLT's model of a concrete sounding result.
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
11 const crossSQL = `
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
12 SELECT ST_AsBinary((ST_Dump(ST_Multi(ST_3DIntersection(
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
13 ST_Translate(
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
14 ST_Extrude(
645
794592cad75a Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 643
diff changeset
15 ST_GeomFromWKB($1, 4326),
643
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
16 0, 0, 100),
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
17 0, 0, -150),
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
18 geom)))).geom)
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
19 FROM b442017
645
794592cad75a Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 643
diff changeset
20 WHERE ST_Intersects(geom, ST_GeomFromWKB($1, 4326))
643
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
21 `
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
22
633
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
23 func crossSection(
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
24 input interface{},
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
25 req *http.Request,
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
26 db *sql.Conn,
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
27 ) (jr JSONResult, err error) {
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
28
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
29 csi := input.(*models.CrossSectionInput)
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
30
643
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
31 // TODO: Use these properties in query
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
32 _ = csi.Properties.Bottleneck
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
33 _ = csi.Properties.Date
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
34
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
35 var rows *sql.Rows
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
36
645
794592cad75a Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 643
diff changeset
37 rows, err = db.QueryContext(
794592cad75a Cross sections: Fixed SQL, endian, WKB and JSON encoding errors.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 643
diff changeset
38 req.Context(), crossSQL, csi.Coordinates.AsWKB())
643
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
39 if err != nil {
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
40 return
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
41 }
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
42 defer rows.Close()
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
43
648
cf62cb84fa23 Cross sections: Added a naive O(N^2) algorithm to join the neighbored line segments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 645
diff changeset
44 var segments models.GeoJSONMultiLineCoordinatesZ
643
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
45
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
46 for rows.Next() {
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
47 var segment models.GeoJSONLineCoordinatesZ
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
48 if err = rows.Scan(&segment); err != nil {
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
49 return
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
50 }
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
51 segments = append(segments, segment)
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
52 }
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
53
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
54 if err = rows.Err(); err != nil {
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
55 return
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
56 }
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
57
648
cf62cb84fa23 Cross sections: Added a naive O(N^2) algorithm to join the neighbored line segments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 645
diff changeset
58 joined := segments.Join()
643
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
59
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
60 jr = JSONResult{
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
61 Result: &models.CrossSectionOutput{
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
62 Type: "Feature",
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
63 Geometry: "MultLineString",
648
cf62cb84fa23 Cross sections: Added a naive O(N^2) algorithm to join the neighbored line segments.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 645
diff changeset
64 Coordinates: joined,
643
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
65 },
756f3fc62da6 Cross sections: Using the database in the web service.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 633
diff changeset
66 }
633
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
67
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
68 return
6093016fac88 Cross sections: started with implementation of web service (WIP).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
diff changeset
69 }