Skip to content

Commit

Permalink
Merge pull request #128 from m-pavel/master
Browse files Browse the repository at this point in the history
Replaced strings split with splitting by regexp
  • Loading branch information
paulmach authored Apr 26, 2023
2 parents 5e33049 + 3331c67 commit 80e521d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
37 changes: 27 additions & 10 deletions encoding/wkt/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wkt

import (
"errors"
"regexp"
"strconv"
"strings"

Expand All @@ -18,6 +19,10 @@ var (

// ErrUnsupportedGeometry is returned when geometry type is not supported by this lib.
ErrUnsupportedGeometry = errors.New("wkt: unsupported geometry")

doubleParen = regexp.MustCompile(`\)[\s|\t]*\)[\s|\t]*,[\s|\t]*\([\s|\t]*\(`)
singleParen = regexp.MustCompile(`\)[\s|\t]*,[\s|\t]*\(`)
noParen = regexp.MustCompile(`[\s|\t]*,[\s|\t]*`)
)

// UnmarshalPoint returns the point represented by the wkt string.
Expand Down Expand Up @@ -131,7 +136,6 @@ func trimSpaceBrackets(s string) string {
if s[len(s)-1] == ')' {
s = s[:len(s)-1]
}

return strings.Trim(s, " ")
}

Expand Down Expand Up @@ -216,7 +220,7 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
}
s = strings.Replace(s, "MULTIPOINT", "", -1)
s = trimSpaceBrackets(s)
ps := strings.Split(s, ",")
ps := splitByRegexp(s, noParen)
mp := orb.MultiPoint{}
for _, p := range ps {
tp, err := parsePoint(trimSpaceBrackets(p))
Expand All @@ -241,9 +245,9 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
}
s = strings.Replace(s, "MULTILINESTRING", "", -1)
ml := orb.MultiLineString{}
for _, l := range strings.Split(trimSpaceBrackets(s), "),(") {
for _, l := range splitByRegexp(trimSpaceBrackets(s), singleParen) {
tl := orb.LineString{}
for _, p := range strings.Split(trimSpaceBrackets(l), ",") {
for _, p := range splitByRegexp(trimSpaceBrackets(l), noParen) {
tp, err := parsePoint(trimSpaceBrackets(p))
if err != nil {
return nil, err
Expand All @@ -260,7 +264,7 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
}
s = strings.Replace(s, "LINESTRING", "", -1)
s = trimSpaceBrackets(s)
ps := strings.Split(s, ",")
ps := splitByRegexp(s, noParen)
ls := orb.LineString{}
for _, p := range ps {
tp, err := parsePoint(trimSpaceBrackets(p))
Expand All @@ -277,11 +281,12 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
}
s = strings.Replace(s, "MULTIPOLYGON", "", -1)
mpol := orb.MultiPolygon{}
for _, ps := range strings.Split(trimSpaceBrackets(s), ")),((") {

for _, ps := range splitByRegexp(trimSpaceBrackets(s), doubleParen) {
pol := orb.Polygon{}
for _, ls := range strings.Split(trimSpaceBrackets(ps), "),(") {
for _, ls := range splitByRegexp(trimSpaceBrackets(ps), singleParen) {
ring := orb.Ring{}
for _, p := range strings.Split(ls, ",") {
for _, p := range splitByRegexp(ls, noParen) {
tp, err := parsePoint(trimSpaceBrackets(p))
if err != nil {
return nil, err
Expand All @@ -301,10 +306,10 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
s = strings.Replace(s, "POLYGON", "", -1)
s = trimSpaceBrackets(s)

rs := strings.Split(s, "),(")
rs := splitByRegexp(s, singleParen)
pol := make(orb.Polygon, 0, len(rs))
for _, r := range rs {
ps := strings.Split(trimSpaceBrackets(r), ",")
ps := splitByRegexp(trimSpaceBrackets(r), noParen)
ring := orb.Ring{}
for _, p := range ps {
tp, err := parsePoint(trimSpaceBrackets(p))
Expand All @@ -322,3 +327,15 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {

return
}

func splitByRegexp(s string, re *regexp.Regexp) []string {
indexes := re.FindAllStringIndex(s, -1)
start := 0
result := make([]string, len(indexes)+1)
for i, element := range indexes {
result[i] = s[start:element[0]]
start = element[1]
}
result[len(indexes)] = s[start:]
return result
}
20 changes: 20 additions & 0 deletions encoding/wkt/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ func TestUnmarshalMultiPoint(t *testing.T) {
s: "MULTIPOINT((1 2),(0.5 1.5))",
expected: orb.MultiPoint{{1, 2}, {0.5, 1.5}},
},
{
name: "spaces",
s: "MULTIPOINT((1 2) , (0.5 1.5))",
expected: orb.MultiPoint{{1, 2}, {0.5, 1.5}},
},
}

for _, tc := range cases {
Expand Down Expand Up @@ -215,6 +220,11 @@ func TestUnmarshalLineString(t *testing.T) {
s: "LINESTRING(1 2,0.5 1.5)",
expected: orb.LineString{{1, 2}, {0.5, 1.5}},
},
{
name: "spaces",
s: "LINESTRING(1 2 , 0.5 1.5)",
expected: orb.LineString{{1, 2}, {0.5, 1.5}},
},
}

for _, tc := range cases {
Expand Down Expand Up @@ -354,6 +364,11 @@ func TestUnmarshalPolygon(t *testing.T) {
s: "POLYGON((1 2,3 4),(5 6,7 8))",
expected: orb.Polygon{{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}},
},
{
name: "two rings with spaces",
s: "POLYGON((1 2,3 4) , (5 6 , 7 8))",
expected: orb.Polygon{{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}},
},
}

for _, tc := range cases {
Expand Down Expand Up @@ -421,6 +436,11 @@ func TestUnmarshalMutilPolygon(t *testing.T) {
s: "MULTIPOLYGON(((1 2,3 4)),((5 6,7 8),(1 2,5 4)))",
expected: orb.MultiPolygon{{{{1, 2}, {3, 4}}}, {{{5, 6}, {7, 8}}, {{1, 2}, {5, 4}}}},
},
{
name: "mulit-polygon with spaces",
s: "MULTIPOLYGON(((1 2,3 4)) , ((5 6,7 8), (1 2,5 4)))",
expected: orb.MultiPolygon{{{{1, 2}, {3, 4}}}, {{{5, 6}, {7, 8}}, {{1, 2}, {5, 4}}}},
},
}

for _, tc := range cases {
Expand Down

0 comments on commit 80e521d

Please sign in to comment.