Skip to content

Commit

Permalink
Add download and process scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
inigoflores committed Nov 24, 2016
1 parent e17ce08 commit 2d57f4b
Show file tree
Hide file tree
Showing 6,302 changed files with 503,425 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# User specific & automatically generated files #
#################################################
.idea/

# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
Icon?
ehthumbs.db
Thumbs.db
*~
Binary file added archive/42602-ALAVA.zip
Binary file not shown.
Binary file added archive/42603-GUIPUZCOA.zip
Binary file not shown.
Binary file added archive/42604-VIZCAYA.zip
Binary file not shown.
Binary file added archive/42605-NAVARRA.zip
Binary file not shown.
Binary file added archive/9082-GUADALAJARA.zip
Binary file not shown.
Binary file added archive/9083-TOLEDO.zip
Binary file not shown.
Binary file added archive/9084-ALMERIA.zip
Binary file not shown.
Binary file added archive/9085-CANTABRIA.zip
Binary file not shown.
Binary file added archive/9086-LUGO.zip
Binary file not shown.
Binary file added archive/9087-GRANADA.zip
Binary file not shown.
Binary file added archive/9088-CASTELLON.zip
Binary file not shown.
Binary file added archive/9089-BURGOS.zip
Binary file not shown.
Binary file added archive/9090-CACERES.zip
Binary file not shown.
Binary file added archive/9091-CORDOBA.zip
Binary file not shown.
Binary file added archive/9092-TARRAGONA.zip
Binary file not shown.
Binary file added archive/9093-MURCIA.zip
Binary file not shown.
Binary file added archive/9094-MELILLA.zip
Binary file not shown.
Binary file added archive/9095-BARCELONA.zip
Binary file not shown.
Binary file added archive/9096-OURENSE.zip
Binary file not shown.
Binary file added archive/9097-SEVILLA.zip
Binary file not shown.
Binary file added archive/9098-SALAMANCA.zip
Binary file not shown.
Binary file added archive/9099-CEUTA.zip
Binary file not shown.
Binary file added archive/9100-CADIZ.zip
Binary file not shown.
Binary file added archive/9101-HUESCA.zip
Binary file not shown.
Binary file added archive/9102-CUENCA.zip
Binary file not shown.
Binary file added archive/9103-PALENCIA.zip
Binary file not shown.
Binary file added archive/9104-LA_RIOJA.zip
Binary file not shown.
Binary file added archive/9105-TERUEL.zip
Binary file not shown.
Binary file added archive/9106-ALICANTE.zip
Binary file not shown.
Binary file added archive/9107-ALBACETE.zip
Binary file not shown.
Binary file added archive/9108-MADRID.zip
Binary file not shown.
Binary file added archive/9109-A_CORUNA.zip
Binary file not shown.
Binary file added archive/9110-AVILA.zip
Binary file not shown.
Binary file added archive/9111-BADAJOZ.zip
Binary file not shown.
Binary file added archive/9112-HUELVA.zip
Binary file not shown.
Binary file added archive/9113-LAS_PALMAS.zip
Binary file not shown.
Binary file added archive/9114-ZARAGOZA.zip
Binary file not shown.
Binary file added archive/9115-GIRONA.zip
Binary file not shown.
Binary file added archive/9116-TENERIFE.zip
Binary file not shown.
Binary file added archive/9117-CIUDAD_REAL.zip
Binary file not shown.
Binary file added archive/9118-VALLADOLID.zip
Binary file not shown.
Binary file added archive/9119-SEGOVIA.zip
Binary file not shown.
Binary file added archive/9120-LLEIDA.zip
Binary file not shown.
Binary file added archive/9121-BALEARES.zip
Binary file not shown.
Binary file added archive/9122-MALAGA.zip
Binary file not shown.
Binary file added archive/9123-ZAMORA.zip
Binary file not shown.
Binary file added archive/9124-SORIA.zip
Binary file not shown.
Binary file added archive/9125-ASTURIAS.zip
Binary file not shown.
Binary file added archive/9126-LEON.zip
Binary file not shown.
Binary file added archive/9127-PONTEVEDRA.zip
Binary file not shown.
Binary file added archive/9128-JAEN.zip
Binary file not shown.
Binary file added archive/9129-VALENCIA.zip
Binary file not shown.
33 changes: 33 additions & 0 deletions datapackage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "ds-codigos-postales",
"title": "Dataset de Códigos Postales",
"description": "Geometrías de todos los códigos postales de España, incluyendo los códigos INE de los municipios asociados",
"sources": [
{
"name": "CNIG - Centro Nacional de Información Geográfica",
"web": "http://centrodedescargas.cnig.es/CentroDescargas/"
}
],
"contributors": [
{
"name": "Javier Escribano"
},
{
"name": "Iñigo Flores"
}
],
"resources": [
{
"name": "codigos_postales",
"path": "data/codigos_postales.geojson",
"format": "geojson",
"mediatype": "application/json",
"bytes": 159860385
}
],
"homepage": "",
"version": "0.1.3",
"license": "ODC-PDDL-1.0",
"repository": "git://github.com/codeforspain/ds-codigos-postales.git",
"last_updated": "2016-11-24"
}
24 changes: 24 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Script

