/* Generates a KML file from a graph file and a node coordinates file. Usage: kmlgen graph.gr graph.co > graph.kml Author: Christian Sommer Download at http://www.sommer.jp DIMACS format specification Some of the DIMACS graph files that come with coordinates can be visualized using Google Earth (http://earth.google.com/) and KML (http://code.google.com/apis/kml/documentation/kmlreference.html). The program compiled from this source code takes as input a .gr and a .co file of the same graph and generates a .kml file. =========================== DIMACS format specification =========================== taken from http://www.dis.uniroma1.it/~challenge9/format.shtml#graph (files with comment lines cannot be read by this program) ================= Graph (.gr files) ================= A graph contains n nodes and m arcs Nodes are identified by integers 1...n Graphs can be interpreted as directed or undirected, depending on the problem being studied Graphs can have parallel arcs and self-loops Arc weights are signed integers By convention, shortest path graph file names should have the suffix .gr. Line types are as follows. Problem line The problem line is unique and must appear as the first non-comment line. This line has the format on the right, where n and m are the number of nodes and the number of arcs, respectively. p sp n m Arc descriptor lines Arc descriptors are of the form on the right, where U and V are the tail and the head node ids, respectively, and W is the arc weight. a U V W ============================ Node coordinates (.co files) ============================ X-Y coordinates in the plane associated with nodes of the graph can be stored in an auxiliary file having the same name of the graph file it is related to and extension .co. For instance, file mygraph.co would contain the node coordinates of graph mygraph.gr. Line types are as follows. Problem line The problem line is unique and must appear as the first non-comment line. This line has the format on the right, where n is the number of coordinate lines that follow. p aux sp co n Coord lines Coordinate lines are of the form on the right, where id , X and Y are the id of the node, its X-coordinate, and its Y-coordinate, respectively. v id X Y */ #include #include #include #include #include #include using namespace std; #define DBLCOORD(i) ((double)i / (double)1000000) int main(int argc, char** argv) { if(argc <= 2) { cout << "Usage: " << argv[0] << " graph.gr graph.co" << endl; return 0; } ifstream grIn(argv[1]); ifstream coIn(argv[2]); string a, b; int numPoint, numEdge, startP, endP, len; long coordLat, coordLng; coIn >> a >> b; coIn >> a >> b; coIn >> numPoint; long * lat = new long[numPoint + 1]; long * lng = new long[numPoint + 1]; // read coordinates for all nodes int coordRead = 0 ; while (coIn >> a >> startP >> coordLat >> coordLng) { coordRead++; assert (startP > 0); assert (startP <= numPoint); lat[startP] = coordLat; lng[startP] = coordLng; } coIn.close (); grIn >> a >> b; grIn >> numPoint >> numEdge; cout << "" << endl; // read and output all edges int edgesRead = 0 ; while (grIn >> a >> startP >> endP >> len) { edgesRead++; assert(startP > 0); assert(endP > 0); assert(len >= 0); cout << ""; cout << "1relativeToGround" << DBLCOORD(lat[startP]) << "," << DBLCOORD(lng[startP]) << ",10 " << DBLCOORD(lat[endP]) << "," << DBLCOORD(lng[endP]) << ",10" << ""; cout << "From: " << startP << ", To: " << endP << ", Length: " << len << ""; cout << "" << endl; } grIn.close(); cout << "" << endl; return 0; }