Skip to content

Instantly share code, notes, and snippets.

View JosiahParry's full-sized avatar
💻
on the 'puter

Josiah Parry JosiahParry

💻
on the 'puter
View GitHub Profile
# https://fosstodon.org/@coolbutuseless/111029564256521659
x <- letters
x <- structure(letters, class = "zero_indexed")
`[.zero_indexed` <- function(obj, i, ...) {
if (is.numeric(i)) {
i <- i + 1
NextMethod()
@JosiahParry
JosiahParry / hausdorff-explorer.R
Last active September 13, 2023 22:22
explore hausdorf distances
library(sf)
#' Plots vertices where Hausdorff distance is calculated from
#'
#' @param x any single geometry
#' @param y any single geometry
#' @returns two points as an `sfc` object that are the locations where Hausdorff distance
#' is calculated from
plot_hd_points <- function(x, y) {
library(arcgislayers)
# this can use the authorization from Pro
token <- auth_binding()
set_auth_token(token)
# read in a shapefile from sf
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"))
# transform to 3857
@JosiahParry
JosiahParry / mstree-sfnetworks.R
Created October 15, 2023 12:02
Spatially implicit edges based on a minimum spanning tree
library(sfdep)
library(sfnetworks)
nb <- st_contiguity(guerry)
wt <- st_weights(nb)
listw <- recreate_listw(nb, wt)
mstree <- spdep::mstree(listw, 1)
edges <- data.frame(
@JosiahParry
JosiahParry / arc-select-point-raster.R
Created October 19, 2023 16:03
get raster attributes at a point
library(arcgis)
img_url <- "https://landsat2.arcgis.com/arcgis/rest/services/Landsat/MS/ImageServer"
# create an ImageServer object
landsat <- arc_open(img_url, token = "")
# get attributes
y <- arc_select(
x,
library(sf, quietly = TRUE)
library(spdep, quietly = TRUE)
nc <- read_sf(system.file("shape/nc.shp", package = "sf"))
# define neighbors
nb <- poly2nb(nc)
# choose variable
x <- nc$SID74
library(sf)
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(arcgis)
#> Attaching core arcgis packages:
#> - {arcgisutils} v0.1.0
#> - {arcgislayers} v0.1.0
# store the URL
furl <- "https://utility.arcgis.com/usrsvcs/servers/6d515686b57f4f4b948502cc88a90347/rest/services/Planning/i15_Crop_Mapping_2021_Provisional/MapServer/0"
new_post <- function(author, title, tags, published_date) {
structure(
list(
author = author,
title = title,
tags = tags,
published_date = published_date
),
class = "post"
)
# Positive Slope Scenario
x <- wk::wkt(
  c(
    "LINESTRING(0.0 0.0, 2.0 2.0)", # target line
    "LINESTRING(0.5 0.75, 2.5 2.75)", # one we've deemed parallel
    # desribes the overlap range (2D)
    "POLYGON((0.5 0.75, 2.0 0.75, 2.0 2.0, 0.5 2.0, 0.5 0.75))" 
  )
) 
library(sf)
library(sfheaders)
# define polygon coords
m <- matrix(
c(2, -2, 2, 2, -2, 2, -2, -2, 2, -2),
ncol = 2, byrow = TRUE
)
# create polygon from matrix