Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/cenuno/pointdexter

Label longitudinal and latitudinal coordinates located inside a polygon.
https://github.com/cenuno/pointdexter

points polygon polygons r spatial-analysis spatial-statistics

Last synced: 3 months ago
JSON representation

Label longitudinal and latitudinal coordinates located inside a polygon.

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE
, comment = "#>"
, fig.path = "man/figures/README-"
, out.width = "100%"
, dpi = 300)
```

# pointdexter

[![CRAN status](https://www.r-pkg.org/badges/version/pointdexter)](https://cran.r-project.org/package=pointdexter)
[![CRAN checks](https://cranchecks.info/badges/worst/pointdexter)](https://cran.r-project.org/web/checks/check_results_pointdexter.html)
[![CRAN Downloads](http://cranlogs.r-pkg.org/badges/pointdexter?color=brightgreen)](http://www.r-pkg.org/pkg/pointdexter)
[![Rdoc](http://www.rdocumentation.org/badges/version/pointdexter)](http://www.rdocumentation.org/packages/pointdexter)
[![Travis build status](https://travis-ci.org/cenuno/pointdexter.svg?branch=master)](https://travis-ci.org/cenuno/pointdexter)

The `pointdexter` package labels longitudinal and latitudinal coordinates located inside a polygon.

## Description

For a singular polygon, the label for each coordinate pair is a logical vector of TRUE and FALSE values. For multiple polygons, the label for each coordinate pair is a character vector based on the names of each polygon.

## Spatial Packages

The package is designed to work with both [sf](https://r-spatial.github.io/sf/) and [SpatialPolygonsDataFrame](https://www.rdocumentation.org/packages/sp/versions/1.2-5/topics/SpatialPolygonsDataFrame-class) objects.

By default, `pointdexter` only installs and loads the [`sp`](https://www.rdocumentation.org/packages/sp/versions/1.3-1) and [`splancs`](https://www.rdocumentation.org/packages/splancs/versions/2.01-40) packages.

However, `pointdexter` also works with the [`sf`](https://www.rdocumentation.org/packages/sf/versions/0.7-2) package. The following is `sf` installation advice from [Matt Herman](https://nycgeo.mattherman.info/):

> Depending on your operating system and available libraries, `sf` can be tricky to install the first time. The [`sf` website](https://r-spatial.github.io/sf/index.html#installing) is a good place to start if you're having trouble. If you're using macOS, [this is a good guide](https://medium.com/@jinwujour/mapping-with-r-on-mac-installation-8c8ef997c6c2) to installing the required libraries.

## Installation

The latest stable release version can be installed from [CRAN](https://cran.r-project.org/package=pointdexter):

```R
install.packages("pointdexter")
```

Development versions can be installed from GitHub:

```R
# note: by default, the development version is the master branch;
# however, that can be changed by changing the
# value in the 'ref' argument
remotes::install_github("cenuno/pointdexter")
```

## Usage

### Build-in Data

`pointdexter` comes with built-in point and polygon data - entirely due to the awesome and accessible [Chicago Data Portal](https://data.cityofchicago.org/) - to help you label points in polygons:

* Chicago Public Schools (CPS) - [School Profile Information, School Year (SY) 2018-2019](https://data.cityofchicago.org/Education/Chicago-Public-Schools-School-Profile-Information-/kh4r-387c)
+ as a data frame

* City of Chicago [boundary](https://data.cityofchicago.org/Facilities-Geographic-Boundaries/Boundaries-City/ewy2-6yfk)
+ as a SpatialPolygonsDataFrame; and
+ as a simple feature

* Chicago's [77 community areas](https://data.cityofchicago.org/Facilities-Geographic-Boundaries/Boundaries-Community-Areas-current-/cauq-8yn6)
+ as a SpatialPolygonsDataFrame; and
+ as a simple feature

* Chicago's [2010 census tracts](https://data.cityofchicago.org/Facilities-Geographic-Boundaries/Boundaries-Census-Tracts-2010/5jrd-6zik)
+ as a SpatialPolygonsDataFrame; and
+ as a simple feature

### Example

Using built-in data, down below is an example of how to use `pointdexter` to label coordinate pairs with their appropriate polygon:

```{r example}
# load necessary packages ----
library(pointdexter) # label coordinate pairs in polygons
library(sp) # classes and methods for spatial data
library(knitr) # general purpose package for dynamic report generation

# load necessary data ----
data("cps_sy1819")
data("community_areas_spdf")

# create list of coordinate pair matrices for each community area ----
community.area.boundaries <-
GetPolygonBoundaries(my.polygon = community_areas_spdf
, labels = community_areas_spdf$community)

# identify the community that each cps school lies in ----
cps_sy1819$community <-
LabelPointsWithinPolygons(lng = cps_sy1819$school_longitude
, lat = cps_sy1819$school_latitude
, polygon.boundaries = community.area.boundaries)

# store relevant columns ----
relevant.columns <-
c("school_id", "short_name"
, "school_longitude", "school_latitude", "community")

# show first few records ----
kable(head(cps_sy1819[, relevant.columns], n = 10))
```

```{r visualize}
# plot only CPS high schools in the the Austin, Rogers Park, and West Elsdon community areas ----

# create filter condition ----
filter.condition <-
which(cps_sy1819$is_high_school &
cps_sy1819$community %in% c("AUSTIN"
, "ROGERS PARK"
, "WEST ELSDON"))

# filter cps records to those that matched our condition ---
df <- cps_sy1819[filter.condition, ]

# note: clear plot space
par(mar = c(0, 0, 1, 0))
plot(x = community_areas_spdf
, main = "CPS High Schools in Austin, Rogers Park, and West Elsdon communities, SY1819"
, cex.main = 0.75
, col = "gray85"
, border = "dodgerblue4")
points(x = df$school_longitude
, y = df$school_latitude
, pch = 19
, col = rgb(red = 212, green = 69, blue = 0
, alpha = 90
, maxColorValue = 255)
, cex = 1)

# store relevant columns ----
relevant.columns <-
c("school_id", "short_name", "is_high_school"
, "school_longitude", "school_latitude", "community")

# print table of those schools that met the condition ----
kable(df[order(df$community), relevant.columns], row.names = FALSE)

```

## Resources

After you've installed the package, be sure to view the help files that introduce you to `pointdexter`'s two functions:

1. [`?pointdexter::GetPolygonBoundaries()`](https://cenuno.github.io/pointdexter/reference/GetPolygonBoundaries.html); and
2. [`?pointdexter::LabelPointsWithinPolygons()`](https://cenuno.github.io/pointdexter/reference/LabelPointsWithinPolygons.html).

## Feedback

### Cite

If you use `pointdexter` for any analysis, I would love to hear about it! You can also cite the package according to `citation("pointdexter")`.

### Contribute

[Issues](https://github.com/cenuno/pointdexter/issues) and [pull requests](https://github.com/cenuno/pointdexter/pulls) are welcome anytime!

## Code of Conduct

Please note that this project is released with a [Contributor Code of Conduct](.github/CODE_OF_CONDUCT.md).
By participating in this project you agree to abide by its terms.