0
$\begingroup$

I have been very busy these days with giving the VertexCoordinates of the cities the geographic coordinates from CityData["CityName","Coordinates"] of the cities. Could somebody please help me on writing algorithm?

Graph[{"Uppsala" -> "Marsta", "Marsta" -> "Uppsala", 
  "UpplandsVasby" -> "Sollentuna", "Sollentuna" -> "UpplandsVasby", 
  "UpplandsVasby" -> "Marsta", "Marsta" -> "UpplandsVasby", 
  "Stockholm" -> "Boo", "Boo" -> "Stockholm", 
  "Stockholm" -> "Lidingo", "Lidingo" -> "Stockholm", 
  "Stockholm" -> "Sollentuna", "Sollentuna" -> "Stockholm", 
  "Stockholm" -> "Taby", "Taby" -> "Stockholm"}, 
 VertexLabels -> "Name"]
$\endgroup$
7
  • $\begingroup$ Which Graph[]? Can you maybe show what you've already done in Mathematica? $\endgroup$ Commented Apr 21, 2013 at 11:37
  • $\begingroup$ please see result $\endgroup$
    – Alex
    Commented Apr 21, 2013 at 12:32
  • $\begingroup$ Are you saying that you would like to plot the graph laid out in such a way that the vertices are placed according to their geographic location? $\endgroup$ Commented Apr 21, 2013 at 12:56
  • $\begingroup$ Exactly!!My graph is just simple here but yes my problem is how to locate vertices on their geographical coordinates and second step to show it in the map.Not just a Plot but the graph itself!!!When I want to show the flow on the vertices. $\endgroup$
    – Alex
    Commented Apr 21, 2013 at 13:09
  • $\begingroup$ Try ref/VertexCoordinates and ref/CityData in the help? $\endgroup$
    – chris
    Commented Apr 21, 2013 at 14:14

2 Answers 2

5
$\begingroup$

You could extract coordinates using CityData and set it by SetProperty.

g = Graph[{"Uppsala" -> "Marsta", "Marsta" -> "Uppsala", 
"UpplandsVasby" -> "Sollentuna", "Sollentuna" -> "UpplandsVasby", 
"UpplandsVasby" -> "Marsta", "Marsta" -> "UpplandsVasby", 
"Stockholm" -> "Boo", "Boo" -> "Stockholm", 
"Stockholm" -> "Lidingo", "Lidingo" -> "Stockholm", 
"Stockholm" -> "Sollentuna", "Sollentuna" -> "Stockholm", 
"Stockholm" -> "Taby", "Taby" -> "Stockholm"}, 
  VertexLabels -> "Name", ImagePadding -> 40];

coords = CityData[#, "Coordinates"] & /@ VertexList[g];

SetProperty[g, {VertexCoordinates -> Reverse[coords, 2], 
  Prolog -> {Gray, CountryData["Sweden", "Polygon"]}}]

enter image description here

If SetProperty doesn't work (v9.0.0)

cities = {"Uppsala", "Marsta", "UpplandsVasby", "Sollentuna", 
"Stockholm", "Boo", "Lidingo", "Taby"};

coords = CityData[#, "Coordinates"] & /@ cities;

g = Graph[
cities, {"Uppsala" -> "Marsta", "Marsta" -> "Uppsala", 
 "UpplandsVasby" -> "Sollentuna", "Sollentuna" -> "UpplandsVasby", 
 "UpplandsVasby" -> "Marsta", "Marsta" -> "UpplandsVasby", 
 "Stockholm" -> "Boo", "Boo" -> "Stockholm", 
 "Stockholm" -> "Lidingo", "Lidingo" -> "Stockholm", 
 "Stockholm" -> "Sollentuna", "Sollentuna" -> "Stockholm", 
 "Stockholm" -> "Taby", "Taby" -> "Stockholm"}, 
   VertexCoordinates -> Reverse[coords, 2], 
   Prolog -> {Gray, CountryData["Sweden", "Polygon"]}, 
   VertexLabels -> "Name", ImagePadding -> 40]
$\endgroup$
3
  • $\begingroup$ thats great !!But when i am evaluating your proposed codes, it just gives me the simple Graph that I had!!Without Sweden Map and coordinates!!What could be wrong?Do you have output in the Graph? $\endgroup$
    – Alex
    Commented Apr 21, 2013 at 15:27
  • $\begingroup$ Which version of Mathematica are you using? In v 9.0.1 it works fine. If it doesn't work, you can just add option when you construct graph or reconstruct graph like: Graph[VertexList[g], EdgeList[g], VertexCoordinates->.., Prolog->...] $\endgroup$
    – halmir
    Commented Apr 21, 2013 at 16:06
  • $\begingroup$ black means "it's defined", so nothing wrong with it. did you try to build graph with coords? I added that version.. $\endgroup$
    – halmir
    Commented Apr 21, 2013 at 18:24
1
$\begingroup$

I tend to use GraphPlot[] instead if I need to depict the graph with other primitives, so here's my take:

cityGraph = {"Uppsala" -> "Marsta", "Marsta" -> "Uppsala", "UpplandsVasby" -> "Sollentuna",
             "Sollentuna" -> "UpplandsVasby", "UpplandsVasby" -> "Marsta",
             "Marsta" -> "UpplandsVasby", "Stockholm" -> "Boo", "Boo" -> "Stockholm",
             "Stockholm" -> "Lidingo", "Lidingo" -> "Stockholm",
             "Stockholm" -> "Sollentuna", "Sollentuna" -> "Stockholm",
             "Stockholm" -> "Taby", "Taby" -> "Stockholm"}

GraphPlot[cityGraph, Background -> ColorData["Legacy", "PowderBlue"], 
          EdgeRenderingFunction -> ({Blue, Line[#]} &), MultiedgeStyle -> 1/20,
          PlotRange -> {{17, 19}, {58, 60}}, 
          Prolog -> {Gray, CountryData["Sweden", {"FullPolygon", "Equirectangular"}]}, 
          VertexCoordinateRules ->
          Map[# -> Reverse[CityData[{#, "Sweden"}, "Coordinates"]] &,
              VertexList[Graph[cityGraph]]], VertexLabeling -> Tooltip, 
          VertexRenderingFunction -> ({Directive[AbsolutePointSize[4], Red],
                                       Tooltip[Point[#1], #2]} &)]

Swedish city graph

(If executed in Mathematica, there should be tooltips associated with each vertex.)

$\endgroup$
5
  • 1
    $\begingroup$ I suppose it ought to be explicitly noted that the coordinates in the polygon returned by CountryData[] are in (long, lat) format, while CityData[] coordinates return in (lat, long) format. $\endgroup$ Commented Apr 21, 2013 at 15:07
  • $\begingroup$ Your coordinates comment about CountryData and CityData and their differences is really amazing !!!Many Thanks!!!Thats very helpful to know that!!But about your suggestion to use GraphPlot the point is I want to show the optimized flow in the my Graph then better to use new version 9 availabilities.I wonder by using GraphPlot I can use "FlowGraph" capabilities?! $\endgroup$
    – Alex
    Commented Apr 21, 2013 at 15:54
  • $\begingroup$ Can you maybe edit your question to show the fancy Graph[] with flows that you want to be embedded into a map? $\endgroup$ Commented Apr 21, 2013 at 16:06
  • $\begingroup$ Many thanks for the suggestion .I will definitely do that But on that way I have to explain many other things not related to issue.After developing whole GraphNetwork I will upload it as sample later. $\endgroup$
    – Alex
    Commented Apr 21, 2013 at 16:45
  • $\begingroup$ It doesn't have to be the entire network. You've already supplied a minimal example; now, how would you put the flows into this small graph you've presented? $\endgroup$ Commented Apr 21, 2013 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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