-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcartogram_worker.py
217 lines (160 loc) · 7.24 KB
/
cartogram_worker.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
from PyQt4.QtCore import pyqtSignal, QObject, QPyNullVariant
from qgis.core import QgsDistanceArea, QgsGeometry, QgsPoint
from cartogram_feature import CartogramFeature
import math
import traceback
class CartogramWorker(QObject):
"""Background worker which actually creates the cartogram."""
finished = pyqtSignal(object, int)
error = pyqtSignal(Exception, basestring)
progress = pyqtSignal(float)
def __init__(self, layer, field_name, iterations):
"""Constructor."""
QObject.__init__(self)
self.layer = layer
self.field_name = field_name
self.iterations = iterations
# used to store the computed minimum value when the input data contains
# zero or null values in the column used to create the cartogram
self.min_value = None
# set default exit code - if this doesn't change everything went well
self.exit_code = -1
def run(self):
ret = None
try:
feature_count = self.layer.featureCount()
step = self.get_step()
steps = 0
for i in range(self.iterations):
(meta_features,
force_reduction_factor) = self.get_reduction_factor(
self.layer, self.field_name)
for feature in self.layer.getFeatures():
if self.exit_code > 0:
break
old_geometry = feature.geometry()
new_geometry = self.transform(meta_features,
force_reduction_factor, old_geometry)
self.layer.dataProvider().changeGeometryValues({
feature.id() : new_geometry})
steps += 1
if step == 0 or steps % step == 0:
self.progress.emit(steps / float(feature_count) * 100)
if self.exit_code == -1:
self.progress.emit(100)
ret = self.layer
except Exception, e:
self.error.emit(e, traceback.format_exc())
self.finished.emit(ret, self.exit_code)
def kill(self):
self.exit_code = 1
def get_reduction_factor(self, layer, field):
"""Calculate the reduction factor."""
data_provider = layer.dataProvider()
meta_features = []
total_area = 0.0
total_value = 0.0
if self.min_value is None:
self.min_value = self.get_min_value(data_provider, field)
for feature in data_provider.getFeatures():
meta_feature = CartogramFeature()
geometry = QgsGeometry(feature.geometry())
area = QgsDistanceArea().measure(geometry)
total_area += area
feature_value = feature.attribute(field)
if type(feature_value) is QPyNullVariant or feature_value == 0:
feature_value = self.min_value / 100
total_value += feature_value
meta_feature.area = area
meta_feature.value = feature_value
centroid = geometry.centroid()
(cx, cy) = centroid.asPoint().x(), centroid.asPoint().y()
meta_feature.center_x = cx
meta_feature.center_y = cy
meta_features.append(meta_feature)
fraction = total_area / total_value
total_size_error = 0
for meta_feature in meta_features:
polygon_value = meta_feature.value
polygon_area = meta_feature.area
if polygon_area < 0:
polygon_area = 0
# this is our 'desired' area...
desired_area = polygon_value * fraction
# calculate radius, a zero area is zero radius
radius = math.sqrt(polygon_area / math.pi)
meta_feature.radius = radius
if desired_area / math.pi > 0:
mass = math.sqrt(desired_area / math.pi) - radius
meta_feature.mass = mass
else:
meta_feature.mass = 0
size_error = max(polygon_area, desired_area) / \
min(polygon_area, desired_area)
total_size_error += size_error
average_error = total_size_error / len(meta_features)
force_reduction_factor = 1 / (average_error + 1)
return (meta_features, force_reduction_factor)
def transform(self, meta_features, force_reduction_factor, geometry):
"""Transform the geometry based on the force reduction factor."""
if geometry.isMultipart():
geometries = []
for polygon in geometry.asMultiPolygon():
new_polygon = self.transform_polygon(polygon, meta_features,
force_reduction_factor)
geometries.append(new_polygon)
return QgsGeometry.fromMultiPolygon(geometries)
else:
polygon = geometry.asPolygon()
new_polygon = self.transform_polygon(polygon, meta_features,
force_reduction_factor)
return QgsGeometry.fromPolygon(new_polygon)
def transform_polygon(self, polygon, meta_features,
force_reduction_factor):
"""Transform the geometry of a single polygon."""
new_line = []
new_polygon = []
for line in polygon:
for point in line:
x = x0 = point.x()
y = y0 = point.y()
# compute the influence of all shapes on this point
for feature in meta_features:
cx = feature.center_x
cy = feature.center_y
distance = math.sqrt((x0 - cx) ** 2 + (y0 - cy) ** 2)
if (distance > feature.radius):
# calculate the force exerted on points far away from
# the centroid of this polygon
force = feature.mass * feature.radius / distance
else:
# calculate the force exerted on points close to the
# centroid of this polygon
xF = distance / feature.radius
# distance ** 2 / feature.radius ** 2 instead of xF
force = feature.mass * (xF ** 2) * (4 - (3 * xF))
force = force * force_reduction_factor / distance
x = (x0 - cx) * force + x
y = (y0 - cy) * force + y
new_line.append(QgsPoint(x, y))
new_polygon.append(new_line)
new_line = []
return new_polygon
def get_step(self):
"""Determine how often the progress bar should be updated."""
feature_count = self.layer.featureCount()
# update the progress bar at each .1% increment
step = feature_count // 1000
# because we use modulo to determine if we should emit the progress
# signal, the step needs to be greater than 1
if step < 2:
step = 2
return step
def get_min_value(self, data_provider, field):
features = []
for feature in data_provider.getFeatures():
feature_value = feature.attribute(field)
if not type(feature_value) is QPyNullVariant \
and feature_value != 0:
features.append(feature.attribute(field))
return min(features)