-
Notifications
You must be signed in to change notification settings - Fork 3
/
geobricks_join_layer_utils.py
132 lines (106 loc) · 4.99 KB
/
geobricks_join_layer_utils.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
import os
import re
import glob
from shutil import copyfile
from PyQt4.QtCore import *
from qgis.core import QgsRendererRangeV2LabelFormat, QgsField, QgsFeature, QgsStyleV2, QgsVectorLayer, QgsGraduatedSymbolRendererV2, QgsSymbolV2
def copy_layer(download_path, indicator_name):
layer_name = indicator_name
clean_layer_name = re.sub('\W+', '_', indicator_name)
output_file = os.path.join(download_path, clean_layer_name + ".shp")
input_base_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "resources")
# copy resource file to output
# resource_files = glob.glob(os.path.join(input_base_path, "ne_110m_admin_0_*"))
resource_files = glob.glob(os.path.join(input_base_path, "ne_110m_admin_0_*"))
for resource_file in resource_files:
base, extension = os.path.splitext(resource_file)
copyfile(resource_file, os.path.join(download_path, clean_layer_name + extension))
return output_file
def create_layer(layer, tmp_layer, data, year, index):
# TODO: remove hardcoded index
index += 5
tmp_data_provider = tmp_layer.dataProvider()
tmp_layer.startEditing()
tmp_feature = QgsFeature()
# Editing output_file
# add column year
layer_data_provider = layer.dataProvider().addAttributes([QgsField(str(year), QVariant.Double)])
layer.startEditing()
# TODO: add data check instead of the addedValue boolean?
addedValue = False
for feat in layer.getFeatures():
if feat['iso_a2'] is not None:
for d in data:
code = d['country']['id']
value = d['value']
if code == feat['iso_a2']:
if value:
# TODO: automatize the index 5 of feat['iso_a2']
layer.changeAttributeValue(feat.id(), index, float(value))
tmp_feature.setAttributes([float(value)])
# TODO add all togheter
tmp_data_provider.addFeatures([tmp_feature])
addedValue = True
break
# TODO: in teory if addedValue is not present should be removed the column year?
layer.commitChanges()
return addedValue
def create_layer_bk(download_path, tmp_layer, indicator, indicator_name, data, year):
tmp_data_provider = tmp_layer.dataProvider()
tmp_layer.startEditing()
tmp_feature = QgsFeature()
# get world bank data
# data = get_world_bank_data(indicator, year)
# getting layer_name
layer_name = indicator_name + " (" + year + ")"
clean_layer_name = re.sub('\W+', '_', indicator_name) + "_" + year
# creating output path
# output_base_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "output")
# if not os.path.exists(output_base_path):
# os.mkdir(output_base_path)
# retrieving input shp
# output_file = os.path.join(output_base_path, clean_layer_name + ".shp")
output_file = os.path.join(download_path, clean_layer_name + ".shp")
input_base_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "resources")
# copy resource file to output
# resource_files = glob.glob(os.path.join(input_base_path, "ne_110m_admin_0_*"))
resource_files = glob.glob(os.path.join(input_base_path, "ne_110m_admin_0_*"))
for resource_file in resource_files:
base, extension = os.path.splitext(resource_file)
copyfile(resource_file, os.path.join(download_path, clean_layer_name + extension))
# Editing output_file
layer = QgsVectorLayer(output_file, layer_name, "ogr")
layer.startEditing()
# TODO: add data check instead of the addedValue boolean?
addedValue = False
for feat in layer.getFeatures():
if feat['iso_a2'] is not None:
for d in data:
code = d['country']['id']
value = d['value']
if code == feat['iso_a2']:
if value:
# TODO: automatize the index 5 of feat['iso_a2']
layer.changeAttributeValue(feat.id(), 5, float(value))
tmp_feature.setAttributes([float(value)])
# TODO add all togheter
tmp_data_provider.addFeatures([tmp_feature])
addedValue = True
break
layer.commitChanges()
return layer, addedValue
def create_join_renderer(layer, field, classes, mode, color='Blues'):
symbol = QgsSymbolV2.defaultSymbol(layer.geometryType())
style = QgsStyleV2().defaultStyle()
colorRamp = style.colorRampRef(color)
renderer = QgsGraduatedSymbolRendererV2.createRenderer(layer, field, classes, mode, symbol, colorRamp)
label_format = create_join_label_format(2)
renderer.setLabelFormat(label_format)
return renderer
def create_join_label_format(precision):
format = QgsRendererRangeV2LabelFormat()
template="%1 - %2 metres"
format.setFormat(template)
format.setPrecision(precision)
format.setTrimTrailingZeroes(True)
return format