Geographic Data in Raku Demo

Last weekend I made a demo presentation showcasing the capabilities of the Raku packages:

This post encapsulates the essence of that presentation, offering a walk-through of how these packages can be leveraged to create good, informative geographic visualizations.

Here is the video recording of the presentation, [AAv4]:


Main Packages

The primary focus of our exploration is on two Raku packages:

  1. Data::Geographics: This package provides comprehensive country and city data, which is crucial for geographic data visualization and analysis.
  2. JavaScript::Google::Charts: This package interfaces with Google Charts, an established framework for creating various types of charts, including geographic plots.

Geographic Data Visualization

Data::Geographics: The Protagonist

The “Data::Geographics” package is the star of the presentation. It provides extensive data on countries and cities, which is essential for geographic data visualization and analysis. Initially, I attempted to create geographic plots using JavaScript freehand, but it proved challenging. Instead, I found it more practical to use the “JavaScript::Google::Charts” package, which offers a more structured framework for creating pre-defined chart types.

Creating Geographic Plots

Using the “JavaScript::Google::Charts” package, I demonstrated how to generate geographic plots. For instance, we visualized country data with a simple plot highlighting countries known to the “Data::Geographics” package in shades of green, while unknown regions were depicted in gray. (That is presentation’s “opening image.”)

Notably, Google Charts geo plots get be generated with suitable Large Language Model prompts and directly displayed in Raku chatbooks.

Data Analysis with Data::Geographics

Beyond simple visualization, certain analytical tasks can be done using the country data in “Data::Geographics”. For example, I conducted a rudimentary analysis of gross domestic product (GDP) and electricity production using linear regression.

The package also includes city data, enabling us to perform proximity searches and create neighbor graphs.


Practical Demonstrations

Country Data

Currently, “Data::Geographics” knows about 29 countries (≈195 data elements for each.) Here are the countries:

#% html
use Data::Geographics;
country-data().keys.sort ==> to-html(:multicolumn, columns => 3)
BotswanaHungarySerbia
BrazilIranSlovakia
BulgariaIraqSouthAfrica
CanadaJapanSouthKorea
ChinaMexicoSpain
CzechRepublicNorthKoreaSweden
DenmarkPolandTurkey
FinlandRomaniaUkraine
FranceRussiaUnitedStates
GermanySaudiArabia(Any)

Name Recognition

The package “DSL::Entity::Geographics” was specially made to recognize city and country names, which is particularly useful for conversational agents.

Here is named entity recognition example:

use DSL::Enitity::Geographics;
entity-city-and-state-name('Las Vegas, Nevada', 'Raku::System')

# United_States.Nevada.Las_Vegas

Correlation Plots

We created correlation plots to analyze the relationship between GDP and electricity production. Using Google Charts’ built-in functionality, we plotted regression lines to visualize trends. But Google Charts’ very nice “trend lines” functionality has certain limitations over logarithmic plots. Hence, that gave us the excuse to do linear regression with “Math::Fitting”:

City Data Tabulation and Visualization

City data visualization was another highlight. We filtered city data to display information such as population and location. By integrating Google Maps links, we provided an interactive way to explore city locations.

Tabulation

#% html
@dsCityData.pick(12)
==> { .sort(*<ID>) }()
==> to-html(field-names => <State City Population LocationLink>)
==> { $_.subst(:g, / <?after '<td>'> ('http' .*?) <before '</td>'> /, { "<a href=\"$0\">link</a>" }) }()
StateCityPopulationLocationLink
AlabamaMontgomery200603link
CaliforniaFresno542107link
MassachusettsWorcester206518link
NevadaLas Vegas641903link
TexasEl Paso678815link
VirginiaChesapeake249422link

City locations

Here are city locations plotted with “JavaScript::D3”:

Here are city locations plotted with “JavaScript::Google::Charts”:

Remark: In both plots above Las Vegas, Nevada and cities close to it are given focus.

Proximity Searches

Using the “Math::Nearest” package, we performed proximity searches to find the nearest neighbors of a given city. This feature is particularly useful for geographic analysis and planning.

Graph Visualization

For visualizing neighbor graphs, we used the packages “WWW::MermaidInk” and “JavaScript::D3”. The former interfaces with a web service to generate graph diagrams. The latter has its own built-in graph plotting functionalities. (Based on the force-directed graph plotting component of D3.js.)

Both approaches allow the creation of appealing visual representations of city connections.

Here is a Nearest Neighbor Graph plotted with “JavaScript::D3”:

Here is a Nearest Neighbor Graph plotted with “WWW::MermaidInk”:


Future Plans and Enhancements

While the current capabilities of “Data::Geographics” and “JavaScript::Google::Charts” are impressive, there is always room for improvement. Future plans include:

  • Enhancing the “Math::Fitting” package to support multidimensional regression.
  • Exploring the potential of “JavaScript::D3” for more flexible and advanced visualizations.

Conclusion

In summary, the combination of “Data::Geographics”, “JavaScript::Google::Charts” in Raku provides a powerful toolkit for geographic data visualization and analysis. “JavaScript::D3” is also very applicable exploratory data analysis. The function objects (functors) created by “Math::Nearest” and “Math::Fitting” make them very convenient to use.


References

Articles, blog posts

[AA1] Anton Antonov, “Age at creation for programming languages stats”, (2024), RakuForPrediction.

Packages

[AAp1] Anton Antonov, Data::Geographics Raku package, (2024), GitHub/antononcube.

[AAp2] Anton Antonov, Data::Reshapers Raku package, (2021-2024), GitHub/antononcube.

[AAp3] Anton Antonov, Data::Summarizers Raku package, (2021-2023), GitHub/antononcube.

[AAp4] Anton Antonov, Data::Translators Raku package, (2023-2024), GitHub/antononcube.

[AAp5] Anton Antonov, Data::TypeSystem Raku package, (2023-2024), GitHub/antononcube.

[AAp6] Anton Antonov, DSL::Entity::Geographics Raku package, (2021-2024), GitHub/antononcube.

[AAp7] Anton Antonov, Math::DistanceFunctions Raku package, (2024), GitHub/antononcube.

[AAp8] Anton Antonov, Math::Nearest Raku package, (2024), GitHub/antononcube.

[AAp9] Anton Antonov, JavaScript::D3 Raku package, (2022-2024), GitHub/antononcube.

[AAp10] Anton Antonov, JavaScript::Google::Charts Raku package, (2024), GitHub/antononcube.

Videos

[AAv1] Anton Antonov, “The Raku-ju hijack hack for D3.js”, (2022), YouTube/@AAA4prediction. (7 min.)

[AAv2] Anton Antonov, “Random mandalas generation (with D3.js via Raku)”, (2022), YouTube/@AAA4prediction. (2 min.)

[AAv3] Anton Antonov, “Exploratory Data Analysis with Raku”, (2024), YouTube/@AAA4prediction. (21 min.)

[AAv4] Anton Antonov, “Geographics data in Raku demo”, (2024), YouTube/@AAA4prediction. (37 min.)

One thought on “Geographic Data in Raku Demo

Leave a comment