0

I have the following two shapefiles:

> summary(precincts1)

Object of class SpatialPolygonsDataFrame

Coordinates:
        min       max
x -74.25545 -73.70002
y  40.49613  40.91540

    Precinct        Shape_Leng       Shape_Area       
 Min.   :  1.00   Min.   : 17083   Min.   : 15286897  
 1st Qu.: 31.50   1st Qu.: 29900   1st Qu.: 37593804  
 Median : 64.50   Median : 46887   Median : 65891025  
 Mean   : 62.57   Mean   : 65720   Mean   :111231564  
 3rd Qu.: 95.50   3rd Qu.: 76375   3rd Qu.:133644443  
 Max.   :123.00   Max.   :309518   Max.   :781725787  

and

> summary(bnd_nhd)

Object of class SpatialPolygonsDataFrame

Coordinates:
       min       max
x 871512.3  912850.5
y 982994.4 1070956.9

   SHAPE_area         SHAPE_len    
 Min.   : 3173813   Min.   : 7879  
 1st Qu.: 9687122   1st Qu.:13514  
 Median :14363449   Median :17044  
 Mean   :19674314   Mean   :19516  
 3rd Qu.:27161251   3rd Qu.:23821  
 Max.   :68101106   Max.   :49269  

Their coordinate systems are different. I can overlay the shapes for "precincts1" on the map with leaflet, but I cannot do the same with for "bnd_nhd". I am using shiny, maptools, and leaflet. How can I convert the shapefile or change the setting on the map so that I can overlay the map for "bnd_nhd"?

chrki
  • 6,143
  • 6
  • 35
  • 55
Ibuki
  • 13
  • 1
  • 1
    You have to transform these coordinates into a common coordinate system that also works on Leaflet, take a look at this: http://stackoverflow.com/questions/33045388/projecting-my-shapefile-data-on-leaflet-map-using-r – chrki Jan 23 '16 at 23:58
  • I tried the following code: > bnd_nhd <- readShapeSpatial("/Users/Ibuki/Sample Shiny App/St. Louis_nbrhds_wards/BND_Nhd88_cw") > proj4string(bnd_nhd) <- CRS("+init=epsg:32610") > bnd_nhd_latlon <- spTransform(bnd_nhd, CRS("+init=epsg:4326")) – Ibuki Jan 24 '16 at 05:42
  • Although the transfromation was successful, the polygons are at wrong spots. And I have no idea how I can project the file correctly and transform it correctly. It is ESRI shape file of St. Louis, MO obtained from http://data.stlouis-mo.gov/downloads.cfm – Ibuki Jan 24 '16 at 05:45
  • The file is under "GIS information" named "Neighborhood & Wards". Please let me know if anyone has any further suggestions to solve this problem. Thank you. – Ibuki Jan 24 '16 at 05:46

1 Answers1

4

This should work:

library("rgdal")
library("leaflet")

bnd_nhd <- readOGR("C:/data/BND_Nhd88_cw.shp",
                  layer="BND_Nhd88_cw")
pol_wrd <- readOGR("C:/data/POL_WRD_2010_Prec.shp",
                  layer="POL_WRD_2010_Prec")

bnd_nhd4326 <- spTransform(bnd_nhd, CRS("+init=epsg:4326"))
pol_wrd4326 <- spTransform(pol_wrd, CRS("+init=epsg:4326"))

m <- leaflet() %>% 
  addTiles() %>%
  addPolygons(data=bnd_nhd4326, weight=2, color="red", group="bnd_nhd") %>%
  addPolygons(data=pol_wrd4326, weight=2, color="blue", group="pol_wrd") %>%
  addLayersControl(
    overlayGroups = c("bnd_nhd", "pol_wrd"),
    options = layersControlOptions(collapsed = FALSE)
  )
m

R Leaflet result

chrki
  • 6,143
  • 6
  • 35
  • 55
  • Thank you very much!! I tried, but I kept getting the following error. Would you be able to tell the problem here? – Ibuki Jan 25 '16 at 01:14
  • > bnd_nhd <- readOGR("/Users/Ibuki/Sample Shiny App/St. Louis_nbrhds_wards/BND_Nhd88_cw.shp", layer="BND_NHD88_cw") Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, : Layer not found – Ibuki Jan 25 '16 at 01:14
  • > bnd_nhd <- readOGR("C:/Users/Ibuki/Sample Shiny App/St. Louis_nbrhds_wards/BND_Nhd88_cw.shp", layer="BND_NHD88_cw") Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, : Cannot open file – Ibuki Jan 25 '16 at 15:49
  • `Layer not found`: Layer names are case sensitive, "BND_Nhd88_cw" is not the same as "BND_NHD88_cw" (you can check this with a GIS program like QGIS or ArcGIS Desktop). `Cannot open file`: Please make sure the path/folder and file names are correct – chrki Jan 25 '16 at 18:19