2

I'm wondering how I can take a .KML (Keyhole Markup Language) file and convert/express it as a graph for use in Mathematica. Even just in general, is it possible to convert a KML file to a graph?

I'm hoping to express the US railroad system in the 19th century as a graph using this data:

http://railroads.unl.edu/resources/

1 Answers1

1

using the KML to CSV converter converted the file South_Depots.kml from the site http://railroads.unl.edu/resources/ to CVS file.

Mathematica graphics

Then inside Mathematica, loaded the data and first drew on the US map

SetDirectory[NotebookDirectory[]];
FileNames["*.csv"]

Mathematica graphics

trainCoord = Import["South_Depots.csv"];
trainCoord = trainCoord[[All, {1, 2}]]; (*only need first 2 columns *)
trainCoord[[All, {1, 2}]] = trainCoord[[All, {2, 1}]]; (*swap coordinates*)
trainCoordGeo = Map[GeoGridPosition[GeoPosition[#], "Mercator"][[1]] &, 
    {trainCoord}, {2}][[1]];  (*make it GEO*)

UScoords = CountryData["UnitedStates", "Coordinates"];
UScoordsGeo = Map[GeoGridPosition[GeoPosition[#], "Mercator"][[1]] &, 
  {UScoords}, {2}][[1]]; (*make it GEO*)


 Graphics[
 {
  {Gray, Polygon[UScoordsGeo]},
  {Red, PointSize[.005], Point[trainCoordGeo]}
  }]

Mathematica graphics

To convert the data to Mathematica graph data, one way is

SetDirectory[NotebookDirectory[]];
trainCoord = Import["South_Depots.csv"];
trainCoord = trainCoord[[All, {1, 2}]];
Needs["GraphUtilities`"];
g = Rule @@@ trainCoord[[1 ;; 20]];
coords = GraphCoordinates[g]

Mathematica graphics

now that you have the CVS data, you can do any other graph operations you want on it inside Mathematica.

reference : how-do-i-plot-coordinates-latitude-and-longitude-pairs-on-a-geographic-map

Update: I just saw Rasher comment above that M supports reading KML. Yes, I just tried it

   trainCoord = Import["South_Depots.kml"];

This can be another option to read the data. It seems to be graphic data.

Mathematica graphics

Nasser
  • 143,286
  • 11
  • 154
  • 359
  • By default it returns graphics, yes, but take a look at Import["file","Elements"] and specifically Import["file","Data"]. – C. E. Jan 17 '14 at 14:55
  • Thank you very much for your answer. I've successfully repeated your steps. My problem now is that the vertices don't have edges connecting them. But why would they? I've only given the graph unconnected points, correct? – Robert Mogel Jan 18 '14 at 00:29
  • @RobertMogel: KML are not obligated to have paths (routes). If they don't, you're probably out of luck, unless you have some other data to associate points to paths. – ciao Jan 18 '14 at 01:05