-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.go
157 lines (144 loc) · 3.31 KB
/
convert.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
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"errors"
"github.com/go-spatial/geom"
"github.com/go-spatial/tegola"
"github.com/go-spatial/tegola/basic"
)
//ErrUnknownGeometry xxx
var ErrUnknownGeometry = errors.New("Unknown Geometry")
//ToGeom xxx
func ToGeom(g tegola.Geometry) (geom.Geometry, error) {
switch geo := g.(type) {
default:
return nil, ErrUnknownGeometry
case tegola.Point:
return geom.Point{geo.X(), geo.Y()}, nil
case tegola.MultiPoint:
pts := geo.Points()
var mpts = make(geom.MultiPoint, len(pts))
for i := range pts {
mpts[i][0] = pts[i].X()
mpts[i][1] = pts[i].Y()
}
return mpts, nil
case tegola.LineString:
pts := geo.Subpoints()
var ln = make(geom.LineString, len(pts))
for i := range pts {
ln[i][0] = pts[i].X()
ln[i][1] = pts[i].Y()
}
return ln, nil
case tegola.MultiLine:
lns := geo.Lines()
var mln = make(geom.MultiLineString, len(lns))
for i := range lns {
pts := lns[i].Subpoints()
mln[i] = make(geom.LineString, len(pts))
for j := range pts {
mln[i][j][0] = pts[j].X()
mln[i][j][1] = pts[j].Y()
}
}
return mln, nil
case tegola.Polygon:
lns := geo.Sublines()
var ply = make(geom.Polygon, len(lns))
for i := range lns {
pts := lns[i].Subpoints()
ply[i] = make(geom.LineString, len(pts))
for j := range pts {
ply[i][j][0] = pts[j].X()
ply[i][j][1] = pts[j].Y()
}
}
return ply, nil
case tegola.MultiPolygon:
mply := geo.Polygons()
var mp = make(geom.MultiPolygon, len(mply))
for i := range mply {
lns := mply[i].Sublines()
mp[i] = make(geom.Polygon, len(lns))
for j := range lns {
pts := lns[j].Subpoints()
mp[i][j] = make(geom.LineString, len(pts))
for k := range pts {
mp[i][j][k][0] = pts[k].X()
mp[i][j][k][1] = pts[k].Y()
}
}
}
return mp, nil
case tegola.Collection:
geometries := geo.Geometries()
var cl = make(geom.Collection, len(geometries))
for i := range geometries {
tgeo, err := ToGeom(geometries[i])
if err != nil {
return nil, err
}
cl[i] = tgeo
}
return cl, nil
}
}
func toBasicLine(g geom.LineString) basic.Line {
l := make(basic.Line, len(g))
for i := range g {
l[i] = basic.Point(g[i])
}
return l
}
func toBasic(g geom.Geometry) basic.Geometry {
switch geo := g.(type) {
default:
return nil
case geom.Point:
return basic.Point(geo)
case geom.MultiPoint:
mp := make(basic.MultiPoint, len(geo))
for i := range geo {
mp[i] = basic.Point(geo[i])
}
return mp
case geom.LineString:
return toBasicLine(geo)
case geom.MultiLineString:
ml := make(basic.MultiLine, len(geo))
for i := range geo {
ml[i] = toBasicLine(geo[i])
}
return ml
case geom.Polygon:
plg := make(basic.Polygon, len(geo))
for i := range geo {
plg[i] = toBasicLine(geo[i])
}
return plg
case geom.MultiPolygon:
mplg := make(basic.MultiPolygon, len(geo))
for i := range geo {
mplg[i] = make(basic.Polygon, len(geo[i]))
for j := range geo[i] {
mplg[i][j] = toBasicLine(geo[i][j])
}
}
return mplg
case geom.Collection:
geometries := geo.Geometries()
bc := make(basic.Collection, len(geometries))
for i := range geometries {
bc[i] = toBasic(geometries[i])
}
return bc
}
}
//ToTegola xxx
func ToTegola(geom geom.Geometry) (tegola.Geometry, error) {
g := toBasic(geom)
if g == nil {
return g, ErrUnknownGeometry
}
return g, nil
}