Descarga y procesa shapefiles de Cartociudad y extrae las geometrías de los códigos postales a un único archivo geojson.

## Modo de Uso

### Descarga

$ node download.js

### Procesado

$ node process.js


## Requisitos

* Node.js
* GDAL/OGR

La última versión de GDAL/OGR en UBUNTU se puede obtener de ppa:ubuntugis/ppa

$ sudo add-apt-repository ppa:ubuntugis/ppa && sudo apt-get update
$ sudo apt-get install gdal-bin
71 changes: 71 additions & 0 deletions scripts/download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* @fileOverview Crea shapefiles de codigos postales en formato ZIP a partir de los archivos que proporciona Cartociudad
* @version 0.0.1
*/

var AdmZip = require('adm-zip');
var fs = require('fs');
var archiver = require('archiver');
var _ = require('underscore');
var async = require('async');
var request = require('request');
var glob = require("glob")
var ogr2ogr = require('ogr2ogr');

// Constantes
var BASEURL = 'http://centrodedescargas.cnig.es/CentroDescargas/downloadFile.do?seq=';
var SOURCES_FOLDER = '../archive/';
var fileList = _.range(9082, 9130).concat(_.range(42602, 42606));
var extensions = ['dbf','prj','shp','shx','cpg'];


// Elimina de la lista los archivos ya generados
glob(SOURCES_FOLDER + "*.zip", {sync: true}).forEach(function(element){
id = parseInt(element.split(SOURCES_FOLDER)[1].split("-")[0]);
if ((index = fileList.indexOf(id)) > -1){
fileList.splice(index, 1);
}
})

// Descarga y procesa de forma concurrente hasta 5 archivos
async.mapLimit(fileList, 5, function(item, callback){
console.log("Descargando " + BASEURL + item);
request({url:BASEURL + item, encoding:null}, function (error, response, body) {
saveProvince(error, body, item);
callback(error,body);
});
}, function(err, results){
console.log('Terminado!');
});


/**
* Procesa y graba a disco el shapefile en zip con los codigos postales
* @param error
* @param body
* @param item
*/
function saveProvince(error,body,item){

var archive = archiver('zip');
var sourceZip = new AdmZip(body);
var zipEntries = sourceZip.getEntries(); // an array of ZipEntry records

zipEntries.some(function(zipEntry) {
if (zipEntry.isDirectory) {
provincia = zipEntry.entryName.split("CARTOCIUDAD_CALLEJERO_")[1].slice(0,-1);
return true;
}
});

var output = fs.createWriteStream(SOURCES_FOLDER + item + "-" + provincia + ".zip");
console.log("Generado " + provincia + ".zip");
archive.pipe(output);

extensions.forEach(function(ext){
entry = sourceZip.getEntry('CODIGO_POSTAL.' + ext);
archive.append(entry.getData(), { name: provincia + "." + ext });
});

archive.finalize();
}
1 change: 1 addition & 0 deletions scripts/node_modules/.bin/semver

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions scripts/node_modules/adm-zip/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2d57f4b

Please sign in to comment.