-
Notifications
You must be signed in to change notification settings - Fork 0
/
read-vfk.py
executable file
·141 lines (107 loc) · 3.66 KB
/
read-vfk.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/python
"""
This script reads VFK data using OGR library. VFK data are converted
into SQLite database.
Reference: http://gdal.org/ogr/drv_vfk.html
"""
import os
import sys
import time
from osgeo import ogr, gdal
# global variables - check VFK line
NUM_OF_LINES = { 'B' : 0,
'D' : 0,
'H' : 0 }
def init_ogr():
"""Initialize OGR library - set environmental variables"""
# enable debug messages (?)
os.environ['CPL_DEBUG'] = 'OFF'
# enable read per data block (will be slower)
### os.environ['OGR_VFK_DB_READ_ALL_BLOCKS'] = 'NO'
# define custom error handler
gdal.PushErrorHandler(error_handler)
def error_handler(err_level, err_no, err_msg):
"""Ignore errors and warnings
@todo: use logging
"""
if err_level == 0 and err_no == 0:
check_vfk_line(0, err_msg)
def open_vfk(filename):
"""Open VFK file as an OGR datasource
@param filename path to the VFK file to be open
@return OGR datasource
"""
ds = ogr.Open(filename)
if ds is None:
fatal_error("Unable to open '%s'" % filename)
return ds
def fatal_error(message):
"""Print error message and exit
@param message the message to be printed
"""
sys.exit(message)
def print_delimiter(delimiter = '-', num = 80):
print delimiter * num
def check_vfk_line(idx, line):
"""Callback function to check line from VFK file
@todo Implement it as an event handler when the new line from VFK
file is read see:
http://trac.osgeo.org/gdal/browser/trunk/gdal/ogr/ogrsf_frmts/vfk/vfkreader.cpp#L125
@param idx line index
@param line line string
"""
try:
key = line[1]
NUM_OF_LINES[key] += 1
except (IndexError, KeyError):
pass
def read_vfk(filename, suppress_output=False):
ds = open_vfk(filename)
nlayers = ds.GetLayerCount()
for lidx in range(nlayers):
layer = ds.GetLayer(lidx)
gtype = ogr.GeometryTypeToName(layer.GetGeomType()).lower()
if gtype == 'none':
gtype = ' ' * 8
if not layer:
fatal_error("Unable to get %d layer" % lidx)
nfeat = layer.GetFeatureCount()
if not suppress_output:
sys.stdout.write("Fetching %-6s ... %6d %11s features detected\n" % \
(layer.GetName(), nfeat, gtype))
# close OGR datasource (flush memory)
ds.Destroy()
def main(filename):
# initialize OGR library for this script
init_ogr()
# open VFK file as an OGR datasource
print "Reading '%s'..." % filename
# first pass
print_delimiter()
t0 = time.clock()
# overwrite existing SQLite database when reading VFK data
os.environ['OGR_VFK_DB_OVERWRITE'] = 'YES'
read_vfk(filename) # get list of OGR layers (ie. VFK blocks &B)
print_delimiter()
print time.clock() - t0, "seconds process time (first pass - from VFK file)"
print_delimiter()
# second pass
t0 = time.clock()
# read data from DB
os.environ['OGR_VFK_DB_OVERWRITE'] = 'NO'
read_vfk(filename, suppress_output=True)
print time.clock() - t0, "seconds process time (second pass - from SQLite DB)"
print_delimiter()
# print "Number of processed records:"
# for key, value in NUM_OF_LINES.iteritems():
# print "\t%s: %d" % (key, value)
# print_delimiter()
return 0
if __name__ == "__main__":
if len(sys.argv) != 2:
print "Usage: read_vfk.py path/to/vfk/file"
sys.exit(1)
filename = sys.argv[1]
if not os.path.exists(filename):
fatal_error("File '%s' not found" % filename)
sys.exit(main(filename))