-
Notifications
You must be signed in to change notification settings - Fork 3
/
polylabel.py
197 lines (153 loc) · 5.1 KB
/
polylabel.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
# Modified slightly by Derek King
# From https://github.com/Twista/python-polylabel
# Based on original from https://github.com/mapbox/polylabel with the following licence:
# ISC License
# Copyright (c) 2016 Mapbox
# Permission to use, copy, modify, and/or distribute this software for any purpose
# with or without fee is hereby granted, provided that the above copyright notice
# and this permission notice appear in all copies.
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD TO
# THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
# IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
# OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.
from math import sqrt
import time
try:
# Python3
from queue import PriorityQueue
from math import inf
except ImportError:
# Python2
from Queue import PriorityQueue
inf = float("inf")
def _point_to_polygon_distance(x, y, polygon):
inside = False
min_dist_sq = inf
b = polygon[-1]
for a in polygon:
if ((a[1] > y) != (b[1] > y) and
(x < (b[0] - a[0]) * (y - a[1]) / (b[1] - a[1]) + a[0])):
inside = not inside
min_dist_sq = min(min_dist_sq, _get_seg_dist_sq(x, y, a, b))
b = a
result = sqrt(min_dist_sq)
if not inside:
return -result
return result
def _get_seg_dist_sq(px, py, a, b):
x = a[0]
y = a[1]
dx = b[0] - x
dy = b[1] - y
if dx != 0 or dy != 0:
t = ((px - x) * dx + (py - y) * dy) / (dx * dx + dy * dy)
if t > 1:
x = b[0]
y = b[1]
elif t > 0:
x += dx * t
y += dy * t
dx = px - x
dy = py - y
return dx * dx + dy * dy
class Cell(object):
def __init__(self, x, y, h, polygon):
self.h = h
self.y = y
self.x = x
self.d = _point_to_polygon_distance(x, y, polygon)
self.max = self.d + self.h * sqrt(2)
def __lt__(self, other):
return self.max < other.max
def __lte__(self, other):
return self.max <= other.max
def __gt__(self, other):
return self.max > other.max
def __gte__(self, other):
return self.max >= other.max
def __eq__(self, other):
return self.max == other.max
def _get_centroid_cell(polygon):
area = 0
x = 0
y = 0
b = polygon[-1] # prev
for a in polygon:
f = a[0] * b[1] - b[0] * a[1]
x += (a[0] + b[0]) * f
y += (a[1] + b[1]) * f
area += f * 3
b = a
if area == 0:
return Cell(polygon[0][0], polygon[0][1], 0, polygon)
return Cell(x / area, y / area, 0, polygon)
pass
def polylabel(polygon, precision=1.0, debug=False, with_distance=False):
# find bounding box
first_item = polygon[0]
min_x = first_item[0]
min_y = first_item[1]
max_x = first_item[0]
max_y = first_item[1]
for p in polygon:
if p[0] < min_x:
min_x = p[0]
if p[1] < min_y:
min_y = p[1]
if p[0] > max_x:
max_x = p[0]
if p[1] > max_y:
max_y = p[1]
width = max_x - min_x
height = max_y - min_y
cell_size = min(width, height)
h = cell_size / 2.0
cell_queue = PriorityQueue()
if cell_size == 0:
if with_distance:
return [min_x, min_y], None
else:
return [min_x, min_y]
# cover polygon with initial cells
x = min_x
while x < max_x:
y = min_y
while y < max_y:
c = Cell(x + h, y + h, h, polygon)
y += cell_size
cell_queue.put((-c.max, time.time(), c))
x += cell_size
best_cell = _get_centroid_cell(polygon)
bbox_cell = Cell(min_x + width / 2, min_y + height / 2, 0, polygon)
if bbox_cell.d > best_cell.d:
best_cell = bbox_cell
num_of_probes = cell_queue.qsize()
while not cell_queue.empty():
_, __, cell = cell_queue.get()
if cell.d > best_cell.d:
best_cell = cell
if debug:
print('found best {} after {} probes'.format(
round(1e4 * cell.d) / 1e4, num_of_probes))
if cell.max - best_cell.d <= precision:
continue
h = cell.h / 2
c = Cell(cell.x - h, cell.y - h, h, polygon)
cell_queue.put((-c.max, time.time(), c))
c = Cell(cell.x + h, cell.y - h, h, polygon)
cell_queue.put((-c.max, time.time(), c))
c = Cell(cell.x - h, cell.y + h, h, polygon)
cell_queue.put((-c.max, time.time(), c))
c = Cell(cell.x + h, cell.y + h, h, polygon)
cell_queue.put((-c.max, time.time(), c))
num_of_probes += 4
if debug:
print('num probes: {}'.format(num_of_probes))
print('best distance: {}'.format(best_cell.d))
if with_distance:
return [best_cell.x, best_cell.y], best_cell.d
else:
return [best_cell.x, best_cell.y]