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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
package types
import (
"fmt"
)
type Geometry struct {
Type string
Point Point
Line Line
Lines Lines
}
func (g Geometry) MarshalRQL() (interface{}, error) {
switch g.Type {
case "Point":
return g.Point.MarshalRQL()
case "LineString":
return g.Line.MarshalRQL()
case "Polygon":
return g.Lines.MarshalRQL()
default:
return nil, fmt.Errorf("pseudo-type GEOMETRY object field 'type' %s is not valid", g.Type)
}
}
func (g *Geometry) UnmarshalRQL(data interface{}) error {
if data, ok := data.(Geometry); ok {
g.Type = data.Type
g.Point = data.Point
g.Line = data.Line
g.Lines = data.Lines
return nil
}
m, ok := data.(map[string]interface{})
if !ok {
return fmt.Errorf("pseudo-type GEOMETRY object is not valid")
}
typ, ok := m["type"]
if !ok {
return fmt.Errorf("pseudo-type GEOMETRY object is not valid, expects 'type' field")
}
coords, ok := m["coordinates"]
if !ok {
return fmt.Errorf("pseudo-type GEOMETRY object is not valid, expects 'coordinates' field")
}
var err error
switch typ {
case "Point":
g.Type = "Point"
g.Point, err = UnmarshalPoint(coords)
case "LineString":
g.Type = "LineString"
g.Line, err = UnmarshalLineString(coords)
case "Polygon":
g.Type = "Polygon"
g.Lines, err = UnmarshalPolygon(coords)
default:
return fmt.Errorf("pseudo-type GEOMETRY object has invalid type")
}
if err != nil {
return err
}
return nil
}
type Point struct {
Lon float64
Lat float64
}
type Line []Point
type Lines []Line
func (p Point) Coords() interface{} {
return []interface{}{p.Lon, p.Lat}
}
func (p Point) MarshalRQL() (interface{}, error) {
return map[string]interface{}{
"$reql_type$": "GEOMETRY",
"coordinates": p.Coords(),
"type": "Point",
}, nil
}
func (p *Point) UnmarshalRQL(data interface{}) error {
g := &Geometry{}
err := g.UnmarshalRQL(data)
if err != nil {
return err
}
if g.Type != "Point" {
return fmt.Errorf("pseudo-type GEOMETRY object has type %s, expected type %s", g.Type, "Point")
}
p.Lat = g.Point.Lat
p.Lon = g.Point.Lon
return nil
}
func (l Line) Coords() interface{} {
coords := make([]interface{}, len(l))
for i, point := range l {
coords[i] = point.Coords()
}
return coords
}
func (l Line) MarshalRQL() (interface{}, error) {
return map[string]interface{}{
"$reql_type$": "GEOMETRY",
"coordinates": l.Coords(),
"type": "LineString",
}, nil
}
func (l *Line) UnmarshalRQL(data interface{}) error {
g := &Geometry{}
err := g.UnmarshalRQL(data)
if err != nil {
return err
}
if g.Type != "LineString" {
return fmt.Errorf("pseudo-type GEOMETRY object has type %s, expected type %s", g.Type, "LineString")
}
*l = g.Line
return nil
}
func (l Lines) Coords() interface{} {
coords := make([]interface{}, len(l))
for i, line := range l {
coords[i] = line.Coords()
}
return coords
}
func (l Lines) MarshalRQL() (interface{}, error) {
return map[string]interface{}{
"$reql_type$": "GEOMETRY",
"coordinates": l.Coords(),
"type": "Polygon",
}, nil
}
func (l *Lines) UnmarshalRQL(data interface{}) error {
g := &Geometry{}
err := g.UnmarshalRQL(data)
if err != nil {
return err
}
if g.Type != "Polygon" {
return fmt.Errorf("pseudo-type GEOMETRY object has type %s, expected type %s", g.Type, "Polygon")
}
*l = g.Lines
return nil
}
func UnmarshalPoint(v interface{}) (Point, error) {
coords, ok := v.([]interface{})
if !ok {
return Point{}, fmt.Errorf("pseudo-type GEOMETRY object field 'coordinates' is not valid")
}
if len(coords) != 2 {
return Point{}, fmt.Errorf("pseudo-type GEOMETRY object field 'coordinates' is not valid")
}
lon, ok := coords[0].(float64)
if !ok {
return Point{}, fmt.Errorf("pseudo-type GEOMETRY object field 'coordinates' is not valid")
}
lat, ok := coords[1].(float64)
if !ok {
return Point{}, fmt.Errorf("pseudo-type GEOMETRY object field 'coordinates' is not valid")
}
return Point{
Lon: lon,
Lat: lat,
}, nil
}
func UnmarshalLineString(v interface{}) (Line, error) {
points, ok := v.([]interface{})
if !ok {
return Line{}, fmt.Errorf("pseudo-type GEOMETRY object field 'coordinates' is not valid")
}
var err error
line := make(Line, len(points))
for i, coords := range points {
line[i], err = UnmarshalPoint(coords)
if err != nil {
return Line{}, err
}
}
return line, nil
}
func UnmarshalPolygon(v interface{}) (Lines, error) {
lines, ok := v.([]interface{})
if !ok {
return Lines{}, fmt.Errorf("pseudo-type GEOMETRY object field 'coordinates' is not valid")
}
var err error
polygon := make(Lines, len(lines))
for i, line := range lines {
polygon[i], err = UnmarshalLineString(line)
if err != nil {
return Lines{}, err
}
}
return polygon, nil
}
|