Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package graphCSV2dot;
- import java.util.*;
- import java.io.*;
- class Road {
- int cityId;
- double lenght;
- public Road(int cityId, double lenght) {
- this.cityId = cityId;
- this.lenght = lenght;
- }
- }
- class City {
- int id;
- String name;
- LinkedList<Road> roads;
- HashMap<Integer, Double> roadId2Len;
- public City(int id, String name) {
- this.id = id;
- this.name = name;
- roads = new LinkedList<Road>();
- roadId2Len = new HashMap<>();
- }
- public void addRoad(int cityId, double len) {
- roads.add(new Road(cityId, len));
- roadId2Len.put(cityId, len);
- }
- }
- public class Main {
- public static void main(String[] args) {
- String fileName="map.csv", dotExe="c:/Programs/graphviz-2.38/bin/dot.exe";
- if (args.length<1) {
- System.err.println("Missing argument #1 - input file name. Trying "+fileName);
- } else fileName=args[0];
- if (args.length<2) {
- System.err.println("Missing argument #2 - dot.exe (full path). Trying "+dotExe);
- } else dotExe=args[1];
- System.err.println("Reading input CSV from: "+fileName);
- Scanner s;
- HashMap<Integer, City> citiesMap = new HashMap<>();
- LinkedList<City> cities = new LinkedList<>();
- try {
- s = new Scanner(new File(fileName), "utf-8");
- String outputFileName=fileName+".gv";
- PrintWriter f = new PrintWriter(outputFileName, "utf-8");
- f.print("strict digraph graphname {\n"
- // + " concentrate=true\n"
- + " label=\"Generated at " + new Date().toString() + "\"\n" + " overlap=false\n"
- // + " ratio=compress\n"
- // + " splines=true\n"
- + " layout=sfdp\n"
- // + " esep = -0.4\n"
- // + " graph [pad=\"0.1\", nodesep=\"0.2\", ranksep=\"1\"];"
- + " charset=\"UTF-8\"\n");
- while (s.hasNext()) {
- String line = s.nextLine();
- String fields[] = line.split(";");
- if (fields.length >= 2) {
- int cityId = Integer.parseInt(fields[0]);
- City c = new City(cityId, fields[1]);
- f.printf(" %d [label=\"%s\"]\n", cityId, fields[1] + "\\nID=" + cityId);
- int i;
- for (i = 2; i < fields.length; i += 2) {
- int cid = Integer.parseInt(fields[i + 1]);
- double len = Double.parseDouble(fields[i]);
- if (len == 0)
- break;
- c.addRoad(cid, len);
- }
- System.out.printf("Adding city: %s (id=%d) with %d roads.\n", c.name, c.id, i/2);
- cities.add(c);
- citiesMap.put(c.id, c);
- }
- }
- s.close();
- for (City c : cities) {
- for (Road r : c.roads) {
- City dstCity = citiesMap.get(r.cityId);
- if (dstCity == null)
- System.err.printf("Destination city %d of road from %s with %d km does not exist\n", r.cityId,
- c.name, r.lenght);
- else {
- // check the road back:
- Double lenBack = dstCity.roadId2Len.get(c.id);
- // if road back has different length, print the edge:
- if (lenBack == null || lenBack != null && r.lenght != lenBack) {
- System.err.printf("Asymetric road between city ID %d and %d\n", c.id, dstCity.id);
- f.printf(" %d -> %d [label=\"%.0f km\"];\n", c.id, r.cityId, r.lenght);
- } else if (c.id < dstCity.id) { // print the current road only once:
- f.printf(" %d -> %d [label=\"%.0f km\" dir=both]\n", c.id, r.cityId, r.lenght);
- }
- }
- }
- }
- f.printf("}\n");
- f.close();
- Runtime.getRuntime().exec(dotExe + " -O -Tsvg "+outputFileName);
- System.out.println("OK, successfuly generated "+outputFileName+" and "+outputFileName+".svg!");
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
RAW Paste Data