From c608674a46a2a99e586b47adf8675dd16231f356 Mon Sep 17 00:00:00 2001 From: Neszt Tibor Date: Wed, 6 May 2020 17:37:10 +0200 Subject: [PATCH] csv2json implemented --- csv2json.pl | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ update.sh.sample | 7 ++++++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100755 csv2json.pl diff --git a/csv2json.pl b/csv2json.pl new file mode 100755 index 0000000..d8f21bf --- /dev/null +++ b/csv2json.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use JSON::XS; + +sub main { + my $csvfilename = shift // die "Must give csv filename!"; + + $csvfilename =~ s/.csv$//; + + my $stat = {}; + + while (<>) { + /^(.*?),(.*?),(.*?),(.*?)$/ or die "Invalid csv line! [$_]"; + my $zip = $1; + my $city = $2; + my $street = $3; + my $housenumber = $4; + + next if !$zip; + next if !$city; + + $stat->{cities}->{$city}->{$zip} = 1; + $stat->{zips}->{$zip}->{$city} = 1; + } + + foreach my $city ( keys %{$stat->{cities}} ) { + $stat->{cities}->{$city} = [keys %{$stat->{cities}->{$city}}]; + } + + foreach my $zip ( keys %{$stat->{zips}} ) { + $stat->{zips}->{$zip} = [keys %{$stat->{zips}->{$zip}}]; + } + + my $ff; + + open($ff, ">$csvfilename-cities-with-zips.json"); + print $ff JSON::XS->new->canonical()->pretty()->encode($stat->{cities}); + close($ff); + + open($ff, ">$csvfilename-zips-with-cities.json"); + print $ff JSON::XS->new->canonical()->pretty()->encode($stat->{zips}); + close($ff); + + return 0; +} + +exit main(@ARGV); diff --git a/update.sh.sample b/update.sh.sample index ee62800..451d57a 100755 --- a/update.sh.sample +++ b/update.sh.sample @@ -25,6 +25,11 @@ osmconvert hungary-latest-filtered.osm --all-to-nodes --csv="addr:postcode addr: echo -n "Osmconvert finished " date -git add hungary-latest-filtered.csv +./csv2json.pl hungary-latest-filtered.csv + +echo -n "Generating json files finished " +date + +git add hungary-latest-filtered.csv hungary-latest-filtered-* GIT_AUTHOR_EMAIL='example@example.com' GIT_COMMITTER_EMAIL='example@example.com' git commit -m 'hungary-latest-filtered.csv daily update' git push -- 2.1.4