add LineString function

master
Oliver Tonnhofer 2013-04-21 16:37:05 +02:00
parent 7f28d079fb
commit 00333e147c
2 changed files with 49 additions and 0 deletions

19
geom/geom.go Normal file
View File

@ -0,0 +1,19 @@
package geom
import (
"gogeos"
"goposm/element"
)
func LineString(nodes []element.Node) []byte {
geos := gogeos.NewGEOS()
defer geos.Finish()
coordSeq := geos.CreateCoordSeq(uint32(len(nodes)), 2)
for i, nd := range nodes {
coordSeq.SetXY(geos, uint32(i), nd.Long, nd.Lat)
}
geom := coordSeq.AsLineString(geos)
defer geos.Destroy(geom)
return geos.AsWKB(geom)
}

30
geom/geom_test.go Normal file
View File

@ -0,0 +1,30 @@
package geom
import (
"goposm/element"
"regexp"
"testing"
)
func _TestLineString(t *testing.T) {
nodes := make([]element.Node, 2)
nodes[0] = element.Node{Lat: 0, Long: 0}
nodes[1] = element.Node{Lat: 0, Long: 10}
wkt := LineString(nodes)
re := regexp.MustCompile("LINESTRING \\(0\\.0* 0\\.0*, 10\\.0* 0\\.0*\\)")
if !re.Match(wkt) {
t.Errorf("%#v", wkt)
}
}
func BenchmarkLineString(b *testing.B) {
size := 16
nodes := make([]element.Node, size)
for i := 0; i < size; i++ {
nodes[i] = element.Node{Lat: 0, Long: float64(i)}
}
for i := 0; i < b.N; i++ {
LineString(nodes)
}
}