Wolfram Notebooks for GIS
Wolfram Notebooks for GIS
Phileas Dazeley-Gaist - Wolfram Research
Friday September 20th 2024 - College of the Atlantic GIS Lab
Friday September 20th 2024 - College of the Atlantic GIS Lab
A Fast Introduction to Wolfram Language For COA GIS Students
What is the Wolfram Language (WL)
What is the Wolfram Language (WL)
◼
The Wolfram Language (WL) is a programming language produced by Wolfram Research.
◼
Wolfram describes WL as the world’s first and only full-scale computational language.
◼
WL has a huge standard library of functions, which makes it an exceptionally versatile programming language out of the box.
◼
It can be used to make art, music, animation, or for data science, web scraping, mathematics, GIS, and many other things.
◼
It’s a language that you learn and discover based on what you need to do, and one that subtly teaches you useful skills as you go.
Learning resources
Learning resources
◼
◼
◼
For WL GIS Detailed Guides, the following pages are great starters:
◼
◼
◼
◼
◼
◼
◼
For a quick introductory course to WL for beginners, check out An Elementary Introduction to the Wolfram Language.
◼
For a quick introductory course to WL for programmers, check out the Wolfram Language Fast Introduction for Programmers.
◼
For more specialised courses, check out Wolfram U
Some WL Essentials for non-programmers
Some WL Essentials for non-programmers
Storing Information: Variables
Storing Information: Variables
To store data in WL, you can assign it to a variable. For example:
Define a variable x to mean a picture of a cat:
In[]:=
x=
;
(The semicolon at the end of the line tells the computer to hide the result)
Now, every time the computer sees the symbol “x”, it knows x represents the cat picture:
Retrieve the variable: (i.e. ask: “What is x?”)
In[]:=
x
Out[]=
We can ask the computer to forget about “x” too using the ClearAll command, like this:
In[]:=
ClearAll[x]
Question: What do you think will happen now if I ask for x?
In[]:=
x
Out[]=
x
Variables can store all kinds of information, like images, numbers, lists, datasets, text, combinations of these, and many other things. For example, let’s define a variable called “cats” to mean a list of cat images + one dog image:
In[]:=
cats=
,
,
,
;
We’ll come back to our “cats” in a moment.
(Optional) Symbols
(Optional) Symbols
In other programming languages, asking for a definition of x, when x isn’t defined would return an error. The computer would say “sorry, I don’t know what x is, you never told me”.
However, WL works a little differently. It’s designed to be what’s called a “symbolic” programming language.
◼
In plain English, this means it doesn’t mind when things are undefined… when things are just symbols.
◼
When it’s not defined, x is just the symbol “x” in WL.
Why is this a big deal?
Why is this a big deal?
In Python, if we wanted to leave x undefined, we’d get an error:
In[]:=
y=1
x+y
x+y
Out[]=
Failure
In WL, however, it’s completely fine, and the result is much more helpful:
In[]:=
y=1;x+y
Out[]=
1+x
This behaviour makes WL a really powerful tool for maths and general programming. It also makes it much more flexible than other programming languages when it comes to expressing your ideas.
Functions
Functions
What are functions?
What are functions?
The ClearAll command is an example of a built-in function in WL.
Functions are pieces of code that can be used to do things.
Built-in functions
Built-in functions
Here is a list of a few WL functions (WL has about six thousand):
In[]:=
{CurrentImage,Plot,GeoGraphics,ImageIdentify,LLMSynthesize,RandomPoint,SpeechRecognize,TextWords};
Calling a function without arguments
Calling a function without arguments
Asking a function to do its thing is called “calling” the function. In WL, function calls use square brackets.
To call a function, you simply write the function name followed by opening and closing square brackets, like this:
Call the built-in function CurrentImage, which gets the current image from my webcam:
In[]:=
CurrentImage[]
Out[]=
(Note: the first time I ran this, I had to give my computer permission to use my webcam.)
Getting information about a function
Getting information about a function
We can ask for information about a function by adding a question mark in front of the function name. For example:
In[]:=
?CurrentImage
Out[]=
◼
If you click on the little “i” bubble at the top right of the result, it will open the WL documentation page for that function. This page contains detailed information about how the function works, and how to use it, with many examples to look through.
◼
You can also pull up the documentation page for a function by selecting the function name, and hitting “f1” on Windows, or “Cmd+shift+f” on mac.
Calling a function with arguments
Calling a function with arguments
Most functions need additional information to know exactly what to do.
If you try to call a function without giving it the information it wants, it will complain. For example:
Call the built-in function ImageIdentify with no arguments:
In[]:=
ImageIdentify[];
◼
ImageIdentify complains that it expected arguments.
◼
It’s basically saying “Hey, I don’t know what image you want me to identify because you didn’t tell me”.
The missing arguments should go in between the square brackets, separated by commas. But what’s missing exactly?
Let’s get more information about the function:
◼
This shows us a quick summary of how the function works, and what it expects from us.
◼
In this case, the first row tells us that we can call ImageIdentify with an image, and it will try to identity what the image is a picture of. Let’s try it:
Identify a cat from an image:
Defining and Calling Your Own Named Functions
Defining and Calling Your Own Named Functions
Let’s make a simple function evilTwinPicture that takes a picture as an argument, and returns the picture mirrored, with inverted colors:
Define the function evilTwinPicture:
Call evilTwinPicture:
Defining and Calling Your Own Pure Functions
Defining and Calling Your Own Pure Functions
We can also write this function in what’s called its “pure function” form. Pure functions are useful when you can’t be bothered to define a function with a name, because you just want to use the function in one place, for example. Here’s what the evilTwinPicture might look like as a pure function:
Define and call the pure function equivalent of evilTwinPicture in one go:
◼
The pound sign (#) means “Blank”, and stands for the function’s argument.
◼
The ampersand (&) tells the computer where the function definition ends
◼
The function works the same way as before (in fact, it’s almost exactly the same code).
Putting It All Together
Putting It All Together
Let’s come back to that list of cats we made earlier:
The cats have been told there’s an intruder among them. They activate their cat defense system, turning into their evil twins.
Let’s write a function to turn all the cats into their evil twins:
Activate cat defenses!
Takeaways
Takeaways
◼
By assembling together functions, you can design very powerful tools. This is called functional programming
◼
Big function definitions can be intimidating if you aren’t used to programming, but the good news is you can always break them down.
◼
Read the code inside out. The first things to be evaluated are the innermost pieces, and the last to be evaluated are the outermost pieces.
◼
Most importantly: If all this is still a little arcane and mystifying to you, that’s okay! Learning to program takes practice and goofing around. The best way to learn is to try it out yourself.
Managing and Representing Geographical Data in WL
Geospatial Data in the Wolfram Knowledgebase
Geospatial Data in the Wolfram Knowledgebase
The Wolfram Knowledgebase is a large collection of data about the real world curated by Wolfram and built-into the Wolfram Language. You can read more about it here.
Wolfram Language has access to many built-in curated geographic data sources and computable geographic entities. Let’s take deeper look at them.
Entities and Entity Discovery
Entities and Entity Discovery
What are WL named entities?
What are WL named entities?
Entities in WL represent named things that the language knows about, and can perform computations with. Type “ctrl+=” to start discovering entities:
For example:
Entity classes
Entity classes
Some entities are grouped in entity classes, for example:
Use EntityList to list the entities in a class:
Entities can also represent non-geographic things, like people, plants, animals, etc.
Getting information about entities:
Getting information about entities:
To get information about an entity, we can ask for one of its properties, like so:
Get the Area property of the Mount Desert Island entity:
To get the full list of properties for an entity, you can use the “Properties” property.
Get the list of properties of the Mount Desert Island Entity:
We can also ask for all the entity information as a dataset:
What does WL know about MDI?
Using entities in functions
Using entities in functions
I said earlier that entities are computable. What this means is that you can use entities or data from entities in functions, and WL will try to interpret the information sensibly.
We’ll see more examples of using entities with geographic functions a little later.
For now, here’s just one example:
Make a satellite view cutout of MDI with a blank background:
An overview of geographic entities:
An overview of geographic entities:
Curated Spatial Data
Curated Spatial Data
WL also offers a collection of curated spatial data functions. Here are just a few:
Here are some examples of geospatial computations using a couple of these functions:
GeoElevationData
GeoElevationData
Get a Digital Elevation Model (DEM) of MDI:
Make a relief plot of the DEM:
Make a contour map of the DEM:
WindVectorData
WindVectorData
Make a grid of 64 equally spaced positions covering Maine:
Sample wind vectors from yesterday in across Maine using weather station data:
Preview a random sample of three vectors:
Plot the sampled vectors using GeoStreamPlot
EarthquakeData
EarthquakeData
Find magnitude 6 to 9 earthquakes around the world recorded in the last year:
Show these earthquakes on a map:
WL Geospatial Data Representations and Functions
WL Geospatial Data Representations and Functions
Representing Geospatial Data
Representing Geospatial Data
WL offers many tools with which to represent and process geographic data. These representations can all be exported to many standard GIS data formats like SHP, GeoJSON, KML, etc.
GeoPositions and GeoGridPositions
GeoPositions and GeoGridPositions
Geographic (geodetic) positions in WL are represented using the GeoPosition function. For example:
Show Null Island on a map:
Sometimes, we might like to use coordinates in other coordinate reference systems. We can do this with GeoGridPosition:
Represent a position in Albers projection coordinates:
Convert this position to a geodetic position:
Lines, Polygons, and other geospatial geometric objects
Lines, Polygons, and other geospatial geometric objects
We can also represent lines, polygons, and other shapes geographically.
Lines are just lists of points, and polygons are just lists of points that are drawn as filled shapes.
Let’s demonstrate:
Let’s demonstrate:
Choose a few random cities around the world:
Trace lines between these cities in the order given by the list:
Note that because we used geographic entities, we never had to deal with the geodetic coordinates directly. If we had wanted to, we could have extracted the coordinates like so:
The the geodetic positions of the cities:
However, for polygons, city entities are ambiguous: If I ask for the polygon of a list of cities, am I asking for a shape made of the outline of all the cities, or am I asking for a polygon with each city as a point? For this reason, we have to use the city positions directly in this case.
Trace polygons joining these cities in the order given by the list:
Using the same polygons, represent the “opposite side” of the world:
Hemispheres, Day and night hemispheres, and the day-night terminator:
Hemispheres, Day and night hemispheres, and the day-night terminator:
Represent the half of the Earth that you are at the center of:
Represent the boundary between this half of the Earth, and the other:
Represent the half of the Earth that where it is currently day:
Represent the half of the Earth where it is currently night:
Represent the current day-night terminator (the line between day and night):
Visible Regions
Visible Regions
Represent the region that is visible from a position, not accounting for terrain:
We can also represent the boundary between the visible region, and what’s beyond the horizon from a point:
Geospatial Functions
Geospatial Functions
Visualisation Functions
Visualisation Functions
General geographic plotting:
GeoGraphics can be used for all kinds of geographic plotting.
Make a map of Europe, with country flags:
It’s the most flexible geographic visualisation function in WL, but depending on your visualisation goals, it’s still often nicer to use others.
Single values at locations:
To plot a list of locations, you can use GeoListPlot:
Plot the list of cities defined in the last section (Representing Geospatial Data):
To plot scalar values at a list of locations, you might also use GeoBubbleChart, or GeoRegionValuePlot
Make a geographic bubble chart of the countries in Asia by population:
Make a map of African countries colored by their population size:
Contour plot and heat maps:
WL also supports geographic contour and density plotting using GeoContourPlot and GeoDensityPlot.
Import a DEM of using GeoElevationData
Plot a contour map of the Manicouagan Reservoir DEM:
Plot a heat map of the Manicouagan Reservoir DEM:
Histograms:
WL has two built-in functions for geographic histogram plotting: GeoSmoothHistogram, and GeoHistogram.
Plot a smooth spatial histogram of earthquake data:
Plot a hexagonal bin spatial histogram of earthquake data:
Vector plots:
WL also has two built-in functions for plotting flows and vector fields on maps: GeoVectorPlot, and GeoStreamPlot.
Use GeoStreamPlot to plot streamlines for wind directions in Maine:
Use GeoVectorPlot to plot vectors for wind directions in Maine:
Graph Plots:
Finally, WL has two functions for geographic network plotting: GeoGraphPlot (for unweighted graphs) and GeoGraphValuePlot (for weighted graphs).
Make a list of connections between a selection of European capital cities and Rome:
Illustrate the saying: “All roads lead to Rome” with a map of cycling directions from a selection of European Capitals to Rome:
Load some flight data for flights that left the Chicago O’Hare airport Yesterday, and :
Plot the top 10 most popular airport destinations from Chicago yesterday:
Geographic Calculations
Geographic Calculations
◼
TravelDirections, TravelDirectionsData: Get travel directions
Get travel directions between the five largest Brazilian cities by population:
Plot the journey:
Get the travel directions as a dataset:
◼
GeoAntipode: Find the position on the opposite side of the Earth from a location.
Find and plot the Antipode of the town of Bar Harbor on a map:
◼
FindGeoLocation: Search locations by street address.
Search for the MIT campus from its address:
◼
GeoIdentify: Find matching geographic entities of a requested type given a location.
Find the MIT entity from its position:
◼
GeoNearest: Find locations that are nearest to a reference location.
Find the 10 nearest Colleges/Universities/Higher education institutions to MIT:
◼
RandomGeoPosition: Get random geographic positions in a region
Get and plot a map of 100 random positions in Arches National Park:
◼
GeoDistance, GeoLenth, GeoArea TravelDistance, TravelTime... And more!
Learning resources (bis)
◼
◼
◼
For WL GIS Detailed Guides, the following pages are great starters:
◼
◼
For a quick introductory course to WL for beginners, check out An Elementary Introduction to the Wolfram Language.
◼
For a quick introductory course to WL for programmers, check out the Wolfram Language Fast Introduction for Programmers.
◼
For more specialised courses, check out Wolfram U