Re: [R-sig-Geo] Adding Census polygons to OSM map?

2023-06-08 Thread Kevin Zembower via R-sig-Geo
Wow, Tim, that is just exactly what I was looking for. I wasn't aware of 
the tmaptools package at all, but even if I knew of it, I doubt that I 
could have put the pieces together to solve my problem.

Thanks so much for your efforts to help solve my problem. I really 
appreciate you sharing your knowledge and experience.

-Kevin

On 6/8/23 09:07, Howard, Tim G (DEC) wrote:
> And I just realized an easier way to get a bounding box for the basemap is to 
> use the spatial information
> you already have in the polygon:
> 
> basem <- read_osm(bb(rw_tract, ext = 1.5))
> 
> The 'ext' option allows you to expand a certain amount beyond the 
> neighborhood polygon.
> You can modify it to your liking.  And skip the lat/lon shenanigans.
> 
> Cheers,
> Tim
> 
>> -Original Message-
>> From: R-sig-Geo  On Behalf Of Howard,
>> Tim G (DEC) via R-sig-Geo
>> Sent: Thursday, June 8, 2023 8:53 AM
>> To: Kevin Zembower ; r-sig-geo@r-project.org
>> Subject: Re: [R-sig-Geo] Adding Census polygons to OSM map?
>>
>> ATTENTION: This email came from an external source. Do not open
>> attachments or click on links from unknown senders or unexpected emails.
>>
>>
>> Kevin,
>> To get the basemap in plot mode you need to download the tiles using
>> read_osm. This works for me, I Simplified a bit:
>>
>> library(sf)
>> library(tmap)
>> library(tigris)
>> options(tigris_use_cache = TRUE)
>> library(tmaptools)
>>
>> ## Get an example census map:
>> rw_tract <- tracts(state = "MD",
>> county = "Baltimore city",
>> year = "2020")
>> rw_tract <- rw_tract[rw_tract$NAME == "2711.01",]
>>
>> # for some reason had to re-jigger the box a bit.
>> # also note your longitudes were backwards.
>> lat_max <- 39.36
>> long_min <- -76.63
>> lat_min <- 39.34
>> long_max <- -76.60
>>
>> bbox <- bb(c(xmin=long_min, ymin=lat_min, xmax=long_max,
>> ymax=lat_max))
>>
>> basem <- read_osm(bbox)
>>
>> tmap_mode("plot")
>>
>> myMap <- tm_shape(basem) +
>>tm_rgb() +
>>tm_shape(rw_tract) +
>>tm_polygons(alpha = 0.2, col = "green") +
>>tm_scale_bar() +
>>tm_layout(title = "Radnor-Winston Neighborhood")
>>
>> # check it out
>> myMap
>> # save it.
>> tmap_save(myMap, "c:/temp/myMapOut.jpg")
>>
>>
>> Hope that helps.
>> Tim
>>
>>
>>
>>
>>
>>
>>> -Original Message-
>>> From: Kevin Zembower 
>>> Sent: Wednesday, June 7, 2023 5:59 PM
>>> To: Howard, Tim G (DEC) ; r-sig-geo@r-
>>> project.org
>>> Subject: Re: Adding Census polygons to OSM map?
>>>
>>> ATTENTION: This email came from an external source. Do not open
>>> attachments or click on links from unknown senders or unexpected emails.
>>>
>>>
>>> Tim, thank you very much. Yes, tmap seems to be moving in the right
>>> direction for me. This is what I can do with it so far:
>>> =
>>> ## Trying with tmap:
>>> library(tidyverse)
>>> library(sf)
>>> library(tmap)
>>> library(terra)
>>> library(spData)
>>> library(spDataLarge)
>>> library(tigris)
>>> options(tigris_use_cache = TRUE)
>>> library(OpenStreetMap)
>>>
>>> ## Get an Open Street Map:
>>> rw_map <- openmap(nw, se,
>>> type = "osm",
>>> mergeTiles = TRUE) %>%
>>>   openproj(projection = "+proj=longlat +ellps=WGS84 +datum=WGS84
>>> +no_defs")
>>>
>>> ## Get an example census map:
>>> rw_tract <- tracts(state = "MD",
>>>  county = "Baltimore city",
>>>  year = "2020") %>%
>>>   filter(NAME == "2711.01")
>>>
>>> ttm()
>>>
>>> ## Quick Tmap; also works:
>>> qtm(rw_tract)
>>>
>>> ## Also works. _polygons combines _fill and _borders:
>>> tm_shape(rw_tract) +
>>>   tm_polygons()
>>>
>>> ## Works:
>>> tm_shape(rw_tract) +
>>>   tm_polygons(alpha = 0.2, col = "green") +
>>>   tm_scale_bar() +
>>>   tm_layout(title = "Radnor-Winston Neighborhood") +
>>>   tm_basemap(server = "OpenS

Re: [R-sig-Geo] Adding Census polygons to OSM map?

2023-06-08 Thread Howard, Tim G (DEC) via R-sig-Geo
And I just realized an easier way to get a bounding box for the basemap is to 
use the spatial information
you already have in the polygon:

basem <- read_osm(bb(rw_tract, ext = 1.5))

The 'ext' option allows you to expand a certain amount beyond the neighborhood 
polygon.
You can modify it to your liking.  And skip the lat/lon shenanigans.

Cheers,
Tim

> -Original Message-
> From: R-sig-Geo  On Behalf Of Howard,
> Tim G (DEC) via R-sig-Geo
> Sent: Thursday, June 8, 2023 8:53 AM
> To: Kevin Zembower ; r-sig-geo@r-project.org
> Subject: Re: [R-sig-Geo] Adding Census polygons to OSM map?
>
> ATTENTION: This email came from an external source. Do not open
> attachments or click on links from unknown senders or unexpected emails.
>
>
> Kevin,
> To get the basemap in plot mode you need to download the tiles using
> read_osm. This works for me, I Simplified a bit:
>
> library(sf)
> library(tmap)
> library(tigris)
> options(tigris_use_cache = TRUE)
> library(tmaptools)
>
> ## Get an example census map:
> rw_tract <- tracts(state = "MD",
>county = "Baltimore city",
>year = "2020")
> rw_tract <- rw_tract[rw_tract$NAME == "2711.01",]
>
> # for some reason had to re-jigger the box a bit.
> # also note your longitudes were backwards.
> lat_max <- 39.36
> long_min <- -76.63
> lat_min <- 39.34
> long_max <- -76.60
>
> bbox <- bb(c(xmin=long_min, ymin=lat_min, xmax=long_max,
> ymax=lat_max))
>
> basem <- read_osm(bbox)
>
> tmap_mode("plot")
>
> myMap <- tm_shape(basem) +
>   tm_rgb() +
>   tm_shape(rw_tract) +
>   tm_polygons(alpha = 0.2, col = "green") +
>   tm_scale_bar() +
>   tm_layout(title = "Radnor-Winston Neighborhood")
>
> # check it out
> myMap
> # save it.
> tmap_save(myMap, "c:/temp/myMapOut.jpg")
>
>
> Hope that helps.
> Tim
>
>
>
>
>
>
> > -Original Message-
> > From: Kevin Zembower 
> > Sent: Wednesday, June 7, 2023 5:59 PM
> > To: Howard, Tim G (DEC) ; r-sig-geo@r-
> > project.org
> > Subject: Re: Adding Census polygons to OSM map?
> >
> > ATTENTION: This email came from an external source. Do not open
> > attachments or click on links from unknown senders or unexpected emails.
> >
> >
> > Tim, thank you very much. Yes, tmap seems to be moving in the right
> > direction for me. This is what I can do with it so far:
> > =
> > ## Trying with tmap:
> > library(tidyverse)
> > library(sf)
> > library(tmap)
> > library(terra)
> > library(spData)
> > library(spDataLarge)
> > library(tigris)
> > options(tigris_use_cache = TRUE)
> > library(OpenStreetMap)
> >
> > ## Get an Open Street Map:
> > rw_map <- openmap(nw, se,
> >type = "osm",
> >mergeTiles = TRUE) %>%
> >  openproj(projection = "+proj=longlat +ellps=WGS84 +datum=WGS84
> > +no_defs")
> >
> > ## Get an example census map:
> > rw_tract <- tracts(state = "MD",
> > county = "Baltimore city",
> > year = "2020") %>%
> >  filter(NAME == "2711.01")
> >
> > ttm()
> >
> > ## Quick Tmap; also works:
> > qtm(rw_tract)
> >
> > ## Also works. _polygons combines _fill and _borders:
> > tm_shape(rw_tract) +
> >  tm_polygons()
> >
> > ## Works:
> > tm_shape(rw_tract) +
> >  tm_polygons(alpha = 0.2, col = "green") +
> >  tm_scale_bar() +
> >  tm_layout(title = "Radnor-Winston Neighborhood") +
> >  tm_basemap(server = "OpenStreetMap")
> >
> > ===
> >
> > So, in that last example, I can plot the basemap from Open Street Maps
> > with the Census tract on top of it. However, from what I've learned so
> > far (just about 4 hours of study), tm_basemap() only works with
> > interactive maps that are 'viewed' (in a browser as HTML) rather than
> > plotted (printed). I don't understand why I can't just get the
> > appearance I want without all the unwanted interactivity features.
> >
> > To take it a step further, I don't understand why I can't just plot
> > both the basemap and the census tract with something like plot() (from
> > ggplot) and sf.
> >
> > Thanks, again, Tim, for your suggestion. I think it's moving me in the
> 

Re: [R-sig-Geo] Adding Census polygons to OSM map?

2023-06-08 Thread Howard, Tim G (DEC) via R-sig-Geo
Kevin, 
To get the basemap in plot mode you need to download the tiles using read_osm. 
This works for me, I 
Simplified a bit:

library(sf)
library(tmap)
library(tigris)
options(tigris_use_cache = TRUE)
library(tmaptools)

## Get an example census map:
rw_tract <- tracts(state = "MD",
   county = "Baltimore city",
   year = "2020") 
rw_tract <- rw_tract[rw_tract$NAME == "2711.01",]

# for some reason had to re-jigger the box a bit.
# also note your longitudes were backwards. 
lat_max <- 39.36 
long_min <- -76.63
lat_min <- 39.34 
long_max <- -76.60 

bbox <- bb(c(xmin=long_min, ymin=lat_min, xmax=long_max, ymax=lat_max))

basem <- read_osm(bbox)

tmap_mode("plot")

myMap <- tm_shape(basem) +
  tm_rgb() +
  tm_shape(rw_tract) +
  tm_polygons(alpha = 0.2, col = "green") + 
  tm_scale_bar() +
  tm_layout(title = "Radnor-Winston Neighborhood")

# check it out
myMap
# save it. 
tmap_save(myMap, "c:/temp/myMapOut.jpg")


Hope that helps. 
Tim






> -Original Message-
> From: Kevin Zembower 
> Sent: Wednesday, June 7, 2023 5:59 PM
> To: Howard, Tim G (DEC) ; r-sig-geo@r-
> project.org
> Subject: Re: Adding Census polygons to OSM map?
> 
> ATTENTION: This email came from an external source. Do not open
> attachments or click on links from unknown senders or unexpected emails.
> 
> 
> Tim, thank you very much. Yes, tmap seems to be moving in the right
> direction for me. This is what I can do with it so far:
> =
> ## Trying with tmap:
> library(tidyverse)
> library(sf)
> library(tmap)
> library(terra)
> library(spData)
> library(spDataLarge)
> library(tigris)
> options(tigris_use_cache = TRUE)
> library(OpenStreetMap)
> 
> ## Get an Open Street Map:
> rw_map <- openmap(nw, se,
>type = "osm",
>mergeTiles = TRUE) %>%
>  openproj(projection = "+proj=longlat +ellps=WGS84 +datum=WGS84
> +no_defs")
> 
> ## Get an example census map:
> rw_tract <- tracts(state = "MD",
> county = "Baltimore city",
> year = "2020") %>%
>  filter(NAME == "2711.01")
> 
> ttm()
> 
> ## Quick Tmap; also works:
> qtm(rw_tract)
> 
> ## Also works. _polygons combines _fill and _borders:
> tm_shape(rw_tract) +
>  tm_polygons()
> 
> ## Works:
> tm_shape(rw_tract) +
>  tm_polygons(alpha = 0.2, col = "green") +
>  tm_scale_bar() +
>  tm_layout(title = "Radnor-Winston Neighborhood") +
>  tm_basemap(server = "OpenStreetMap")
> 
> ===
> 
> So, in that last example, I can plot the basemap from Open Street Maps with
> the Census tract on top of it. However, from what I've learned so far (just
> about 4 hours of study), tm_basemap() only works with interactive maps
> that are 'viewed' (in a browser as HTML) rather than plotted (printed). I 
> don't
> understand why I can't just get the appearance I want without all the
> unwanted interactivity features.
> 
> To take it a step further, I don't understand why I can't just plot both the
> basemap and the census tract with something like plot() (from
> ggplot) and sf.
> 
> Thanks, again, Tim, for your suggestion. I think it's moving me in the right
> direction.
> 
> -Kevin
> 
> On 6/7/23 10:57, Howard, Tim G (DEC) wrote:
> > Kevin,
> > the tmap​ package might be what you are looking for.
> >
> > https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fcran
> > .r-project.org%2Fweb%2Fpackages%2Ftmap%2Fvignettes%2Ftmap-
> getstarted.h
> >
> tml=05%7C01%7Ctim.howard%40dec.ny.gov%7C4d3f99e6eeff4493a3a
> 108db6
> >
> 7a26858%7Cf46cb8ea79004d108ceb80e8c1c81ee7%7C0%7C0%7C63821771946
> 393381
> >
> 9%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzI
> iLCJBTiI6
> >
> Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C=MY4b3C%2Bqm7w
> ycxiX%2F3ww5
> > zMLltw59fmCgQTAeSxGu60%3D=0
> >
> > https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fcran
> > .r-
> project.org%2Fweb%2Fpackages%2Ftmap%2Findex.html=05%7C01%7
> Ctim
> >
> .howard%40dec.ny.gov%7C4d3f99e6eeff4493a3a108db67a26858%7Cf46cb8e
> a7900
> >
> 4d108ceb80e8c1c81ee7%7C0%7C0%7C638217719463933819%7CUnknown%7
> CTWFpbGZs
> >
> b3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn
> 0%3D
> >
> %7C3000%7C%7C%7C=R61uJO4g0WW1GLh47uXGCB%2FX6%2B9grO4
> WYue3XxkfdQ0
> > %3D=0
> >
> >
> > Cheers,
> > Tim
> >
> > From: R-sig-Geo  on behalf of Kevin
> > Zembower via R-sig-Geo 
> > Sent: Monday, June 5, 2023 11:21 AM
> > To: r-sig-geo@r-project.org 
> > Subject: [R-sig-Geo] Adding Census polygons to OSM map?
> >
> > ATTENTION: This email came from an external source. Do not open
> attachments or click on links from unknown senders or unexpected emails.
> >
> >
> > Hello, again,
> >
> > I've given up my work with leaflet, trying to map my neighborhood with
> > US Census boundaries. Even though it was quick and easy to add the
> > Census boundaries to the map, I couldn't create the labels I wanted
> > (see
> > 

Re: [R-sig-Geo] Adding Census polygons to OSM map?

2023-06-07 Thread Kevin Zembower via R-sig-Geo
Tim, thank you very much. Yes, tmap seems to be moving in the right 
direction for me. This is what I can do with it so far:
=
## Trying with tmap:
library(tidyverse)
library(sf)
library(tmap)
library(terra)
library(spData)
library(spDataLarge)
library(tigris)
options(tigris_use_cache = TRUE)
library(OpenStreetMap)

## Get an Open Street Map:
rw_map <- openmap(nw, se,
   type = "osm",
   mergeTiles = TRUE) %>%
 openproj(projection = "+proj=longlat +ellps=WGS84 +datum=WGS84 
+no_defs")

## Get an example census map:
rw_tract <- tracts(state = "MD",
county = "Baltimore city",
year = "2020") %>%
 filter(NAME == "2711.01")

ttm()

## Quick Tmap; also works:
qtm(rw_tract)

## Also works. _polygons combines _fill and _borders:
tm_shape(rw_tract) +
 tm_polygons()

## Works:
tm_shape(rw_tract) +
 tm_polygons(alpha = 0.2, col = "green") +
 tm_scale_bar() +
 tm_layout(title = "Radnor-Winston Neighborhood") +
 tm_basemap(server = "OpenStreetMap")

===

So, in that last example, I can plot the basemap from Open Street Maps 
with the Census tract on top of it. However, from what I've learned so 
far (just about 4 hours of study), tm_basemap() only works with 
interactive maps that are 'viewed' (in a browser as HTML) rather than 
plotted (printed). I don't understand why I can't just get the 
appearance I want without all the unwanted interactivity features.

To take it a step further, I don't understand why I can't just plot both 
the basemap and the census tract with something like plot() (from 
ggplot) and sf.

Thanks, again, Tim, for your suggestion. I think it's moving me in the 
right direction.

-Kevin

On 6/7/23 10:57, Howard, Tim G (DEC) wrote:
> Kevin,
> the tmap​ package might be what you are looking for.
> 
> https://cran.r-project.org/web/packages/tmap/vignettes/tmap-getstarted.html
> 
> https://cran.r-project.org/web/packages/tmap/index.html
> 
> 
> Cheers,
> Tim
> 
> From: R-sig-Geo  on behalf of Kevin Zembower 
> via R-sig-Geo 
> Sent: Monday, June 5, 2023 11:21 AM
> To: r-sig-geo@r-project.org 
> Subject: [R-sig-Geo] Adding Census polygons to OSM map?
> 
> ATTENTION: This email came from an external source. Do not open attachments 
> or click on links from unknown senders or unexpected emails.
> 
> 
> Hello, again,
> 
> I've given up my work with leaflet, trying to map my neighborhood with
> US Census boundaries. Even though it was quick and easy to add the
> Census boundaries to the map, I couldn't create the labels I wanted (see
> https://stat.ethz.ch/pipermail/r-sig-geo/2023-June/029284.html). Also,
> it seems like leaflet added a lot of overhead that I didn't need, such
> as interactive maps. I just need a color printed 2D map for my use.
> 
> I'm now trying to work with tigris and OpenStreetMap, but I can't draw
> the US Census boundaries on the OSM map. Here's what I have so far:
> 
> ==
> ## Experiment, using sf:
> library(tidyverse)
> library(tigris)
> options(tigris_use_cache = TRUE)
> library(sf)
> library(OpenStreetMap)
> ## library(sp)
> ## library(ggplot2)
> 
> lat_max <- 39.3525 #Distance from 39.35 to 39.34 = 0.691mi
> long_max <- -76.617 #Distance from -76.61 to -76.62 = 0.5343 mi
> lat_min <- 39.3455
> long_min <- -76.6095
> nw <- c(lat_max, long_max)
> se <- c(lat_min, long_min)
> 
> rw_map <- openmap(nw, se,
> type = "osm",
> mergeTiles = TRUE) %>%
>   openproj() %>%
>   OpenStreetMap::autoplot.OpenStreetMap() +
>   xlab("long") + ylab("lat")
> 
> rw_map
> 
> rw_tract <- tracts(state = "MD",
>   county = "Baltimore city",
>   year = "2020") %>%
>   filter(NAME == "2711.01")
>   ## openproj()
>   ## st_transform('+proj=longlat +datum=WGS84')
>   ## spTransform('osm')
> 
> OpenStreetMap::autoplot.OpenStreetMap(rw_tract, add = TRUE)
> ==
> 
> The commented out sections show some of the things I've tried so far.
> I'd like to be able to draw the rw_tract geometry data on the rw_map
> image. What I'd like is a 'addPolygon()' function in OpenStreetMap, like
> I found in leaflet.
> 
> Can anyone offer me any suggestions or advice for accomplishing my task?
> Thanks so much.
> 
> -Kevin
> 
> ___
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo


___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


Re: [R-sig-Geo] Adding Census polygons to OSM map?

2023-06-07 Thread Howard, Tim G (DEC) via R-sig-Geo
Kevin,
the tmap​ package might be what you are looking for.

https://cran.r-project.org/web/packages/tmap/vignettes/tmap-getstarted.html

https://cran.r-project.org/web/packages/tmap/index.html


Cheers,
Tim

From: R-sig-Geo  on behalf of Kevin Zembower 
via R-sig-Geo 
Sent: Monday, June 5, 2023 11:21 AM
To: r-sig-geo@r-project.org 
Subject: [R-sig-Geo] Adding Census polygons to OSM map?

ATTENTION: This email came from an external source. Do not open attachments or 
click on links from unknown senders or unexpected emails.


Hello, again,

I've given up my work with leaflet, trying to map my neighborhood with
US Census boundaries. Even though it was quick and easy to add the
Census boundaries to the map, I couldn't create the labels I wanted (see
https://stat.ethz.ch/pipermail/r-sig-geo/2023-June/029284.html). Also,
it seems like leaflet added a lot of overhead that I didn't need, such
as interactive maps. I just need a color printed 2D map for my use.

I'm now trying to work with tigris and OpenStreetMap, but I can't draw
the US Census boundaries on the OSM map. Here's what I have so far:

==
## Experiment, using sf:
library(tidyverse)
library(tigris)
options(tigris_use_cache = TRUE)
library(sf)
library(OpenStreetMap)
## library(sp)
## library(ggplot2)

lat_max <- 39.3525 #Distance from 39.35 to 39.34 = 0.691mi
long_max <- -76.617 #Distance from -76.61 to -76.62 = 0.5343 mi
lat_min <- 39.3455
long_min <- -76.6095
nw <- c(lat_max, long_max)
se <- c(lat_min, long_min)

rw_map <- openmap(nw, se,
   type = "osm",
   mergeTiles = TRUE) %>%
 openproj() %>%
 OpenStreetMap::autoplot.OpenStreetMap() +
 xlab("long") + ylab("lat")

rw_map

rw_tract <- tracts(state = "MD",
 county = "Baltimore city",
 year = "2020") %>%
 filter(NAME == "2711.01")
 ## openproj()
 ## st_transform('+proj=longlat +datum=WGS84')
 ## spTransform('osm')

OpenStreetMap::autoplot.OpenStreetMap(rw_tract, add = TRUE)
==

The commented out sections show some of the things I've tried so far.
I'd like to be able to draw the rw_tract geometry data on the rw_map
image. What I'd like is a 'addPolygon()' function in OpenStreetMap, like
I found in leaflet.

Can anyone offer me any suggestions or advice for accomplishing my task?
Thanks so much.

-Kevin

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo