Message Boards Message Boards

Is there a workaround to enable .svg files to be imported?

I have finally found a detailed vector based map of the US that includes the counties and is exactly what I have been looking for to create an interactive thematic map for health policy planning. I know that Mathematica 9.0 can export .svg but I just learned that it does not import that format. Has anyone a suggestion about perhaps converting the .svg to a format that M can import? The file I have found is just perfect for my needs and I don't know if I can find another like it in a natively importable format.
Thank you,
JPS
4 Replies
You perhaps mean this map? I think one of the most amazing Mathematica blogs I ever seen - Lunchtime Playground: Fun with Mathematica - has exactly what you need. The conclusion is that you can import .SVG file as XML data. He made a nice post about it and attached his notebook and additional files. You will need to get the files from his site ( or from here), but because of the author's kind sharing terms (and because the code's exceptional quality), I would like to repost the code here - you can find it below the image. Also take a look att the following recources:

Mathematica North America map
Analyzing U.S. 2008 Elections with Mathematica
What would a minimal example for a choropleth map in Mathematica look like?


 (* source http://flowingdata.com/2009/11/12/how-to-make-a-us-county-thematic-map-using-free-tools *)
 SetDirectory[NotebookDirectory[]];
 xml = Import["USA_Counties_with_FIPS_and_names.svg", "XML"];
 
 (* extract path *)
 dataxml = Cases[xml, XMLElement["path", x_, _] :> x, Infinity];
 
 (* extract polygon *)
 extractMap[path_] :=
  Module[{d, di, id}, d = "d" /. path; id = "id" /. path;
   di = Map[Partition[#, 2] &,
     ToExpression[
      StringSplit[
       StringReplace[
        StringSplit[d, "M"], {"L" -> ",", "e" -> "E", "z" -> ""}],
       ","]]]; di[[All, All, 2]] = -di[[All, All, 2]]; {id, di}];
polys = extractMap[#] & /@ dataxml;
statelines = polys[[-2, 2]];
counties = polys[[1 ;; -3]];

(* import data *)
data = Import["unemployment09.csv", "CSV", "Numeric" -> False];
graphdata =
  ToString[#[[2]] <> #[[3]]] -> ToExpression[#[[9]]] & /@ data;
rate = # /. graphdata & /@ counties[[All, 1]];

(* processing missing data *)
missing = First[Position[rate, _String]];
rate[[missing]] = -1;

(* assign color by rate *)
colorsystem =
  RGBColor @@@ ({{241, 238, 246}, {212, 185, 218}, {201, 148,
       199}, {223, 101, 176}, {221, 28, 119}, {151, 0, 67}}/255);
hotcolor[r_] :=
  Which[r > 10, colorsystem[[6]], r > 8, colorsystem[[5]], r > 6,
   colorsystem[[4]], r > 4, colorsystem[[3]], r > 2, colorsystem[[2]],
    True, colorsystem[[1]]];
colors = If[# > 0, hotcolor[#], White] & /@ rate;
maps = Transpose[{counties, colors}];
Graphics[{EdgeForm[
   LightGray], {FaceForm[#[[2]]], Polygon[#[[1, 2]]]} & /@ maps,
  Thick, White, Line[statelines]}, ImageSize -> 1200]
POSTED BY: Vitaliy Kaurov
Extraordinary!  Thank you Vitaliy, you saved me hours of work and possibly even saved my project from oblivion. You, this site and its many contributors are a godsend for those of us who wander lost in forest Mathematica in the dying light of day.
JPS
Hi Vitaliy:
I modified the code to indicate the paths for relevant files on my Mac and I fixed small errors that were generated but I am left with enigmatic error messages and would welcome any advice you chose to offer. The notebook file can be downloaded from:
https://dl.dropboxusercontent.com/u/78485648/Choropleth%20Map%20of%20US%20from%20Vitaliy%20Kaurov%20and%20Nathan%20Yau.nb

The remaining error messages:
Import::nffil: File not found during Import. >>
ReplaceAll::reps: {$Failed} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>

The message "ReplaceAll" occurs three times at the end.

Thank you,
JPS
The errors tell you what is wrong. There are two Import functions in that code and they require external files to be located as specified by the path inside Import. It seems you do not have those files.
POSTED BY: Vitaliy Kaurov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract