Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding flag options 2 and 4 for st_asgeojson #982

Merged
merged 2 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 63 additions & 3 deletions sql/expression/function/geojson.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math"
"strconv"
"strings"

"github.com/dolthub/go-mysql-server/sql"
Expand Down Expand Up @@ -121,6 +122,22 @@ func RoundFloatSlices(v interface{}, p float64) interface{} {
return nil
}

// GetSRID returns the SRID given a Geometry type, will return -1 otherwise
func GetSRID(val interface{}) int {
switch v := val.(type) {
case sql.Geometry:
return GetSRID(v.Inner)
case sql.Point:
return int(v.SRID)
case sql.Linestring:
return int(v.SRID)
case sql.Polygon:
return int(v.SRID)
default:
return -1
}
}

// Eval implements the sql.Expression interface.
func (g *AsGeoJSON) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
// Evaluate child
Expand All @@ -135,8 +152,22 @@ func (g *AsGeoJSON) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
}

// Create map object to hold values
obj := make(map[string]interface{}, 3) // TODO: needs to be 3 when including bounding box
obj := make(map[string]interface{})
switch v := val.(type) {
case sql.Geometry:
switch inner := v.Inner.(type) {
case sql.Point:
obj["type"] = "Point"
obj["coordinates"] = PointToSlice(inner)
case sql.Linestring:
obj["type"] = "LineString"
obj["coordinates"] = LineToSlice(inner)
case sql.Polygon:
obj["type"] = "Polygon"
obj["coordinates"] = PolyToSlice(inner)
default:
return nil, ErrInvalidArgumentType.New(g.FunctionName())
}
case sql.Point:
obj["type"] = "Point"
obj["coordinates"] = PointToSlice(v)
Expand All @@ -160,6 +191,7 @@ func (g *AsGeoJSON) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
if err != nil {
return nil, err
}
// Return null if precision is null
if p == nil {
return nil, nil
}
Expand Down Expand Up @@ -203,6 +235,7 @@ func (g *AsGeoJSON) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
if err != nil {
return nil, err
}
// Return null if flag is null
if flag == nil {
return nil, nil
}
Expand All @@ -226,15 +259,42 @@ func (g *AsGeoJSON) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
if _flag < 0 || _flag > 7 {
return nil, sql.ErrInvalidArgumentDetails.New(g.FunctionName(), _flag)
}
// TODO: figure out exactly what flags are; only 1,3,5 have bbox
if _flag%2 == 1 {

switch _flag {
// Flags 1,3,5 have bounding box
case 1, 3, 5:
// Calculate bounding box
res := FindBBox(val)
for i, r := range res {
res[i] = math.Round(r*prec) / prec
}
obj["bbox"] = res
// Flag 2 and 4 add CRS URN (EPSG: <srid>); only shows up if SRID != 0
case 2, 4:
// CRS obj only shows up if srid != 0
srid := GetSRID(val)
if srid != 0 {
// Create CRS URN Object
crs := make(map[string]interface{})
crs["type"] = "name"

// Create properties
props := make(map[string]interface{})
// Flag 2 is short format CRS URN, while 4 is long format
sridStr := strconv.Itoa(srid)
if _flag == 2 {
props["name"] = "EPSG:" + sridStr
} else {
props["name"] = "urn:ogc:def:crs:EPSG::" + sridStr
}
// Add properties to crs
crs["properties"] = props

// Add CRS to main object
obj["crs"] = crs
}
}

return sql.JSONDocument{Val: obj}, nil
}

Expand Down
63 changes: 63 additions & 0 deletions sql/expression/function/geojson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,69 @@ func TestAsGeoJSON(t *testing.T) {
require.NoError(err)
require.Equal(sql.JSONDocument{Val: map[string]interface{}{"coordinates": [][][2]float64{{{0, 0}, {0, 1}, {1, 1}, {0, 0}}}, "type": "Polygon", "bbox": [4]float64{0, 0, 1, 1}}}, v)
})
t.Run("convert point with srid 0 and flag 2", func(t *testing.T) {
require := require.New(t)
f, err := NewAsGeoJSON(
expression.NewLiteral(sql.Point{X: 1, Y: 2}, sql.PointType{}),
expression.NewLiteral(1, sql.Int64),
expression.NewLiteral(2, sql.Int64),
)
require.NoError(err)

v, err := f.Eval(sql.NewEmptyContext(), nil)
require.NoError(err)
obj := map[string]interface{}{
"coordinates": [2]float64{1, 2},
"type": "Point",
}
require.Equal(sql.JSONDocument{Val: obj}, v)
})
t.Run("convert point with srid 4326 and flag 2", func(t *testing.T) {
require := require.New(t)
f, err := NewAsGeoJSON(
expression.NewLiteral(sql.Point{SRID: 4326, X: 1, Y: 2}, sql.PointType{}),
expression.NewLiteral(1, sql.Int64),
expression.NewLiteral(2, sql.Int64),
)
require.NoError(err)

v, err := f.Eval(sql.NewEmptyContext(), nil)
require.NoError(err)
obj := map[string]interface{}{
"crs": map[string]interface{}{
"type": "name",
"properties": map[string]interface{}{
"name": "EPSG:4326",
},
},
"coordinates": [2]float64{1, 2},
"type": "Point",
}
require.Equal(sql.JSONDocument{Val: obj}, v)
})
t.Run("convert point with srid 4326 and flag 4", func(t *testing.T) {
require := require.New(t)
f, err := NewAsGeoJSON(
expression.NewLiteral(sql.Point{SRID: 4326, X: 1, Y: 2}, sql.PointType{}),
expression.NewLiteral(1, sql.Int64),
expression.NewLiteral(4, sql.Int64),
)
require.NoError(err)

v, err := f.Eval(sql.NewEmptyContext(), nil)
require.NoError(err)
obj := map[string]interface{}{
"crs": map[string]interface{}{
"type": "name",
"properties": map[string]interface{}{
"name": "urn:ogc:def:crs:EPSG::4326",
},
},
"coordinates": [2]float64{1, 2},
"type": "Point",
}
require.Equal(sql.JSONDocument{Val: obj}, v)
})
t.Run("convert null is null", func(t *testing.T) {
require := require.New(t)
f, err := NewAsGeoJSON(
Expand Down