-1
$\begingroup$

I am trying to visualize data of a dataset on a shapefile in Mathematica, but cannot find an understandable example. Let's assume I have a normal Shapefile with one Field "District No." inside and 5 polygons that are numbered from 1 to 5 in that field.

Now this is an example of my dataset structure:

myDataset = 
 Dataset[{<|"District No." -> 1, 
    "Party 1" -> RandomInteger[10000; 100000], 
    "Party 2" -> RandomInteger[10000; 100000], 
    "Party 3" ->  RandomInteger[10000; 100000], 
    "Party 4" -> RandomInteger[10000; 100000], 
    "Party 5" -> RandomInteger[10000; 100000], 
    "Party 6" -> RandomInteger[10000; 100000],  
    "Party 7" -> RandomInteger[10000; 100000], 
    "Invalid" -> RandomInteger[10000; 100000], 
    "Absent" -> RandomInteger[10000; 100000]|>,
   <|"District No." -> 2, "Party 1" -> RandomInteger[10000; 100000], 
    "Party 2" -> RandomInteger[10000; 100000], 
    "Party 3" ->  RandomInteger[10000; 100000], 
    "Party 4" -> RandomInteger[10000; 100000], 
    "Party 5" -> RandomInteger[10000; 100000], 
    "Party 6" -> RandomInteger[10000; 100000],  
    "Party 7" -> RandomInteger[10000; 100000], 
    "Invalid" -> RandomInteger[10000; 100000], 
    "Absent" -> RandomInteger[10000; 100000]|>,
   <|"District No." -> 3, "Party 1" -> RandomInteger[10000; 100000], 
    "Party 2" -> RandomInteger[10000; 100000], 
    "Party 3" ->  RandomInteger[10000; 100000], 
    "Party 4" -> RandomInteger[10000; 100000], 
    "Party 5" -> RandomInteger[10000; 100000], 
    "Party 6" -> RandomInteger[10000; 100000],  
    "Party 7" -> RandomInteger[10000; 100000], 
    "Invalid" -> RandomInteger[10000; 100000], 
    "Absent" -> RandomInteger[10000; 100000]|>,
   <|"District No." -> 4, "Party 1" -> RandomInteger[10000; 100000], 
    "Party 2" -> RandomInteger[10000; 100000], 
    "Party 3" ->  RandomInteger[10000; 100000], 
    "Party 4" -> RandomInteger[10000; 100000], 
    "Party 5" -> RandomInteger[10000; 100000], 
    "Party 6" -> RandomInteger[10000; 100000],  
    "Party 7" -> RandomInteger[10000; 100000], 
    "Invalid" -> RandomInteger[10000; 100000], 
    "Absent" -> RandomInteger[10000; 100000]|>,
   <|"District No." -> 5, "Party 1" -> RandomInteger[10000; 100000], 
    "Party 2" -> RandomInteger[10000; 100000], 
    "Party 3" ->  RandomInteger[10000; 100000], 
    "Party 4" -> RandomInteger[10000; 100000], 
    "Party 5" -> RandomInteger[10000; 100000], 
    "Party 6" -> RandomInteger[10000; 100000],  
    "Party 7" -> RandomInteger[10000; 100000], 
    "Invalid" -> RandomInteger[10000; 100000], 
    "Absent" -> RandomInteger[10000; 100000]|>
   }]

How can I visualize the values either of one of the parties (columns) of the dataset and assign them to each polygon or assign, for example the Max of each party to the respective district and visualize that in a nice TemperatureMap?

I found this code example, but I don't understand it (you need to scroll down there):

Labeled[Row[{Graphics[MapThread[{FaceForm[ColorData["Temperature"][1-#1]],#2}&,{resc,polys}],ImageSize->Large],BarLegend[{ColorData["Temperature"][1-#]&/@Sort[resc][[1;;-1;;5]],{N[dmin,2],N[dmax+.01,2]}},8]}],\[IndentingNewLine]Style["Jones minus Spencer per ballot by Ward",20,FontFamily->"Source Sans Pro"],Top]

I get it that the only really relevant line is this:

Row[{Graphics[MapThread[{FaceForm[ColorData["Temperature"][1-#1]],#2}&,{resc,polys}],ImageSize->Large]

But where is the part where I assign a dataset-column to the district column in the shapefile?

So far, I have imported the shapefile like this:

shp = FileNames["Bezirk*", {NotebookDirectory[] <> "Shp"}];
FileFormat /@ shp
shpfile = SelectFirst[shp, FileFormat[#] === "SHP" &];
shpdata = Import[shpfile, "Data"]; (*Import the Shapefile Data*)
shppolygons = "Geometry" /. shpdata[[1]];

All I need is the most basic and primitive example.

And yes, this question is related with this one

New contributor
Kai is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\endgroup$
1
  • $\begingroup$ I am not all that familiar with shape files, but I recently came across this Q&A: Accessing geometry in Shape files (SHP). I wonder if something there could be of help to you. $\endgroup$
    – MarcoB
    Jun 22 at 1:55

1 Answer 1

1
$\begingroup$

Thre are many ways of getting information from datasets. I don't have the dataset with the geometry related to your problem, so I will fudge it with a shape file I have on my system (the departments of France). Here is your myDataset with the first column changed to code_insee to match a column with unique entries in my department shape file.

enter image description here

Now I import my department shapefile:

shapeFileToDataset[path_] := (
  Module[{dataset, q1, q2},
   dataset = (Import/*ResourceFunction["ToAssociations"]/*Dataset)[
     path, "Data"];
   q1 = Query[1, {"Geometry"}][dataset] // Transpose;
   q2 = Query[1, "LabeledData"][dataset] // Transpose;
   Transpose@Dataset@Merge[{Normal@q2, Normal@q1}, Identity]
   ]
  )

dset = shapeFileToDataset[<<path to shape file>>];

Now I extract 5 rows and eliminate colums which are not needed, keeping only code_insee and the geometry (details of these operations not shown).

enter image description here

Now we can join the two datasets based on the key column code_insee (note: gd is the above dataset with two columns).

jdataset = JoinAcross[gd, myDataset, "code_insee"]

enter image description here

Now we can use the geometry. Here is an example:

(* function returning colors for a party *)
getPartyColors[party_] := 
 ColorData["LightTemperatureMap"][#] & /@ 
  Rescale[(jdataset[All, party] // Normal)]

(* getting a color map for each of the 7 party columns *)
pcolor = getPartyColors[#] & /@ {"Party 1", "Party 2", "Party 3", "Party 4", "Party 5", "Party 6", "Party 7"}

(* getting the geometries of the districts (note: in our case these are French departments) *)
geometries = jdataset[All, "Geometry"] // Normal;

(* Thread the colors of the desired entry (here Party 5) with the geometry of the districts and show the map *)
th = Thread[{pcolor[[5]], geometries}] /. 
  RGBColor[a___] -> GeoStyling[RGBColor[a]];
GeoGraphics[{EdgeForm[Black], th}, GeoBackground -> "VectorMinimal"]

enter image description here

$\endgroup$

Your Answer

Kai is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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