aboutsummaryrefslogtreecommitdiffstats
path: root/pbf2db/pbf2db.py
blob: c6089fc41a571fc462e4a8c40f8cf1559e765306 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/python

import osmformat_pb2
import fileformat_pb2
import sys
import socket
import zlib
from binarystream import BinaryStream


headerSizeMax = 64 * 1024
bodySizeMax = 32*1024*1024

f = open("berlin.osm.pbf")
stream = BinaryStream(f)
headerSize = socket.ntohl(stream.readUInt32());

if headerSizeMax < headerSize:
    raise ValueError("Header to long")

headerbuff = stream.readBytes(headerSize)
blobheader = fileformat_pb2.BlobHeader()
blobheader.ParseFromString(headerbuff)
bodysize = blobheader.datasize

if bodySizeMax < bodysize:
    raise ValueError("Body to fat")

blobbuff = stream.readBytes(bodysize)
blob = fileformat_pb2.Blob()
blob.ParseFromString(blobbuff)

if blob.raw != "": 
    rawstr = blob.raw
else:
    rawstr = zlib.decompress(blob.zlib_data)

headerblock = osmformat_pb2.HeaderBlock()
headerblock.ParseFromString(rawstr)

print "Source:",headerblock.source
print "Writingprog:",headerblock.writingprogram
print "required features:",headerblock.required_features