add elements, model and binary package

master
Oliver Tonnhofer 2013-01-28 07:55:34 +01:00
parent 354c89945c
commit bcabc7a339
6 changed files with 148 additions and 28 deletions

31
binary/serialize.go Normal file
View File

@ -0,0 +1,31 @@
package binary
import (
"code.google.com/p/goprotobuf/proto"
"goposm/element"
"goposm/model"
)
// struct MarshalError {
// msg string
// }
func Marshal(elem interface{}) ([]byte, error) {
switch typedElem := elem.(type) {
case element.Node:
return MarshalNode(typedElem)
default:
panic("invalid elem to marshal")
}
return []byte{}, nil
}
func MarshalNode(node element.Node) ([]byte, error) {
pbfNode := &model.Node{}
foo := int64(node.Id)
pbfNode.Id = &foo
pbfNode.FromWgsCoord(node.Long, node.Lat)
return proto.Marshal(pbfNode)
}

39
element/element.go Normal file
View File

@ -0,0 +1,39 @@
package element
type Tags map[string]string
type OSMID int64
type OSMElem struct {
Id OSMID
Tags Tags
}
type Node struct {
OSMElem
Lat float64
Long float64
}
type Way struct {
OSMElem
Nodes []OSMID
}
type MemberType int
const (
NODE MemberType = iota
WAY MemberType = iota
RELATION MemberType = iota
)
type Member struct {
Id OSMID
Type MemberType
Role string
}
type Relation struct {
OSMElem
Members []Member
}

View File

@ -1,5 +1,5 @@
// Code generated by protoc-gen-go.
// source: model.proto
// source: model/model.proto
// DO NOT EDIT!
package model
@ -52,6 +52,39 @@ func (x *RelationMember_MemberType) UnmarshalJSON(data []byte) error {
return nil
}
type Node struct {
Id *int64 `protobuf:"varint,1,req,name=id" json:"id,omitempty"`
Long *uint32 `protobuf:"varint,2,req,name=long" json:"long,omitempty"`
Lat *uint32 `protobuf:"varint,3,req,name=lat" json:"lat,omitempty"`
Tags []*Tag `protobuf:"bytes,4,rep,name=tags" json:"tags,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (this *Node) Reset() { *this = Node{} }
func (this *Node) String() string { return proto.CompactTextString(this) }
func (*Node) ProtoMessage() {}
func (this *Node) GetId() int64 {
if this != nil && this.Id != nil {
return *this.Id
}
return 0
}
func (this *Node) GetLong() uint32 {
if this != nil && this.Long != nil {
return *this.Long
}
return 0
}
func (this *Node) GetLat() uint32 {
if this != nil && this.Lat != nil {
return *this.Lat
}
return 0
}
type Tag struct {
Key *string `protobuf:"bytes,1,req,name=key" json:"key,omitempty"`
Val *string `protobuf:"bytes,2,req,name=val" json:"val,omitempty"`

View File

@ -1,5 +1,13 @@
package model;
message Node {
required int64 id = 1;
required uint32 long = 2;
required uint32 lat= 3;
repeated Tag tags = 4;
}
message Tag {
required string key = 1;
required string val = 2;

View File

@ -3,12 +3,12 @@ package model
import "code.google.com/p/goprotobuf/proto"
import "log"
type Node struct {
Id int64
Tags map[string]string
Lon uint32
Lat uint32
}
// type Node struct {
// Id int64
// Tags map[string]string
// Lon uint32
// Lat uint32
// }
const COORD_FACTOR float64 = 11930464.7083 // ((2<<31)-1)/360.0
@ -21,14 +21,16 @@ func intToCoord(coord uint32) float64 {
}
func (this *Node) WgsCoord() (lon float64, lat float64) {
lon = intToCoord(this.Lon)
lat = intToCoord(this.Lat)
lon = intToCoord(this.GetLong())
lat = intToCoord(this.GetLat())
return
}
func (this *Node) FromWgsCoord(lon float64, lat float64) {
this.Lon = coordToInt(lon)
this.Lat = coordToInt(lat)
longInt := coordToInt(lon)
latInt := coordToInt(lat)
this.Long = &longInt
this.Lat = &latInt
}
func (this *Way) Marshal() []byte {

View File

@ -2,12 +2,14 @@ package main
import (
"code.google.com/p/goprotobuf/proto"
"encoding/binary"
structs "encoding/binary"
"fmt"
// "goposm/osmpbf/fileformat"
"bytes"
"compress/zlib"
"goposm/model"
"goposm/binary"
"goposm/element"
// "goposm/model"
"io"
"log"
"os"
@ -66,7 +68,7 @@ func (pbf *PBF) BlockPositions() (positions chan BlockPosition) {
func (pbf *PBF) nextBlobHeaderSize() (size int32) {
pbf.offset += 4
binary.Read(pbf.file, binary.BigEndian, &size)
structs.Read(pbf.file, structs.BigEndian, &size)
return
}
@ -121,13 +123,6 @@ func ReadPrimitiveBlock(file *os.File, offset int64, size int32) *osmpbf.Primiti
return block
}
// type Node struct {
// Id int64
// Tags map[string]string
// Lon uint32
// Lat uint32
// }
func DenseNodeTags(stringtable []string, keyvals []int32) (tags map[string]string, nextPos int) {
tags = make(map[string]string)
nextPos = 0
@ -147,10 +142,20 @@ func DenseNodeTags(stringtable []string, keyvals []int32) (tags map[string]strin
return
}
func ReadDenseNodes(dense *osmpbf.DenseNodes, block *osmpbf.PrimitiveBlock) (nodes []model.Node) {
const COORD_FACTOR float64 = 11930464.7083 // ((2<<31)-1)/360.0
func coordToInt(coord float64) uint32 {
return uint32((coord + 180.0) * COORD_FACTOR)
}
func intToCoord(coord uint32) float64 {
return float64((float64(coord) / COORD_FACTOR) - 180.0)
}
func ReadDenseNodes(dense *osmpbf.DenseNodes, block *osmpbf.PrimitiveBlock) (nodes []element.Node) {
var lastId int64
var lastLon, lastLat int64
nodes = make([]model.Node, len(dense.Id))
nodes = make([]element.Node, len(dense.Id))
granularity := int64(block.GetGranularity())
latOffset := block.GetLatOffset()
lonOffset := block.GetLonOffset()
@ -160,10 +165,9 @@ func ReadDenseNodes(dense *osmpbf.DenseNodes, block *osmpbf.PrimitiveBlock) (nod
lastId += dense.Id[i]
lastLon += dense.Lon[i]
lastLat += dense.Lat[i]
nodes[i].Id = lastId
nodes[i].FromWgsCoord(
(coordScale * float64(lonOffset+(granularity*lastLon))),
(coordScale * float64(latOffset+(granularity*lastLat))))
nodes[i].Id = element.OSMID(lastId)
nodes[i].Long = (coordScale * float64(lonOffset+(granularity*lastLon)))
nodes[i].Lat = (coordScale * float64(latOffset+(granularity*lastLat)))
}
return nodes
}
@ -188,7 +192,10 @@ func blockPositions(filename string) {
dense := group.GetDense()
if dense != nil {
nodes := ReadDenseNodes(dense, block)
lon, lat := nodes[1].WgsCoord()
lon, lat := nodes[0].Long, nodes[0].Lat
data, _ := binary.Marshal(nodes[0])
fmt.Printf("len: %d", len(data))
fmt.Printf("%v", data)
fmt.Printf("%12d %10.8f %10.8f\n", nodes[0].Id, lon, lat)
nodesCounter += len(dense.Id)
}