Skip to content

Commit

Permalink
update xx_address data
Browse files Browse the repository at this point in the history
  • Loading branch information
erikarasnick committed Nov 5, 2024
1 parent 2fb69bb commit 578ac66
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
4 changes: 2 additions & 2 deletions inst/codec_data/xx_address/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

[![latest github release for xx_address dpkg](https://img.shields.io/github/v/release/geomarker-io/codec?sort=date&filter=xx_address-*&display_name=tag&label=%5B%E2%98%B0%5D&labelColor=%238CB4C3&color=%23396175)](https://github.com/geomarker-io/codec/releases?q=xx_address&expanded=false)

Census tract-level measures of crime incidents (including property crimes, violent crimes, other crimes, and gunshots) in Hamilton County, Ohio. Tract-level measures are derived from the data packages stored in the [`xx_address` repository](https://github.com/geomarker-io/xx_address). Version 1.0.1 of the `xx_address` CoDEC data resource harmonizes [`crime_incidents-v0.1.1`](https://github.com/geomarker-io/xx_address/releases/tag/crime_incidents-v0.1.1) and [`shotspotter-v0.1.1`](https://github.com/geomarker-io/xx_address/releases/tag/shotspotter-v0.1.1). View the metadata for each of these data packages for more information about their sources.
Census tract-level measures of crime incidents (including property crimes, violent crimes, other crimes, gunshots, and reported shootings) in Hamilton County, Ohio. Tract-level measures are derived from the data packages stored in the [`xx_address` repository](https://github.com/geomarker-io/xx_address), including [`crime_incidents-v0.1.2`](https://github.com/geomarker-io/xx_address/releases/tag/crime_incidents-v0.1.2), [`shotspotter-v0.1.2`](https://github.com/geomarker-io/xx_address/releases/tag/shotspotter-v0.1.2), and [`reported_shootings-v0.1.0`](https://github.com/geomarker-io/xx_address/releases/tag/reported_shootings-v0.1.0). View the metadata for each of these data packages for more information about their sources.

Crime measures were geocoded to the street range, then aggregated to the tract level by summing the number of crimes for all streets that intersect the tract. If a street range overlaps more than one tract, the crimes are counted for the tract in which the majority of the street lies.
Jittered (within the same block) latitude and longitude corresponding to the location of each reported crime are available from each data source. Crimes aggregated to the tract level by summing the number of crimes for each tract. For higher resolution crime data, see the [`xx_address` repository](https://github.com/geomarker-io/xx_address).
51 changes: 37 additions & 14 deletions inst/codec_data/xx_address/xx_address.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@ library(dplyr)
library(sf)
library(dpkg)
library(geoarrow)
library(addr)
options(arrow.unsafe_metadata = TRUE)

crime_incidents <-
dpkg::stow("gh://geomarker-io/xx_address/crime_incidents-v0.1.1") |>
dpkg::stow("gh://geomarker-io/xx_address/crime_incidents-v0.1.2") |>
arrow::read_parquet() |>
mutate(geometry = sf::st_as_sfc(geometry)) |>
st_as_sf(crs = 4326) |>
select(date_time, lon_jittered, lat_jittered, category) |>
filter(!is.na(lon_jittered), !is.na(lat_jittered)) |>
st_as_sf(coords = c("lon_jittered", "lat_jittered"), crs = 4326) |>
st_transform(st_crs(cincy::tract_tigris_2010)) |>
st_join(cincy::tract_tigris_2010, largest = TRUE) |>
st_drop_geometry() |>
mutate(
year = lubridate::year(date_time),
month = lubridate::month(date_time)
) |>
select(-tlid, -address_x, -date_time) |>
) |>
select(-date_time) |>
group_by(census_tract_id_2010, year, month, category) |>
tally()|>
tidyr::pivot_wider(
Expand All @@ -31,29 +33,48 @@ crime_incidents <-
)

shotspotter <-
dpkg::stow("gh://geomarker-io/xx_address/shotspotter-v0.1.1") |>
dpkg::stow("gh://geomarker-io/xx_address/shotspotter-v0.1.2") |>
arrow::read_parquet() |>
mutate(geometry = sf::st_as_sfc(geometry)) |>
st_as_sf(crs = 4326) |>
select(date_time, lon_jittered, lat_jittered) |>
filter(!is.na(lon_jittered), !is.na(lat_jittered)) |>
st_as_sf(coords = c("lon_jittered", "lat_jittered"), crs = 4326) |>
st_transform(st_crs(cincy::tract_tigris_2010)) |>
st_join(cincy::tract_tigris_2010, largest = TRUE) |>
st_drop_geometry() |>
mutate(
year = lubridate::year(date_time),
month = lubridate::month(date_time)
) |>
select(-tlid, -address_x, -date_time) |>
select(-date_time) |>
group_by(census_tract_id_2010, year, month) |>
tally() |>
rename(gunshots = n)

reported_shootings <-
dpkg::stow("gh://geomarker-io/xx_address/reported_shootings-v0.1.0") |>
arrow::read_parquet() |>
select(date, lon_jittered, lat_jittered) |>
filter(!is.na(lon_jittered), !is.na(lat_jittered)) |>
st_as_sf(coords = c("lon_jittered", "lat_jittered"), crs = 4326) |>
st_transform(st_crs(cincy::tract_tigris_2010)) |>
st_join(cincy::tract_tigris_2010, largest = TRUE) |>
st_drop_geometry() |>
mutate(
year = lubridate::year(date),
month = lubridate::month(date)
) |>
select(-date) |>
group_by(census_tract_id_2010, year, month) |>
tally() |>
rename(reported_shootings = n)

all_tracts <-
cincy::tract_tigris_2010 |>
st_drop_geometry() |>
as_tibble() |>
mutate(date = list(seq.Date(
from = as.Date("2011-01-01"),
to = as.Date("2024-06-01"),
to = as.Date("2024-11-01"),
by = "month"
))) |>
tidyr::unnest(cols = c(date)) |>
Expand All @@ -64,18 +85,20 @@ all_tracts <-
select(-date)

d_out <-
left_join(all_tracts, crime_incidents, by = c("census_tract_id_2010", "year", "month")) |>
left_join(shotspotter, by = c("census_tract_id_2010", "year", "month")) |>
purrr::reduce(
.x = list(all_tracts, crime_incidents, shotspotter, reported_shootings),
.f = \(x, y) left_join(x, y, by = c("census_tract_id_2010", "year", "month"))
) |>
mutate(
across(c(property, violent, other, gunshots),
across(c(property:reported_shootings),
\(x) ifelse(is.na(x), 0, x))) |>
filter(!is.na(census_tract_id_2010))

out_dpkg <-
d_out |>
as_codec_dpkg(
name = "xx_address",
version = "0.1.0",
version = "0.2.0",
title = "Crime",
homepage = "https://geomarker.io/codec",
description = paste(readLines(fs::path_package("codec", "codec_data", "xx_address", "README.md")), collapse = "\n")
Expand Down

0 comments on commit 578ac66

Please sign in to comment.