| 1 |
\documentclass{article} |
| 2 |
\usepackage{verbatim,fancyvrb} |
| 3 |
\usepackage{pifont} |
| 4 |
%\usepackage[authoryear]{natbib} |
| 5 |
\usepackage[pdftex]{graphicx} |
| 6 |
\usepackage{gretl} |
| 7 |
\usepackage[hyphens]{url} |
| 8 |
\usepackage[pdftex]{hyperref} |
| 9 |
\usepackage[letterpaper,body={6.3in,9.15in},top=.8in,left=1.1in]{geometry} |
| 10 |
|
| 11 |
\setlength{\parindent}{0pt} |
| 12 |
\setlength{\parskip}{1ex} |
| 13 |
\setlength{\abovecaptionskip}{0pt} |
| 14 |
|
| 15 |
\renewcommand{\deg}{$^{\circ}$} |
| 16 |
|
| 17 |
\newcommand{\startappendices}{% |
| 18 |
\newcounter{appcount} |
| 19 |
\setcounter{appcount}{0} |
| 20 |
\renewcommand{\thesection}{Appendix \Alph{appcount}}} |
| 21 |
|
| 22 |
\newcommand{\myappendix}[1]{% |
| 23 |
\addtocounter{appcount}{1} |
| 24 |
\section{#1}} |
| 25 |
|
| 26 |
\newenvironment{funcdoc} |
| 27 |
{\noindent\hrulefill\\[-10pt]} |
| 28 |
{\medskip} |
| 29 |
|
| 30 |
\definecolor{steel}{rgb}{0.03,0.20,0.45} |
| 31 |
|
| 32 |
\hypersetup{pdftitle=geoplot: cartography in gretl, |
| 33 |
pdfauthor={Allin Cottrell and Jack Lucchetti}, |
| 34 |
colorlinks=true, |
| 35 |
linkcolor=blue, |
| 36 |
urlcolor=red, |
| 37 |
citecolor=steel, |
| 38 |
bookmarksnumbered=true, |
| 39 |
plainpages=false |
| 40 |
} |
| 41 |
|
| 42 |
\begin{document} |
| 43 |
|
| 44 |
\title{\textsf{geoplot}: cartography in gretl} |
| 45 |
\author{Allin Cottrell and Jack Lucchetti} |
| 46 |
\date{February 28, 2024} |
| 47 |
|
| 48 |
\maketitle |
| 49 |
|
| 50 |
This document describes the \textsf{geoplot} addon package for gretl, |
| 51 |
introduced in version 2020c. We have found it preferable, for reasons |
| 52 |
of efficiency, to implement its core mapping functionality via |
| 53 |
built-in functions, coded in C. For basic usage, therefore, it's not |
| 54 |
necessary to load the package explicitly via ``\texttt{include |
| 55 |
geoplot.gfn}'', as one would usually expect for an addon. |
| 56 |
|
| 57 |
Section~\ref{sec:quick} in this document is meant to give the reader a |
| 58 |
quick introduction to the main cartographic features we offer. In |
| 59 |
order to produce more refined maps, however, we provide more |
| 60 |
specialized facilities that are explained in the rest of the document. |
| 61 |
|
| 62 |
Section~\ref{sec:prelim} below covers some preliminaries which we hope |
| 63 |
will help the reader understand the basics of cartography via |
| 64 |
gretl. Section~\ref{sec:workflow} describes the basic workflow for |
| 65 |
producing a map image. Section~\ref{sec:example} then provides a |
| 66 |
simple worked example and section~\ref{sec:pairing} addresses a |
| 67 |
potential stumbling block. Section~\ref{sec:opts} goes over in detail |
| 68 |
the options to the core \texttt{geoplot} function; |
| 69 |
section~\ref{sec:qualitative} discusses some spcifics of plotting |
| 70 |
qualitative data; section~\ref{sec:gui} tells you what's available via |
| 71 |
gretl's graphical interface; and section~\ref{sec:expert} explains |
| 72 |
some ``expert'' refinements. Three appendices cover some technical |
| 73 |
points that may be of interest to expert users. |
| 74 |
|
| 75 |
\section{Quick start} |
| 76 |
\label{sec:quick} |
| 77 |
|
| 78 |
The sample file \texttt{us2020.geojson} contains the main results of |
| 79 |
the 2020 presidential elections in the US. If you open it via the |
| 80 |
command |
| 81 |
\begin{code} |
| 82 |
open us2020.geojson --frompkg=geoplot |
| 83 |
\end{code} |
| 84 |
you will see a dataset that looks very much like an ordinary gretl |
| 85 |
dataset: |
| 86 |
\begin{center} |
| 87 |
\begin{tabular}{rrrrrr} |
| 88 |
\hline |
| 89 |
& name & region & dem & biden & rep \\ |
| 90 |
\hline |
| 91 |
1 & Maryland & South & 0.6536 & 1 & 0.3215 \\ |
| 92 |
2 & Minnesota & Midwest & 0.5240 & 1 & 0.4528 \\ |
| 93 |
3 & Montana & West & 0.4055 & 0 & 0.5692 \\ |
| 94 |
4 & North Dakota & Midwest & 0.3178 & 0 & 0.6512 \\ |
| 95 |
5 & Hawaii & West & 0.6373 & 1 & 0.3427 \\ |
| 96 |
6 & Idaho & West & 0.3307 & 0 & 0.6384 \\ |
| 97 |
% 7 & Washington & West & 0.5797 & 1 & 0.3877 \\ |
| 98 |
% 8 & Arizona & West & 0.4936 & 1 & 0.4906 \\ |
| 99 |
% 9 & California & West & 0.6348 & 1 & 0.3432 \\ |
| 100 |
\multicolumn{6}{c}{\vdots} \\ |
| 101 |
\hline |
| 102 |
\end{tabular} |
| 103 |
\end{center} |
| 104 |
|
| 105 |
In fact this dataset is special, since it also contains cartographic |
| 106 |
information ``in the background''. The question of how to create such |
| 107 |
a file will be dealt with in Sections~\ref{sec:prelim} and |
| 108 |
\ref{sec:workflow}, but for the moment let's just skip this point. |
| 109 |
|
| 110 |
To plot a thematic map for the share of votes received by the |
| 111 |
Republicans in each state, you could simply open a console and type |
| 112 |
\begin{code} |
| 113 |
geoplot(rep) |
| 114 |
\end{code} |
| 115 |
and a plot like the one shown in Figure~\ref{fig:us2020-rep} |
| 116 |
should appear on your screen. Achieving the same via the GUI is quite |
| 117 |
easy; we'll deal with this in Section~\ref{sec:gui}. |
| 118 |
|
| 119 |
\begin{figure}[htbp] |
| 120 |
\centering |
| 121 |
\includegraphics[scale=0.75]{us2020-rep} |
| 122 |
\caption{Output of \texttt{geoplot(rep)}} |
| 123 |
\label{fig:us2020-rep} |
| 124 |
\end{figure} |
| 125 |
|
| 126 |
The interpretation of the map in Figure~\ref{fig:us2020-rep} should be |
| 127 |
obvious, with ``hotter'' colors for the states where the Republican |
| 128 |
party enjoyed more support, as shown by the legend to the right of the |
| 129 |
map. If we try the same with the \texttt{region} variable via |
| 130 |
\begin{code} |
| 131 |
geoplot(region) |
| 132 |
\end{code} |
| 133 |
we get a map similar to Figure~\ref{fig:us2020-reg}. In this case the |
| 134 |
colors are the same for states in the same US region: note that the |
| 135 |
format of the legend has also changed appropriately. The difference is |
| 136 |
due to the fact that \texttt{rep} is a quantitative variable, while |
| 137 |
\texttt{region} is qualitative (and string-valued). Gretl is aware of |
| 138 |
the difference and plots the map with the appropriate adjustments. A |
| 139 |
similar example that you can try by yourself is plotting a map for the |
| 140 |
binary variable \texttt{biden}. More on this in |
| 141 |
Section~\ref{sec:qualitative}. |
| 142 |
|
| 143 |
\begin{figure}[htbp] |
| 144 |
\centering |
| 145 |
\includegraphics[scale=0.75]{us2020-reg} |
| 146 |
\caption{Output of \texttt{geoplot(region)}} |
| 147 |
\label{fig:us2020-reg} |
| 148 |
\end{figure} |
| 149 |
|
| 150 |
\begin{figure}[htbp] |
| 151 |
\centering |
| 152 |
\includegraphics[scale=0.75]{us2020-dem} |
| 153 |
\caption{Output of \dtk{geoplot(dem, _(palette="blues"))}} |
| 154 |
\label{fig:us2020-dem} |
| 155 |
\end{figure} |
| 156 |
|
| 157 |
Many details of the maps---such as their color scheme, size, and so |
| 158 |
on---can be customized to the user's needs. To this aim, you can |
| 159 |
supply an options bundle to the \cmd{geoplot} function to inflect |
| 160 |
its behavior. For example, the line |
| 161 |
\begin{code} |
| 162 |
geoplot(dem, _(palette="blues")) |
| 163 |
\end{code} |
| 164 |
produces something like Figure~\ref{fig:us2020-dem}. In this case, we |
| 165 |
use the \texttt{palette} option to represent the degree of Democratic |
| 166 |
support by shades of blue. The available options and their meaning are |
| 167 |
explained in section~\ref{sec:opts}. |
| 168 |
|
| 169 |
And now, let's take a deeper look under the hood. |
| 170 |
|
| 171 |
\section{Preliminaries} |
| 172 |
\label{sec:prelim} |
| 173 |
|
| 174 |
Given gretl's user base and vocation, we assume that people will be |
| 175 |
primarily interested in ``thematic'' maps, in which geographical areas |
| 176 |
get different colors according to some variable of interest (for |
| 177 |
example regions of a country are colored according to their |
| 178 |
unemployment rates). Fancier maps are out of scope for the |
| 179 |
present. From here on we'll simply refer to maps of this sort as |
| 180 |
``maps'' and the geographical entities they contain (which may be |
| 181 |
countries, states, counties, l\"ander or whatever) as |
| 182 |
``regions''. Plotting a map typically involves drawing a number of |
| 183 |
polygons, filled with appropriate colors, to some device (the screen, |
| 184 |
or a file). |
| 185 |
|
| 186 |
The essential ingredients for doing this are |
| 187 |
\begin{enumerate} |
| 188 |
\item A geometrical description of the regions. |
| 189 |
\item The data for coloring the polygons. |
| 190 |
\item Appropriate software for producing the map. |
| 191 |
\end{enumerate} |
| 192 |
|
| 193 |
\subsection{The geometry} |
| 194 |
\label{sec:geometry} |
| 195 |
|
| 196 |
Let's say we have $n$ regions, indexed by $i$. Region $i$ is |
| 197 |
represented geometrically as a collection of $k_i$ polygons (think |
| 198 |
islands in an archipelago), indexed by $j$. Each polygon is defined by |
| 199 |
$h_{i,j}$ coordinates. Typically, each coordinate vector has two |
| 200 |
elements, latitude and longitude. |
| 201 |
|
| 202 |
The information on each region has two components: |
| 203 |
\begin{description} |
| 204 |
\item[Metadata] At minimum this should include the region's |
| 205 |
identifier(s), as strings and/or numerical codes. Other information, |
| 206 |
such as land area, may also be included. You can think of this as a |
| 207 |
dataset with $n$ observations and several variables, possibly |
| 208 |
string-valued. |
| 209 |
\item[Polygons] A representation of the region's shape on the map, in |
| 210 |
the form of one or more polygons, each taking the form of an array |
| 211 |
of X--Y pairs, typically latitude and longitude. You can think of |
| 212 |
this as an array of arrays of 2-column matrices: the outer array is |
| 213 |
of size $n$; inner array $i$ contains $k_i$ matrices, each with two |
| 214 |
columns. |
| 215 |
\end{description} |
| 216 |
|
| 217 |
Several file formats can be used for storing the geometry |
| 218 |
information.\footnote{The site |
| 219 |
\url{https://ec.europa.eu/eurostat/web/gisco/geodata/reference-data/administrative-units-statistical-units/nuts} |
| 220 |
offers a nice collection for European NUTS regions (NUTS = |
| 221 |
Nomenclature of Territorial Units for Statistics).} |
| 222 |
Gretl supports what are probably the two most common formats: |
| 223 |
\begin{itemize} |
| 224 |
\item GeoJSON files: these are plain JSON files with an internal |
| 225 |
structure specified by RFC 7946.\footnote{See |
| 226 |
\url{https://datatracker.ietf.org/doc/html/rfc7946}.} Such a file takes |
| 227 |
the form of an array of regions (or ``features''), with each element |
| 228 |
containing the metadata under the key \texttt{properties} and the |
| 229 |
polygons under the key \texttt{geometry}. |
| 230 |
\item ESRI shapefiles: these come as collections of several files, |
| 231 |
usually zipped together. The essential components are an |
| 232 |
\textsf{xBase} file, with \texttt{dbf} extension, holding the |
| 233 |
metadata; the shapefile proper with extension \texttt{shp}, holding |
| 234 |
the polygons; and an index file with extension \texttt{shx}, used to |
| 235 |
speed up operations when reading the data. The website |
| 236 |
\url{https://gadm.org/} offers a huge collection of such files. |
| 237 |
\end{itemize} |
| 238 |
|
| 239 |
\subsection{The payload data} |
| 240 |
\label{sec:payload} |
| 241 |
|
| 242 |
By ``payload'' we mean the data used for coloring the regions. We |
| 243 |
assume that the payload is available as a gretl series. This typically |
| 244 |
means that the user has a data file (in native \texttt{gdt} format or |
| 245 |
some other format gretl can read) in which each line represents a |
| 246 |
region, as illustrated in Table~\ref{tab:payload}. Note in particular |
| 247 |
the ``id'' column. We assume that the map metadata contain sufficient |
| 248 |
information to establish a correspondence with the dataset containing |
| 249 |
the payload: either a numerical code or a suitable string-valued |
| 250 |
variable. One thing one quickly learns in exploring a variety of |
| 251 |
\texttt{geojson} and \texttt{shp} files is that there's no telling how |
| 252 |
the regions will be ordered; one \textit{cannot} assume that they |
| 253 |
occur in what one might think of as ``standard'' order (e.g.\ |
| 254 |
alphabetical order for US states). |
| 255 |
|
| 256 |
\begin{table}[htbp] |
| 257 |
\begin{center} |
| 258 |
\begin{tabular}{llrr} |
| 259 |
%\hline |
| 260 |
id & State & Pop2019 & Pop2010 \\ |
| 261 |
\hline \\ [-1.75ex] |
| 262 |
1 & Alabama & 4903185 & 4779736 \\ |
| 263 |
2 & Alaska & 731545 & 710231 \\ |
| 264 |
101 & American Samoa & 55641 & 55519 \\ |
| 265 |
3 & Arizona & 6278717 & 6392017 \\ |
| 266 |
4 & Arkansas & 3017825 & 2915918 \\ |
| 267 |
5 & California & 39512223 & 37254523 \\ |
| 268 |
6 & Colorado & 5758736 & 5029196 \\ |
| 269 |
7 & Connecticut & 3565287 & 3574097 \\ |
| 270 |
& \vdots & \\ |
| 271 |
%\hline |
| 272 |
\end{tabular} |
| 273 |
\end{center} |
| 274 |
\caption{Typical format for a payload dataset} |
| 275 |
\label{tab:payload} |
| 276 |
\end{table} |
| 277 |
|
| 278 |
\subsection{The software backend} |
| 279 |
\label{sec:software} |
| 280 |
|
| 281 |
In principle one could represent maps using any one of the many |
| 282 |
plotting libraries available, but gretl uses \textsf{gnuplot}, which |
| 283 |
we already use for all other kinds of plot. Some insight into the |
| 284 |
\textsf{gnuplot} commands we use can be gained from \ref{sec:gnuplot}. |
| 285 |
|
| 286 |
Given the geometry data and the payload, writing a \textsf{gnuplot} |
| 287 |
script for producing the map is straightforward. And in most cases |
| 288 |
\textsf{gnuplot} can produce a plot in short order. You might have to |
| 289 |
wait a little if there are many regions, of complex shapes, |
| 290 |
represented in high precision in the source map file. |
| 291 |
|
| 292 |
\section{The workflow} |
| 293 |
\label{sec:workflow} |
| 294 |
|
| 295 |
Typical workflow for producing a thematic map in gretl is likely to be |
| 296 |
as follows. |
| 297 |
|
| 298 |
\begin{enumerate} |
| 299 |
\item You \cmd{open} the map-datafile as a gretl dataset; this reads |
| 300 |
in the metadata so gretl's \dollar{nobs} will be equal to the number |
| 301 |
of regions, $n$.\label{item:open} |
| 302 |
\item You add the payload data, via the \texttt{append} or |
| 303 |
\texttt{join} commands or in some other way.\label{item:join} |
| 304 |
\item You decide on some details of your map (appearance, format, |
| 305 |
etc.), with sensible defaults being available.\label{item:look} |
| 306 |
\item You create the map.\label{item:map} |
| 307 |
\item Optionally, you can save the data in gretl's native \texttt{gdt} |
| 308 |
format (but see below).\label{item:save} |
| 309 |
\end{enumerate} |
| 310 |
|
| 311 |
Point \ref{item:open} is handled by using the \texttt{open} command on |
| 312 |
the map-datafile. The filename extensions recognized for this purpose |
| 313 |
are \texttt{json} or \texttt{geojson} for GeoJSON files, and |
| 314 |
\texttt{dbf} or \texttt{shp} for ESRI shapefiles.\footnote{In |
| 315 |
principle we could read the polygons at this point and store them in |
| 316 |
RAM, but for now we don't. We just read in the metadata, but store |
| 317 |
the path to the associated geometry file internally.} |
| 318 |
Point~\ref{item:join} is also handled by standard gretl commands. |
| 319 |
|
| 320 |
Points \ref{item:look} and \ref{item:map} are handled by the |
| 321 |
\texttt{geoplot} function, which can be called in either of two ways, |
| 322 |
corresponding to these two signatures: |
| 323 |
\begin{code} |
| 324 |
function void geoplot(const series payload[null], const bundle options[null]) |
| 325 |
|
| 326 |
function void geoplot(string mapfile, const series payload[null], |
| 327 |
const bundle options[null]) |
| 328 |
\end{code} |
| 329 |
The first case is applicable if the map to be shown has already been |
| 330 |
loaded as a gretl dataset. The second case is required if you want to |
| 331 |
use a map file which has not been so loaded: then you need to give its |
| 332 |
name. The \texttt{payload} argument is the (optional) series with |
| 333 |
which to colorize the polygons, and \texttt{options} is an (optional) |
| 334 |
bundle to contain one or more elements governing the appearance or |
| 335 |
destination of the plot. |
| 336 |
\begin{itemize} |
| 337 |
\item If the \texttt{payload} argument is given as \texttt{null} or |
| 338 |
omitted then the map is drawn ``as is'', without any |
| 339 |
colorization. This can be useful if you just want to see what the |
| 340 |
polygons look like. |
| 341 |
\item If the \texttt{options} bundle is omitted all options are set to |
| 342 |
their default values---which means, among other things, that you see |
| 343 |
the map on screen but nothing is saved. For full details on the |
| 344 |
available options see section~\ref{sec:opts}. |
| 345 |
\end{itemize} |
| 346 |
|
| 347 |
Note that once a map is loaded as a dataset you can retrieve its |
| 348 |
filename using the \verb|$mapfile| accessor. This is convenient if you |
| 349 |
wish to read the full content of the file (including the polygons) as |
| 350 |
a bundle: |
| 351 |
\begin{code} |
| 352 |
bundle b = bread($mapfile) |
| 353 |
\end{code} |
| 354 |
%$ |
| 355 |
Finally, a cautionary note on point~\ref{item:save} above: the saved |
| 356 |
\texttt{gdt} file will \emph{not} contain the geometry information, |
| 357 |
but rather a pointer to the appropriate file (GeoJSON or |
| 358 |
shapefile). As a consequence, as long as the file holding the polygon |
| 359 |
info remains in the same place, you can simply re-open your |
| 360 |
\texttt{gdt} file and create new maps. However, you can't send it to |
| 361 |
someone else and expect it to work for map creation, unless you also |
| 362 |
supply them with the original geometry file. |
| 363 |
|
| 364 |
\section{An example} |
| 365 |
\label{sec:example} |
| 366 |
|
| 367 |
For this example we'll produce a map showing GDP per capita of the six |
| 368 |
founder countries of the EU, using three files: script |
| 369 |
\texttt{founders.inp}, data file \texttt{founders.csv} and map |
| 370 |
\texttt{founders.geojson}. In this case the required files are small |
| 371 |
enough to be readily inspected by hand. The content of |
| 372 |
\texttt{founders.csv}, which holds what will be the payload, is shown |
| 373 |
in Listing~\ref{tab:founders-csv}. |
| 374 |
|
| 375 |
\begin{script}[htbp] |
| 376 |
\begin{scode} |
| 377 |
Name,code,pop,area,gdp |
| 378 |
Belgium,BE,11365834,30528,534230 |
| 379 |
France,FR,67024633,632833,2833687 |
| 380 |
Germany,DE,82437641,357386,3874437 |
| 381 |
Italy,IT,61219113,301338,2147744 |
| 382 |
Luxembourg,LU,589370,2586.4,65683 |
| 383 |
Netherlands,NL,17220721,41543,880716 |
| 384 |
\end{scode} |
| 385 |
\caption{Content of \texttt{founders.csv}} |
| 386 |
\label{tab:founders-csv} |
| 387 |
\end{script} |
| 388 |
|
| 389 |
\begin{script}[htbp] |
| 390 |
\begin{scode} |
| 391 |
{"type": "FeatureCollection", "features": [ |
| 392 |
{"geometry": {"type": "Polygon", "coordinates": [[[40.40360, |
| 393 |
30.79039], [40.59686, 30.49366], [40.65087, 30.29746], ... ]]}, |
| 394 |
"type": "Feature", "properties": {"CNTR_NAME": "Belgique", |
| 395 |
"ISO3_CODE": "BEL", "CNTR_ID": "BE", "NAME_ENGL": "Belgium", |
| 396 |
"FID": "BE"}, "id": "BE"}, |
| 397 |
{"geometry": {"type": "MultiPolygon", "coordinates": [[[[40.18497, |
| 398 |
29.45664], [40.23634, 29.39875], [40.57754, 29.35021], ...], |
| 399 |
[[[42.66689, 20.70300], [42.57348, 20.41660], ...]]}, |
| 400 |
"type": "Feature", "properties": {"CNTR_NAME": "France", |
| 401 |
"ISO3_CODE": "FRA", "CNTR_ID": "FR", "NAME_ENGL": "France", |
| 402 |
"FID": "FR"}, "id": "FR"}, |
| 403 |
... |
| 404 |
\end{scode} |
| 405 |
\caption{Excerpt of \texttt{founders.geojson}} |
| 406 |
\label{tab:json-xrpt} |
| 407 |
\end{script} |
| 408 |
|
| 409 |
The JSON file is too big to show here in full but small enough to |
| 410 |
examine in any text editor; listing~\ref{tab:json-xrpt} contains a |
| 411 |
representative excerpt. Note that while Belgium is a single polygon |
| 412 |
France is an array of polygons (``\texttt{MultiPolygon}''), because of |
| 413 |
Corsica. |
| 414 |
|
| 415 |
\begin{script}[htbp] |
| 416 |
\begin{scode} |
| 417 |
open founders.geojson --frompkg=geoplot |
| 418 |
|
| 419 |
join founders.csv gdp pop --ikey=FID --okey=code |
| 420 |
series gdppc = 1000*gdp/pop |
| 421 |
|
| 422 |
opts = _(plotfile = "GDPpc.plt", inlined = 1) |
| 423 |
geoplot(gdppc, opts) |
| 424 |
\end{scode} |
| 425 |
%$ |
| 426 |
\caption{Content of \texttt{founders.inp}} |
| 427 |
\label{tab:founders-script} |
| 428 |
\end{script} |
| 429 |
|
| 430 |
The \texttt{founders.inp} script is shown in |
| 431 |
Listing~\ref{tab:founders-script}, and what we get after opening the |
| 432 |
\texttt{geojson} file in gretl is in Listing~\ref{tab:founders-meta}. |
| 433 |
|
| 434 |
\begin{script}[htbp] |
| 435 |
\begin{scode} |
| 436 |
CNTR_NAME ISO3_CODE CNTR_ID NAME_ENGL FID |
| 437 |
|
| 438 |
1 Belgique BEL BE Belgium BE |
| 439 |
2 France FRA FR France FR |
| 440 |
3 Deutschland DEU DE Germany DE |
| 441 |
4 Italia ITA IT Italy IT |
| 442 |
5 Luxemburg LUX LU Luxembourg LU |
| 443 |
6 Nederland NLD NL Netherlands NL |
| 444 |
\end{scode} |
| 445 |
\caption{The ``founders'' metadata} |
| 446 |
\label{tab:founders-meta} |
| 447 |
\end{script} |
| 448 |
|
| 449 |
Next, we perform a \cmd{join} operation with \texttt{FID} (from the |
| 450 |
JSON file) as the inner key and \texttt{code} (from the CSV file) as |
| 451 |
the outer key. Finally, we call the \cmd{geoplot} function to create |
| 452 |
an on-screen map. We specify the \texttt{payload} to colorize and via |
| 453 |
the \texttt{options} argument we add a couple of points: the |
| 454 |
\textsf{gnuplot} input file should be saved under the name |
| 455 |
\texttt{GDPpc.plt}, and the geometry data should be ``inlined'' in |
| 456 |
this file, making it self-contained. |
| 457 |
|
| 458 |
Running the script will produce a \textsf{gnuplot} file resembling the |
| 459 |
following: |
| 460 |
|
| 461 |
\begin{code} |
| 462 |
set term wxt persist |
| 463 |
|
| 464 |
unset key |
| 465 |
set cbrange [33.3288:117.018] |
| 466 |
set xrange [31.7826:51.4213] |
| 467 |
set yrange [14.7701:36.0553] |
| 468 |
$MapData << EOD |
| 469 |
40.4036 30.79039 47.00315 |
| 470 |
40.59686 30.49366 47.00315 |
| 471 |
|
| 472 |
[...] |
| 473 |
|
| 474 |
38.92268 31.40258 51.142806 |
| 475 |
38.59809 31.50169 51.142806 |
| 476 |
38.59737 31.60855 51.142806 |
| 477 |
|
| 478 |
EOD |
| 479 |
plot for [i=0:*] $MapData index i with filledcurves fc palette, \ |
| 480 |
$MapData using 1:2 with lines lc "white" lw 1 |
| 481 |
\end{code} |
| 482 |
%$ |
| 483 |
|
| 484 |
and feeding the above into \textsf{gnuplot} yields the map shown in |
| 485 |
Figure~\ref{fig:founders}. |
| 486 |
|
| 487 |
\begin{figure}[htbp] |
| 488 |
\begin{center} |
| 489 |
\includegraphics[scale=0.9]{GDPpc.pdf} |
| 490 |
\end{center} |
| 491 |
\caption{Output of \texttt{founders.inp} (default gnuplot palette)} |
| 492 |
\label{fig:founders} |
| 493 |
\end{figure} |
| 494 |
|
| 495 |
\section{Ensuring correct alignment} |
| 496 |
\label{sec:pairing} |
| 497 |
|
| 498 |
In order to produce a correct map it is essential that everything be |
| 499 |
aligned properly: the map metadata, the payload series, and the |
| 500 |
geometries of the regions. ``Region $i$'' must have the same referent |
| 501 |
in all three contexts. |
| 502 |
|
| 503 |
If the source map (GeoJSON or shapefile) is not broken we can assume |
| 504 |
that the original metadata and the geometries are indeed aligned |
| 505 |
correctly. But problems may arise (a) in aligning the payload and (b) |
| 506 |
if one wishes to exclude some regions from the map. We discuss these |
| 507 |
issues in turn. |
| 508 |
|
| 509 |
\subsection{Aligning the payload} |
| 510 |
|
| 511 |
If the payload you wish to plot is already included in the map |
| 512 |
metadata, there's no problem. But when the map data and the payload |
| 513 |
come from different sources it may be tricky to get them aligned |
| 514 |
properly. This problem is not specific to the mapping apparatus---it's |
| 515 |
a more general issue concerning the matching of data from different |
| 516 |
sources, addressed at length in the chapter titled ``Joining data |
| 517 |
sources'' in the \GUG{}---but it may be helpful to offer a few |
| 518 |
comments here. |
| 519 |
|
| 520 |
As mentioned above, the relevant tools provided by gretl are |
| 521 |
\texttt{append} and \texttt{join}. A simple \texttt{append} will work |
| 522 |
only if the regions appear in the same order in the map and payload |
| 523 |
datasets. This is fairly easily checked if the number of regions is |
| 524 |
small and each dataset contains readily comparable |
| 525 |
identifiers. Otherwise---if the orders clearly differ or it's hard to |
| 526 |
tell---it will be necessary to use \texttt{join}. |
| 527 |
|
| 528 |
Look back at Listing~\ref{tab:founders-script}. In that case the map |
| 529 |
and payload datasets contained the same set of two-letter identifiers |
| 530 |
for the countries---albeit under different names, \texttt{FID} and |
| 531 |
\texttt{code}---so \texttt{join} using the \option{ikey} and |
| 532 |
\option{okey} options worked fine. In a different case, however, the |
| 533 |
respective identifiers may not match up. For example, region names |
| 534 |
might be in English in one dataset and in, say, Italian in the |
| 535 |
other. Then you'll have to exercise your intelligence, but one idea is |
| 536 |
to create an intermediate ``Rosetta stone'' file, maybe as CSV, giving |
| 537 |
the mapping between the two identifiers, as in: |
| 538 |
\begin{code} |
| 539 |
# rosetta.csv |
| 540 |
ID_en,ID_it |
| 541 |
Apulia,Puglia |
| 542 |
Sardinia,Sardegna |
| 543 |
... |
| 544 |
\end{code} |
| 545 |
Then you can \texttt{join} the Rosetta file to the map dataset, hence |
| 546 |
adding the required identifier that's initially lacking. |
| 547 |
|
| 548 |
\subsection{Sub-sampling} |
| 549 |
\label{sec:sub-sample} |
| 550 |
|
| 551 |
In some cases one may wish to leave out certain outlying regions. For |
| 552 |
example, it's quite common to produce thematic maps of the USA that |
| 553 |
omit Hawaii, and perhaps Alaska. In principle leaving out regions |
| 554 |
threatens to break the required alignment of payload and geometry, but |
| 555 |
this is handled as follows: if you sub-sample the map dataset using |
| 556 |
the \texttt{smpl} command, \texttt{geoplot} automatically drops the |
| 557 |
associated polygons from the plot. |
| 558 |
|
| 559 |
This is illustrated in Listing~\ref{tab:USA}. Figure~\ref{fig:USA} |
| 560 |
shows the results with and without exclusion of Alaska and |
| 561 |
Hawaii.\footnote{Given the role of this example we don't bother adding |
| 562 |
a real payload, but just simulate data using the \texttt{normal} |
| 563 |
function.} |
| 564 |
|
| 565 |
\begin{script}[p] |
| 566 |
\begin{scode} |
| 567 |
open us-states.geojson --quiet --frompkg=geoplot |
| 568 |
x = normal() # fake up some data! |
| 569 |
opts = _(plotfile = "us0.plt", palette = "blues") |
| 570 |
|
| 571 |
# show the entire USA |
| 572 |
opts.title = "USA (complete)" |
| 573 |
geoplot(x, opts) |
| 574 |
|
| 575 |
# skip Alaska and Hawaii |
| 576 |
smpl postal != "AK" && postal != "HI" --restrict |
| 577 |
opts.title = "USA (mainland)" |
| 578 |
opts.plotfile = "us1.plt" |
| 579 |
geoplot(x, opts) |
| 580 |
\end{scode} |
| 581 |
\caption{US maps, complete vs contiguous states} |
| 582 |
\label{tab:USA} |
| 583 |
\end{script} |
| 584 |
|
| 585 |
\begin{figure}[p] |
| 586 |
\begin{center} |
| 587 |
\includegraphics[scale=0.9]{us0.pdf} |
| 588 |
|
| 589 |
\includegraphics[scale=0.9]{us1.pdf} |
| 590 |
\end{center} |
| 591 |
\caption{Output of Listing \ref{tab:USA}} |
| 592 |
\label{fig:USA} |
| 593 |
\end{figure} |
| 594 |
|
| 595 |
A related case is where the payload value is missing (\texttt{NA}) for |
| 596 |
one or more regions. Here you have a choice. By default, regions whose |
| 597 |
payload is \texttt{NA} are shown in outline, not colored, but either |
| 598 |
of two alternatives can be selected by passing a string under the key |
| 599 |
\texttt{missvals} in the options bundle: if the value is \verb|"skip"| |
| 600 |
the affected regions are omitted; if it's \verb|"fill"| they are |
| 601 |
colored gray. (The value \verb|"outline"| may be given, confirming the |
| 602 |
default.) |
| 603 |
|
| 604 |
\section{Options for the \texttt{geoplot} function} |
| 605 |
\label{sec:opts} |
| 606 |
|
| 607 |
We first present all the currently supported options in alphabetical |
| 608 |
order. Below the listing we give some further explanation of the usage |
| 609 |
of \texttt{plotfile}, \texttt{show} and \texttt{inlined}. |
| 610 |
|
| 611 |
\begin{description} |
| 612 |
\item[\texttt{cbrange}:] a 2-vector, representing a \textsf{gnuplot} |
| 613 |
variable of the same name. This option is useful if you're creating |
| 614 |
two or more plots with comparable quantitative payloads and you wish |
| 615 |
to impose a common mapping from payload values to colors. To achieve |
| 616 |
this you should determine the minima and maxima of the payload |
| 617 |
across the plots and set the \texttt{cbrange} limits to accommodate |
| 618 |
all the data. The first (minimum) value should be no greater than |
| 619 |
the least payload value and the second (maximum) no less than the |
| 620 |
greatest payload value. Default: in general the payload to colors |
| 621 |
mapping differs across distinct plots, based on the per-plot data |
| 622 |
ranges. |
| 623 |
\item[\texttt{border}:] boolean, show a rectangular border around the |
| 624 |
map. Default: true. |
| 625 |
\item[\texttt{height}:] scalar, giving the height of the plot in |
| 626 |
pixels. Default: 600. Even if the desired output is a vector graphic |
| 627 |
(PDF or EPS) rather than a bitmap (see \texttt{plotfile} below), |
| 628 |
setting this value relative to the default can be used to adjust the |
| 629 |
size of the plot. For example \texttt{height = 400} will give a PDF |
| 630 |
graphic that's two-thirds of the default size. |
| 631 |
\item[\texttt{inlined}:] boolean, to have the polygon data written |
| 632 |
directly into the \textsf{gnuplot} file. Default: false, the data |
| 633 |
are read from a separate file. |
| 634 |
\item[\texttt{keypos}:] string, specifying the position of the key for |
| 635 |
discrete plot colors (relevant only when plotting qualitative data; |
| 636 |
see Section~\ref{sec:qualitative}). This string must be a valid |
| 637 |
argument to gnuplot's \cmd{set key} command. The most common usage |
| 638 |
takes the form of two words: \texttt{left}, \texttt{right} or |
| 639 |
\texttt{center} along with \texttt{top}, \texttt{bottom} or |
| 640 |
\texttt{center} (in either order). These words can be preceded by |
| 641 |
\texttt{outside} to place the key outside of the plot area. For |
| 642 |
example, \verb|keypos="bottom center"| or |
| 643 |
\verb|keypos="outside top left"|. The default position is bottom |
| 644 |
right. |
| 645 |
\item[\texttt{linecolor}:] string, naming the color in which to draw |
| 646 |
the borders of the regions. By default this is white if a payload is |
| 647 |
plotted, black if only outlines are shown. |
| 648 |
\item[\texttt{linewidth}:] scalar, giving the width of the lines |
| 649 |
representing the borders of the regions. Setting this to 0 |
| 650 |
suppresses those lines (unless no payload is supplied). Default: |
| 651 |
1.0. |
| 652 |
\item[\texttt{literal}:] string containing \textsf{gnuplot} commands, |
| 653 |
for insertion before the actual \texttt{plot} command. |
| 654 |
\item[\texttt{logscale}:] boolean, use log scale for the |
| 655 |
payload. Default: false. |
| 656 |
\item[\texttt{missvals}:] string, see section~\ref{sec:sub-sample}. |
| 657 |
\item[\texttt{plotfile}:] filename, allowing the user to direct |
| 658 |
output, either to a specified \textsf{gnuplot} command file or to a |
| 659 |
graphic file. If a command file is wanted the extension should be |
| 660 |
\texttt{plt}. If a graphic file is wanted you should give one of the |
| 661 |
following extensions: \texttt{png}, \texttt{pdf}, \texttt{eps}, |
| 662 |
\texttt{emf}, \texttt{svg} or \texttt{html}. If \texttt{plotfile} |
| 663 |
is given, but not as a full path, the file is written to the user's |
| 664 |
working directory. |
| 665 |
\item[\texttt{projection}:] string, see~\ref{app:proj}. |
| 666 |
\item[\texttt{palette}:] string, the exact specification of which |
| 667 |
depends on whether the payload data are quantitative or |
| 668 |
qualitative. For the qualitative case see |
| 669 |
Section~\ref{sec:qualitative}. In the quantitative case the string |
| 670 |
should give either a \textsf{gnuplot} \texttt{set palette} command |
| 671 |
or a predefined option, of which there are currently three: |
| 672 |
\texttt{blues}, \texttt{oranges} and \texttt{green-to-red}. For |
| 673 |
example, the syntax |
| 674 |
\begin{code} |
| 675 |
options.palette = "set palette defined (0 '#D4E4F2', 1 'steelblue')" |
| 676 |
\end{code} |
| 677 |
will give you a pleasing blue gradient---which also happens to be |
| 678 |
what you get by giving |
| 679 |
\begin{code} |
| 680 |
options.palette = "blues" |
| 681 |
\end{code} |
| 682 |
If no such string is given you get the default built-in |
| 683 |
\textsf{gnuplot} palette. |
| 684 |
\item[\texttt{show}:] boolean, should the plot should be shown |
| 685 |
on-screen right away? Default: true. (Otherwise a file of some sort |
| 686 |
is written but not displayed---see \texttt{plotfile} above.) |
| 687 |
\item[\texttt{tics}:] boolean, for turning on the printing of X |
| 688 |
(longitude) and Y (latitude) tics. Default: tics are suppressed, |
| 689 |
unless \texttt{geoplot} is invoked without any payload. |
| 690 |
\item[\texttt{title}:] string specifying a title for the |
| 691 |
plot. Default: no title. |
| 692 |
\item[\texttt{xrange}:] 2-element vector, containing the longitude |
| 693 |
range used on the plot (see \ref{sec:xyranges}). Default: |
| 694 |
automatically determined. |
| 695 |
\item[\texttt{yrange}:] 2-element vector, containing the latitude |
| 696 |
range used on the plot (see \ref{sec:xyranges}). Default: |
| 697 |
automatically determined. |
| 698 |
\end{description} |
| 699 |
|
| 700 |
It may be helpful to run through various \texttt{geoplot} scenarios |
| 701 |
with an eye to usage of the options. |
| 702 |
|
| 703 |
\begin{enumerate} |
| 704 |
\item You just want to see the map on-screen. Then don't give |
| 705 |
\texttt{plotfile} (or set it to \texttt{null}) and accept the |
| 706 |
default of \texttt{show} = 1.\label{just-see} |
| 707 |
\item You want to see the map on-screen but also save the plot command |
| 708 |
file that generated it (maybe you want to edit the commands or pass |
| 709 |
them to \textsf{gnuplot} independently of gretl). Then specify |
| 710 |
\texttt{plotfile} with a \texttt{plt} extension.\label{see-and-save} |
| 711 |
\item As in case 2 but you don't care to see the map on-screen: add |
| 712 |
\texttt{show} = 0. |
| 713 |
\item You want to generate a graphic file (maybe for inclusion in a |
| 714 |
document or web page). Then give \texttt{plotfile} with one of the |
| 715 |
recognized graphic format extensions. In this case \texttt{show} is |
| 716 |
automatically turned off. |
| 717 |
\end{enumerate} |
| 718 |
|
| 719 |
Note that if you set \texttt{show} to 0 and do \textit{not} specify |
| 720 |
\texttt{plotfile} that is tantamount to saying ``Don't do anything!'', |
| 721 |
which is regarded as an error. |
| 722 |
|
| 723 |
\subsection{Inlined data or not?} |
| 724 |
|
| 725 |
As mentioned above, \texttt{geoplot} can pass the map coordinates to |
| 726 |
\textsf{gnuplot} either by writing them directly into the plot command |
| 727 |
file (\texttt{inlined} = 1), or by writing them to a separate data |
| 728 |
file whose name is recorded in the command file (\texttt{inlined} = |
| 729 |
0). |
| 730 |
|
| 731 |
From the user's point of view, this distinction makes a difference |
| 732 |
only if you're saving the plot command file (as in |
| 733 |
scenario~\ref{just-see} or \ref{see-and-save} above). In that |
| 734 |
context, the advantage of having the data inlined is that, being all |
| 735 |
in one file, the information required to generate a map cannot easily |
| 736 |
become ``unstuck''. The disadvantage is that the command file may be |
| 737 |
very large, and perhaps not so easy to edit; besides the actual |
| 738 |
\textsf{gnuplot} commands it may contain many thousands of lines of |
| 739 |
coordinates data (which in general \textit{should not be touched}, on |
| 740 |
pain of breaking the map). At present \texttt{inlined} is set to |
| 741 |
0 by default but that may change; we recommend making an explicit |
| 742 |
choice via the \texttt{options} bundle. |
| 743 |
|
| 744 |
Note that if you specify \texttt{plotfile} as a \textsf{gnuplot} |
| 745 |
command file, but not \texttt{inlined}, you'll get two output files: |
| 746 |
the specified \texttt{plt} file plus a data file named by adding the |
| 747 |
extension \texttt{dat}. For example you might get \texttt{mygeo.plt} |
| 748 |
and \texttt{mygeo.plt.dat}, while with \texttt{inlined} = 1 you'd just |
| 749 |
get a big \texttt{mygeo.plt}. |
| 750 |
|
| 751 |
\section{Quantitative versus qualitative payloads} |
| 752 |
\label{sec:qualitative} |
| 753 |
|
| 754 |
In the foregoing we have assumed that if a payload is supplied the |
| 755 |
data are quantitative---either continuous, or if discrete then at |
| 756 |
least ordinal. However, one may wish to plot qualitative (categorical) |
| 757 |
data, and that calls for a different sort of color palette. In the |
| 758 |
quantitative case one probably wants a graduated scheme---either |
| 759 |
shades of a single hue or perhaps a ``heat map'' comprising more than |
| 760 |
one hue. If the data are categorical, clearly distinct colors are |
| 761 |
likely wanted. |
| 762 |
|
| 763 |
How can \texttt{geoplot} tell the difference? Well, in a gretl dataset |
| 764 |
a categorical variable is likely represented by a string-valued |
| 765 |
series, but may take the form of a numeric series where the numbers |
| 766 |
are a pure encoding with no quantitative significance. String-valued |
| 767 |
series (and also 0/1 dummy series) will be recognized by |
| 768 |
\texttt{geoplot} as qualitative automatically, but otherwise a numeric |
| 769 |
series called \texttt{cvals} which serves as an encoding should be |
| 770 |
marked as such, using the command |
| 771 |
\begin{code} |
| 772 |
setinfo cvals --coded |
| 773 |
\end{code} |
| 774 |
If a payload series is recognized as qualitative with $k$ values, the |
| 775 |
default \texttt{geoplot} palette is not the gnuplot default (which is |
| 776 |
of the heat-map type) but rather an automatic selection of $k$ |
| 777 |
distinct colors. And the values accepted under the \texttt{palette} |
| 778 |
key in the \texttt{options} bundle are simpler than in the |
| 779 |
quantitative case. We expect either the name of an array of $k$ |
| 780 |
strings, each of which contains a color specification comprehensible |
| 781 |
by \textsf{gnuplot}, or the keyword \texttt{auto} (which just confirms |
| 782 |
automatic color selection). Here's an example, suitable for a binary |
| 783 |
payload variable ($k=2$): |
| 784 |
\begin{code} |
| 785 |
strings mycolors = defarray("#D22532", "#244999") |
| 786 |
opts.palette = "mycolors" |
| 787 |
\end{code} |
| 788 |
In addition a second, comma-separated element can be given when the |
| 789 |
payload data are qualitative, namely an array of strings to appear in |
| 790 |
the ``key'' or legend for the plot. If the payload series is |
| 791 |
string-valued its strings will be used by default for this purpose, |
| 792 |
but you may wish to supply abbreviated or translated strings. This |
| 793 |
facility can also be used to give suitable strings for a 0/1 binary |
| 794 |
series (which cannot have string values since that requires a minimum |
| 795 |
numeric value of 1 or greater). Extending the example above, we might |
| 796 |
do: |
| 797 |
\begin{code} |
| 798 |
strings mycolors = defarray("#D22532", "#244999") |
| 799 |
strings zlabels = defarray("Republican", "Democratic") |
| 800 |
opts.palette = "mycolors,zlabels" |
| 801 |
\end{code} |
| 802 |
The position of the legend can be adjusted by using the \cmd{keypos} |
| 803 |
option (see Section \ref{sec:opts}). |
| 804 |
|
| 805 |
Examples of maps showing qualitative data are provided by the scripts |
| 806 |
\texttt{swiss-langs.inp} and \texttt{us-2020.inp}. You can find these |
| 807 |
scripts and others via the item \textsf{Resource from addon} under the |
| 808 |
\textsf{File} menu in the gretl GUI. Figure~\ref{fig:swiss} shows two |
| 809 |
variants of a map representing the main language of each Swiss canton |
| 810 |
(via a string-valued series named \texttt{mainlang}). The |
| 811 |
customizations in the second map are achieved via |
| 812 |
\begin{code} |
| 813 |
strings langcolors = defarray("#E87E7E", "#9DA8E0", "#85E1C3", "#E1C385") |
| 814 |
strings zlabels = defarray("Deutsch", "Français", "Rumantsch", "Italiano") |
| 815 |
opts.palette = "langcolors,zlabels" |
| 816 |
\end{code} |
| 817 |
Give a palette string of (e.g.) \verb|"auto,zlabels"| if you want to |
| 818 |
adjust the strings but use the default colors. |
| 819 |
|
| 820 |
\begin{figure}[htbp] |
| 821 |
\begin{center} |
| 822 |
\includegraphics[scale=0.7]{swiss_langs1.pdf} |
| 823 |
|
| 824 |
\includegraphics[scale=0.7]{swiss_langs2.pdf} |
| 825 |
\end{center} |
| 826 |
\caption{Main language per Swiss canton: the upper plot uses the |
| 827 |
default palette and strings; the lower one uses a customized palette |
| 828 |
and translated strings.} |
| 829 |
\label{fig:swiss} |
| 830 |
\end{figure} |
| 831 |
|
| 832 |
|
| 833 |
\section{Maps via the GUI} |
| 834 |
\label{sec:gui} |
| 835 |
|
| 836 |
To this point we have referred exclusively to executing commands and |
| 837 |
calling functions. You can, of course, execute commands and call |
| 838 |
functions in the GUI program via script or via the gretl console, but |
| 839 |
what about point-and-click? Well, there is a certain amount you can do |
| 840 |
in that way. |
| 841 |
|
| 842 |
First, you can open a shapefile or GeoJSON file using the menu item |
| 843 |
\textsf{/File/Open data/User file}. In the bottom right-hand corner of |
| 844 |
the ``open file'' dialog, use the pull-down list to select |
| 845 |
``Shapefiles'' or ``GeoJSON files''. You can also drag-and-drop such |
| 846 |
files onto the main gretl window to the same effect. |
| 847 |
|
| 848 |
\begin{figure}[htbp] |
| 849 |
\begin{center} |
| 850 |
\includegraphics[width=0.333\textwidth]{geoplot_GUI.png} |
| 851 |
\end{center} |
| 852 |
\caption{Graphical interface to the \cmd{geoplot} command.} |
| 853 |
\label{fig:gui} |
| 854 |
\end{figure} |
| 855 |
Once map metadata are loaded in this way, the option \textsf{Display |
| 856 |
map} becomes available via the context (``right-click'') menu in the |
| 857 |
main window, and also under \textsf{/View/Graph specified vars}; |
| 858 |
it will trigger a window like the one in Figure~\ref{fig:gui}. |
| 859 |
|
| 860 |
If the current dataset contains one or more series that seem to gretl to |
| 861 |
be plausible ``payload'' variables,\footnote{Admittedly, the heuristic |
| 862 |
employed for this purpose is not terribly clever.} invoking |
| 863 |
\textsf{Display map} will produce a dialog box that allows you to |
| 864 |
select one, in which case you'll get a choice of color palette to |
| 865 |
represent it. Otherwise you just get to view the map outlines. The |
| 866 |
options \texttt{border}, \texttt{linewidth}, \texttt{logscale} and |
| 867 |
\texttt{height} (see Section~\ref{sec:opts}) can also be selected via |
| 868 |
the GUI. |
| 869 |
|
| 870 |
Further, the plot window shown by \textsf{Display map} offers a |
| 871 |
right-click menu that allows saving the map (as PDF, EPS, PNG or EMF), |
| 872 |
copying it to the clipboard, or saving it ``as an icon''. |
| 873 |
|
| 874 |
Note that the options offered by the GUI are, at the moment, quite |
| 875 |
limited compared to the ones available via scripting. We may at some |
| 876 |
point expand the \textsf{Display map} dialog to offer more of the |
| 877 |
choices available via the \texttt{options} argument to the |
| 878 |
\texttt{geoplot} function. |
| 879 |
|
| 880 |
\section{``Expert'' refinements} |
| 881 |
\label{sec:expert} |
| 882 |
|
| 883 |
To recap, we have explained how map metadata can be brought in via the |
| 884 |
\texttt{open} command (or via the GUI); how to add ``payload'' data; |
| 885 |
and how to generate a map using the \texttt{geoplot} function. So far |
| 886 |
so good, but that leaves open some questions that might occur to |
| 887 |
ambitious users. Can I open a map file, add a payload series, and |
| 888 |
save a modified version of the map file including the payload? And in |
| 889 |
relation to a map of the USA (for example), is there a way to include |
| 890 |
Alaska and Hawaii, but ``tuck them underneath'' the continental US, |
| 891 |
like I see in graphics on the web? |
| 892 |
|
| 893 |
Short answer: Yes. You can import a GeoJSON file (or shapefile) as a |
| 894 |
gretl ``bundle'' by means of the \texttt{bread} function; make changes |
| 895 |
to the bundle; then save it as GeoJSON using \texttt{bwrite}. |
| 896 |
|
| 897 |
To get control over this it's necessary to understand the structure of |
| 898 |
the bundle that \texttt{bread} produces when fed map input. This |
| 899 |
mimics the structure of a GeoJSON file (even if the input comes from a |
| 900 |
shapefile), as shown in Listing~\ref{tab:geojson}; the labeling of |
| 901 |
elements is as in GeoJSON, with gretl types in parentheses. |
| 902 |
|
| 903 |
\begin{script}[htbp] |
| 904 |
\begin{scode} |
| 905 |
FeatureCollection (bundle) |
| 906 |
features (array of n bundles) |
| 907 |
features[i] (bundle) |
| 908 |
features[i].properties (bundle) |
| 909 |
features[i].geometry (bundle) |
| 910 |
features[i].geometry.type (string) |
| 911 |
features[i].geometry.coordinates (array) |
| 912 |
\end{scode} |
| 913 |
\caption{Structure of map data, gretl types in parentheses} |
| 914 |
\label{tab:geojson} |
| 915 |
\end{script} |
| 916 |
|
| 917 |
\subsection{Injecting payload data} |
| 918 |
\label{sec:inject} |
| 919 |
|
| 920 |
First let's look at the case of injecting a payload series into a map |
| 921 |
file. Listing~\ref{tab:add-gdp} revisits the EU founders map discussed |
| 922 |
in section~\ref{sec:example}. As before, we start by opening the |
| 923 |
metadata and ``joining'' the GDP per capita data. But now we open the |
| 924 |
GeoJSON as a bundle, and for each \texttt{feature} (country) we |
| 925 |
augment its \texttt{properties} bundle with the corresponding value of |
| 926 |
the \texttt{gdppc} series (under the key ``\texttt{gdppc}''). Finally, |
| 927 |
we write the modified data to file. |
| 928 |
|
| 929 |
\begin{script}[p] |
| 930 |
\begin{scode} |
| 931 |
open founders.geojson --quiet --frompkg=geoplot |
| 932 |
join founders.csv gdp pop --ikey=FID --okey=code |
| 933 |
series gdppc = 1000*gdp/pop |
| 934 |
|
| 935 |
# open full GeoJSON as bundle |
| 936 |
bundle b = bread($mapfile) |
| 937 |
|
| 938 |
# add GDP per capita to properties |
| 939 |
loop i=1..nelem(b.features) |
| 940 |
b.features[i].properties.gdppc = gdppc[i] |
| 941 |
endloop |
| 942 |
|
| 943 |
# save modified geojson file |
| 944 |
bwrite(b, "founders_mod.json") |
| 945 |
\end{scode} |
| 946 |
\caption{Adding payload data to a map file: |
| 947 |
\dtk{founders_mod.inp}} |
| 948 |
\label{tab:add-gdp} |
| 949 |
\end{script} |
| 950 |
|
| 951 |
\subsection{Rearranging regions} |
| 952 |
\label{sec:rearrange} |
| 953 |
|
| 954 |
Now how about rearranging regions of a given country (or more |
| 955 |
generally, \texttt{features} within a given |
| 956 |
\texttt{FeatureCollection})? Here we use the function |
| 957 |
\dtk{geoplot_translate_feature}. Unlike the \texttt{geoplot} |
| 958 |
function this is not ``built in'' so before calling it one must do |
| 959 |
\begin{code} |
| 960 |
include geoplot.gfn |
| 961 |
\end{code} |
| 962 |
The signature of the feature-translation function is |
| 963 |
\begin{code} |
| 964 |
void geoplot_translate_feature(bundle *b, int f, |
| 965 |
matrix shift, |
| 966 |
matrix center[null], |
| 967 |
matrix scale[null]) |
| 968 |
\end{code} |
| 969 |
You pass in a map bundle obtained via \texttt{bread} (in pointer |
| 970 |
form); the sequential index, \texttt{f}, of the feature to translate; |
| 971 |
and a 2-vector \texttt{shift} giving displacement in the X and Y |
| 972 |
directions. If in addition you want to rescale the feature you pass |
| 973 |
two more 2-vectors: \texttt{center} holds the coordinates of the |
| 974 |
feature's centroid and \texttt{scale} the scale factors to apply in |
| 975 |
the two directions. |
| 976 |
|
| 977 |
An example script is shown in Listing~\ref{tab:translate} and the |
| 978 |
result of plotting the modified GeoJSON file in |
| 979 |
Figure~\ref{fig:usmod}. We obtain the second argument to pass to the |
| 980 |
translator by inspection of the map dataset: Alaska is feature 48 and |
| 981 |
Hawaii feature 5. In this case we decide to move Alaska 34\deg\ |
| 982 |
East and 35\deg\ South and shrink it substantially. Getting the |
| 983 |
effect one wants is likely to take some trial and error, but two |
| 984 |
\textsf{geoplot} features can be helpful. |
| 985 |
|
| 986 |
\begin{script}[p] |
| 987 |
\begin{scode} |
| 988 |
include geoplot.gfn |
| 989 |
|
| 990 |
open us-states.geojson --quiet --frompkg=geoplot |
| 991 |
bundle b = bread($mapfile) |
| 992 |
|
| 993 |
# Shrink Alaska and place underneath the "lower 48" |
| 994 |
matrix shift = {34, -35} |
| 995 |
matrix center = {-150.885, 62.5503} |
| 996 |
matrix scale = {0.3, 0.35} |
| 997 |
geoplot_translate_feature(&b, 48, shift, center, scale) |
| 998 |
|
| 999 |
# Shift Hawaii alongside Alaska |
| 1000 |
shift = {51, 5} |
| 1001 |
geoplot_translate_feature(&b, 5, shift) |
| 1002 |
|
| 1003 |
# save modified geojson file |
| 1004 |
bwrite(b, "us_modified.json") |
| 1005 |
\end{scode} |
| 1006 |
\caption{Moving Alaska and Hawaii} |
| 1007 |
\label{tab:translate} |
| 1008 |
\end{script} |
| 1009 |
|
| 1010 |
\begin{figure}[p] |
| 1011 |
\centering |
| 1012 |
\includegraphics{usmod.pdf} |
| 1013 |
\caption{Alaska and Hawaii moved} |
| 1014 |
\label{fig:usmod} |
| 1015 |
\end{figure} |
| 1016 |
|
| 1017 |
First, if you plot just the map outlines (no payload) then you should |
| 1018 |
see the X and Y values on the axes, giving you at least a rough idea |
| 1019 |
of the shifts you might want. Second, \textsf{geoplot} contains the |
| 1020 |
function \dtk{geoplot_describe_json} which gives you a good deal |
| 1021 |
of relevant information. The signature of this function is |
| 1022 |
\begin{code} |
| 1023 |
bundle geoplot_describe_json (const bundle jb, int verbose[1]) |
| 1024 |
\end{code} |
| 1025 |
You pass a map bundle and a verbosity level, as in |
| 1026 |
\begin{code} |
| 1027 |
include geoplot.gfn |
| 1028 |
bundle us = bread("us-states.geojson") |
| 1029 |
geoplot_describe_json(us, 3) |
| 1030 |
\end{code} |
| 1031 |
On searching the \texttt{verbose} = 3 output for Alaska one finds |
| 1032 |
(here's a small snippet): |
| 1033 |
\begin{code} |
| 1034 |
48: geometry type = MultiPolygon, no id |
| 1035 |
... |
| 1036 |
name: Alaska |
| 1037 |
... |
| 1038 |
Extents: X = {-171.791,-129.98}; Y = {54.4042,70.6964} |
| 1039 |
\end{code} |
| 1040 |
The \texttt{Extents} data enable you to figure out plausible |
| 1041 |
values for the \texttt{center} of a feature. |
| 1042 |
|
| 1043 |
\section{Coda} |
| 1044 |
|
| 1045 |
In the foregoing we have mostly kept things simple with toy |
| 1046 |
examples. In concluding, we'll show off with a ``real'' example: |
| 1047 |
Figure~\ref{fig:ita-covid} shows the distribution of COVID cases |
| 1048 |
across Italian provinces as of 2020-05-15. The appearance of this plot |
| 1049 |
was tuned using the following option settings: |
| 1050 |
|
| 1051 |
\begin{code} |
| 1052 |
string cmds = sprintf("set colorbox user origin 0.9,0.45 size 0.03,0.4\n") |
| 1053 |
cmds ~= sprintf("set xrange [6.4:20.5]") |
| 1054 |
bundle opts = _(plotfile = "covid.pdf") |
| 1055 |
opts.logscale = 1 |
| 1056 |
opts.border = 0 |
| 1057 |
opts.linewidth = 0.4 |
| 1058 |
opts.palette = "green-to-red" |
| 1059 |
opts.literal = cmds |
| 1060 |
opts.height = 900 |
| 1061 |
\end{code} |
| 1062 |
|
| 1063 |
As you will have seen in the previous figures, the default |
| 1064 |
\textsf{gnuplot} ``colorbox'' is quite large, occupying the full |
| 1065 |
height of the plot. This doesn't look so great for a tall, skinny |
| 1066 |
country like Italy, so we used the \texttt{literal} option to pass in |
| 1067 |
commands to make the colorbox smaller and reposition it; to prevent |
| 1068 |
the colorbox from going off the right edge of the plot we also made |
| 1069 |
the \texttt{xrange} a little wider than the default. We were able to |
| 1070 |
determine what the revised \texttt{xrange} should look like by |
| 1071 |
examining a plot with no payload, showing the latitude values on the |
| 1072 |
axes. |
| 1073 |
|
| 1074 |
\begin{figure}[p] |
| 1075 |
\centering |
| 1076 |
\includegraphics{covid.pdf} |
| 1077 |
\caption{COVID-19 cases per 10\,000 by province as of |
| 1078 |
2020-05-15, log scale} |
| 1079 |
\label{fig:ita-covid} |
| 1080 |
\end{figure} |
| 1081 |
|
| 1082 |
\section{Change log} |
| 1083 |
|
| 1084 |
2024-02-28: Fix obsolete use of \verb|--write| with the \texttt{outfile} |
| 1085 |
command.\\[6pt] |
| 1086 |
2023-05-01: Add \texttt{keypos} option for adjusting the key |
| 1087 |
position when plotting qualitative data. \\[6pt] |
| 1088 |
2023-01-17: Update the documentation to explain how to handle |
| 1089 |
qualitative payload data; fix bug whereby output to an image |
| 1090 |
file could fail. \\[6pt] |
| 1091 |
2021-02-12: Fix some incorrect type specifiers in |
| 1092 |
\verb|geoplot_utils.inp|, revealed by improved type-checking following |
| 1093 |
the gretl 2021a release. \\[6pt] |
| 1094 |
2020-05-27: Initial release |
| 1095 |
|
| 1096 |
\clearpage |
| 1097 |
\startappendices |
| 1098 |
|
| 1099 |
\myappendix{Representation of polygons in gnuplot} |
| 1100 |
\label{sec:gnuplot} |
| 1101 |
|
| 1102 |
The |
| 1103 |
following \textsf{gnuplot} code |
| 1104 |
\begin{code} |
| 1105 |
unset key |
| 1106 |
plot '-' using 1:2:3 with filledcurves fillcolor "blue" |
| 1107 |
0 0 0 |
| 1108 |
0.5 0 0.866 |
| 1109 |
1 0 0 |
| 1110 |
e |
| 1111 |
\end{code} |
| 1112 |
produces an equilateral triangle: \includegraphics[height=2cm]{triangle.pdf} |
| 1113 |
|
| 1114 |
\medskip |
| 1115 |
|
| 1116 |
The internal coloring of polygons can be set using ``\texttt{fillcolor |
| 1117 |
palette}''. For example, |
| 1118 |
\begin{code} |
| 1119 |
unset key |
| 1120 |
$coords << EOD |
| 1121 |
0 0 2.5 |
| 1122 |
0 1 2.5 |
| 1123 |
1 1 2.5 |
| 1124 |
1 0 2.5 |
| 1125 |
|
| 1126 |
2 1 3.5 |
| 1127 |
2 2 3.5 |
| 1128 |
3 2 3.5 |
| 1129 |
3 1 3.5 |
| 1130 |
|
| 1131 |
4 0 3 |
| 1132 |
4 1 3 |
| 1133 |
5 1 3 |
| 1134 |
5 0 3 |
| 1135 |
EOD |
| 1136 |
plot for [i=0:*] $coords index i with filledcurves fillcolor palette |
| 1137 |
\end{code} |
| 1138 |
(where the third column of the data indexes into the palette) produces |
| 1139 |
\begin{center} |
| 1140 |
\includegraphics[width=0.6\textwidth]{squares} |
| 1141 |
\end{center} |
| 1142 |
|
| 1143 |
A nice way to customize the palette is via ``\texttt{set |
| 1144 |
palette defined}'' (see the \textsf{gnuplot} manual; also see |
| 1145 |
section~\ref{sec:opts} above). |
| 1146 |
|
| 1147 |
\clearpage |
| 1148 |
\myappendix{Projections} |
| 1149 |
\label{app:proj} |
| 1150 |
|
| 1151 |
\textsf{Geoplot} assumes (mostly, but see below) that incoming map |
| 1152 |
coordinates are given as degrees of latitude (Y) and longitude |
| 1153 |
(X). This is mandated by RFC 7946, which has governed GeoJSON since |
| 1154 |
2016; it also appears to be the most common case for ESRI shapefiles. |
| 1155 |
|
| 1156 |
Everyone knows that the Earth is not actually a sphere, but let's |
| 1157 |
pretend it is for simplicity. Then a degree of latitude is always the |
| 1158 |
same length on the ground: 1/360 of the planet's circumference. But |
| 1159 |
the length of a degree of longitude varies, from 1/360 of Earth's |
| 1160 |
circumference at the equator to zero at the poles. So imagine that we |
| 1161 |
pass the X--Y pairs to our plotting engine on the assumption that ``a |
| 1162 |
degree'' is always the same size: the result will be more or less OK |
| 1163 |
close to the equator but at higher or lower latitudes features will be |
| 1164 |
seriously stretched horizontally (or squashed vertically) relative to |
| 1165 |
what we're used to seeing. To avoid this effect some sort of |
| 1166 |
projection is required. |
| 1167 |
|
| 1168 |
By default geoplot uses what we might call a ``quasi-Mercator'' |
| 1169 |
projection. In most cases this should produce maps that look quite |
| 1170 |
acceptable and it has the advantage of simplicity. All we do is take |
| 1171 |
the height of the plot as specified by the user (or a default of 600 |
| 1172 |
pixels) and figure out what the width should be to make a degree of |
| 1173 |
longitude the same size as a degree of latitude at the mid-point |
| 1174 |
latitude. However, we offer four alternatives, as |
| 1175 |
follows:\footnote{``EPSG'' stands for the European Petroleum Survey |
| 1176 |
Group, which hosts the industry-standard geodetic database; see |
| 1177 |
\url{https://epsg.org/}.} |
| 1178 |
\begin{center} |
| 1179 |
\begin{tabular}{cll} |
| 1180 |
EPSG id & description & option string \\[4pt] |
| 1181 |
\textsf{3857} & standard Mercator & \verb|"Mercator"| \\ |
| 1182 |
\textsf{4326} & ``null'' projection & \verb|"EPSG4326"| \\ |
| 1183 |
\textsf{2163} & U.S. National Atlas Equal Area & |
| 1184 |
\verb|"EPSG2163"| \\ |
| 1185 |
\textsf{3035} & Europe Equal Area & \verb|"EPSG3035"| |
| 1186 |
\end{tabular} |
| 1187 |
\end{center} |
| 1188 |
|
| 1189 |
Figure~\ref{fig:proj} compares the available projections for the |
| 1190 |
contiguous United States. In this case the default \textsf{geoplot} |
| 1191 |
projection and standard Mercator are practically |
| 1192 |
indistinguishable. \textsf{EPSG:4326}, which treats degrees as |
| 1193 |
everywhere the same size, exhibits the horizontal stretching mentioned |
| 1194 |
above. \textsf{EPSG:2163} gives the impression of looking at the USA |
| 1195 |
on a section of the globe. |
| 1196 |
|
| 1197 |
\begin{figure}[htbp] |
| 1198 |
\centering |
| 1199 |
\begin{tabular}{cc} |
| 1200 |
\includegraphics[scale=0.9]{prj0.pdf} & |
| 1201 |
\includegraphics[scale=0.9]{mercator.pdf} \\[-6pt] |
| 1202 |
\small{Default} & \small{Mercator} \\ |
| 1203 |
\includegraphics[scale=0.9]{epsg4326.pdf} & |
| 1204 |
\includegraphics[scale=0.9]{epsg2163.pdf} \\[-6pt] |
| 1205 |
\small{EPSG:4326} & \small{EPSG:2163} |
| 1206 |
\end{tabular} |
| 1207 |
\vspace{1ex} |
| 1208 |
\caption{Comparison of projections} |
| 1209 |
\label{fig:proj} |
| 1210 |
\end{figure} |
| 1211 |
|
| 1212 |
To select one of the alternatives, you add the appropriate string to |
| 1213 |
the \texttt{options} bundle passed to the \texttt{geoplot} function |
| 1214 |
under the key \texttt{projection}. Please note that \textsf{EPSG:2163} |
| 1215 |
is specially tuned for the USA, and will produce weird-looking or |
| 1216 |
non-existent results for other parts of the world. \textsf{EPSG:3035} |
| 1217 |
is similarly tuned for Europe. They are both so-called Lambert |
| 1218 |
Azimuthal Equal Area projections. |
| 1219 |
|
| 1220 |
\subsection*{Non-standard coordinates} |
| 1221 |
|
| 1222 |
In certain map files---maybe GeoJSON predating RFC 7946, and perhaps |
| 1223 |
some shapefiles---the X--Y coordinates are not in the expected form of |
| 1224 |
degrees of latitude and longitude. In that case they probably already |
| 1225 |
encode some sort of projection, and so should not be ``re-projected''. |
| 1226 |
For GeoJSON files, \textsf{geoplot} makes an attempt to determine |
| 1227 |
whether a non-standard coordinate system is used, as was allowed under |
| 1228 |
the obsolete GeoJSON 2008 specification. In that case we automatically |
| 1229 |
cancel projection; we also do this if the X or Y values are out of |
| 1230 |
bounds for representing degrees (that is, |X| > 360 or |Y| > 180). |
| 1231 |
|
| 1232 |
Failing such automatic detection, one can try specifying |
| 1233 |
\textsf{EPSG:4326} to get the X and Y units to be treated as equal in |
| 1234 |
size, in effect canceling projection, as in |
| 1235 |
\begin{code} |
| 1236 |
bundle options |
| 1237 |
options.projection = "EPSG4326" |
| 1238 |
\end{code} |
| 1239 |
|
| 1240 |
\subsection*{Limiting the area shown} |
| 1241 |
\label{sec:xyranges} |
| 1242 |
|
| 1243 |
Section~\ref{sec:sub-sample} explains how to exclude certain features |
| 1244 |
from a map using the \texttt{smpl} command. In some cases, however, |
| 1245 |
one may wish to limit what is shown in a different way, by specifying |
| 1246 |
ranges for latitude and longitude. This can be done by supplying |
| 1247 |
2-vectors in the \texttt{geoplot} options bundle under the keys |
| 1248 |
\texttt{xrange} (longitude) and \texttt{yrange} (latitude). For |
| 1249 |
example, to plot just the area from 40\deg\ to 60\deg\ North and |
| 1250 |
10\deg\ to 30\deg\ East you can do |
| 1251 |
\begin{code} |
| 1252 |
options.yrange = {40,60} |
| 1253 |
options.xrange = {10,30} |
| 1254 |
\end{code} |
| 1255 |
Specifying such ranges in degrees works fine if you are using the |
| 1256 |
default \texttt{geoplot} projection, Mercator or \textsf{EPSG:4326}. |
| 1257 |
But it doesn't work for azimuthal projections, where neither meridians |
| 1258 |
nor parallels are straight lines. Rather, you should first plot the |
| 1259 |
entire map using the projection you want, with axis tics turned on to |
| 1260 |
show the linearized coordinates. Then specify the ranges in terms of |
| 1261 |
these values. For example, here's how one might restrict a US map to |
| 1262 |
the contiguous states via ranges: |
| 1263 |
\begin{code} |
| 1264 |
open us-states.geojson --frompkg=geoplot |
| 1265 |
bundle opts |
| 1266 |
opts.projection = "EPSG2163" # azimuthal |
| 1267 |
opts.tics = 1 |
| 1268 |
# take a look-see |
| 1269 |
geoplot(null, opts) |
| 1270 |
# suitable ranges, by inspection |
| 1271 |
opts.xrange = {-330,410} |
| 1272 |
opts.yrange = {-350,130} |
| 1273 |
geoplot(null, opts) |
| 1274 |
\end{code} |
| 1275 |
|
| 1276 |
\subsection*{Further reading} |
| 1277 |
|
| 1278 |
For anyone wishing to follow up on this sort of thing, there are many |
| 1279 |
websites presenting information on coordinate systems and |
| 1280 |
projections. Two of the most useful ones, in our experience, are: |
| 1281 |
|
| 1282 |
Reference materials: \url{https://spatialreference.org/} |
| 1283 |
|
| 1284 |
Explanation: |
| 1285 |
\url{https://source.opennews.org/articles/choosing-right-map-projection/} |
| 1286 |
|
| 1287 |
\clearpage |
| 1288 |
\myappendix{Specialized functions} |
| 1289 |
\label{app:special} |
| 1290 |
|
| 1291 |
The functions shown below are implemented in hansl and included in the |
| 1292 |
\textsf{geoplot} addon; to use them you must first do |
| 1293 |
\begin{verbatim} |
| 1294 |
include geoplot.gfn |
| 1295 |
\end{verbatim} |
| 1296 |
|
| 1297 |
\begin{funcdoc} |
| 1298 |
\begin{verbatim} |
| 1299 |
bundle geoplot_describe_json (const bundle jb, int verbose[1]) |
| 1300 |
\end{verbatim} |
| 1301 |
Provides a systematic description of the GeoJSON bundle \texttt{jb}, |
| 1302 |
the amount of detail depending on the \texttt{verbose} setting, |
| 1303 |
which has a maximum of 3. By assigning the return value one can |
| 1304 |
obtain a bundle containing the information but for some purposes the |
| 1305 |
printed output may suffice. See section~\ref{sec:rearrange}. |
| 1306 |
\end{funcdoc} |
| 1307 |
|
| 1308 |
\begin{funcdoc} |
| 1309 |
\begin{verbatim} |
| 1310 |
void geoplot_set_properties (bundle *b, list L) |
| 1311 |
\end{verbatim} |
| 1312 |
Rewrites the \texttt{properties} within the bundle representation of |
| 1313 |
a map, \texttt{b}, to include all and only the series referenced in |
| 1314 |
the list \texttt{L}. This provides a means of adding ``payload'' |
| 1315 |
data (see section~\ref{sec:payload}) and also pruning unwanted |
| 1316 |
metadata. In the example below, \texttt{us-states.geojson} |
| 1317 |
originally contains 40 items of metadata per state, most of them |
| 1318 |
unlikely to be of interest. |
| 1319 |
\begin{code} |
| 1320 |
# example |
| 1321 |
open us-states.geojson --quiet --frompkg=geoplot |
| 1322 |
join statepop.gdt population --ikey=postal --okey=Code |
| 1323 |
|
| 1324 |
# select only the properties we actually want |
| 1325 |
list L = name postal population |
| 1326 |
bundle b = bread($mapfile) |
| 1327 |
geoplot_set_properties(&b, L) |
| 1328 |
bwrite(b, "us_pruned.json") |
| 1329 |
\end{code} |
| 1330 |
\end{funcdoc} |
| 1331 |
|
| 1332 |
\begin{funcdoc} |
| 1333 |
\begin{verbatim} |
| 1334 |
void geoplot_translate_feature (bundle *b, int f, |
| 1335 |
matrix shift, |
| 1336 |
matrix center[null], |
| 1337 |
matrix scale[null]) |
| 1338 |
\end{verbatim} |
| 1339 |
Shifts the feature with sequential index \texttt{f}, optionally |
| 1340 |
rescaling it. See section~\ref{sec:rearrange} for an example and |
| 1341 |
explanation. |
| 1342 |
\end{funcdoc} |
| 1343 |
|
| 1344 |
\begin{funcdoc} |
| 1345 |
\begin{verbatim} |
| 1346 |
matrix geoplot_seek_feature(const bundle b, |
| 1347 |
string name, |
| 1348 |
bool do_plot[1]) |
| 1349 |
\end{verbatim} |
| 1350 |
Searches the map bundle \texttt{b} for features matching |
| 1351 |
\texttt{name} (on a case-insensitive basis). If one or more matches |
| 1352 |
are found their 1-based indices are returned in a row vector. If a |
| 1353 |
single match is found metadata for the feature are printed and if |
| 1354 |
\dtk{do_plot} is not set to zero a plot of the feature is |
| 1355 |
shown. |
| 1356 |
\begin{code} |
| 1357 |
include geoplot.gfn |
| 1358 |
open us-states.geojson --frompkg=geoplot --quiet |
| 1359 |
map = bread($mapfile) |
| 1360 |
# no matches |
| 1361 |
geoplot_seek_feature(map, "nowhere") |
| 1362 |
# two matches |
| 1363 |
geoplot_seek_feature(map, "CAROLINA") |
| 1364 |
# one match, plot shown |
| 1365 |
geoplot_seek_feature(map, "Florida") |
| 1366 |
\end{code} |
| 1367 |
\end{funcdoc} |
| 1368 |
|
| 1369 |
\begin{funcdoc} |
| 1370 |
\begin{verbatim} |
| 1371 |
void geoplot_simplify(bundle *b, scalar preserve[0.1:1:0.75]) |
| 1372 |
\end{verbatim} |
| 1373 |
Simplifies the polygons in the map bundle \texttt{b} using the |
| 1374 |
Visvalingam--Whyatt algorithm.\footnote{See |
| 1375 |
\url{https://hull-repository.worktribe.com/output/459275}. And for |
| 1376 |
a nice illustration see \url{https://bost.ocks.org/mike/simplify/} |
| 1377 |
(accessed 2023-01-16).} This may be useful if for a certain |
| 1378 |
geography of interest the only map file readily available is at a |
| 1379 |
higher resolution than you need. Smaller values of the |
| 1380 |
\texttt{preserve} parameter preserve less detail or in other words |
| 1381 |
simplify the map more radically; the default value of 0.75 may be |
| 1382 |
considered conservative if you start with a very detailed map. |
| 1383 |
\begin{code} |
| 1384 |
include geoplot.gfn |
| 1385 |
open highres.geojson --quiet |
| 1386 |
bundle map = bread($mapfile) |
| 1387 |
geoplot_simplify(&map, 0.5) |
| 1388 |
bwrite(map, "simplified.geojson") |
| 1389 |
open simplified.geojson --quiet |
| 1390 |
# see if the level of detail is OK |
| 1391 |
geoplot() |
| 1392 |
\end{code} |
| 1393 |
\end{funcdoc} |
| 1394 |
|
| 1395 |
\end{document} |
| 1396 |
|
| 1397 |
%%% Local Variables: |
| 1398 |
%%% mode: latex |
| 1399 |
%%% TeX-master: t |
| 1400 |
%%% End: |