imposm3/geom/geom.go

74 lines
1.5 KiB
Go
Raw Normal View History

2013-04-21 18:37:05 +04:00
package geom
import (
2013-05-10 12:08:31 +04:00
"errors"
2013-04-21 18:37:05 +04:00
"goposm/element"
2013-05-06 21:24:49 +04:00
"goposm/geom/geos"
2013-04-21 18:37:05 +04:00
)
2013-05-10 12:08:31 +04:00
var (
ErrorOneNodeWay = errors.New("need at least two separate nodes for way")
)
2013-05-06 21:24:49 +04:00
func PointWKB(g *geos.GEOS, node element.Node) ([]byte, error) {
coordSeq, err := g.CreateCoordSeq(1, 2)
2013-05-06 18:17:07 +04:00
if err != nil {
return nil, err
}
// coordSeq inherited by LineString
2013-05-06 21:24:49 +04:00
coordSeq.SetXY(g, 0, node.Long, node.Lat)
geom, err := coordSeq.AsPoint(g)
2013-05-06 18:17:07 +04:00
if err != nil {
return nil, err
}
2013-05-06 21:24:49 +04:00
defer g.Destroy(geom)
return g.AsWKB(geom)
2013-05-06 18:17:07 +04:00
}
2013-05-06 21:24:49 +04:00
func LineStringWKB(g *geos.GEOS, nodes []element.Node) ([]byte, error) {
2013-05-10 12:08:31 +04:00
if len(nodes) < 2 {
return nil, ErrorOneNodeWay
}
2013-05-06 21:24:49 +04:00
coordSeq, err := g.CreateCoordSeq(uint32(len(nodes)), 2)
2013-04-24 00:02:27 +04:00
if err != nil {
return nil, err
}
// coordSeq inherited by LineString
2013-04-21 18:37:05 +04:00
for i, nd := range nodes {
2013-05-06 21:24:49 +04:00
coordSeq.SetXY(g, uint32(i), nd.Long, nd.Lat)
2013-04-21 18:37:05 +04:00
}
2013-05-06 21:24:49 +04:00
geom, err := coordSeq.AsLineString(g)
2013-04-24 00:02:27 +04:00
if err != nil {
return nil, err
}
2013-05-06 21:24:49 +04:00
defer g.Destroy(geom)
return g.AsWKB(geom)
2013-04-21 18:37:05 +04:00
}
2013-04-24 00:02:27 +04:00
2013-05-06 21:24:49 +04:00
func PolygonWKB(g *geos.GEOS, nodes []element.Node) ([]byte, error) {
coordSeq, err := g.CreateCoordSeq(uint32(len(nodes)), 2)
2013-04-24 00:02:27 +04:00
if err != nil {
return nil, err
}
// coordSeq inherited by LineString, no destroy
for i, nd := range nodes {
2013-05-06 21:24:49 +04:00
err := coordSeq.SetXY(g, uint32(i), nd.Long, nd.Lat)
2013-04-24 00:02:27 +04:00
if err != nil {
return nil, err
}
}
2013-05-06 21:24:49 +04:00
geom, err := coordSeq.AsLinearRing(g)
2013-04-24 00:02:27 +04:00
if err != nil {
return nil, err
}
// geom inherited by Polygon, no destroy
2013-05-06 21:24:49 +04:00
geom = g.CreatePolygon(geom, nil)
2013-04-24 00:02:27 +04:00
if err != nil {
return nil, err
}
2013-05-06 21:24:49 +04:00
defer g.Destroy(geom)
2013-04-24 00:02:27 +04:00
2013-05-06 21:24:49 +04:00
return g.AsWKB(geom)
2013-04-24 00:02:27 +04:00
}