Skip to content

Instantly share code, notes, and snippets.

@bohdanszymanik
Last active August 1, 2025 04:10
Show Gist options
  • Save bohdanszymanik/807f45da5023351b1faa303d41a61f7a to your computer and use it in GitHub Desktop.
Save bohdanszymanik/807f45da5023351b1faa303d41a61f7a to your computer and use it in GitHub Desktop.
Clip meshblock 2013 to Auckland area - copied from a Microsoft Fabric Spark(R) notebook so I st_read file will be an odd location
library(sf)
library(dplyr)
# you need the polygon dataset not the polyline one!
nz_coastline <- st_read("/lakehouse/default/Files/lds-nz-coastlines-and-islands-polygons-topo-150k-SHP/nz-coastlines-and-islands-polygons-topo-150k.shp")
mb2013_akld <- st_read("/lakehouse/default/Files/statsnz-meshblock-2013-SHP-akld/meshblock-2013.shp")
# different CRS for the two regions
# nz_coastline is NZGD2000 while mb2013_akld is WGS 84
# Let's reconcile both to WGS84
nz_coastline_wgs84 <- st_transform(nz_coastline, st_crs(mb2013_akld))
mb2013_akld_clipped <- st_intersection(mb2013_akld, nz_coastline_wgs84)
# fyi - this is loads faster if you first clip the nz_coastline to the extent of the mb2013_akld bounding box
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment