Dec 31, 2008

How to grab photos from Flickr

Happy new year 2009!

I have a post about make a Flickr image wall with Mathematica

First, you need get some basic knowledge about Flickr API, Photo Source Url

The coding is very simple, just play with it, then you can modify it to try something new.

catxml = Import[
"http://www.flickr.com/services/rest/?method=flickr.groups.pools.getphotos&api_key=your_API_key&group_id=75601600@N00&per_page=500&page=1&safe_search=1", "XML"];

This line will do the query from Flickr group Somebody else's cat, return 500 images’ meta data

cats = Cases[catxml, XMLElement["photo", id_, {}] :> id, Infinity];
ids = #[[All, 2]] & /@ cats;
ids = RandomSample[ids, 100];

This three lines will extract 100 random photo ids

cs="http://farm"<>#[[5]]<>".static.flickr.com/"<>#[[4]]<>"/"<>#[[1]]<>"_"<>#[[3]]<>"_s.jpg" &/@ids;
imgs=Import[#] & /@ cs;

Then get the real url to each photo and import them. Here we use small size (75 by 75) “_s.jpg”

ImageAssemble[Partition[imgs, 10]]

Then put them together into one image

So we finish it just in several lines.

Dec 11, 2008

How to make a Ternary Plot

For the reader who are curious about the post on Ternary Plot

The code is very simple, here is the mathematica notebook, and the pdf version if the notebook somehow doesn't work.

Sorry for the late reply, I seldom check the comments for the older posts.

Dec 7, 2008

Tip: create 3D Map

Here is a simple explanation on how to create 3D map in the previous post.

The key is to use Extrude function (I copied it somewhere):

extrude[pts_, h_] := Module[{vb, vt, len = Length[pts], nh},
If[! NumericQ[h], nh = 0., nh = N@h];
vb = Table[{pts[[i, 1]], pts[[i, 2]], 0}, {i, len}];
vt = Table[{pts[[i, 1]], pts[[i, 2]], nh}, {i, len}];
GraphicsComplex[
Join[vb, vt], {Polygon[Range[len]],
Polygon[Append[
Table[{i, i + 1, len + i + 1, len + i}, {i, len - 1}], {len, 1,
len + 1, 2 len}]], Polygon[Range[len + 1, 2 len]]}]]

Let's create a map to show per-capita oil consumption in South America.

(* get the data *)

(* Falkland Islands is removed *)

mapdata = {First@CountryData[#, "Polygon"],
CountryData[#, "OilConsumption"]/
CountryData[#, "Population"]} & /@
DeleteCases[CountryData["SouthAmerica"], "FalklandIslands"];
mapdata[[All, 2]] = Rescale[mapdata[[All, 2]]];

(* 3D map *)

Graphics3D[{EdgeForm[Darker[ColorData["Rainbow"][#[[2]]]]],
FaceForm[ColorData["Rainbow"][#[[2]]]],
extrude[#[[1, 1]], #[[2]]]} & /@ mapdata, Boxed -> False,
Lighting -> "Neutral", BoxRatios -> {1, 1, 0.2}, ImageSize -> 800]

The visual quality of 3D map is low, however, it is good enough for the classroom.

Dec 3, 2008

Mathematica 7: import ESRI Shape file

Finally, ESRI Shape file is supported in Mathematica 7: Import["dir/file.shp"]

However, I feel it is necessary to explain it in a little more detail for users who are not that familiar with Mathematica.

Let's import a  shape file from Indiana GIS Atlas which contains county boundaries.

(*import data*)
data=First@Import["http://129.79.145.7/arcims/statewide_mxd/downloads/LANDSURVEY_COUNTY_POLY_IN.zip", Data"];
(*check what is inside the data*)
data[[All, 1]]
{"LayerName", "Geometry", "Labels", "LabeledData"}
(* check the field name in attribute table *)
"Labels" /. data
{"AREA", "PERIMETER", "NAME_U", "NAME_L", "NCAPC"}
(* attribute data is stored in "LabeldData" *)
area = "AREA" /. ("LabeledData" /. data);
area = Rescale[area];
(* get all the geometry *)
geometry = "Geometry" /. data;
(* put two data together *)
mapdata = Transpose[{geometry, area}];
(* generate the thematic map *)
Graphics[{EdgeForm[{Thick, Gray}], FaceForm[ColorData["Rainbow"][#[[2]]]], #[[1]]} & /@ mapdata, Frame -> False, ImageSize -> 400]

With some extra efforts, a 3D version is generated.