inegi-geo 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5809f4c7ab83ec51d1f2f394bf646ad17b1924c3a501668fa790130e08c795db
4
- data.tar.gz: a4e4ff7f78fa14e656aef924f8b533eabedb865edbf6e8c402f1b4756ca498b1
3
+ metadata.gz: 3386d962c2a0810d6899facc342e594fd2c1ff2e62c5dada4ea8f3c9e9eeb4b7
4
+ data.tar.gz: 437bd3a648cb88623cce3a7b25c7f153773ef504de854f163d40107a6ca2f8a6
5
5
  SHA512:
6
- metadata.gz: b7ab14fffe1fcf0891037370fe7daa9c3a237ec3d6dd7fec44895bbd0c64f94dfbaa4e0a5fdc682dda887fcaa2be99e70244ab81c432859bd49f5737e12cd90b
7
- data.tar.gz: 28a8953792cd930983698611a12f0bd3b1fb458ed69be39a981771c9150049c84525f04a3c132b03fb5e21098c9eeda607437884f7e70d1e05accde5b4a5bdb5
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 import_states
81
- State.transaction do
82
- dataset = Rails.root.join('datasets', 'states.csv')
83
- rows = CSV.read(dataset)
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
- State.import(headers, rows, validate: false)
86
- end unless State.count > 0
139
+ klass.import(headers, rows, validate: false)
140
+ end
141
+
142
+ puts "\rImported #{dataset.pluralize}: #{klass.count}"
143
+ end
87
144
 
88
- puts "Imported states: #{State.count}"
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)
@@ -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.2'
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
- CVE_ENT: 'state_code',
57
- CVE_MUN: 'municipality_code',
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
- NOM_ENT: 'state_name',
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
- PTOT: 'total_population',
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inegi-geo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - mroutis