Skip to content
Prev 29258 / 29559 Next

Mapping my own polygon?

Mike, thanks, again, for all your help. You got me started down the 
correct path.

Here's what's finally working for me:
========================================
library(tidyverse)
library(tidycensus)
library(sf)
library(tmap)
library(tigris)
options(tigris_use_cache = TRUE)
library(tmaptools)

rw_block_list <- c("3000", "3001", "3002", "3005", "3006", "3007",
                    "3008", "3009", "3010", "3011", "3012")

## Get the RW blocks from the census:
rw_blocks <- blocks(state = "MD",
                     county = "Baltimore city",
                     year = "2020") %>%
     filter(substr(GEOID20, 6, 11) == "271101" &
            substr(GEOID20, 12, 15) %in% rw_block_list)

## Create a map of just the RW blocks:
rw_base_blocks <- read_osm(bb(rw_blocks, ext = 1.3))

tmap_mode("plot")

## Line below gives map in meters
(RW_block_map <- tm_shape(rw_base_blocks, projection = 6487) +
## Line below gives map in degrees
## (RW_block_map <- tm_shape(rw_base_blocks, projection = 6487) +
      tm_rgb() +
      tm_shape(rw_blocks) +
      tm_fill("MAP_COLORS", alpha = 0.2, palette = "Accent") +
      tm_borders() +
      tm_scale_bar() +
      ## tm_grid() + tm_xlab("Long") + tm_ylab("Lat") +
      tm_grid() +
      tm_layout(title = "Radnor-Winston Neighborhood")
)

tmap_save(RW_block_map, "rw_map.png")
=======================================

Based on the map in meters, I can pick off the coordinates of a polygon 
in my neighborhood, and convert it to the correct location in degrees:

===========================================
## Test polygon:
base_x <- 433000
base_y <- 186000
rw_neigh_pg_m <- data.frame(
     matrix(
         c(300, 900,
           500, 900,
           500, 700,
           300, 700
         ),
         ncol = 2, byrow = TRUE)
) %>% + matrix(c(rep(base_x, nrow(.)), rep(base_y, nrow(.))),
                nrow = nrow(.)) %>%
sf::st_as_sf(coords = c(1,2), dim = "XY") %>%
summarize(geometry = st_combine(geometry)) %>%
st_cast("POLYGON") %>%
st_set_crs(6487)

## Convert to degrees:
(rw_neigh_pg_d <- rw_neigh_pg_m %>%
     st_transform(4326)
)
=============================================

Thanks, again, for all your help. I would have spent many additional 
hours on randomly trying things without your guidance.

-Kevin
On 6/12/23 21:47, Michael Sumner wrote:
Message-ID: <01000188b5335af6-a4e30994-08e6-4609-9414-ea8bd3e3cc7d-000000@email.amazonses.com>
In-Reply-To: <CAAcGz9_qMjDdUbQmhEVaC6HjyFNMuag7YEUvwzbhrXZ0-2oiBQ@mail.gmail.com>