#!/usr/bin/perl #(3/3/2016): Convert BigQuery geo export from Visual GKG into GeoJSON for mapping in CartoDB... open(FILE, "./IN.csv"); while() { ($docurl, $imgurl, $geo) = split/,/, $_, 3; $geo=~s/\"//g; $imgurl=~s/[\"\']//g; $docurl=~s/[\"\']//g; $imgurl=~s/\\/\//g; #fix some odd images... foreach $rec (split//, $geo) { $name = ''; $latlong = ''; $lat = ''; $long = ''; ($name, $confidence, $id, $latlong) = split//, $rec; ($lat, $long) = split/,/, $latlong; $lat = sprintf("%0.6f", $lat); $long = sprintf("%0.6f", $long); $latlong = "$long, $lat"; $name=~s/[^A-Za-z\- ]//g; $name=~s/[\"\']//g; #if ($LOCS_CNT{$latlong} < 50) { $LOCS_HTML{$latlong} .= "Article
"; } #article textual links if ($LOCS_CNT{$latlong} < 50) { $LOCS_HTML{$latlong} .= " "; } #article thumbnail links if ($LOCS_IMG{$latlong} eq '') { $LOCS_IMG{$latlong} = $imgurl; } if ($LOCS_NAME{$latlong} eq '') { $LOCS_NAME{$latlong} = $name; } $LOCS_CNT{$latlong}++; } } close(FILE); open(OUT, ">OUT.geojson"); print OUT "{ \"type\": \"FeatureCollection\", \"features\": [\n\n"; foreach $latlong (keys %LOCS_HTML) { if ($LOCS_NAME{$latlong} eq '') { $LOCS_NAME{$latlong} = 'Untitled'; } $LOCS_HTML{$latlong}=~s/"/\\"/g; #JSON escape quotes in our HTML... if ($WROTEROWS > 0) { print OUT ",\n"; } print OUT "{ \"type\": \"Feature\", \"geometry\": { \"type\": \"Point\", \"coordinates\": [$latlong] }, \"properties\": { \"name\": \"$LOCS_NAME{$latlong}\", \"numarts\": $LOCS_CNT{$latlong}, \"centerimage\": \"$LOCS_IMG{$latlong}\", \"Articles\": \"$LOCS_HTML{$latlong}\" } }"; $WROTEROWS++; } print OUT "\n\n] } "; close(OUT); print "Distinct Locations: $WROTEROWS\n";