Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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:


share|improve this question
You could try a KML to CSV converter. CSV can be imported in Mathematica. –  Sjoerd C. de Vries Jan 17 at 6:56
Maybe this can help blog.wolfram.com/2009/04/17/mapping-gps-data –  Hubble07 Jan 17 at 7:05
That blog uses gpx not kml. –  Sjoerd C. de Vries Jan 17 at 7:34
Well then he can do this first kml2gpx.com –  Hubble07 Jan 17 at 7:42
I'm sure you're aware MM supports KML as an import (and export) format. To turn an imported file into a graph (I'll assume you mean in the topological sense) you'll need to pull the appropriate attributes from the import and parse them into data that can be used with the MM graph functions. MM does not intrinsically have conversion from KML to graph format. –  rasher Jan 17 at 8:45

1 Answer 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


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*)

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

Mathematica graphics

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

trainCoord = Import["South_Depots.csv"];
trainCoord = trainCoord[[All, {1, 2}]];
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

share|improve this answer
By default it returns graphics, yes, but take a look at Import["file","Elements"] and specifically Import["file","Data"]. –  Pickett Jan 17 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 at 0: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. –  rasher Jan 18 at 1:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.