Last active
August 8, 2019 10:48
-
-
Save jagedn/1f181eddb671af109dffe12080ea0fbb to your computer and use it in GitHub Desktop.
Groovy Bikey example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab(group='com.jerolba', module='bikey', version='0.9.0') | |
import com.jerolba.bikey.* | |
bikey = new TableBikeyMap() | |
[ | |
'https://datos.madrid.es/egob/catalogo/300201-2-calle30-accidentes-historico.csv', //2016 | |
'https://datos.madrid.es/egob/catalogo/300201-0-calle30-accidentes-historico.csv', //2017 | |
'https://datos.madrid.es/egob/catalogo/300201-4-calle30-accidentes-historico.csv' //2018 | |
].each{ url -> | |
int count=0 | |
url.toURL().openStream().withReader('ISO-8859-1'){ r-> | |
r.eachLine{ line -> | |
count++ | |
if( count==1 ) | |
return | |
String[] fields = line.split(';',-1) | |
Date date = Date.parse('dd/MM/yyyy',fields[0]) | |
int weekday = date[Calendar.DAY_OF_WEEK] | |
String[] time = fields[1].replace('.',':').split(':') | |
int hour = ( (( (time[0] as int) * 60 ) + (time[1] as int )) /60) as int | |
if( !bikey.containsKey( weekday, hour) ) | |
bikey.put weekday, hour, [ ] | |
bikey.get( weekday, hour).add fields | |
} | |
} | |
println "$url loaded $count" | |
} | |
max = bikey.sort{ -1*it.value.size() }.first() | |
println "dia con más accidentes $max.row $max.column con ${max.value.size()} incidencias" | |
totalHL = bikey.entrySet().sum{ it.value.findAll{it[16].isNumber()}.sum{ it[16] as int } ?: 0 } | |
totalHG = bikey.entrySet().sum{ it.value.findAll{it[17].isNumber()}.sum{ it[17] as int } ?: 0 } | |
totalVM = bikey.entrySet().sum{ it.value.findAll{it[18].isNumber()}.sum{ it[18] as int } ?: 0 } | |
println "total heridos leves $totalHL" | |
println "total heridos graves $totalHG" | |
println "total victimas mortales $totalVM" | |
[ | |
"Peor dia heridos leves":16, | |
"Peor dia heridos graves":17, | |
"Peor dia muertos":18 | |
].each{ f-> | |
item = bikey.sort{ it.value.collect{ it[f.value].isNumber() ? it[f.value] as int : 0}.max() }.last().value.findAll{it[f.value].isNumber()}.sort{it[f.value] as int}.last() | |
println "${f.key} ${item[0]} a las ${item[1]} con ${item[f.value]}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ejemplo sencillo para parsear varios ficheros csv cuyos primeros campos son fecha y hora y contabilizar los registros por dia de la semana y hora
En concreto parseamos las incidencias de la M30 de Madrid 2016-2018