forked from carsonfarmer/cartogram
-
Notifications
You must be signed in to change notification settings - Fork 2
/
doCartogram.py
435 lines (407 loc) · 18.1 KB
/
doCartogram.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# ----------------------------------------------------------------------------
#
# Cartogram Creator
#
# A QGIS plugin for creating cartograms based on polygon
# shapefile. Uses algorithm proposed in:
# Dougenik, J. A, N. R. Chrisman, and D. R. Niemeyer. 1985.
# "An algorithm to construct continuous cartograms."
# Professional Geographer 37:75-81
#
# This plugin uses python code adapted from Eric Wolfs pyCartogram.py
# See about dialog for more information.
#
# EMAIL: carson.farmer (at) gmail.com
# WEB : www.carsonfarmer.com
#
# ----------------------------------------------------------------------------
#
# licensed under the terms of GNU GPL 2
#
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# ----------------------------------------------------------------------------
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
import math
from form import Ui_Dialog
class Dialog(QDialog, Ui_Dialog):
def __init__(self, iface):
QDialog.__init__(self)
self.iface = iface
# Set up the user interface from Designer.
self.setupUi(self)
QObject.connect(self.toolOut, SIGNAL("clicked()"), self.outFile)
QObject.connect(self.inShape, SIGNAL("currentIndexChanged(QString)"),
self.update
)
# populate layer list
self.progressBar.setValue(0)
layermap = QgsMapLayerRegistry.instance().mapLayers()
import pdb
pdb.set_trace()
for name, layer in layermap.iteritems():
if layer.type() == QgsMapLayer.VectorLayer:
if layer.geometryType() == QGis.Polygon:
self.inShape.addItem(unicode(layer.name()))
def update(self, inputLayer):
self.inFields.clear()
changedLayer = self.getVectorLayerByName(inputLayer)
changedFields = self.getFieldList(changedLayer)
for cf in changedFields:
if cf.type() == QVariant.Int or cf.type() == QVariant.Double:
self.inFields.addItem(unicode(cf.name()))
for cf in changedFields:
if cf.type() == QVariant.Int or cf.type() == QVariant.Double:
self.inFields.addItem(unicode(cf.name()))
def accept(self):
if self.inShape.currentText() == "":
QMessageBox.information(self,
"Cartogram Creator",
"No input shapefile specified"
)
elif self.outShape.text() == "":
QMessageBox.information(self,
"Cartogram Creator",
"Please specify output shapefile"
)
else:
keep = bool(self.chkKeep.isChecked())
iterations = int(self.spnIterations.value())
inField = self.inFields.currentText()
inLayer = self.getVectorLayerByName(
unicode(self.inShape.currentText()))
self.progressBar.setValue(5)
outPath = self.outShape.text()
self.progressBar.setValue(10)
if "\\" in outPath:
outPath.replace("\\", "/")
self.progressBar.setValue(15)
tempList = self.cartogram(inLayer,
outPath,
iterations,
unicode(inField),
keep,
self.progressBar
)
if not keep:
for temp in tempList:
myInfo = QFileInfo(unicode(temp))
myBase = unicode(temp).strip(".shp")
if (myInfo.exists()):
QFile.remove(myBase + ".shp")
myInfo.setFile(myBase + ".shx")
if (myInfo.exists()):
QFile.remove(myBase + ".shx")
myInfo.setFile(myBase + ".dbf")
if (myInfo.exists()):
QFile.remove(myBase + ".dbf")
myInfo.setFile(myBase + ".prj")
if (myInfo.exists()):
QFile.remove(myBase + ".prj")
idx = outPath.rfind("/") - len(outPath) + 1
outName = outPath[idx:]
outName = outName[:len(outName)]
self.progressBar.setValue(100)
self.outShape.clear()
message = "Created output polygon shapefile:\n" + \
unicode(outPath) + \
"\n\nWould you like to add the new layer to the " + \
"TOC?"
addToTOC = QMessageBox.question(self,
"Cartogram Creator",
message,
QMessageBox.Yes,
QMessageBox.No,
QMessageBox.NoButton)
if addToTOC == QMessageBox.Yes:
vlayer = QgsVectorLayer(outPath + ".shp",
unicode(outName),
"ogr"
)
QgsMapLayerRegistry.instance().addMapLayer(vlayer)
self.progressBar.setValue(0)
def outFile(self):
self.outShape.clear()
fileDialog = QFileDialog()
fileDialog.setConfirmOverwrite(False)
outName = fileDialog.getSaveFileName(self,
"Cartogram Creator",
".",
"Shapefiles (*.shp)"
)
fileCheck = QFile(outName)
if fileCheck.exists():
QMessageBox.warning(self,
"Cartogram Creator",
"Cannot overwrite existing shapefile..."
)
else:
filePath = QFileInfo(outName).absoluteFilePath()
if filePath[-4:] != ".shp":
filePath = filePath + ".shp"
if outName:
self.outShape.clear()
self.outShape.insert(filePath)
def cartogram(self,
vlayer,
outPath,
iterations,
inField,
keep,
progressBar):
provider = vlayer.dataProvider()
totalFeats = provider.featureCount()
aLocal = []
dMean = 0
dForceReductionFactor = 0
dTotalArea = 0
dTotalValue = 0
tempList = []
for (i, attr) in [a for a in enumerate(provider.fields().toList())]:
if (inField == attr.name()):
index = i # get 'area' field index
basePath = outPath
basePath.replace(".shp", "")
for i in range(1, iterations + 1):
if (i > 1):
vlayer = QgsVectorLayer(tempPath, "", "ogr")
provider = vlayer.dataProvider()
if (i < iterations):
tempPath = basePath + "_" + unicode(i) + ".shp"
tempList.append(tempPath)
else:
tempPath = outPath + ".shp"
progressBar.setValue(20)
(dMean, aLocal, dForceReductionFactor, dTotalArea, dTotalValue) = \
self.getInfo(vlayer, provider, index)
progressBar.setValue(40)
feature = QgsFeature()
allAttrs = provider.attributeIndexes()
fieldList = self.getFieldList(vlayer)
sRs = provider.crs()
progressBar.setValue(45)
writer = QgsVectorFileWriter(unicode(tempPath),
"UTF-8",
fieldList,
QGis.WKBPolygon,
sRs
)
outfeat = QgsFeature()
geometry2 = QgsGeometry()
progressBar.setValue(50)
ink = 100.00 / provider.featureCount()
start = 1.00
for feature in provider.getFeatures():
geometry = feature.geometry()
geometry2 = self.TransformGeometry(aLocal,
dForceReductionFactor,
geometry,
totalFeats
)
outfeat.setGeometry(geometry2)
outfeat.setAttributes(feature.attributes())
writer.addFeature(outfeat)
start = start + ink
progressBar.setValue(start)
del writer
del aLocal
return tempList
# Gets vector layer by layername in canvas
def getVectorLayerByName(self, myName):
layermap = QgsMapLayerRegistry.instance().mapLayers()
for name, layer in layermap.iteritems():
if layer.type() == QgsMapLayer.VectorLayer \
and layer.name() == myName:
if layer.isValid():
return layer
else:
return None
# Return the field list of a vector layer
def getFieldList(self, vlayer):
vprovider = vlayer.dataProvider()
feat = QgsFeature()
allAttrs = vprovider.attributeIndexes()
vlayer.select(allAttrs)
myFields = vprovider.fields()
return myFields
# Gets the information required for calcualting size reduction factor
def getInfo(self, vlayer, provider, index):
allAttrs = provider.attributeIndexes()
vlayer.select(allAttrs)
featCount = provider.featureCount()
sRs = provider.crs()
feat = QgsFeature()
aLocal = []
cx = 0
cy = 0
dAreaTotal = 0.00
dTotalValue = 0.00
for feat in provider.getFeatures():
lfeat = Holder()
geom = QgsGeometry(feat.geometry())
dDistArea = QgsDistanceArea()
area = dDistArea.measure(geom)
lfeat.dArea = area # save area of this feature
lfeat.lFID = feat.id() # save id for this feature
dAreaTotal = dAreaTotal + area # save total area of all polygons
atMap = feat.attributes()
lfeat.dValue = atMap[index] # save 'area' value for this feature
# QMessageBox.information(self,
# "Generate Centroids",
# unicode(lfeat.dValue)
# )
dTotalValue += lfeat.dValue
centroid = geom.centroid()
# get centroid info
(cx, cy) = centroid.asPoint().x(), centroid.asPoint().y()
lfeat.ptCenter_x = cx # save centroid x for this feature
lfeat.ptCenter_y = cy # save centroid y for this feature
aLocal.append(lfeat)
# ratio of actual area to total 'area' value...
dFraction = dAreaTotal / dTotalValue
dSizeErrorTotal = 0
dSizeError = 0
for i in range(featCount):
lf = aLocal[i] # info for current feature
dPolygonValue = lf.dValue
dPolygonArea = lf.dArea
if (dPolygonArea < 0): # area should never be less than zero
dPolygonArea = 0
# this is our 'desired' area...
dDesired = dPolygonValue * dFraction
# calculate radius, a zero area is zero radius
dRadius = math.sqrt(dPolygonArea / math.pi)
lf.dRadius = dRadius
tmp = dDesired / math.pi
if tmp > 0:
# calculate area mass, don't think this should be negative
lf.dMass = math.sqrt(dDesired / math.pi) - dRadius
else:
lf.dMass = 0
# both radius and mass are being added to the feature list for
# later on...
# calculate size error...
dSizeError = \
max(dPolygonArea, dDesired)/min(dPolygonArea, dDesired)
#this is the total size error for all polygons
dSizeErrorTotal = dSizeErrorTotal + dSizeError
# average error
dMean = dSizeErrorTotal / featCount
# need to read up more on why this is done
dForceReductionFactor = 1 / (dMean + 1)
return (dMean, aLocal, dForceReductionFactor, dAreaTotal, dTotalValue)
# Actually changes the x,y of each point
def TransformGeometry(self,
aLocal,
dForceReductionFactor,
geom,
featCount):
# QMessageBox.information(self,
# "Generate Centroids",
# "is it getting here?")
multi_geom = QgsGeometry()
temp_lines = []
temp_polys = []
temp_geom = []
if geom.isMultipart():
multi_geom = geom.asMultiPolygon() # multi_geom is a multipolygon
for g in multi_geom: # i is a polygon
for k in g: # k is a line
for j in k: # j is a point
x = x0 = j.x()
y = y0 = j.y()
# Compute the influence of all shapes on this point
for i in range(featCount):
lf = aLocal[i]
cx = lf.ptCenter_x
cy = lf.ptCenter_y
# Pythagorean distance
distance = math.sqrt((x0 - cx) ** 2 +
(y0 - cy) ** 2
)
if (distance > lf.dRadius):
# Calculate the force on verteces far away
# from the centroid of this feature
Fij = lf.dMass * lf.dRadius / distance
else:
# Calculate the force on verteces far away
# from the centroid of this feature
xF = distance / lf.dRadius
Fij = lf.dMass * (xF ** 2) * (4 - (3 * xF))
Fij = Fij * dForceReductionFactor / distance
x = (x0 - cx) * Fij + x
y = (y0 - cy) * Fij + y
temp_lines.append(QgsPoint(x, y))
temp_polys.append(temp_lines)
temp_lines = []
temp_geom.append(temp_polys)
temp_polys = []
newGeom = QgsGeometry.fromMultiPolygon(temp_geom)
temp_geom = []
else:
multi_geom = geom.asPolygon()
for k in multi_geom: # k is a line
for j in k: # j is a point
x = x0 = j.x()
y = y0 = j.y()
# Compute the influence of all shapes on this point
for i in range(featCount):
lf = aLocal[i]
cx = lf.ptCenter_x
cy = lf.ptCenter_y
# Pythagorean distance
distance = math.sqrt((x0 - cx) ** 2 +
(y0 - cy) ** 2
)
if (distance > lf.dRadius):
# Calculate the force on verteces far away from
# the centroid of this feature
Fij = lf.dMass * lf.dRadius / distance
else:
# Calculate the force on verteces far away from
# the centroid of this feature
xF = distance / lf.dRadius
Fij = lf.dMass * (xF ** 2) * (4 - (3 * xF))
Fij = Fij * dForceReductionFactor / distance
# print " " + unicode(i) + " " + \
# unicode(distance) + " " + unicode(Fij)
x = (x0 - cx) * Fij + x
y = (y0 - cy) * Fij + y
temp_lines.append(QgsPoint(x, y))
temp_polys.append(temp_lines)
temp_lines = []
newGeom = QgsGeometry.fromPolygon(temp_polys)
temp_polys = []
# End: Loop through all the points
return newGeom
# Feature stores various pre-calculated values about each feature
class Holder(object):
count = 0
def __init__(self):
Holder.count = Holder.count + 1
self.lFID = -1
self.lGElemPos = -1
self.ptCenter_x = -1
self.ptCenter_y = -1
self.dNew_area = -1
self.dFactor = -1
self.sName = ""
self.dValue = -1
self.dArea = -1
self.dMass = -1
self.dRadius = -1
self.dVertices = -1