Home > Software engineering >  Overlay bathymetric data onto OSM multipolygons
Overlay bathymetric data onto OSM multipolygons

Time:08-10

I want to draw the map of a lake with the bathymetries I have taken with the sonar. I have a .sl2 file (Lowrance sonar) that I have converted to .csv (Namely, Sonar_13_07_1.csv). Finally I get 3 columns with 30665 rows, here is an example of the first rows:

latitude,longitude,waterDepthM
39.8197123940846,-3.11133523036904,0
39.8197193169248,-3.11133523036904,0
39.8197193169248,-3.11134424374202,0
39.8197262397644,-3.11134424374202,0
39.8197331626032,-3.11135325711499,0
39.8197400854413,-3.11135325711499,0
39.8197470082787,-3.11135325711499,0
39.8197539311154,-3.11135325711499,0
39.8197608539514,-3.11135325711499,0
39.8197608539514,-3.11134424374202,0
39.8197677767867,-3.11134424374202,0
39.8197677767867,-3.11135325711499,0
39.8197746996213,-3.11135325711499,0
39.8197746996213,-3.11134424374202,0
39.8197816224553,-3.11134424374202,0
39.8197885452885,-3.11134424374202,0
39.8197885452885,-3.11133523036904,0
39.819795468121,-3.11133523036904,0
39.8198023909528,-3.11132621699607,0
39.8198023909528,-3.11133523036904,0
39.819809313784,-3.11132621699607,0
39.8198162366144,-3.11132621699607,0
39.8198231594442,-3.11132621699607,0
39.8198370051015,-3.11132621699607,0
39.8198300822732,-3.11132621699607,0
39.8198439279292,-3.11132621699607,0
39.8198508507561,-3.11133523036904,0
39.8198508507561,-3.11132621699607,0
39.8198577735824,-3.11133523036904,0
39.8198646964079,-3.11133523036904,0
39.8198646964079,-3.11134424374202,0
39.8198716192328,-3.11135325711499,0
39.8198716192328,-3.11134424374202,0
39.8198716192328,-3.11136227048797,0
39.8198785420569,-3.11135325711499,0
39.8198716192328,-3.11136227048797,-0.691144658182553
39.8198785420569,-3.11135325711499,-0.691144658182553
39.8198716192328,-3.11136227048797,-0.72783260768886
39.8198785420569,-3.11135325711499,-0.72783260768886
39.8198716192328,-3.11136227048797,-0.735494858005278
39.8198785420569,-3.11135325711499,-0.735494858005278
39.8198716192328,-3.11136227048797,-0.754367615888273
39.8198785420569,-3.11135325711499,-0.754367615888273
39.8198716192328,-3.11136227048797,-0.762954301055886
39.8198785420569,-3.11135325711499,-0.762954301055886
39.8198785420569,-3.11136227048797,-0.762954301055886

I manage to plot it like this:

library(ggplot2)

ggplot(Sonar_13_07_1, aes(longitude, latitude, colour = waterDepthM))   geom_point()   coord_equal()   xlim(x_coords)   ylim(y_coords)

On the other hand, I plotted the base map of the lake:

library(osmdata)
  library(sf)

> x_coords <- c(-3.109, -3.117)
> y_coords <- c(39.817, 39.832)
> bounding_box <- matrix(nrow = 2, ncol=2, byrow = T,
  data = c(x_coords, y_coords),
  dimnames = list(c("x", "y"),
  c("min", "max")))
> osm_water_sf <- osmdata::opq(bbox = bounding_box) %>% # Limit query to bounding_box
  osmdata::add_osm_feature(key = 'natural', value = 'water') %>% # Limit query to waterbodies
  osmdata::osmdata_sf() # Convert to simple features

ggplot(data=osm_water_sf$osm_polygons)  
    geom_sf(color="blue", fill="lightblue")  
    xlim(x_coords)   ylim(y_coords)

But finally I don't manage to merge the two plots into one, superimposing the bathymetric data on the base map. Also, my intention would be to obtain contours from the bathymetric data of the lake, but of course, I'm already stuck with the merging of both plots.

CodePudding user response:

Use the sf package to convert your sonar data frame to a spatial data frame. Something like:

library(sf)
Sonar_13_07_1 = st_as_sf(Sonar_13_07_1, coords=c("longitude","latitude"), crs=4326)

then you have an object you can put in a ggplot construction via an sf geom alongside your OSM vector data. Something like:

ggplot(data=osm_water_sf$osm_polygons)  
    geom_sf(color="blue", fill="lightblue")  
    geom_sf(data=Sonar_13_07_1, aes(col=waterDepthM))  
    xlim(x_coords)   ylim(y_coords)

CodePudding user response:

Thanks for the suggestion, I have managed to have a spatial data frame like this:

"","waterDepthM","geometry"
"1",0,c(-3.11133523036904, 39.8197123940846)
"2",0,c(-3.11133523036904, 39.8197193169248)
"3",0,c(-3.11134424374202, 39.8197193169248)
"4",0,c(-3.11134424374202, 39.8197262397644)
"5",0,c(-3.11135325711499, 39.8197331626032)
"6",0,c(-3.11135325711499, 39.8197400854413)
"7",0,c(-3.11135325711499, 39.8197470082787)
"8",0,c(-3.11135325711499, 39.8197539311154)
"9",0,c(-3.11135325711499, 39.8197608539514)
"10",0,c(-3.11134424374202, 39.8197608539514)

but when I try to plot both data frames together, I only get a base map of the lake with the depth legend, but no depth values appear.

 ggplot()  
      geom_sf(data=osm_water_sf$osm_polygons, color="blue", fill="NA")  
      geom_sf(data=Sonar_13_07_1, aes(col=waterDepthM))  
      xlim(x_coords)   ylim(y_coords)

plot output

  • Related