diff --git a/scripts/README.md b/scripts/README.md old mode 100644 new mode 100755 index 18c55f8..27cf52b --- a/scripts/README.md +++ b/scripts/README.md @@ -9,12 +9,13 @@ Si no los encuentra, los descarga. $ php script.php [COMMAND SUBCOMMAND]] [OPTIONS] -Si se invoca sin subcomandos o argumentos, executa: +Si se invoca sin subcomandos o argumentos, ejecuta: 1. download all 2. process all - 3. update - 4. convert-to-json + 3. merge + 4. update + ### Opciones @@ -28,7 +29,6 @@ Si se invoca sin subcomandos o argumentos, executa: year YEAR MONTH Descarga únicamente el periodo YEAR MONTH (Month es 1 ó 7) all Descarga todos los años - OPTIONS --force, -f Fuerza la descarga de los archivos fuente, aunque existan @@ -45,11 +45,18 @@ Si se invoca sin subcomandos o argumentos, executa: all Procesa todo + merge Hace el merge con ds-organizacion-administrativa para obtener el + nombre del municipio, que no viene en los archivos fuente. + + convert-to-json Convierte todos los archivos .csv almacenados en /data a .json. + update Actualiza el archivo datapackage.json + OPTIONS + --json, -j Habilita la creación de recursos para los archivos JSON en datapackage.json diff --git a/scripts/lib/Config.php b/scripts/lib/Config.php index c927ea7..c43ea99 100755 --- a/scripts/lib/Config.php +++ b/scripts/lib/Config.php @@ -84,17 +84,27 @@ class Config "description" => "Código INE del municipio", "pattern" => "[0-9]{5}" ], + [ + "name" => "municipio_nombre", + "type" => "string", + "description" => "Nombre del municipio", + ], + [ + "name" => "codigo_unidad_poblacional", + "type" => "number", + "description" => "Codigo unidad poblacional", + "pattern" => "[0-9]{7}" + ], [ "name" => "entidad_singular_nombre", "type" => "string", "description" => "Nombre entidad singular", ], [ - "name" => "municipio_nombre", + "name" => "nucleo_nombre", "type" => "string", - "description" => "Nombre del municipio", + "description" => "Nombre nucleo/diseminado", ], - ] ] ], @@ -139,11 +149,22 @@ class Config "description" => "Código INE del municipio", "pattern" => "[0-9]{5}" ], + [ + "name" => "codigo_unidad_poblacional", + "type" => "number", + "description" => "Codigo unidad poblacional", + "pattern" => "[0-9]{7}" + ], [ "name" => "entidad_singular_nombre", "type" => "string", "description" => "Nombre entidad singular", ], + [ + "name" => "nucleo_nombre", + "type" => "string", + "description" => "Nombre nucleo/diseminado", + ], ] ] @@ -199,11 +220,22 @@ class Config "description" => "Código INE del municipio", "pattern" => "[0-9]{5}" ], + [ + "name" => "codigo_unidad_poblacional", + "type" => "number", + "description" => "Codigo unidad poblacional", + "pattern" => "[0-9]{7}" + ], [ "name" => "entidad_singular_nombre", "type" => "string", "description" => "Nombre entidad singular", ], + [ + "name" => "nucleo_nombre", + "type" => "string", + "description" => "Nombre nucleo/diseminado", + ], [ "name" => "year", "type" => "number", diff --git a/scripts/lib/MergeCommand.php b/scripts/lib/MergeCommand.php index b215877..15484d8 100755 --- a/scripts/lib/MergeCommand.php +++ b/scripts/lib/MergeCommand.php @@ -13,7 +13,7 @@ class MergeCommand extends ConsoleKit\Command public function execute(array $args, array $options = array()) { - $box = new ConsoleKit\Widgets\Box($this->getConsole(), 'Haciendo merge con ds-organizacion-administrativagit'); + $box = new ConsoleKit\Widgets\Box($this->getConsole(), 'Haciendo merge con ds-organizacion-administrativa'); $box->write();$this->getConsole()->writeln(""); shell_exec(' @@ -29,10 +29,10 @@ class MergeCommand extends ConsoleKit\Command "); shell_exec(' - curl https://raw.githubusercontent.com/codeforspain/ds-organizacion-administrativa/master/data/municipios.csv \ - | csvcut -c \'municipio_id,nombre\' \ + curl https://raw.githubusercontent.com/codeforspain/ds-organizacion-administrativa/master/data/municipios.csv \ + | csvcut -c "municipio_id,nombre" \ | csvjoin --snifflimit 0 -I -c "municipio_id" ../data/codigos_postales_municipiosid_entidades.csv - \ - | csvcut -c "codigo_postal,entidad_singular_nombre,municipio_id,nombre" \ + | csvcut -c "codigo_postal,municipio_id,nombre,codigo_unidad_poblacional,entidad_singular_nombre,nucleo_nombre" \ > ../data/codigos_postales_municipios_entidades.csv '); diff --git a/scripts/lib/ProcessCommand.php b/scripts/lib/ProcessCommand.php index e4988a3..292d65b 100755 --- a/scripts/lib/ProcessCommand.php +++ b/scripts/lib/ProcessCommand.php @@ -167,18 +167,27 @@ class ProcessCommand extends ConsoleKit\Command } while (($line = fgets($zippedSource)) !== false) { + $line = iconv("windows-1252", "UTF-8", $line); + $codigo_postal = substr($line,42,5); $municipio_id = substr($line,0,5); - $nombre_entidad_singular = $this->titleCase( - (iconv("windows-1252", "UTF-8", trim(substr($line,110,25)))) - ); + + $codigo_unidad_poblacional = substr($line,13,7); + //$nombre_entidad_colectiva = $this->titleCase(trim(substr($line,85,25))); + $nombre_entidad_singular = $this->titleCase(trim(substr($line,110,25))); + $nombre_nucleo = $this->titleCase(trim(substr($line,135,25))); if ($includeYear) { - $items[$codigo_postal.$municipio_id.$year.$month] = compact('codigo_postal','municipio_id', 'year', 'month'); - $itemsEntidades[$codigo_postal.$municipio_id.$nombre_entidad_singular.$year.$month] = compact('codigo_postal','municipio_id','nombre_entidad_singular','year','month'); + $items[$codigo_postal.$municipio_id.$year.$month] = + compact('codigo_postal','municipio_id', 'year', 'month'); + $itemsEntidades[$codigo_postal.$municipio_id.$codigo_unidad_poblacional.$year.$month] = + compact('codigo_postal','municipio_id','codigo_unidad_poblacional', + 'nombre_entidad_singular','nombre_nucleo','year','month'); } else { $items[$codigo_postal.$municipio_id] = compact('codigo_postal','municipio_id'); - $itemsEntidades[$codigo_postal.$municipio_id.$nombre_entidad_singular] = compact('codigo_postal','municipio_id','nombre_entidad_singular'); + $itemsEntidades[$codigo_postal.$municipio_id.$codigo_unidad_poblacional] = + compact('codigo_postal','municipio_id','codigo_unidad_poblacional', + 'nombre_entidad_singular','nombre_nucleo'); } $i++; @@ -197,7 +206,21 @@ class ProcessCommand extends ConsoleKit\Command } - private function titleCase($string, $delimiters = array(" ", "-", "/"), $exceptions = array("de", "del", "la")) { + /** + * @param $string Cadena de texto a convertir + * @param array $delimiters Carácteres delimitadores + * @param array $exceptions Palabras a las que no se les cambia la capitalización + * @return string + */ + private function titleCase($string, $delimiters = array(), $exceptions = array()) { + + if (empty($delimiters)) { + $delimiters = array(" ", "-", "/",",","'"); + } + + if (empty($exceptions)) { + $exceptions = array("de", "del", "la","II",'III','IV','XIII','XXIII'); + } $string = mb_convert_case($string, MB_CASE_TITLE, "UTF-8"); @@ -207,9 +230,11 @@ class ProcessCommand extends ConsoleKit\Command foreach ($words as $wordnr => $word){ $wordLowerCase = strtolower($word); - if (in_array($wordLowerCase, $exceptions)){ - // check exceptions list for any words that should be in lower case + $wordUpperCase = strtoupper($word); + if (in_array($wordLowerCase, $exceptions)){ // check exceptions list for any words that should be in lower case $word = $wordLowerCase; + } else if (in_array($wordUpperCase, $exceptions)){ // check exceptions list for any words that should be in upper case + $word = $wordUpperCase; } elseif (!in_array($word, $exceptions) ){ // convert to uppercase (non-utf8 only) diff --git a/scripts/lib/UpdateCommand.php b/scripts/lib/UpdateCommand.php old mode 100644 new mode 100755 index 7586876..e65f477 --- a/scripts/lib/UpdateCommand.php +++ b/scripts/lib/UpdateCommand.php @@ -21,7 +21,7 @@ class UpdateCommand extends ConsoleKit\Command $box->write();$this->getConsole()->writeln(""); // Comprobamos si hay que omitir JSON - if (!isset($options['nojson']) && !isset($options['n'])){ + if (isset($options['json']) || isset($options['j'])){ foreach ( Config::$datapackage['resources'] as $resource){ $resource['format'] = 'json'; $parts = explode('.',$resource['path']); diff --git a/scripts/script.php b/scripts/script.php index 6523c54..d81323d 100755 --- a/scripts/script.php +++ b/scripts/script.php @@ -21,7 +21,7 @@ if (sizeof($argv)==1) { $console->run(['process']); //$console->run(['convert-to-json']); // No generamos los archivos json, alguno sobrepasa los 100 Mb (limite Github) $console->run(['merge']); - $console->run(['update','--nojson']); + $console->run(['update']); } else { $console->run(); }