@@ -2,6 +2,7 @@ package wkt
22
33import (
44 "errors"
5+ "regexp"
56 "strconv"
67 "strings"
78
1819
1920 // ErrUnsupportedGeometry is returned when geometry type is not supported by this lib.
2021 ErrUnsupportedGeometry = errors .New ("wkt: unsupported geometry" )
22+
23+ doubleParen = regexp .MustCompile (`\)[\s|\t]*\)[\s|\t]*,[\s|\t]*\([\s|\t]*\(` )
24+ singleParen = regexp .MustCompile (`\)[\s|\t]*,[\s|\t]*\(` )
25+ noParen = regexp .MustCompile (`[\s|\t]*,[\s|\t]*` )
2126)
2227
2328// UnmarshalPoint returns the point represented by the wkt string.
@@ -131,7 +136,6 @@ func trimSpaceBrackets(s string) string {
131136 if s [len (s )- 1 ] == ')' {
132137 s = s [:len (s )- 1 ]
133138 }
134-
135139 return strings .Trim (s , " " )
136140}
137141
@@ -216,7 +220,7 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
216220 }
217221 s = strings .Replace (s , "MULTIPOINT" , "" , - 1 )
218222 s = trimSpaceBrackets (s )
219- ps := strings . Split (s , "," )
223+ ps := splitByRegexp (s , noParen )
220224 mp := orb.MultiPoint {}
221225 for _ , p := range ps {
222226 tp , err := parsePoint (trimSpaceBrackets (p ))
@@ -241,9 +245,9 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
241245 }
242246 s = strings .Replace (s , "MULTILINESTRING" , "" , - 1 )
243247 ml := orb.MultiLineString {}
244- for _ , l := range strings . Split (trimSpaceBrackets (s ), "),(" ) {
248+ for _ , l := range splitByRegexp (trimSpaceBrackets (s ), singleParen ) {
245249 tl := orb.LineString {}
246- for _ , p := range strings . Split (trimSpaceBrackets (l ), "," ) {
250+ for _ , p := range splitByRegexp (trimSpaceBrackets (l ), noParen ) {
247251 tp , err := parsePoint (trimSpaceBrackets (p ))
248252 if err != nil {
249253 return nil , err
@@ -260,7 +264,7 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
260264 }
261265 s = strings .Replace (s , "LINESTRING" , "" , - 1 )
262266 s = trimSpaceBrackets (s )
263- ps := strings . Split (s , "," )
267+ ps := splitByRegexp (s , noParen )
264268 ls := orb.LineString {}
265269 for _ , p := range ps {
266270 tp , err := parsePoint (trimSpaceBrackets (p ))
@@ -277,11 +281,12 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
277281 }
278282 s = strings .Replace (s , "MULTIPOLYGON" , "" , - 1 )
279283 mpol := orb.MultiPolygon {}
280- for _ , ps := range strings .Split (trimSpaceBrackets (s ), ")),((" ) {
284+
285+ for _ , ps := range splitByRegexp (trimSpaceBrackets (s ), doubleParen ) {
281286 pol := orb.Polygon {}
282- for _ , ls := range strings . Split (trimSpaceBrackets (ps ), "),(" ) {
287+ for _ , ls := range splitByRegexp (trimSpaceBrackets (ps ), singleParen ) {
283288 ring := orb.Ring {}
284- for _ , p := range strings . Split (ls , "," ) {
289+ for _ , p := range splitByRegexp (ls , noParen ) {
285290 tp , err := parsePoint (trimSpaceBrackets (p ))
286291 if err != nil {
287292 return nil , err
@@ -301,10 +306,10 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
301306 s = strings .Replace (s , "POLYGON" , "" , - 1 )
302307 s = trimSpaceBrackets (s )
303308
304- rs := strings . Split (s , "),(" )
309+ rs := splitByRegexp (s , singleParen )
305310 pol := make (orb.Polygon , 0 , len (rs ))
306311 for _ , r := range rs {
307- ps := strings . Split (trimSpaceBrackets (r ), "," )
312+ ps := splitByRegexp (trimSpaceBrackets (r ), noParen )
308313 ring := orb.Ring {}
309314 for _ , p := range ps {
310315 tp , err := parsePoint (trimSpaceBrackets (p ))
@@ -322,3 +327,15 @@ func Unmarshal(s string) (geom orb.Geometry, err error) {
322327
323328 return
324329}
330+
331+ func splitByRegexp (s string , re * regexp.Regexp ) []string {
332+ indexes := re .FindAllStringIndex (s , - 1 )
333+ start := 0
334+ result := make ([]string , len (indexes )+ 1 )
335+ for i , element := range indexes {
336+ result [i ] = s [start :element [0 ]]
337+ start = element [1 ]
338+ }
339+ result [len (indexes )] = s [start :]
340+ return result
341+ }
0 commit comments