-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
146 lines (133 loc) · 4.83 KB
/
service.go
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
package geojson
import (
"fmt"
"math"
"strconv"
"strings"
"github.com/dranih/go-crud-api/pkg/database"
"github.com/dranih/go-crud-api/pkg/record"
)
type Service struct {
reflection *database.ReflectionService
records *record.RecordService
}
func NewGeoJsonService(reflection *database.ReflectionService, records *record.RecordService) *Service {
return &Service{reflection, records}
}
func (s *Service) HasTable(table string) bool {
return s.reflection.HasTable(table)
}
func (s *Service) GetType(table string) string {
return s.reflection.GetType(table)
}
func (s *Service) getGeometryColumnName(tableName string, params *map[string][]string) string {
var geometryParam, geometryColumnName string
if param, exists := (*params)["geometry"]; exists && len(param) > 0 {
geometryParam = param[0]
}
table := s.reflection.GetTable(tableName)
for _, columnName := range table.GetColumnNames() {
if geometryParam != "" && geometryParam != columnName {
continue
}
column := table.GetColumn(columnName)
if column.IsGeometry() {
geometryColumnName = columnName
break
}
}
if geometryColumnName != "" {
(*params)["mandatory"] = append((*params)["mandatory"], fmt.Sprintf("%s.%s", tableName, geometryColumnName))
}
return geometryColumnName
}
func (s *Service) setBoudingBoxFilter(geometryColumnName string, params *map[string][]string) {
if param, exists := (*params)["bbox"]; exists && len(param) > 0 {
c := strings.Split(param[0], ",")
if _, exists := (*params)["filter"]; !exists {
(*params)["filter"] = []string{}
}
(*params)["filter"] = append((*params)["filter"], fmt.Sprintf("%s,sin,POLYGON((%s %s,%s %s,%s %s,%s %s,%s %s))", geometryColumnName, c[0], c[1], c[2], c[1], c[2], c[3], c[0], c[3], c[0], c[1]))
}
if param, exists := (*params)["tile"]; exists && len(param) > 0 {
if zxy := strings.Split(param[0], ","); len(zxy) == 3 {
c := []float64{}
z, err1 := strconv.ParseFloat(zxy[0], 64)
x, err2 := strconv.ParseFloat(zxy[1], 64)
y, err3 := strconv.ParseFloat(zxy[2], 64)
if err1 == nil && err2 == nil && err3 == nil {
c = append(c, s.convertTileToLatLonOfUpperLeftCorner(z, x, y)...)
c = append(c, s.convertTileToLatLonOfUpperLeftCorner(z, x+1, y+1)...)
(*params)["filter"] = append((*params)["filter"], fmt.Sprintf("%s,sin,POLYGON((%f %f,%f %f,%f %f,%f %f,%f %f))", geometryColumnName, c[0], c[1], c[2], c[1], c[2], c[3], c[0], c[3], c[0], c[1]))
}
}
}
}
func (s *Service) convertTileToLatLonOfUpperLeftCorner(z, x, y float64) []float64 {
n := z * z
lon := x/n*360 - 180
lat := (180 / math.Pi) * math.Atan(math.Sinh(math.Pi*(1-2*y/n)))
return []float64{lon, lat}
}
func (s *Service) convertRecordToFeature(record interface{}, primaryKeyColumnName, geometryColumnName string) (*Feature, error) {
var id int
if recordMap, ok := record.(map[string]interface{}); ok {
if primaryKeyColumnName != "" {
if v1, exists := recordMap[primaryKeyColumnName]; exists {
if v, err := strconv.Atoi(fmt.Sprint(v1)); err == nil {
id = v
delete(recordMap, primaryKeyColumnName)
}
}
}
var geometry *Geometry
var err error
if v, exists := recordMap[geometryColumnName]; exists {
if v == nil {
geometry = nil
} else {
geometry, err = NewGeometryFromWkt(fmt.Sprint(v))
if err != nil {
return nil, err
}
}
delete(recordMap, geometryColumnName)
}
return &Feature{id, recordMap, geometry}, nil
} else {
return nil, nil
}
}
func (s *Service) getPrimaryKeyColumnName(tableName string, params *map[string][]string) string {
primaryKeyColumn := s.reflection.GetTable(tableName).GetPk()
if primaryKeyColumn == nil {
return ""
}
primaryKeyColumnName := primaryKeyColumn.GetName()
(*params)["mandatory"] = append((*params)["mandatory"], fmt.Sprintf("%s.%s", tableName, primaryKeyColumnName))
return primaryKeyColumnName
}
func (s *Service) List(tableName string, params map[string][]string) (*FeatureCollection, error) {
geometryColumnName := s.getGeometryColumnName(tableName, ¶ms)
s.setBoudingBoxFilter(geometryColumnName, ¶ms)
primaryColumnName := s.getPrimaryKeyColumnName(tableName, ¶ms)
records := s.records.List(tableName, params)
var features []*Feature
for _, record := range records.GetRecords() {
if f, err := s.convertRecordToFeature(record, primaryColumnName, geometryColumnName); err != nil {
return nil, err
} else {
features = append(features, f)
}
}
return NewFeatureCollection(features, records.GetResults()), nil
}
func (s *Service) Read(tableName, id string, params map[string][]string) (*Feature, error) {
geometryColumnName := s.getGeometryColumnName(tableName, ¶ms)
primaryColumnName := s.getPrimaryKeyColumnName(tableName, ¶ms)
if record, err := s.records.Read(nil, tableName, params, id); err == nil {
return s.convertRecordToFeature(record, primaryColumnName, geometryColumnName)
} else {
return nil, err
}
}