Skip to content

Commit

Permalink
GetType and GetCoords implementation (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchavakis authored Dec 10, 2021
1 parent 0fce0f6 commit d242c16
Show file tree
Hide file tree
Showing 8 changed files with 811 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Turf for Go is a ported library in GoLang ported from the Turf.js library.
- [ ] clustersDbscan
- [ ] clustersKmeans

## Meta
## Meta - Invariant
- [x] coordAll
- [x] coordEach
- [ ] coordReduce
Expand All @@ -131,9 +131,9 @@ Turf for Go is a ported library in GoLang ported from the Turf.js library.
- [ ] flattenEach
- [ ] flattenReduce
- [x] getCoord
- [ ] getCoords
- [x] getCoords
- [ ] getGeom
- [ ] getType
- [x] getType
- [ ] geomEach
- [ ] geomReduce
- [ ] propEach
Expand Down
2 changes: 1 addition & 1 deletion geojson/feature/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (f *Feature) ToLineString() (*geometry.LineString, error) {

// ToMultiLineString converts a MultiLineString faeture to MultiLineString geometry.
func (f *Feature) ToMultiLineString() (*geometry.MultiLineString, error) {
if f.Geometry.GeoJSONType != geojson.MiltiLineString {
if f.Geometry.GeoJSONType != geojson.MultiLineString {
return nil, errors.New("invalid geometry type")
}
var coords [][][]float64
Expand Down
2 changes: 1 addition & 1 deletion geojson/geometry/geometry.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (g *Geometry) ToLineString() (*LineString, error) {

// ToMultiLineString converts a MultiLineString faeture to MultiLineString geometry.
func (g *Geometry) ToMultiLineString() (*MultiLineString, error) {
if g.GeoJSONType != geojson.MiltiLineString {
if g.GeoJSONType != geojson.MultiLineString {
return nil, errors.New("invalid geometry type")
}
var coords [][][]float64
Expand Down
4 changes: 2 additions & 2 deletions geojson/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const (
MultiPoint OBjectType = "MultiPoint"
// LineString Defines a LineString Type https://tools.ietf.org/html/rfc7946#section-3.1.4
LineString OBjectType = "LineString"
// MiltiLineString Defines a MultiLineString Type https://tools.ietf.org/html/rfc7946#section-3.1.5
MiltiLineString OBjectType = "MultiLineString"
// MultiLineString Defines a MultiLineString Type https://tools.ietf.org/html/rfc7946#section-3.1.5
MultiLineString OBjectType = "MultiLineString"
// Polygon Defines a Polygon Type https://tools.ietf.org/html/rfc7946#section-3.1.6
Polygon OBjectType = "Polygon"
// MultiPolygon Defines a MultiPolygon Type https://tools.ietf.org/html/rfc7946#section-3.1.7
Expand Down
141 changes: 141 additions & 0 deletions invariant/invariant.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,144 @@ func GetCoord(coord interface{}) ([]float64, error) {

return nil, errors.New("coord must be GeoJSON Point or an Array of numbers")
}

// GetCoords unwrap coordinates from a Feature, Geometry, Object or an Array
//
// Example:
//
// coords: &geometry.Point{
// Lat: 23.52,
// Lng: 44.34,
// },
//
// coords,err := GetCoords(coords)
// = []float64{44.34, 23.52}
//
func GetCoords(coords interface{}) (interface{}, error) {
if coords == nil {
return nil, errors.New("coord is required")
}
switch gtp := coords.(type) {
case *feature.Feature: // Feature
if gtp.Type == geojson.Feature {
switch gtp.Geometry.GeoJSONType {
case geojson.Point:
_, err := gtp.Geometry.ToPoint()
if err != nil {
return nil, err
}
return gtp.Geometry.Coordinates, nil
case geojson.MultiPoint:
_, err := gtp.Geometry.ToMultiPoint()
if err != nil {
return nil, err
}
return gtp.Geometry.Coordinates, nil
case geojson.LineString:
_, err := gtp.Geometry.ToLineString()
if err != nil {
return nil, err
}
return gtp.Geometry.Coordinates, nil
case geojson.Polygon:
_, err := gtp.Geometry.ToPolygon()
if err != nil {
return nil, err
}
return gtp.Geometry.Coordinates, nil
case geojson.MultiLineString:
_, err := gtp.Geometry.ToMultiLineString()
if err != nil {
return nil, err
}
return gtp.Geometry.Coordinates, nil
case geojson.MultiPolygon:
_, err := gtp.Geometry.ToMultiPolygon()
if err != nil {
return nil, err
}
return gtp.Geometry.Coordinates, nil
}
}
// Geometry
case *geometry.Polygon:
result := [][][]float64{}
for i := 0; i < len(gtp.Coordinates); i++ {
coords := [][]float64{}
for j := 0; j < len(gtp.Coordinates[i].Coordinates); j++ {
coords = append(coords, []float64{gtp.Coordinates[i].Coordinates[j].Lng, gtp.Coordinates[i].Coordinates[j].Lat})
}
result = append(result, coords)
}
return result, nil
case *geometry.LineString:
result := [][]float64{}
for i := 0; i < len(gtp.Coordinates); i++ {
result = append(result, []float64{gtp.Coordinates[i].Lng, gtp.Coordinates[i].Lat})
}
return result, nil
case *geometry.MultiLineString:
result := [][][]float64{}
for i := 0; i < len(gtp.Coordinates); i++ {
tmp := [][]float64{}
for j := 0; j < len(gtp.Coordinates[i].Coordinates); j++ {
tmp = append(tmp, []float64{gtp.Coordinates[i].Coordinates[j].Lng, gtp.Coordinates[i].Coordinates[j].Lat})
}
result = append(result, tmp)
}
return result, nil
case *geometry.Point:
result := []float64{}
result = append(result, gtp.Lng, gtp.Lat)
return result, nil
case *geometry.MultiPoint:
result := [][]float64{}
for i := 0; i < len(gtp.Coordinates); i++ {
tmp := []float64{}
tmp = append(tmp, gtp.Coordinates[i].Lng, gtp.Coordinates[i].Lat)
result = append(result, tmp)
}
return result, nil
case *geometry.MultiPolygon:
result := [][][][]float64{}
for i := 0; i < len(gtp.Coordinates); i++ {
tmp := [][][]float64{}
for j := 0; j < len(gtp.Coordinates[i].Coordinates); j++ {
tmPoly := [][]float64{}
for k := 0; k < len(gtp.Coordinates[i].Coordinates[j].Coordinates); k++ {
tmPoly = append(tmPoly, []float64{gtp.Coordinates[i].Coordinates[j].Coordinates[k].Lng, gtp.Coordinates[i].Coordinates[j].Coordinates[k].Lat})
}
tmp = append(tmp, tmPoly)
}
result = append(result, tmp)
}
return result, nil
case []float64:
if utils.IsArray(gtp) && len(gtp) >= 2 {
return gtp, nil
}
}
return nil, errors.New("coord must be GeoJSON Point or an Array of numbers")
}

// GetType returns the GeoJSON object's type
//
// Examples:
//
// fp, err := feature.FromJSON("{ \"type\": \"Feature\", \"properties\": {}, \"geometry\": { \"type\": \"Point\", \"coordinates\": [102, 0.5] } }")
// result := GetType(fp)
// ="Point"
//
func GetType(geojson interface{}) string {
switch gtp := geojson.(type) {
case *feature.Feature:
return string(gtp.Geometry.GeoJSONType)
case *feature.Collection:
return string(gtp.Type)
case *geometry.Collection:
return string(gtp.Type)
case *geometry.Geometry:
return string(gtp.GeoJSONType)
}
return "invalid"
}
Loading

0 comments on commit d242c16

Please sign in to comment.