inegi-geo 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +101 -7
- data/inegi-geo.gemspec +1 -1
- data/lib/inegi/geo/transformer.rb +13 -8
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3386d962c2a0810d6899facc342e594fd2c1ff2e62c5dada4ea8f3c9e9eeb4b7
|
4
|
+
data.tar.gz: 437bd3a648cb88623cce3a7b25c7f153773ef504de854f163d40107a6ca2f8a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f71a53284746a4140a8ad6a58cba9fd57bb85f85bf5a008932af71a82d1c66b23706169159f3d890756f34281e28d4c55113ec229cf6174d9052a952d4de2f9
|
7
|
+
data.tar.gz: 4ddc344749385d437892c7be9aca860bbef026a0832601fca510278d5c73f391618f4fe0b117147565928a8495ee86459dadff0b3198e47fc02d842acdc42a92
|
data/README.md
CHANGED
@@ -48,10 +48,58 @@ rake inegi:geo:download inegi:geo:transform
|
|
48
48
|
# CONVERTED: states.dbf -> states.csv
|
49
49
|
```
|
50
50
|
|
51
|
+
Delete the `.zip` and `.dbf` or put them on your `.gitignore` if you want
|
52
|
+
to save them locally.
|
53
|
+
|
54
|
+
```bash
|
55
|
+
rm -f datasets/*[^.csv]
|
56
|
+
```
|
57
|
+
|
51
58
|
After editing the CSV to select the columns that you want, you can use
|
52
59
|
[activerecord-import][activerecord-import] to insert the data in the CSVs into
|
53
60
|
your database.
|
54
61
|
|
62
|
+
```bash
|
63
|
+
# head -n 5 states.csv
|
64
|
+
|
65
|
+
"code","name"
|
66
|
+
"01","Aguascalientes"
|
67
|
+
"02","Baja California"
|
68
|
+
"03","Baja California Sur"
|
69
|
+
"04","Campeche"
|
70
|
+
```
|
71
|
+
|
72
|
+
```bash
|
73
|
+
# head -n 5 municipalities.csv
|
74
|
+
|
75
|
+
"state_code","code","name"
|
76
|
+
"01","001","Aguascalientes"
|
77
|
+
"01","002","Asientos"
|
78
|
+
"01","003","Calvillo"
|
79
|
+
"01","004","Cosío"
|
80
|
+
```
|
81
|
+
|
82
|
+
```bash
|
83
|
+
# head -n 5 localities.csv
|
84
|
+
|
85
|
+
"state_code","municipality_code","code","name"
|
86
|
+
"01","001","0001","Aguascalientes"
|
87
|
+
"01","001","0094","Granja Adelita"
|
88
|
+
"01","001","0096","Agua Azul"
|
89
|
+
"01","001","0100","Rancho Alegre"
|
90
|
+
```
|
91
|
+
|
92
|
+
The size of the CSVs:
|
93
|
+
|
94
|
+
```txt
|
95
|
+
Size Name
|
96
|
+
---- ----
|
97
|
+
10M localities.csv
|
98
|
+
68k municipalities.csv
|
99
|
+
612 states.csv
|
100
|
+
```
|
101
|
+
|
102
|
+
|
55
103
|
_:warning: Review and understand the code before copying and pasting it._
|
56
104
|
|
57
105
|
In this example, I chose to store `State`, `Municipality`, and `Locality`.
|
@@ -77,18 +125,64 @@ rails generate model locality code:string \
|
|
77
125
|
require 'activerecord-import/base'
|
78
126
|
require 'activerecord-import/active_record/adapters/postgresql_adapter'
|
79
127
|
|
80
|
-
def
|
81
|
-
|
82
|
-
|
83
|
-
|
128
|
+
def import_from_csv(dataset)
|
129
|
+
file = Rails.root.join('datasets', "#{dataset}.csv")
|
130
|
+
klass = dataset.classify.constantize
|
131
|
+
|
132
|
+
return if klass.count > 0
|
133
|
+
|
134
|
+
print "\rImporting #{dataset.pluralize}..."
|
135
|
+
|
136
|
+
klass.transaction do
|
137
|
+
rows = CSV.read(file)
|
84
138
|
headers = rows.delete_at(0)
|
85
|
-
|
86
|
-
end
|
139
|
+
klass.import(headers, rows, validate: false)
|
140
|
+
end
|
141
|
+
|
142
|
+
puts "\rImported #{dataset.pluralize}: #{klass.count}"
|
143
|
+
end
|
87
144
|
|
88
|
-
|
145
|
+
def update_references
|
146
|
+
return unless Municipality.where(state_id: nil).exists? ||
|
147
|
+
Locality.where(municipality_id: nil).exists?
|
148
|
+
|
149
|
+
print "\rUpdating references..."
|
150
|
+
|
151
|
+
states = <<~SQL
|
152
|
+
UPDATE municipalities
|
153
|
+
SET state_id = states.id
|
154
|
+
FROM states
|
155
|
+
WHERE municipalities.state_code = states.code;
|
156
|
+
|
157
|
+
UPDATE localities
|
158
|
+
SET state_id = states.id
|
159
|
+
FROM states
|
160
|
+
WHERE localities.state_code = states.code;
|
161
|
+
SQL
|
162
|
+
|
163
|
+
municipalities = <<~SQL
|
164
|
+
UPDATE localities
|
165
|
+
SET municipality_id = municipalities.id
|
166
|
+
FROM municipalities
|
167
|
+
WHERE localities.state_code = municipalities.state_code
|
168
|
+
AND localities.municipality_code = municipalities.code;
|
169
|
+
SQL
|
170
|
+
|
171
|
+
ActiveRecord::Base.connection.execute(states)
|
172
|
+
ActiveRecord::Base.connection.execute(municipalities)
|
173
|
+
|
174
|
+
puts "\rUpdating references: DONE"
|
89
175
|
end
|
176
|
+
|
177
|
+
import_from_csv 'states'
|
178
|
+
import_from_csv 'municipalities'
|
179
|
+
import_from_csv 'localities'
|
180
|
+
update_references
|
90
181
|
```
|
91
182
|
|
183
|
+
The code above imports the data from the CSV to the models, and update
|
184
|
+
the references. The time to run this (in my computer) is below **1 second**.
|
185
|
+
|
92
186
|
## References
|
93
187
|
- [guivaloz/INEGI](https://github.com/guivaloz/INEGI)
|
94
188
|
- [evilmartians/fias](https://github.com/evilmartians/fias)
|
data/inegi-geo.gemspec
CHANGED
@@ -2,7 +2,7 @@ $:.push File.expand_path('../lib', __FILE__)
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = 'inegi-geo'
|
5
|
-
s.version = '0.0.
|
5
|
+
s.version = '0.0.3'
|
6
6
|
s.authors = ['mroutis']
|
7
7
|
s.email = ['outis@civica.digital']
|
8
8
|
s.summary = 'Imports INEGI geospatial database into a readable CSV'
|
@@ -53,20 +53,25 @@ class Inegi::Geo::Transformer
|
|
53
53
|
|
54
54
|
def translate_header(header)
|
55
55
|
dictionary = {
|
56
|
-
|
57
|
-
|
56
|
+
AMBITO: 'area_type',
|
57
|
+
ALTITUD: 'altitude',
|
58
|
+
LATITUD: 'latitude',
|
59
|
+
LONGITUD: 'longitude',
|
58
60
|
CVE_CAB: 'head_code',
|
59
|
-
CVE_LOC: 'locality_code',
|
60
61
|
CVE_CAP: 'capital_code',
|
61
|
-
|
62
|
+
CVE_CARTA: 'map_code',
|
63
|
+
CVE_ENT: 'state_code',
|
64
|
+
CVE_LOC: 'locality_code',
|
65
|
+
CVE_MUN: 'municipality_code',
|
62
66
|
NOM_ABR: 'state_abbreviation',
|
63
|
-
NOM_CAP: 'capital_name',
|
64
|
-
NOM_MUN: 'municipality_name',
|
65
67
|
NOM_CAB: 'head_name',
|
68
|
+
NOM_CAP: 'capital_name',
|
69
|
+
NOM_ENT: 'state_name',
|
66
70
|
NOM_LOC: 'locality_name',
|
67
|
-
|
68
|
-
PMAS: 'masculine_population',
|
71
|
+
NOM_MUN: 'municipality_name',
|
69
72
|
PFEM: 'femenine_population',
|
73
|
+
PMAS: 'masculine_population',
|
74
|
+
PTOT: 'total_population',
|
70
75
|
VTOT: 'inhabited_residences',
|
71
76
|
}
|
72
77
|
|