Skip to content

Commit

Permalink
succumb to hexen ... closes #185
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge committed Oct 9, 2019
1 parent 2b4a9f1 commit 2349737
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 18 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ makefile$
^src/Makevars$
^src/Makevars.in$
^tests/timing-benchmark.R$
data-raw/
^\.travis\.yml$
^\.Rproj\.user$
^codemeta\.json$
8 changes: 7 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,20 @@ knitr::opts_chunk$set(
```


# osmdata <a href='https://docs.ropensci.org/osmdata/'><img src='man/figures/osmhex.png' align="right" height=210 width=182/></a>



[![Build Status](https://travis-ci.org/ropensci/osmdata.svg?branch=master)](https://travis-ci.org/ropensci/osmdata)
[![Build status](https://ci.appveyor.com/api/projects/status/github/ropensci/osmdata?svg=true)](https://ci.appveyor.com/project/ropensci/osmdata)
[![codecov](https://codecov.io/gh/ropensci/osmdata/branch/master/graph/badge.svg)](https://codecov.io/gh/ropensci/osmdata)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/osmdata)](http://cran.r-project.org/web/packages/osmdata)
[![CRAN Downloads](http://cranlogs.r-pkg.org/badges/grand-total/osmdata?color=orange)](http://cran.r-project.org/package=osmdata)
[![Project Status: Active](http://www.repostatus.org/badges/latest/active.svg)](http://www.repostatus.org/#active)

![](./man/figures/title.png)
<!--![](./man/figures/title.png)-->

<\br>
[![](https://badges.ropensci.org/103_status.svg)](https://github.com/ropensci/onboarding/issues/103)
[![status](http://joss.theoj.org/papers/0f59fb7eaeb2004ea510d38c00051dd3/status.svg)](http://joss.theoj.org/papers/0f59fb7eaeb2004ea510d38c00051dd3)

Expand Down
30 changes: 13 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<!-- README.md is generated from README.Rmd. Please edit that file -->

# osmdata <a href='https://docs.ropensci.org/osmdata/'><img src='man/figures/osmhex.png' align="right" height=210 width=182/></a>

[![Build
Status](https://travis-ci.org/ropensci/osmdata.svg?branch=master)](https://travis-ci.org/ropensci/osmdata)
[![Build
Expand All @@ -11,7 +13,9 @@ Downloads](http://cranlogs.r-pkg.org/badges/grand-total/osmdata?color=orange)](h
[![Project Status:
Active](http://www.repostatus.org/badges/latest/active.svg)](http://www.repostatus.org/#active)

![](./man/figures/title.png)
<!--![](./man/figures/title.png)-->

\<\>
[![](https://badges.ropensci.org/103_status.svg)](https://github.com/ropensci/onboarding/issues/103)
[![status](http://joss.theoj.org/papers/0f59fb7eaeb2004ea510d38c00051dd3/status.svg)](http://joss.theoj.org/papers/0f59fb7eaeb2004ea510d38c00051dd3)

Expand All @@ -24,8 +28,8 @@ can be used to download raster tiles based on OSM data.)
[Overpass](https://overpass-turbo.eu) is a read-only API that extracts
custom selected parts of OSM data. Data can be returned in a variety of
formats, including as [Simple Features
(`sf`)](https://cran.r-project.org/package=sf) or [Spatial
[Spatial (`sp`)](https://cran.r-project.org/package=sp), or [Silicate
(`sf`)](https://cran.r-project.org/package=sf), [Spatial
(`sp`)](https://cran.r-project.org/package=sp), or [Silicate
(`sc`)](https://github.com/hypertidy/silicate) objects.

## Installation
Expand All @@ -47,7 +51,7 @@ To load the package and check the version:
library(osmdata)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright
packageVersion("osmdata")
#> [1] '0.0.9.1'
#> [1] '0.1.1.4'
```

## Usage
Expand All @@ -59,8 +63,7 @@ converted to [`R Simple Features
(sf)`](https://cran.r-project.org/package=sf) objects with
`osmdata_sf()` or to [`R Spatial
(sp)`](https://cran.r-project.org/package=sp) objects with
`osmdata_sp()`. For
example,
`osmdata_sp()`. For example,

``` r
x <- opq(bbox = c(-0.27, 51.47, -0.20, 51.50)) %>% # Chiswick Eyot in London, U.K.
Expand Down Expand Up @@ -115,14 +118,8 @@ boxes:
``` r
b <- getbb ("bangalore", format_out = "polygon")
class (b); head (b [[1]])
#> [1] "list"
#> [,1] [,2]
#> [1,] 77.46010 12.90384
#> [2,] 77.46022 12.90045
#> [3,] 77.46211 12.89759
#> [4,] 77.46246 12.89686
#> [5,] 77.46499 12.89312
#> [6,] 77.46547 12.89238
#> [1] "matrix"
#> [1] 77.4601
```

### Features
Expand Down Expand Up @@ -196,7 +193,7 @@ returned in a variety of formats, currently including:

Logical OR combinations can be implemented with the package’s internal
`c` method, so that the above example can be extended to all amenities
that are either restanrants OR pubs with
that are either restaurants OR pubs with

``` r
pubs <- opq ("portsmouth usa") %>%
Expand Down Expand Up @@ -254,7 +251,6 @@ citation ("osmdata")

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

[![ropensci\_footer](http://ropensci.org/public_images/github_footer.png)](http://ropensci.org)
200 changes: 200 additions & 0 deletions data-raw/hex-script.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@

# hex sticker script (issue 185)

## Get a background map, here of Kichijoji in Tokyo

```{r}
library (osmdata)
library (osmplotr)
library (magrittr)
bb <- getbb ("kichijoji japan")
bb [2, 1] <- bb [2, 1] - 0.2 * diff (bb [2, ])
bb [2, 2] <- mean (bb [2, ])
hw <- opq (bb) %>%
add_osm_feature (key = "highway") %>%
osmdata_sf (quiet = FALSE) %>%
osm_poly2line () %>%
extract2 ("osm_lines")
b <- opq (bb) %>%
add_osm_feature (key = "building") %>%
osmdata_sf (quiet = FALSE) %>%
extract2 ("osm_polygons")
g1 <- opq (bb) %>%
add_osm_feature (key = "landuse", value = "grass") %>%
osmdata_sf (quiet = FALSE) %>%
extract2 ("osm_polygons")
g2 <- opq (bb) %>%
add_osm_feature (key = "leisure", value = "park") %>%
osmdata_sf (quiet = FALSE) %>%
extract2 ("osm_polygons")
g <- sf::st_sf (osm_id = c (g1$osm_id, g2$osm_id),
geometry = c (g1$geometry, g2$geometry))
w <- opq (bb) %>%
add_osm_feature (key = "natural", value = "water") %>%
osmdata_sf (quiet = FALSE) %>%
extract2 ("osm_polygons")
#osm_structures (col_scheme = "light")
map <- osm_basemap (bbox = bb, bg = "gray95") %>%
add_osm_objects (hw, col = "#969696FF") %>%
add_osm_objects (b, col = "#C8C8C8FF") %>%
add_osm_objects (g, col = "#C8FFC8FF") %>%
add_osm_objects (w, col = "#C8C8DCFF")
#print_osm_map (map, filename = "kichijoji.png")
saveRDS (map, file = "map.Rds")
```

Reduce image to square:
```{r}
library (magrittr)
map <- readRDS ("map.Rds")
f <- "kichijoji.png"
osmplotr::print_osm_map (map, filename = f)
magick::image_read (f) %>%
magick::image_trim () %>%
magick::image_write (f)
i <- magick::image_read (f) %>%
magick::image_info ()
chop <- floor (i$width - i$height) / 2
chop <- paste0 (i$height, "x", i$height, "+", chop, "+0")
magick::image_read (f) %>%
magick::image_crop (chop) %>%
magick::image_write (f)
```



# crop image to hex

image is 1260 h X 2100 w

```{r}
f <- "kichijoji.png"
library (magrittr)
img <- png::readPNG (f)
# define hexagon and set all outer pixels to 1
s3 <- sqrt (3) / 2
border <- data.frame (x = 1 + c (rep (-s3, 2), 0, rep (s3, 2), 0, -s3),
y = 1 + c (0.5, -0.5, -1, -0.5, 0.5, 1, 0.5))
border$x <- round (dim (img) [2] * border$x / 2)
border$y <- round (dim (img) [1] * border$y / 2)
#h <- 7
#w <- h * diff (range (border$x)) / diff (range (border$y))
#x11 (width = w, height = h)
#plot (border, type = "l")
border
library (sp)
library (raster)
p <- Polygon (border)
p <- SpatialPolygons (list (Polygons (list (p), "p")))
n1 <- dim (img) [1]
n2 <- dim (img) [2]
r <- raster (nrows = n1, ncols = n2, xmn = 1, xmx = n2, ymn = 1, ymx = n1,
vals = TRUE)
r [mask (r, p)] <- FALSE
r <- !r
#plot (r)
r <- as.matrix (r)
index <- which (!r)
index_not <- which (r)
for (i in 1:3) {
i1 <- img [, , i]
i1 [index] <- 0
img [, , i] <- i1
}
mmand::display (img)
# Then add a 4th channel for alpha values
img4 <- array (dim = c (dim (img) [1:2], 4))
for (i in 1:3) {
i1 <- img [, , i]
img4 [, , i] <- i1
i1 [index] <- 0
i1 [index_not] <- 1
img4 [, , 4] <- i1
}
png::writePNG (img4, f)
```

## make the hex logo

```{r}
library (ggplot2)
# trace outline of hexagon from centre bottom point in anti-clockwise direction
s3 <- sqrt (3) / 2
border <- data.frame (x = 1 + c (rep (-s3, 2), 0, rep (s3, 2), 0, -s3),
y = 1 + c (0.5, -0.5, -1, -0.5, 0.5, 1, 0.5))
asp <- diff (range (border$x)) / diff (range (border$y)) # aspect ratio for image
f <- "kichijoji.png"
d <- data.frame(x = 1, y = 1, image = f)
size <- 1.0
hex <- ggplot() +
ggimage::geom_image (aes_ (x = ~x, y = ~y, image = ~image), d,
size = 1.05, asp = asp) +
geom_polygon (aes_ (x = ~x, y = ~y), data = border,
size = 5, fill = NA, color = "#555555")
#extrafont::loadfonts ()
lab_dat <- data.frame (x = 1 - 0.0001,
y = 1 + 0.0001,
lab = 'osmdata')
aes <- ggplot2::aes (x, y, label = lab)
fs <- 30 # font size
hex <- hex + ggplot2::geom_text (dat = lab_dat,
mapping = aes,
size = fs,
colour = 'gray80',
family = 'SF Alien Encounters',
fontface = 1,
nudge_y = 0.0001,
nudge_x = 0.0001)
hex <- hex + ggplot2::geom_text (dat = lab_dat,
mapping = aes,
size = fs,
colour = 'black',
fontface = 1,
family = 'SF Alien Encounters')
th <- theme_minimal ()
th$panel.background <- element_rect (fill = "transparent", size = 0)
th$line <- element_blank ()
th$axis.text <- element_blank ()
th$axis.title <- element_blank ()
th$plot.margin <- margin (rep (unit (0, 'null'), 4))
#th$plot.margin <- margin (rep (unit (-0.5, 'line'), 4))
th$legend.position <- 'none'
th$axis.ticks.length <- unit (0, 'null')
hex <- hex + th
print (hex)
```
```{r}
fname <- file.path (here::here (), "man", "figures", "osmhex.png")
ggsave (hex, filename = fname, width = 7, height = 7 * asp)
```

it is then necessary to read the png back in and re-convert the border pixels
to alpha = 0
```{r}
fname <- file.path (here::here (), "man", "figures", "osmhex.png")
img <- png::readPNG (fname)
img4 <- array (1, dim = c (dim (img) [1:2], 4))
index_out <- which (img [, , 1] == 1 & img [, , 2] == 1 & img [, , 3] == 1)
index_in <- which (!seq (img [, , 1]) %in% index_out)
for (i in 1:3) {
i1 <- img [, , i]
img4 [, , i] <- i1
i1 [index_in] <- 1
i1 [index_out] <- 0
img4 [, , 4] <- i1
}
fname <- file.path (here::here (), "man", "figures", "osmhex.png")
png::writePNG (img4, fname)
```


Binary file added man/figures/osmhex.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2349737

Please sign in to comment.