-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplanet_up.py
executable file
·170 lines (118 loc) · 4.59 KB
/
planet_up.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Affero General Public License
version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of this license along
with this program; if not, see http://www.gnu.org/licenses/.
"""
__version__ = "1.0.0"
__author__ = "Bernd Weigelt"
__copyright__ = "Copyright 2020 Bernd Weigelt"
__license__ = "AGPLv3"
__maintainer__ = "Bernd Weigelt"
__email__ = "[email protected]"
__status__ = "released"
import os
import argparse
WORK_DIR = os.path.expanduser('~') + "/map_build/"
def info(msg):
print(("II: " + msg))
def warn(msg):
print(("WW: " + msg))
def error(msg):
print(("EE: " + msg))
def checkprg(programmtofind, solutionhint):
# test if an executable can be found by
# following $PATH
# raise message if fails and returns 1
# on success return 0
# search follows $PATH
if os.system("which " + programmtofind) == 1:
error(programmtofind + " not found")
print(solutionhint)
quit()
def is_there(find, solutionhint):
# test if a file or dir can be found at a predefined place
# raise message if fails and returns 1
# on success return 0
if not os.path.exists(find):
error(find + " not found")
print(solutionhint)
hint = ("mkdir " + WORK_DIR)
is_there(WORK_DIR, hint)
os.chdir(WORK_DIR)
for dir in ['planet', 'o5m']:
if not os.path.exists(dir):
os.mkdir(dir)
# argparse
parser = argparse.ArgumentParser(
prog='PROG',
formatter_class=argparse.RawDescriptionHelpFormatter,
description=('''\
To create and update a local copy of the OSM planet file
'''))
parser = argparse.ArgumentParser(
prog='PROG',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
args = parser.parse_args()
for tool in ['osmconvert', 'osmupdate', 'osmfilter']:
hint = tool + " missed, please use mk_osmtools to build it from sources"
checkprg(tool, hint)
os.chdir(WORK_DIR + "/planet")
if not os.path.exists("planet.osm.pbf") and not os.path.exists("planet.o5m"):
print()
info("Download started. Size ~50 Gigabytes... please wait! ")
os.system("wget http://ftp5.gwdg.de/pub/misc/openstreetmap/"
+ "planet.openstreetmap.org/pbf/planet-latest.osm.pbf")
conv_hint = ("\n\n Download successful!"
+ "\n You can now convert the downloaded planet file,"
+ "\n from OSM.PBF to O5M. "
+ "\n O5M stores the raw data in a larger file, ~40 percent,"
+ "\n but extracting data is much faster. Converting the file"
+ "\n take 15 to 20 minutes on a fast drive."
+ "\n\n Start converting? [Y|n]")
conv = input(conv_hint)
if conv != "n":
os.system("osmconvert "
+ "--drop-version "
+ "--drop-author "
+ "planet-latest.osm.pbf -o=planet.o5m")
if os.path.exists("planet.o5m"):
os.remove("planet-latest.osm.pbf")
else:
os.rename("planet-latest.osm.pbf", "planet.osm.pbf")
print()
info("if needed in the future, you can convert the planet file"
+ "\n in the planet dir with "
+ "\n\n 'osmconvert planet.osm.pbf -o=planet.o5m' ")
# updating the planet file(s)
print()
info("Now updating the existing planet file(s)")
print()
# to use only one temp dir for osmupdate work one level higher
os.chdir(WORK_DIR)
if os.path.exists("planet/planet.o5m"):
os.rename("planet/planet.o5m", "planet/planet_temp.o5m")
os.system(" osmupdate -v --daily --keep-tempfiles "
+ "--drop-version "
+ "--drop-author "
+ "planet/planet_temp.o5m planet/planet.o5m ")
if os.path.exists("planet/planet.o5m"):
os.remove("planet/planet_temp.o5m")
if os.path.exists("planet/planet.osm.pbf"):
os.rename("planet/planet.osm.pbf", "planet/plamet_temp.osm.pbf")
os.system(" osmupdate -v --daily --keep-tempfiles "
+ "--drop-version "
+ "--drop-author "
+ "planet/planet_temp.osm.pbf planet/planet.osm.pbf")
if os.path.exists("planet/planet.osm.pbf"):
os.remove("planet/planet_temp.osm.pbf")
print()
info("Habe fertig!")
quit()