-
Notifications
You must be signed in to change notification settings - Fork 12
/
scanaerial.py
executable file
·328 lines (264 loc) · 9.38 KB
/
scanaerial.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
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 3 of the License.
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
This script will search an area with similar color and send
it back to JOSM
"""
from __future__ import division
from future import standard_library
standard_library.install_aliases()
from builtins import str
from builtins import range
__author__ = "Darafei Praliaskouski, Jonas Stein, Ruben W., Vort"
__license__ = "GPL"
__credits__ = ["Lakewalker-developer-Team", "JOSM-developer-Team", "Malenki"]
__email__ = "[email protected]"
__maintainer__ = "Jonas Stein"
__status__ = "Development"
try:
import configparser
except ImportError:
import configparser as configparser
import sys
from time import process_time as clock
from sys import argv, stdout, setrecursionlimit
from canvas import WmsCanvas
from debug import debug
from scanaerial_functions import distance, point_line_distance, douglas_peucker, bing_img_url
try:
import psyco
psyco.full()
except ImportError:
pass
try:
CONFIG_NAME = argv[4]
except IndexError:
CONFIG_NAME = "scanaerial.cfg"
try:
config = configparser.ConfigParser()
config.read_file(open(sys.path[0] + '/' + CONFIG_NAME))
except:
debug('could not read config')
exit(1)
### main ###
try:
INPUT_LAT = float(argv[1])
INPUT_LON = float(argv[2])
except (IndexError, ValueError):
debug("this program expects latitude longitude, now running debug mode")
INPUT_LAT = 51.0720147
INPUT_LON = 7.2181707
FIXED_ZOOM = 11
if config.has_option('WMS', 'fixedzoomlevel'):
FIXED_ZOOM = config.getint('WMS', 'fixedzoomlevel')
try:
ZOOM = int(float(argv[3]))
except (IndexError, ValueError):
ZOOM = FIXED_ZOOM
if ZOOM == 0:
ZOOM = FIXED_ZOOM
# Coordinates from command string.
# (format is decimal, for SAS-planet go to Settings and set'em there as --d.
# You can use SAS-Planet: click right mouse button on center of forest.
# SET SOME CONSTANTS
BLACK = 0
WHITE = 1
PROGRAM_START_TIMESTAMP = clock()
if not config.has_option('WMS', 'server_api'):
raise Exception('server_api must be specified')
if not config.has_option('WMS', 'server_name'):
raise Exception('server_name must be specified')
if not config.has_option('WMS', 'server_url'):
raise Exception('server_url must be specified')
SERVER_API = config.get('WMS', 'server_api')
if (SERVER_API != "wms") and (SERVER_API != "tms") and (SERVER_API != "bing"):
raise Exception('server_api must be one of: wms, tms, bing')
SERVER_NAME = config.get('WMS', 'server_name')
SERVER_URL = config.get('WMS', 'server_url')
if SERVER_API == "bing":
SERVER_URL = bing_img_url(SERVER_URL)
EMPTY_TILE_BYTES = 0
if config.has_option('WMS', 'empty_tile_bytes'):
EMPTY_TILE_BYTES = config.getint('WMS', 'empty_tile_bytes')
EMPTY_TILE_CHECKSUM = 0
if config.has_option('WMS', 'empty_tile_checksum'):
EMPTY_TILE_CHECKSUM = config.getint('WMS', 'empty_tile_checksum')
PROJECTION = config.get('WMS', 'projection')
TILE_SIZE = (config.getint('WMS', 'tile_sizex'),
config.getint('WMS', 'tile_sizey'))
WAY_TAGS = {"source:tracer": "scanaerial",
"source:zoomlevel": ZOOM,
"source:position": SERVER_NAME}
WAY_TAGS = dict(list(WAY_TAGS.items()) + config.items('TAGS'))
POLYGON_TAGS = WAY_TAGS.copy()
POLYGON_TAGS["type"] = "multipolygon"
# smoothness of way, bigger = less dots and turns = 0.6-1.3 is ok
DOUGLAS_PEUCKER_EPSILON = config.getfloat('SCAN', 'douglas_peucker_epsilon')
# sensivity for colour change, bigger = larger area covered = 20-23-25 is ok
colour_str = config.getfloat('SCAN', 'colour_str')
SIZE_LIMIT = 100
if config.has_option('SCAN', 'size_limit'):
SIZE_LIMIT = config.getint('SCAN', 'size_limit')
web = WmsCanvas(SERVER_URL, SERVER_API, PROJECTION, ZOOM,
TILE_SIZE, "RGB", EMPTY_TILE_BYTES, EMPTY_TILE_CHECKSUM)
was_expanded = True
normales_list = []
mask = WmsCanvas(None, SERVER_API, PROJECTION, ZOOM, TILE_SIZE, "1")
## Getting start pixel ##
x, y = web.PixelFrom4326(INPUT_LON, INPUT_LAT)
x, y = int(x), int(y)
INITCOLOUR = web[x, y]
colour_table = {}
DIRECTIONS = [(1, 0), (-1, 0), (0, 1), (0, -1)]
queue = set([(x, y), ])
mask[x, y] = WHITE
ttz = clock()
normales_list = set([])
norm_dir = {(0, -1): 0, (1, 0): 1, (0, 1): 2, (-1, 0): 3}
while queue:
px = queue.pop()
for d in DIRECTIONS:
x1, y1 = px[0] + d[0], px[1] + d[1]
if (x - SIZE_LIMIT) > x1:
continue
if (x + SIZE_LIMIT) < x1:
continue
if (y - SIZE_LIMIT) > y1:
continue
if (y + SIZE_LIMIT) < y1:
continue
if mask[x1, y1] is not WHITE:
col = web[x1, y1]
if col not in colour_table:
try:
colour_table[col] = (
distance(INITCOLOUR, col) <= colour_str)
except:
debug(web.tiles)
debug(mask.tiles)
if colour_table[col]:
mask[x1, y1] = WHITE
queue.add((x1, y1))
debug("First walk (masking): %s" % str(clock() - ttz))
debug("Colour table has %s entries" % len(colour_table))
queue = [(x, y), ]
ttz = clock()
mask.MaxFilter(config.getint('SCAN', 'maxfilter_setting'))
debug("B/W MaxFilter: %s" % str(clock() - ttz))
del web
oldmask = mask
mask = WmsCanvas(None, SERVER_API, PROJECTION, ZOOM, TILE_SIZE, "1")
bc = 1
ttz = clock()
while queue:
px = queue.pop()
for d in DIRECTIONS:
x1, y1 = px[0] + d[0], px[1] + d[1]
if mask[x1, y1] is not WHITE and oldmask[x1, y1] is WHITE:
mask[x1, y1] = WHITE
bc += 1
queue.append((x1, y1))
if oldmask[x1, y1] is not WHITE:
normales_list.add(((x1 + px[0]) / 2.,
(y1 + px[1]) / 2.,
norm_dir[px[0] - x1, px[1] - y1]))
debug("Found %s normales here." % len(normales_list))
debug("Second walk (leaving only poly): %s" % str(clock() - ttz))
stdout.write('<osm version="0.6">')
node_num = 0
way_num = 0
outline = []
popped = False
lin = []
tz = clock()
while normales_list:
if not popped:
x, y, d = normales_list.pop()
lin = [(x, y), ]
popped = True
found = False
if d == 0: # up-pointing vector
search = [(x + 0.5, y - 0.5, 3), (x + 1, y, 0), (x + 0.5, y + 0.5, 1)]
if d == 1:
search = [(x + 0.5, y + 0.5, 0), (x, y + 1, 1), (x - 0.5, y + 0.5, 2)]
if d == 2:
search = [(x - 0.5, y + 0.5, 1), (x - 1, y, 2), (x - 0.5, y - 0.5, 3)]
if d == 3:
search = [(x - 0.5, y - 0.5, 2), (x, y - 1, 3), (x + 0.5, y - 0.5, 0)]
for kp in search:
if kp in normales_list:
normales_list.remove(kp)
x, y, d = kp
lin.append((x, y))
found = True
break
if not found:
popped = False
if not config.getint('SCAN', 'deactivate_simplifying'):
lin.append(lin[0])
lin = douglas_peucker(lin, DOUGLAS_PEUCKER_EPSILON)
lin.pop()
debug("line found; simplified to %s" % len(lin))
else:
debug("skipped simplifing")
if len(lin) >= 6:
outline.append(lin)
lin = []
if lin:
if not config.getint('SCAN', 'deactivate_simplifying'):
lin.append(lin[0])
lin = douglas_peucker(lin, DOUGLAS_PEUCKER_EPSILON)
lin.pop()
debug("line post-found; simplified to %s" % len(lin))
else:
debug("skipped simplifing")
if len(lin) >= 4:
outline.append(lin)
debug("Normales walk: %s, " % (str(clock() - ttz),))
roles = {}
for lin in outline:
area = 0
prx, pry = lin[-1]
for x, y in lin:
area += (x * pry - y * prx) * 1.0 / 2
prx = x
pry = y
node_num -= 1
lon, lat = oldmask.PixelAs4326(x, y)
stdout.write('<node id="%s" lon="%s" lat="%s" version="1" />' %
(node_num, lon, lat))
way_num -= 1
roles[way_num] = (area > 0)
stdout.write('<way id="%s" version="1">' % (way_num))
for y in range(node_num, node_num + len(lin)):
stdout.write('<nd ref="%s" />' % (y))
stdout.write('<nd ref="%s" />' % (node_num))
if len(outline) == 1:
for z in list(WAY_TAGS.items()):
stdout.write(' <tag k="%s" v="%s" />"' % z)
stdout.write("</way>")
if way_num < -1:
stdout.write('<relation id="-1" version="1">')
for y in range(way_num, 0):
role = ("inner", "outer")[int(roles[y])]
stdout.write('<member type="way" ref="%s" role="%s" />' % (y, role))
for z in list(POLYGON_TAGS.items()):
stdout.write(' <tag k="%s" v="%s" />"' % z)
stdout.write('</relation>')
stdout.write("</osm>")
stdout.flush()
debug("All done in: %s" % str(clock() - PROGRAM_START_TIMESTAMP))
""" TODO
* Zoomlevel to the source? May be soon there are higher resolutions and on different
zoomlevels things look quite different
"""