How to Extract Specific Max and Min Coordinates of Local Authorities in UK Using Open GeoPortal Stats Dataset with R Programming Language

Understanding Geospatial Data and Mapping in R

=====================================================

Introduction to UK Local Authorities and GeoPortal Stats

As a technical blogger, it’s essential to delve into the world of geospatial data and mapping. In this article, we’ll explore how to extract specific max and min coordinates of local authorities within the UK using the Open GeoPortal Stats dataset.

Background: GeoPortal Stats Dataset

The Open GeoPortal Stats dataset is an open-source repository providing access to geographic information on the UK’s administrative boundaries. The dataset consists of five spatial layers:

  • Boundaries
  • Concessions
  • Environment
  • Leisure and Land Use
  • Tax

Each layer contains a set of polygon features representing local authorities, with attributes like name, population, and geographical coordinates.

Understanding Coordinates in GeoPortal Stats

The dataset provides two sets of coordinates for each feature: long (longitude) and lat (latitude). These values are essential for mapping and zooming into specific regions.

However, when visualizing the entire UK map, it’s common to display only one set of coordinates for each feature. This is where filtering or summarizing data becomes necessary.

Filtering Data: Finding Max and Min Coordinates

One approach to find the max and min coordinates within a local authority is to filter the dataset using the lad19nm column, which represents the name of the local authority. We’ll use the dplyr package in R to perform this filtering.

library(dplyr)
library(ggplot2)

# Sample data from Open GeoPortal Stats
map_data <- read_csv("https://opendata.arcgis.com/datasets/7f9a3e4c-1b6c-48f0-b3ef-ebc8dfe9ac50/default.aspx")

# Filter the dataset to only show Edinburgh
edinburgh.map.data <- map_data %>% 
  filter(lad19nm == "City of Edinburgh")

# Visualize the filtered data
ggplot(data = edinburgh.map.data, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=group)) +
  theme(legend.position = "none")

Summarizing Data: Finding Max and Min Coordinates

Another approach is to summarize the dataset by extracting max and min coordinates for each local authority. We’ll use the summarise function in dplyr to achieve this.

# Sample data from Open GeoPortal Stats
map_data <- read_csv("https://opendata.arcgis.com/datasets/7f9a3e4c-1b6c-48f0-b3ef-ebc8dfe9ac50/default.aspx")

# Filter the dataset to only show Edinburgh
edinburgh.map.data <- map_data %>% 
  filter(lad19nm == "City of Edinburgh") %>% 
  summarise(
    min.lat = min(lat, na.rm=T),
    max.lat = max(lat, na.rm=T),
    min.long = min(long, na.rm=T),
    max.long = max(long, na.rm=T)
  )

# Visualize the summarized data
ggplot(data = map_data, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=group))+
  coord_equal(
    xlim = c(edinburgh.map.data$min.long, edinburgh.map.data$max.long),
    ylim = c(edinburgh.map.data$min.lat, edinburgh.map.data$max.lat)
  ) +
  theme(legend.position = "none")

Using xlim and ylim: Limiting the Map View

In R’s ggplot2, we can use xlim and ylim functions to set the x-axis and y-axis limits for a map. This allows us to zoom into specific regions.

However, using these functions alone may not provide the desired results if you want to visualize the surrounding areas of Edinburgh.

Conclusion

In this article, we explored how to find specific max and min coordinates of local authorities within the UK using the Open GeoPortal Stats dataset. We discussed two approaches: filtering data and summarizing data to extract max and min coordinates for a given local authority. Additionally, we demonstrated how to use xlim and ylim functions in R’s ggplot2 to limit the map view.

Further Reading


Last modified on 2023-06-13