This repository was archived by the owner on Feb 23, 2020. It is now read-only.
forked from rubund/addrnodeimport
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddrnodeimport
More file actions
executable file
·198 lines (171 loc) · 14.7 KB
/
Copy pathaddrnodeimport
File metadata and controls
executable file
·198 lines (171 loc) · 14.7 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python3
#
# addrnodeimport
#
# (c) Ruben Undheim 2014
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
munipfirst=False
import os
import sys
import glob
import re
if len(sys.argv) != 5:
print ("Missing command line argument.\nUsage: "+sys.argv[0]+" <top-zip-file> <municipality number> <work-dir> <offline-mode>")
sys.exit()
if len(sys.argv) == 5 and sys.argv[4] == "1":
network=False
else:
network=True
topzipfile = sys.argv[1]
munipnumber = sys.argv[2]
munipnumberpadded = "%04d" % (int(munipnumber))
basenametopzipfile = os.path.basename(topzipfile)
#prefix = basenametopzipfile.split('.')[0]
cachedir = sys.argv[3]
apiurl = "http://overpass-api.de/api"
#apiurl = "http://overpass.osm.rambler.ru/cgi"
#if not os.path.isdir(cachedir+"/"+prefix):
# os.system("unzip "+topzipfile+" -d "+cachedir+"/"+prefix+"")
#prefix = str(cachedir)+"/"+str(prefix)+"/elveg/adresse"
#if not os.path.isfile(prefix+"/"+str(munipnumberpadded)+"adresser.sos"):
# ret = os.system("cd "+prefix+" ; unzip "+str(munipnumberpadded)+"Adresser.ZIP");
# if ret != 0:
# print ("Failed to find this municipality")
# sys.exit()
#os.system("mv "+prefix+"/*"+str(munipnumberpadded)+"adresser.sos "+prefix+"/"+str(munipnumberpadded)+"adresser.sos")
#os.system("sed -i 's/ANSI/ISO-8859-10/' "+prefix+"/"+str(munipnumberpadded)+"adresser.sos")
#os.system("sosi2osm "+prefix+"/"+str(munipnumberpadded)+"adresser.sos /usr/share/addrnodeimport/kartverketadresser.lua > "+prefix+"/"+str(munipnumberpadded)+"Adresser.osm")
#os.system("/usr/lib/addrnodeimport/python/sosiaddr.py "+prefix+"/"+str(munipnumberpadded)+"adresser.sos > "+prefix+"/"+str(munipnumberpadded)+"Adresser.osm")
#osmfilename = prefix+"/"+str(munipnumberpadded)+"Adresser.osm"
if not os.path.isdir("/tmp/osm_temp"):
os.system("mkdir -p /tmp/osm_temp");
os.system("rm -rf /var/cache/addrnodeimport/reports/changed_"+munipnumberpadded+".osm")
os.system("rm -rf /var/cache/addrnodeimport/reports/duplicates_"+munipnumberpadded+".osm")
os.system("rm -rf /var/cache/addrnodeimport/reports/newnodes_"+munipnumberpadded+".osm")
os.system("rm -rf /var/cache/addrnodeimport/reports/notmatched_"+munipnumberpadded+".osm")
os.system("rm -rf /var/cache/addrnodeimport/reports/onlynumber_"+munipnumberpadded+".osm")
os.system("rm -rf /var/cache/addrnodeimport/reports/otherobjects_"+munipnumberpadded+".osm")
os.system("rm -rf /var/cache/addrnodeimport/reports/postcodecityfixes_"+munipnumberpadded+".osm")
os.system("rm -rf /var/cache/addrnodeimport/reports/veivegfixes_"+munipnumberpadded+".osm")
os.system("rm -rf /var/cache/addrnodeimport/reports/new/"+munipnumberpadded)
if os.path.isfile(""+cachedir+"/municipality_borders/"+str(munipnumberpadded)+".osm"):
os.system("perl /usr/lib/addrnodeimport/perl/osm2poly.pl "+cachedir+"/municipality_borders/"+str(munipnumberpadded)+".osm > "+cachedir+"/municipality_borders/"+str(munipnumberpadded)+".txt")
boundarea = os.popen("getedges -m 0.01 "+cachedir+"/municipality_borders/"+str(munipnumberpadded)+".osm").read()
if network:
os.system("rm -rf /tmp/osm_temp/ways_"+munipnumberpadded+".osm")
os.system("rm -rf /tmp/osm_temp/nodes_"+munipnumberpadded+".osm")
if not os.path.isfile("/tmp/osm_temp/ways_"+munipnumberpadded+".osm" ):
os.system(" wget \""+apiurl+"/interpreter?data=((way[\\\"addr:housenumber\\\"] "+boundarea+";>;);(way[\\\"abandoned:addr:housenumber\\\"] "+boundarea+";>;););out meta;\" -O /tmp/osm_temp/ways_"+munipnumberpadded+".osm")
if not os.path.isfile("/tmp/osm_temp/nodes_"+munipnumberpadded+".osm" ):
os.system(" wget \""+apiurl+"/interpreter?data=((node[\\\"addr:housenumber\\\"] "+boundarea+";<;);(node[\\\"abandoned:addr:housenumber\\\"] "+boundarea+";<;););out meta;\" -O /tmp/osm_temp/nodes_"+munipnumberpadded+".osm")
else:
os.system("rm -rf /tmp/osm_temp/ways_"+munipnumberpadded+".osm")
os.system("rm -rf /tmp/osm_temp/nodes_"+munipnumberpadded+".osm")
print (boundarea)
boundareamatch = re.compile("\((\d+\.\d+),(\d+\.\d+),(\d+\.\d+),(\d+\.\d+)\)")
matches = boundareamatch.match(boundarea)
bottomcoord = matches.group(1)
leftcoord = matches.group(2)
topcoord = matches.group(3)
rightcoord = matches.group(4)
if munipfirst:
print ("\nExtracting municipality from pbf:")
os.system("osmosis --read-pbf file=\"/var/cache/addrnodeimport/norway-latest.osm.pbf\" --bounding-box bottom="+bottomcoord+" left="+leftcoord+" top="+topcoord+" right="+rightcoord+" --write-pbf file=\"/tmp/osm_temp/objects_"+munipnumberpadded+".pbf\"")
print ("\nExtracting address nodes:")
os.system("osmosis --read-pbf file=\"/tmp/osm_temp/objects_"+munipnumberpadded+".pbf\" --tf accept-nodes \"addr:housenumber\"=* --tf reject-ways --tf reject-relations --write-xml file=\"/tmp/osm_temp/nodes_"+munipnumberpadded+"-tmp1.osm\"")
os.system("osmosis --read-pbf file=\"/tmp/osm_temp/objects_"+munipnumberpadded+".pbf\" --tf accept-nodes \"abandoned:addr:housenumber\"=* --tf reject-ways --tf reject-relations --write-xml file=\"/tmp/osm_temp/nodes_"+munipnumberpadded+"-tmp2.osm\"")
os.system("osmosis --rx \"/tmp/osm_temp/nodes_"+munipnumberpadded+"-tmp1.osm\" --rx \"/tmp/osm_temp/nodes_"+munipnumberpadded+"-tmp2.osm\" --merge --wx \"/tmp/osm_temp/nodes_"+munipnumberpadded+".osm\"")
print ("\nExtracting address ways:")
os.system("rm -rf /tmp/osm_temp/nodes_"+munipnumberpadded+"-tmp1.osm /tmp/osm_temp/nodes_"+munipnumberpadded+"-tmp2.osm")
os.system("osmosis --read-pbf file=\"/tmp/osm_temp/objects_"+munipnumberpadded+".pbf\" --tf accept-ways \"addr:housenumber\"=* --tf reject-relations --used-node --write-xml file=\"/tmp/osm_temp/ways_"+munipnumberpadded+"-tmp1.osm\"")
os.system("osmosis --read-pbf file=\"/tmp/osm_temp/objects_"+munipnumberpadded+".pbf\" --tf accept-ways \"abandoned:addr:housenumber\"=* --tf reject-relations --used-node --write-xml file=\"/tmp/osm_temp/ways_"+munipnumberpadded+"-tmp2.osm\"")
os.system("osmosis --rx \"/tmp/osm_temp/ways_"+munipnumberpadded+"-tmp1.osm\" --rx \"/tmp/osm_temp/ways_"+munipnumberpadded+"-tmp2.osm\" --merge --wx \"/tmp/osm_temp/ways_"+munipnumberpadded+".osm\"")
os.system("rm -rf /tmp/osm_temp/ways_"+munipnumberpadded+"-tmp1.osm /tmp/osm_temp/ways_"+munipnumberpadded+"-tmp2.osm /tmp/osm_temp/objects_"+munipnumberpadded+".pbf")
else:
if not os.path.isfile("/tmp/osm_temp/nodes.pbf") or not os.path.isfile("/tmp/osm_temp/ways.pbf"):
print ("\nExtracting all way addresses:")
os.system("osmosis --read-pbf file=\"/var/cache/addrnodeimport/norway-latest.osm.pbf\" --tf accept-ways \"addr:housenumber\"=* --tf reject-relations --used-node --write-pbf file=\"/tmp/osm_temp/ways-tmp1.pbf\"")
os.system("osmosis --read-pbf file=\"/var/cache/addrnodeimport/norway-latest.osm.pbf\" --tf accept-ways \"abandoned:addr:housenumber\"=* --tf reject-relations --used-node --write-pbf file=\"/tmp/osm_temp/ways-tmp2.pbf\"")
os.system("osmosis --read-pbf \"/tmp/osm_temp/ways-tmp1.pbf\" --read-pbf \"/tmp/osm_temp/ways-tmp2.pbf\" --merge --write-pbf \"/tmp/osm_temp/ways.pbf\"")
print ("\nExtracting all node addresses:")
os.system("osmosis --read-pbf file=\"/var/cache/addrnodeimport/norway-latest.osm.pbf\" --tf accept-nodes \"addr:housenumber\"=* --tf reject-ways --tf reject-relations --write-pbf file=\"/tmp/osm_temp/nodes-tmp1.pbf\"")
os.system("osmosis --read-pbf file=\"/var/cache/addrnodeimport/norway-latest.osm.pbf\" --tf accept-nodes \"abandoned:addr:housenumber\"=* --tf reject-ways --tf reject-relations --write-pbf file=\"/tmp/osm_temp/nodes-tmp2.pbf\"")
os.system("osmosis --read-pbf \"/tmp/osm_temp/nodes-tmp1.pbf\" --read-pbf \"/tmp/osm_temp/nodes-tmp2.pbf\" --merge --write-pbf \"/tmp/osm_temp/nodes.pbf\"")
print ("\nGet ways for municipality:")
os.system("osmosis --read-pbf file=\"/tmp/osm_temp/ways.pbf\" --bounding-box bottom="+bottomcoord+" left="+leftcoord+" top="+topcoord+" right="+rightcoord+" --write-xml file=\"/tmp/osm_temp/ways_"+munipnumberpadded+".osm\"")
print ("\nGet nodes for municipality:")
os.system("osmosis --read-pbf file=\"/tmp/osm_temp/nodes.pbf\" --bounding-box bottom="+bottomcoord+" left="+leftcoord+" top="+topcoord+" right="+rightcoord+" --write-xml file=\"/tmp/osm_temp/nodes_"+munipnumberpadded+".osm\"")
if not os.path.isfile(""+cachedir+"/municipality_borders/"+str(munipnumberpadded)+".osm"):
os.system("mkdir -p "+cachedir+"/municipality_borders/")
if os.path.isfile("/usr/share/addrnodeimport/municipality_borders/"+str(munipnumberpadded)+".osm"):
os.system("cp /usr/share/addrnodeimport/municipality_borders/"+str(munipnumberpadded)+".osm "+cachedir+"/municipality_borders/")
else:
os.system("tar -xf /usr/share/addrnodeimport/municipality_borders.tar.gz -C "+cachedir+"")
os.system("cp "+cachedir+"/osm/"+str(munipnumberpadded)+".osm "+cachedir+"/municipality_borders/")
# alreadyextracted = glob.glob("munip_borders/"+munipnumberpadded+"/"+str(munipnumberpadded)+"*_Adm*.sos")
# if alreadyextracted == None or len(alreadyextracted) == 0:
# print (alreadyextracted)
# zipfiles = glob.glob("/home/ruben/n50/Kartdata_"+str(munipnumber)+"_*N50_SOSI.zip")
# if zipfiles != None and len(zipfiles) > 0:
# zipfile = zipfiles[0]
# print ("there is"+zipfile)
# os.system("mkdir -p munip_borders")
# os.system("unzip "+zipfile+" -d munip_borders/"+munipnumberpadded+"")
#
# adminfiles = glob.glob("munip_borders/"+munipnumberpadded+"/"+str(munipnumberpadded)+"*_Adm*.sos")
# if adminfiles != None and len(adminfiles) > 0:
# adminfile = adminfiles[0]
# print ("here now "+adminfile)
# os.system("sosi2osm "+adminfile+" default.lua > munip_borders/"+munipnumberpadded+".osm")
# os.system("osmosispolygon -o munip_borders/"+str(munipnumberpadded)+"-tmp.osm munip_borders/"+munipnumberpadded+".osm")
# os.system("sed -i 's/>/>\\n/g' munip_borders/"+munipnumberpadded+"-tmp.osm")
# os.system("mv munip_borders/"+str(munipnumberpadded)+"-tmp.osm munip_borders/"+munipnumberpadded+".osm")
os.system("mkdir -p "+cachedir+"/reports")
if os.path.isfile(""+cachedir+"/municipality_borders/"+str(munipnumberpadded)+".txt"):
os.system("osmosis --read-xml enableDateParsing=no file=\"/tmp/osm_temp/ways_"+munipnumberpadded+".osm\" --bounding-polygon file=\""+cachedir+"/municipality_borders/"+str(munipnumberpadded)+".txt\" --write-xml file=/tmp/osm_temp/ways_"+munipnumberpadded+"-tmp.osm")
os.system("mv /tmp/osm_temp/ways_"+munipnumberpadded+"-tmp.osm /tmp/osm_temp/ways_"+munipnumberpadded+".osm")
os.system("osmosis --read-xml enableDateParsing=no file=\"/tmp/osm_temp/nodes_"+munipnumberpadded+".osm\" --bounding-polygon file=\""+cachedir+"/municipality_borders/"+str(munipnumberpadded)+".txt\" --write-xml file=/tmp/osm_temp/nodes_"+munipnumberpadded+"-tmp.osm")
os.system("mv /tmp/osm_temp/nodes_"+munipnumberpadded+"-tmp.osm /tmp/osm_temp/nodes_"+munipnumberpadded+".osm")
os.system("/usr/lib/addrnodeimport/python/rest_simple_fetch.py \""+cachedir+"/municipality_borders/"+str(munipnumberpadded)+".osm\" \"/tmp/osm_temp/newnodeswithin_"+munipnumberpadded+"-tmp.osm\"")
afternumber = os.popen("countnodes /tmp/osm_temp/newnodeswithin_"+munipnumberpadded+"-tmp.osm").read()
beforenumber = afternumber # XXX: Cheat. just to make new REST interface work
reportfile3 = open(""+cachedir+"/reports/report3_"+munipnumberpadded+".txt","w")
reportfile3.write("Before: "+beforenumber+"After: "+afternumber+"")
reportfile3.close()
print ("Processing "+munipnumberpadded+"...")
reportcontent = os.popen("getmissingandreport -s -t "+cachedir+"/reports/veivegfixes_"+munipnumberpadded+".osm -n "+cachedir+"/reports/notmatched_"+munipnumberpadded+".osm -d "+cachedir+"/reports/duplicates_"+munipnumberpadded+".osm -e "+cachedir+"/reports/otherobjects_"+munipnumberpadded+".osm -o "+cachedir+"/reports/newnodes_"+munipnumberpadded+".osm -w /tmp/osm_temp/ways_"+munipnumberpadded+".osm -c /usr/share/addrnodeimport/corrections.xml /tmp/osm_temp/nodes_"+munipnumberpadded+".osm \"/tmp/osm_temp/newnodeswithin_"+munipnumberpadded+"-tmp.osm\"").read()
reportcontent2 = os.popen("fillinpostcode -c /usr/share/addrnodeimport/corrections.xml -o "+cachedir+"/reports/postcodecityfixes_"+munipnumberpadded+".osm -n "+cachedir+"/reports/onlynumber_"+munipnumberpadded+".osm -s /tmp/osm_temp/nodes_"+munipnumberpadded+".osm -w /tmp/osm_temp/ways_"+munipnumberpadded+".osm \"/tmp/osm_temp/newnodeswithin_"+munipnumberpadded+"-tmp.osm\"").read()
reportcontent3 = os.popen("findchanged "+cachedir+"/reports/notmatched_"+munipnumberpadded+".osm "+cachedir+"/reports/newnodes_"+munipnumberpadded+".osm "+cachedir+"/reports/changed_"+munipnumberpadded+".osm").read()
os.system("mkdir -p "+cachedir+"/reports/new/"+munipnumberpadded+"/")
if os.path.isfile("/tmp/osm_temp/newnodeswithin_"+munipnumberpadded+"-tmp.osm"):
reportcontent4 = os.popen("addrnodeimport_create_change -c /usr/share/addrnodeimport/corrections.xml /tmp/osm_temp/nodes_"+munipnumberpadded+".osm /tmp/osm_temp/ways_"+munipnumberpadded+".osm /tmp/osm_temp/newnodeswithin_"+munipnumberpadded+"-tmp.osm "+cachedir+"/reports/new/"+munipnumberpadded+"/").read()
#else:
# reportcontent4 = os.popen("addrnodeimport_create_change -c /usr/share/addrnodeimport/corrections.xml /tmp/osm_temp/nodes_"+munipnumberpadded+".osm /tmp/osm_temp/ways_"+munipnumberpadded+".osm "+osmfilename+" "+cachedir+"/reports/new/"+munipnumberpadded+"/").read()
os.system("mv "+cachedir+"/reports/new/"+munipnumberpadded+"/changes.osm "+cachedir+"/reports/new/"+munipnumberpadded+"/changes_"+munipnumberpadded+".osm")
os.system("mv "+cachedir+"/reports/new/"+munipnumberpadded+"/notmatched_manual.osm "+cachedir+"/reports/new/"+munipnumberpadded+"/notmatched_manual_"+munipnumberpadded+".osm")
os.system("mv "+cachedir+"/reports/new/"+munipnumberpadded+"/newnodes_manual.osm "+cachedir+"/reports/new/"+munipnumberpadded+"/newnodes_manual_"+munipnumberpadded+".osm")
reportfile = open(""+cachedir+"/reports/report_"+munipnumberpadded+".txt","w")
reportfile.write(reportcontent)
reportfile.close()
reportfile2 = open(""+cachedir+"/reports/report2_"+munipnumberpadded+".txt","w")
reportfile2.write(reportcontent2)
reportfile2.close()
reportfile4 = open(""+cachedir+"/reports/report4_"+munipnumberpadded+".txt","w")
reportfile4.write(reportcontent3)
reportfile4.close()
reportfile4 = open(""+cachedir+"/reports/report5_"+munipnumberpadded+".txt","w")
reportfile4.write(reportcontent4)
reportfile4.close()