Mapping

Create Mile Points from a LINESTRING or MULTILINESTRING in R

Here is an R function that takes a LINESTRING or touching MULTILINESTRING and returns a series of points along the line string at each mile, much like the “tombstone” mile markers along an expressway. This is helpful for making maps when you want to plot distance for hikers or drivers looking at their odometer of their car. It has the option to “reverse” the linestring so you can have the points going the opposite direction of the linestring, such as south to north. The code can be modified for quarter mile points or however you find useful.

library(tidyverse)
library(sf)
library(units)

make_milepoint <- \(linestring, reverse = FALSE) {
  # make sure were using a projected coordinate systm
  linestring <- st_transform(linestring, 5070)
  
  # merge parts together into a multilinestring
  linestring <- linestring %>% group_by() %>% summarise()
  
  # if multiline string, then attempt to join together
  # this will raise an exception if the linestring is not contiguous 
  if (st_geometry_type(linestring) == 'MULTILINESTRING')
    linestring <- st_line_merge(linestring )
  
  # reverse the string if we want to 
  # go from the other end
  if (reverse) 
    linestring <- st_reverse(linestring)
  
  # length of string in miles
  linestring.distance = st_length(linestring) %>% set_units('mi') %>% 
    drop_units()
  
  # percent of string equals each mile including start and end
  linestring.sample.percent <- seq(0, 1, 1/linestring.distance) %>% c(1)
  
  # sample line string, convert multi-points to points, convert to sf
  # add a column listing the mile-points, round to two digits
  st_line_sample(linestring, sample = linestring.sample.percent) %>%
    st_cast('POINT') %>%
    st_as_sf() %>%
    mutate(
      mile = round(linestring.sample.percent * linestring.distance, digits = 2))
}

Here is an example of the 1 mile points it outputs along Piseco-Powley Road.

And here is a static (paper) map I created using this code plus adding coordinates and elevation for this hiking map.

Soda Range Trail

The GeoParquet Ecosystem at 1.0.0 | Cloud-Native Geospatial Foundation

The GeoParquet Ecosystem at 1.0.0 | Cloud-Native Geospatial Foundation

One of the things I’m most proud of about GeoParquet 1.0.0 is how robust the ecosystem already is. For the 1.0.0 announcement, I started to write up all the cool libraries and tools supporting GeoParquet, and the awesome data providers who are already providing interesting data in the spec. But I realized it would at least double the length of the blog post to do it justice, so I decided to save it for its own post.

The Real Powers of R Statistical Language for Map Making

There are two major — and distinct ways that the R Statistical language is a powerful tool for map making — either paired with QGIS or without.

When you should use R Statistical Language with QGIS

If you are looking for a more complicated map, one of the best ways to process and wrangle map data, Shapefiles and other geographic data is within R Studio. Much of the prep work for making a map is best down in R as you can type commands quickly, pipe and mutate data, dissolve and buffer shapes easily with R and the sf package. No mouse clicks required. It’s easy to save repeated processes in an R script.

Then once the data is ready to be mapped, export it to a geopackage or shapefile, usually which I store in the /tmp directory, as I don’t need to save the data as I can quickly re-create it with the R script. From there, I can load it into QGIS, then make the map look pretty, manipulate the styling and labeling in QGIS.

When you should use R Statistical Language Alone

R with ggplot is good for simple maps, and those you want to run off frequently with updated data, and aren’t concerned with pretty labeling. Basic, clear thematic maps where labels aren’t needed or the automatic labeling of ggrepel can be tolerated due to relatively few label overlaps. In many cases, for basic maps, ggplot makes a cleaner output, lines things up better and produces better output out of the box then what you might quickly throw together in QGIS from exported data in R.

Why I don’t use R scripting in QGIS directly

While it’s possible to do R scripting in QGIS, I would rather work in RStudio with it’s preview windows, code completion and other tools rather the use QGIS. It’s just easier to do the data wrangling and manipulation of shapefiles in RStudio, and then export to a Geopackage and load the final data in QGIS.