4
$\begingroup$

I'm trying to work with the new GeoGraphics functionality in Mathematica 10, starting with the example given here. None of the code seems to run well for me, though. I try to run the first command:

allLibraries = EntityValue["PublicLibrary", "Entities"]

and get the output

$Failed

I feel dumb failing at something so basic but I really can't find many resources on this topic. It seems like their interface is very picky and doesn't behave as they say it should, for example they have another example here which begins with the code

allUSZIPCodes = EntityList[EntityClass["ZIPCode", {"Country", "UnitedStates"}]];

which simply returns itself as output for me without downloading any data. If I instead run just

EntityList["ZIPCode"]

it downloads all zip codes as I was expecting it to.

What am I doing wrong and are there any other resources on this other than the examples I've linked above?

$\endgroup$
  • 1
    $\begingroup$ Perhaps FindLibrary is what you are looking for? $\endgroup$ – Jason B. Jun 28 '16 at 13:14
3
$\begingroup$

EntityValue[] gives a list of available entity types, and "PublicLibrary" is not among them. Moreover,

EntityValue["PublicLibrary", "EntityCount"]

returns unevaluated, whereas

EntityValue["PublicSchool", "EntityCount"]

returns 103959. I would conclude, therefore, that Wolfram jumped the gun with its example.

Returning to this problem later, I noticed that "LibraryBranch" is an available entity type (There are 21857 branch libraries.) Even with this substitution, the next two lines of the example timed out repeatedly.

EntityValue::ctimeout: A computation timed out. >>
EntityValue::nodat: Unable to download data. Some or all results may be missing. >>

Note that

EntityValue[Entity["LibraryBranch"], "Properties"]

lists "City" as a valid property, although "Books" is not. In any case, I made no further progress.

$\endgroup$
2
$\begingroup$

I think what happened here is that "PublicLibrary" got split into "LibrarySystem" and "LibraryBranch" databases, only the first of which has the information we need.

allLibraries = EntityValue["LibrarySystem", "Entities"];
cities = EntityValue[allLibraries, 
   EntityProperty["LibrarySystem", "City"]];
books = EntityValue[allLibraries, 
   EntityProperty["LibrarySystem", "Books"]];
booksInTheCities = {#[[1, 1]], Total[Last /@ #]} & /@ 
   GatherBy[
    Select[Transpose[{cities, books}], 
     FreeQ[#, _Missing, \[Infinity]] &], First];
coords = EntityValue[(First /@ booksInTheCities), "Position"];
booksAndPositions = Transpose[{coords, Last /@ booksInTheCities}];
projection = {"LambertAzimuthal", 
   "Centering" -> GeoPosition[{37.1558, -95.883}]};
data = {GeoGridPosition[#1, projection][[1]], #2} & @@@ 
   booksAndPositions;
weightedData = WeightedData @@ Transpose[Take[data, All]];
bookDensityP = SmoothKernelDistribution[weightedData, "Silverman"];
bookDensity[{lat_Real, long_Real}] := 
  With[{xy = 
     First[GeoGridPosition[GeoPosition[{lat, long}], projection]]}, 
   Flatten[{xy, PDF[bookDensityP, xy]}]];
{{latmin, latmax}, {longmin, longmax}} = 
  GeoBounds[Entity["Country", "UnitedStates"]];
bookPlot = 
  ContourPlot[
   Sqrt[Last[bookDensity[{lat, long}]]], {long, longmin, 
    longmax}, {lat, latmin, latmax}, Frame -> False, PlotRange -> All,
    Contours -> 100, MaxRecursion -> 2, 
   ColorFunction -> ColorData["SunsetColors"], PlotRangePadding -> 0, 
   ContourStyle -> None];
GeoGraphics[{GeoStyling[{"GeoImage", bookPlot}], 
  CountryData["UnitedStates", "Polygon"], GrayLevel[0.3], Opacity[1], 
  PointSize[0.001], Point[coords]}, 
 GeoRange -> Entity["Country", "UnitedStates"]]

There is a lot of data, so this takes a while to download, and I made a couple of changes to the GeoGraphics call to use the "Polygon" property of the "CountryData" entity,

Mathematica graphics

$\endgroup$
1
$\begingroup$

I find its useful in cases with curated data to see what Wolfram Alpha does for a similar query/function.

In this case it does seem there is underlying data for public libraries in W|A, so this is possibly just an access/terminology problem. Will explore a bit further and edit this post.

From MMA try:

== public library <ctrl-enter>

and go from there.

Example direct from W|A below.

WolframAlpha Library query

$\endgroup$

Your Answer

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.