-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
summary.go
190 lines (156 loc) · 4.74 KB
/
summary.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
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
// Copyright 2023 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package geo
import (
"fmt"
"strings"
"github.com/cockroachdb/errors"
"github.com/twpayne/go-geom"
"github.com/dolthub/doltgresql/postgres/parser/geo/geopb"
)
// Summary returns a text summary of the contents of the geometry type.
//
// Flags shown square brackets after the geometry type have the following meaning:
// M: has M coordinate
// Z: has Z coordinate
// B: has a cached bounding box
// G: is geography
// S: has spatial reference system
func Summary(
t geom.T, hasBoundingBox bool, shape geopb.ShapeType, isGeography bool,
) (string, error) {
return summary(t, hasBoundingBox, geopb.SRID(t.SRID()) != geopb.UnknownSRID, shape, isGeography, 0)
}
func summary(
t geom.T, hasBoundingBox bool, hasSRID bool, shape geopb.ShapeType, isGeography bool, offset int,
) (summaryLine string, err error) {
f, err := summaryFlag(t, hasBoundingBox, hasSRID, isGeography)
if err != nil {
return "", err
}
summaryLine += strings.Repeat(" ", offset)
switch t := t.(type) {
case *geom.Point:
return summaryLine + fmt.Sprintf("%s[%s]", shape.String(), f), nil
case *geom.LineString:
return summaryLine + fmt.Sprintf("%s[%s] with %d points", shape.String(), f, t.NumCoords()), nil
case *geom.Polygon:
numLinearRings := t.NumLinearRings()
summaryLine += fmt.Sprintf("%s[%s] with %d ring", shape.String(), f, t.NumLinearRings())
if numLinearRings > 1 {
summaryLine += "s"
}
for i := 0; i < numLinearRings; i++ {
ring := t.LinearRing(i)
summaryLine += fmt.Sprintf("\n ring %d has %d points", i, ring.NumCoords())
}
return summaryLine, nil
case *geom.MultiPoint:
numPoints := t.NumPoints()
summaryLine += fmt.Sprintf("%s[%s] with %d element", shape.String(), f, numPoints)
if 1 < numPoints {
summaryLine += "s"
}
for i := 0; i < numPoints; i++ {
point := t.Point(i)
line, err := summary(point, false, hasSRID, geopb.ShapeType_Point, isGeography, offset+2)
if err != nil {
return "", err
}
summaryLine += "\n" + line
}
return summaryLine, nil
case *geom.MultiLineString:
numLineStrings := t.NumLineStrings()
summaryLine += fmt.Sprintf("%s[%s] with %d element", shape.String(), f, numLineStrings)
if 1 < numLineStrings {
summaryLine += "s"
}
for i := 0; i < numLineStrings; i++ {
lineString := t.LineString(i)
line, err := summary(lineString, false, hasSRID, geopb.ShapeType_LineString, isGeography, offset+2)
if err != nil {
return "", err
}
summaryLine += "\n" + line
}
return summaryLine, nil
case *geom.MultiPolygon:
numPolygons := t.NumPolygons()
summaryLine += fmt.Sprintf("%s[%s] with %d element", shape.String(), f, numPolygons)
if 1 < numPolygons {
summaryLine += "s"
}
for i := 0; i < numPolygons; i++ {
polygon := t.Polygon(i)
line, err := summary(polygon, false, hasSRID, geopb.ShapeType_Polygon, isGeography, offset+2)
if err != nil {
return "", err
}
summaryLine += "\n" + line
}
return summaryLine, nil
case *geom.GeometryCollection:
numGeoms := t.NumGeoms()
summaryLine += fmt.Sprintf("%s[%s] with %d element", shape.String(), f, numGeoms)
if 1 < numGeoms {
summaryLine += "s"
}
for i := 0; i < numGeoms; i++ {
g := t.Geom(i)
gShape, err := shapeTypeFromGeomT(g)
if err != nil {
return "", err
}
line, err := summary(g, false, hasSRID, gShape, isGeography, offset+2)
if err != nil {
return "", err
}
summaryLine += "\n" + line
}
return summaryLine, nil
default:
return "", errors.Newf("unsupported geom type: %T", t)
}
}
func summaryFlag(
t geom.T, hasBoundingBox bool, hasSRID bool, isGeography bool,
) (f string, err error) {
layout := t.Layout()
if layout.MIndex() != -1 {
f += "M"
}
if layout.ZIndex() != -1 {
f += "Z"
}
if hasBoundingBox {
f += "B"
}
if isGeography {
f += "G"
}
if hasSRID {
f += "S"
}
return f, nil
}