-
Notifications
You must be signed in to change notification settings - Fork 30
/
ibge.py
167 lines (136 loc) · 4.7 KB
/
ibge.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
# -*- coding: utf-8 -*-
from django.contrib.gis.gdal import DataSource, SpatialReference, CoordTransform, OGRGeometry, OGRGeomType
from django.contrib.gis.gdal.geometries import Polygon
from django.template.defaultfilters import slugify
from municipios.models import UF, Municipio, MUNICIPIOS_GEO, SRID
import sys
KEEP_LOWCASE = ('de', 'da', 'das', 'do', 'dos',)
UF_SIGLAS = (
(11, 'RO'),
(12, 'AC'),
(13, 'AM'),
(14, 'RR'),
(15, 'PA'),
(16, 'AP'),
(17, 'TO'),
(21, 'MA'),
(22, 'PI'),
(23, 'CE'),
(24, 'RN'),
(25, 'PB'),
(26, 'PE'),
(27, 'AL'),
(28, 'SE'),
(29, 'BA'),
(31, 'MG'),
(32, 'ES'),
(33, 'RJ'),
(35, 'SP'),
(41, 'PR'),
(42, 'SC'),
(43, 'RS'),
(50, 'MS'),
(51, 'MT'),
(52, 'GO'),
(53, 'DF')
)
UF_SIGLAS_DICT = dict([(str(c), u) for c, u in UF_SIGLAS])
def capitalize_name(s):
res = []
for w in s.lower().split(' '):
if w in KEEP_LOWCASE:
res.append(w)
else:
res.append(w.capitalize())
return u" ".join(res)
def convert_shapefile(shapefilename, srid=4674):
"""
shapefilename: considera nomenclatura de shapefile do IBGE para determinar se é UF
ou Municípios.
ex. 55UF2500GC_SIR.shp para UF e 55MU2500GC_SIR.shp para Municípios
srid: 4674 (Projeção SIRGAS 2000)
"""
# /home/nando/Desktop/IBGE/2010/55MU2500GC_SIR.shp
ds = DataSource(shapefilename)
is_uf = shapefilename.upper().find('UF') != -1
transform_coord = None
if srid != SRID:
transform_coord = CoordTransform(SpatialReference(srid), SpatialReference(SRID))
if is_uf:
model = UF
else:
model = Municipio
ct = 0
for f in ds[0]:
# 3D para 2D se necessário
if f.geom.coord_dim != 2:
f.geom.coord_dim = 2
# converte para MultiPolygon se necessário
if isinstance(f.geom, Polygon):
g = OGRGeometry(OGRGeomType('MultiPolygon'))
g.add(f.geom)
else:
g = f.geom
# transforma coordenadas se necessário
if transform_coord:
g.transform(transform_coord)
# força 2D
g.coord_dim = 2
kwargs = {}
if is_uf:
kwargs['nome'] = capitalize_name(unicode(f.get(CAMPO_NOME_UF),'latin1'))
kwargs['geom'] = g.ewkt
kwargs['id_ibge'] = f.get(CAMPO_GEOCODIGO_UF)
kwargs['regiao'] = capitalize_name(unicode(f.get(CAMPO_REGIAO_UF), 'latin1'))
kwargs['uf'] = UF_SIGLAS_DICT.get(kwargs['id_ibge'])
else:
kwargs['nome'] = capitalize_name(unicode(f.get(CAMPO_NOME_MU),'latin1'))
kwargs['geom'] = g.ewkt
kwargs['id_ibge'] = f.get(CAMPO_GEOCODIGO_MU)
kwargs['uf'] = UF.objects.get(pk=f.get(CAMPO_GEOCODIGO_MU)[:2])
kwargs['uf_sigla'] = kwargs['uf'].uf
kwargs['nome_abreviado'] = slugify(kwargs['nome'])
# tenta corrigir nomes duplicados, são em torno de 242 nomes repetidos
# adicionando a sigla do estado no final
if Municipio.objects.filter(nome_abreviado=kwargs['nome_abreviado']).count() > 0:
kwargs['nome_abreviado'] = u'%s-%s' % (kwargs['nome_abreviado'], kwargs['uf_sigla'].lower())
instance = model(**kwargs)
instance.save()
ct += 1
print ct, (is_uf and "Unidades Federativas criadas" or "Municipios criados")
def update_sedes_municipais(shapefilename, srid=4618):
# dados de um shapefile de 2001
ds = DataSource(shapefilename)
transform_coord = None
if srid != SRID:
transform_coord = CoordTransform(SpatialReference(srid), SpatialReference(SRID))
ct = 0
cta = 0
for f in ds[0]:
ct +=1
cod = f.get('CODIGO')
muns = Municipio.objects.extra(where=['CAST(id_ibge AS VARCHAR) ILIKE %s'], params=['%s%%' % cod])
if muns:
if len(muns) > 1:
print "Mais de 1 municipio para", cod
for m in muns:
print m.id_ibge, m
else:
g = f.geom
g.srs = SpatialReference(srid)
g.srid = srid
if transform_coord:
g.transform(transform_coord)
print g.ewkt
muns[0].sede = g.ewkt
muns[0].save()
cta += 1
else:
print cod, "nao econtrado!"
print "Atualizados", cta, "sedes"
print "Total de", ct, "registros no shapefile"
if __name__ == "__main__":
if len(sys.argv) == 1:
print "Informe o arquivo shapefile!"
else:
convert_shapefile(sys.argv[1])