jaramana / R-Polygon_to_Points

This script lets you assigns attributes from a Polygon (using a shapefile in this example) to points.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

R Pivot Table

This script lets you assigns attributes from a Polygon (using a shapefile in this example) to points.

Motivation

There have been requests to identify which community districts (polygon) a point is located in.

Code

# Load Packages
library(lubridate)
library(sf)
library(sp)
library(readr)
library(rgeos)
library(rgdal)

# Set directory
setwd("/PATH/TO/DIRECTORY")

# Load data from CabConnect
data <- read_csv("DOHMH_Farmers_Markets.csv")

# Read boro shapefiles
polygon <- readOGR(dsn = "nycd_19d", layer = "nycd")
polygon <-
  spTransform(polygon,
              CRS(
                "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
              ))


# Create Point Data Frame
LonLat <- 
  sp::SpatialPointsDataFrame(
    coordinates(cbind(data$Longitude, data$Latitude)),
    data,
    proj4string = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
  )

# Bind GeoCoded Data to Main Data Frame
data <- cbind(data, over(LonLat, polygon)$BoroCD)

# Change column name
colnames(data)[colnames(data)=="over(LonLat, polygon)$BoroCD"] <- "NYCD"

# Write .csv
write.csv(data,"DOHMH_Farmers_Markets_NYCD.csv", row.names = FALSE)

About

This script lets you assigns attributes from a Polygon (using a shapefile in this example) to points.


Languages

Language:R 100.0%