{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# EMODnet Chemistry [Points]: Beach Litter - Mean number of Cigarette related items per 100m & to 1 survey - without UNEP_MARLIN - Other sources\n", "***\n", "### Jupyter open Notebook produced by **[Marine-Analyst.eu](http://marine-analyst.eu)**. Metadata record is available at dataset **[landing page](http://marine-analyst.eu/dev.py?N=simple&O=752)**.\n", "***\n", "The Marine-Analyst provides augmented data access and reproducible data analysis for marine data. The Marine Analyst is intended for the general public with a focus on educational content. It allows students, teachers and academia to access marine knowledge. Marine topics are developed under the web portal main menu.\n", "The Marine Analyst is an essential tool for oceanographers, scientists, engineers, managers and policy-makers who are analysing the state and dynamics of Europe's seas. The Marine Analyst Jupyter hub provides you with usefull data analysis codes.\n", "Alternatively you can manage your analyses and download links for your data selections via a **[personal dashboard](http://marine-analyst.eu/dev.py?N=simple&O=638&layer=520)**.\n", "This Jupyter notebook is licensed under the **[MIT License](https://github.com/Marine-Analyst/Jupyter/blob/main/LICENSE)**. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Edit the longitude and latitude coordinates to define the geographical area:\n", "###################################\n", "minlon=11.3 #minimum longitude\n", "minlat=53.6 #minimum latitude\n", "maxlon=15.5 #maximum longitude\n", "maxlat=55.9 #maximum latitude\n", "###################################\n", "# This Jupyter Notebook has been automatically generated from R Markdown Notebook and may contain inconsistencies.\n", "# Edit it, execute it and save it as HTML document.\n", "# Anytime you have a question, a suggestion or an issue, you can contact us at my-beach@knowcean.eu.\n", "# Your collaboration is highly appreciated!\n", "###################################\n", "wdpaid=paste(minlon,minlat,maxlon,maxlat,sep=\"_\")\n", "wdpaidsplit <- unlist(strsplit(wdpaid, \"[_]\"))\n", "xmin <- as.numeric(wdpaidsplit[1])\n", "ymin <- as.numeric(wdpaidsplit[2])\n", "xmax <- as.numeric(wdpaidsplit[3])\n", "ymax <- as.numeric(wdpaidsplit[4])\n", "Sessionid<-\"JupyterNotebook752\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "source_provider <- \"EMODnet Chemistry\"\n", "source_provider_url <- \"https://www.emodnet.eu\"\n", "layer_title<-\"Beach Litter - Mean number of Cigarette\"\n", "layer=\"bl_cigarette_cleaning\"\n", "wfs_url <- \"https://www.ifremer.fr/services/wfs/emodnet_chemistry2?\"\n", "wms_url <- \"https://www.ifremer.fr/services/wms/emodnet_chemistry2?\"\n", "wms_layer=\"bl_cigarette_cleaning\"\n", "layer_id<-752\n", "map_legend <- \"litterabundance\"\n", "map_label<-\"beachname\"\n", "link_csv<-paste0(\"./Report-\", layer_id, \"_\", Sessionid, \"_\", wdpaid, \"-csvfile.csv\",sep=\"\")\n", "csvfile_name = paste(\"Report-\", layer_id, \"_\", Sessionid, \"_\", wdpaid, \"-csvfile.csv\",sep=\"\")\n", "link_geojson<-paste0(\"./Report-\", layer_id, \"_\", Sessionid, \"_\", wdpaid, \"-geojsonfile.geojson\",sep=\"\")\n", "geojsonfile_name = paste(\"Report-\", layer_id, \"_\", Sessionid, \"_\", wdpaid, \"-geojsonfile.geojson\",sep=\"\")\n", "temp_path<- \".\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "library(rgdal)\n", "library(downloader)\n", "library(ggplot2)\n", "library(mapdata)\n", "library(ggmap)\n", "library(ggrepel)\n", "library(httr)\n", "library(sf)\n", "library(rasterVis)\n", "library(rgeos)\n", "library(sp)\n", "library(raster)\n", "library(dplyr)\n", "library(XML)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Data information" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "This visualization product displays smoking related items abundance per beach per year obtained during research & cleaning operations. EMODnet Chemistry included the gathering of marine litter in its 3rd phase. \n", "\n", "Since the beginning of 2018, data of beach litter have been gathered and processed in the EMODnet Chemistry Marine Litter Database (MLDB). The harmonization of all the data has been the most challenging task considering the heterogeneity of the data sources, sampling protocols and reference lists used on a European scale. Preliminary processing were necessary to harmonize all the data : - Exclusion of OSPAR 1000 protocol, - Separation of monitoring surveys from research & cleaning operations - Exclusion of beaches with no coordinates - Normalization of survey lengths and survey numbers per year - Some categories & some litter types have been removed Abundances have been calculated on each beach and year using the following computation: Cigarette related items abundance=(total number of Cigarette related items (normalized at 100m))/(Number of surveys on the year) Percentiles 50, 75 & 95 have been calculated taking into account data from all years. Cigarette related items reference codes taken into account for this product and information on data processing and calculation are detailed in the document attached p15. https://www.emodnet-chemistry.eu/repository/20190321_EMODnet_Beach_Seabed_ML_Products.pdf" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Script for Wekeo environment\n", "sr=SpatialPolygons(list(Polygons(list(Polygon(cbind(c(xmin, xmin, xmax, xmax),c(ymax, ymin, ymin, ymax)))),\"1\")))\n", "mpa=SpatialPolygonsDataFrame(sr, data.frame(cbind(1:1), row.names=c(\"1\")))\n", "proj4string(mpa)<-CRS(\"+proj=longlat +datum=WGS84\")\n", "bbox<-paste(xmin,ymin,xmax,ymax,sep=\",\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Link to Marine Analyst dataset page\n", "link_marineanalyst <- paste0(\"http://marine-analyst.eu/dev.py?N=simple&O=\",layer_id,\"&maxlat=\",ymax,\"&maxlon=\",xmax,\"&minlon=\",xmin,\"&minlat=\",ymin)\n", "# Link to open the openlayer page for EMODnet HA\n", "openlayer<-paste0(\"http://www.marine-analyst.eu/openlayers3/openlayer.py?wms_url=\",wms_url,\"/wms&wms_layer=\",layer,\"&bbox=\",bbox)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Metadata" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "Access metadata from landing page
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Geographical extent" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Coordinates" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print (paste(\"West-Longitude:\",round(xmin,2)))\n", "print (paste(\"South-Latitude:\",round(ymin,2)))\n", "print (paste(\"East-Longitude:\",round(xmax,2)))\n", "print (paste(\"North-Latitude:\",round(ymax,2)))" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Defined area" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "value<-(xmax-xmin)*(ymax-ymin)\n", "if (value > 100) {\n", " zoom_value<-6\n", "} else if (value > 1) {\n", " zoom_value<-7\n", "} else {\n", " zoom_value<-8\n", "}\n", "base<-get_map(location=c(xmin-1,ymin-1,xmax+1,ymax+1), zoom=zoom_value, maptype=\"terrain-background\", source = \"stamen\")\n", "terrain <- ggmap(base)\n", "map <- terrain + geom_polygon(data=mpa,aes(x=long,y=lat,group=group,fill=\"mpa\"),colour=\"green\",fill=\"blue\",alpha=.1) +\n", "ggtitle(\"\")+xlab(\"Longitude\")+ylab(\"Latitude\")\n", "plot(map)" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under ODbL." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# `r toString(layer_title)`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Access data" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "The Web Feature Service (WFS) of the `r toString(source_provider)` portal allows collecting the data:
`r toString(wfs_url)`service=WFS&request=GetCapabilities&version=1.1.0\n", "

**Available labels:**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# DescribeFeatureType request function\n", "DescribeFeatureType<-function(layer){\n", "layer<-as.character(layer)\n", "\n", "con<-paste0(wfs_url,\"service=WFS&version=1.1.0&request=DescribeFeatureType&typeName=\",layer,\"&outpuformat=XMLSCHEMA\")\n", "\n", "xml <- \"file.xml\"\n", "xml <- tempfile(xml)\n", "httr::GET(con,write_disk(xml))\n", "xmldoc <- XML::xmlParse(xml)\n", "xml_data <- XML::xmlToList(xmldoc)\n", "data.catalog <- data.frame(t(xml_data$complexType$complexContent$extension$sequence),row.names=NULL)\n", "\n", "return(data.catalog)\n", "}\n", "\n", "WFS_DescribeFeatureType <- DescribeFeatureType(layer)\n", "\n", "WFS_Colnames<-c()\n", "\n", "for (i in 1:ncol(WFS_DescribeFeatureType)) {\n", "WFS_Colnames<-append(WFS_Colnames, WFS_DescribeFeatureType[i]$element$element[1][[\"name\"]])\n", "}\n", "\n", "WFS_Colnames\n", "\n", "rez_nblist<- c(1:length(WFS_Colnames))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "getWFSgml3<-function(layer){\n", "layer<-as.character(layer)\n", "con<-paste0(wfs_url,\"service=WFS&version=1.0.0&request=GetFeature&typeName=\",layer,\"&OUTPUTFORMAT=gml3&srsName=EPSG%3A4326\")\n", "pipo<-sf::st_read(con)\n", "return(pipo)\n", "}\n", "\n", "wfs_data<-getWFSgml3(layer)\n", "\n", "#Transform mpa in Simple feature collection to perform the subsetting because wfs_data contains the whole info - use bbox and tyname are exclusive (Mapserver)\n", "mpaSP <- as(mpa, \"SpatialPolygonsDataFrame\")\n", "wfs_data<-wfs_data[st_as_sf(mpaSP),]\n", "# get the geometry as lat and lon cols\n", "wfs_data <- wfs_data %>% dplyr::mutate(lat = sf::st_coordinates(.)[,1],lon = sf::st_coordinates(.)[,2])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "st_write(wfs_data, file.path(temp_path,csvfile_name), layer = csvfile_name, driver = \"csv\", delete_dsn = TRUE)\n", "st_write(wfs_data, file.path(temp_path,geojsonfile_name), layer = geojsonfile_name, driver = \"GeoJSON\", delete_dsn = TRUE)" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "
**Download data for the defined geographical extent:**
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Excel fileGeographic information
**csv****geojson**
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Table" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "Browse table's columns by using the left and right arrows. Turn the table's pages with help of the previous/next buttons.

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if(nrow(wfs_data) > 0) {\n", "wfs_data\n", "} else {\n", "print(\"No data available for the defined geographical extent\")\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if(nrow(wfs_data) > 0) {\n", "map <- ggplot() +\n", "borders(\"worldHires\", fill = \"gray\", colour = \"black\", xlim = range(xmin,xmax), ylim = range(ymin,ymax), size = .25) +\n", " theme(legend.position = \"bottom\") +\n", " theme(panel.grid.minor.y= element_blank(), panel.grid.minor.x = element_blank()) +\n", " geom_polygon(data=mpa,aes(x=long,y=lat,group=group,fill=\"mpa\"),colour=\"green\",fill=\"blue\",alpha=.1) +\n", "geom_sf() +\n", "geom_point(data = wfs_data, aes(x = lat, y = lon, size =.data[[map_legend]]), fill = \"red\", color = \"red\", alpha = .4) +\n", "coord_sf(xlim = c(xmin, xmax),ylim = c(ymin, ymax))+\n", "ggtitle(layer_title)+xlab(\"Longitude (x)\")+ylab(\"Latitude (y)\")\n", "map\n", "} else {\n", "print(\"No data available for the defined geographical extent\")\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Map with id" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if(nrow(wfs_data) > 0) {\n", "centroid<- st_centroid(wfs_data)\n", "centroid<- cbind(wfs_data, st_coordinates(st_centroid(wfs_data$geometry)))\n", "map <- ggplot() +\n", " borders(\"worldHires\", fill = \"gray\", colour = \"black\", xlim = range(xmin,xmax), ylim = range(ymin,ymax), size = .25) +\n", " theme(legend.position = \"bottom\") +\n", " theme(panel.grid.minor.y= element_blank(), panel.grid.minor.x = element_blank()) +\n", " geom_polygon(data=mpa,aes(x=long,y=lat,group=group,fill=\"mpa\"),colour=\"green\",fill=\"blue\",alpha=.1) +\n", " geom_sf() +\n", " geom_point(data = wfs_data, aes(x = lat, y = lon, size = .data[[map_legend]]), fill = \"red\", color = \"red\", alpha = .4) +\n", " geom_text(data=centroid,aes(x=lat, y=lon, label=.data[[map_label]]), color = \"black\", fontface = \"bold\", size=2, hjust= 0, vjust=2, check_overlap = TRUE) +\n", " coord_sf(xlim = c(xmin, xmax),ylim = c(ymin, ymax))+\n", " ggtitle(layer_title)+xlab(\"Longitude (x)\")+ylab(\"Latitude (y)\")\n", "map\n", "} else {\n", "print(\"No data available for the defined geographical extent\")\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Interactive map" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "Visualise and access data with Openlayer (click on the map)
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if(nrow(wfs_data) > 0) {\n", "getWMSmap<-function (wms_layer,xmin,xmax,ymin,ymax)\n", "{\n", "width <- 960\n", "height <- as.integer(width * (ymax-ymin) / (xmax-xmin))\n", "wms_layer<-as.character(wms_layer)\n", "bbox <- paste(xmin, ymin, xmax, ymax, sep = \",\")\n", "con<-paste0(wms_url,\"/wms?SERVICE=WMS&VERSION=1.1.0&request=GetMap&layers=\",wms_layer,\"&format=image/jpeg&srs=EPSG:4326&bbox=\",bbox,\"&height=\",height,\"&width=\",width,\"\")\n", " wms <- \"img.png\"\n", " wms <- tempfile(wms)\n", " download(con, wms, quiet = TRUE, mode = \"wb\")\n", " img <- brick(wms)\n", "names(img) <- c(\"img.1\", \"img.2\", \"img.3\")\n", "img[img$img.1 == 255 & img$img.2 == 255 & img$img.3 == 255] <- NA\n", "wms_basemap_url=\"http://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv\"\n", "wms_basemap_layer=\"gebco_latest\"\n", "con<-paste0(wms_basemap_url,\"?SERVICE=WMS&VERSION=1.1.0&request=GetMap&layers=\",wms_basemap_layer,\"&format=image/png&srs=EPSG:4326&bbox=\",bbox,\"&height=\",height,\"&width=\",width,\"\")\n", " wms <- \"img.png\"\n", " wms <- tempfile(wms)\n", " download(con, wms, quiet = TRUE, mode = \"wb\")\n", " basemap <- brick(wms)\n", "names(basemap) <- c(\"img.1\", \"img.2\", \"img.3\")\n", "img <- merge(basemap,img)\n", "img@extent@xmin <- xmin\n", "img@extent@ymin <- ymin\n", "img@extent@xmax <- xmax\n", "img@extent@ymax <- ymax\n", "proj4string(img)<-CRS(\"+proj=longlat +datum=WGS84\")\n", "return(img)\n", "}\n", "wms_img<-getWMSmap(wms_layer,xmin,xmax,ymin,ymax)\n", "rggbplot <- function(inRGBRst,npix=NA,scale = 'lin'){\n", " rgblinstretch <- function(rgbDf){\n", " maxList <- apply(rgbDf,2,max)\n", " minList <- apply(rgbDf,2,min)\n", " temp<-rgbDf\n", " for(i in c(1:3)){\n", " temp[,i] <- (temp[,i]-minList[i])/(maxList[i]-minList[i])\n", " }\n", " return(temp)\n", " }\n", " rgbeqstretch<-function(rgbDf){\n", " temp<-rgbDf\n", " for(i in c(1:3)){\n", " unique <- na.omit(temp[,i])\n", " if (length(unique>0)){\n", " ecdf<-ecdf(unique)\n", " temp[,i] <- apply(temp[,i,drop=FALSE],2,FUN=function(x) ecdf(x))\n", " }\n", " }\n", " return(temp)\n", " }\n", " npix <- ncell(inRGBRst)\n", " x <- sampleRegular(inRGBRst, size=npix, asRaster = TRUE)\n", " dat <- as.data.frame(x, xy=TRUE)\n", " colnames(dat)[3:5]<-c('r','g','b')\n", " if(scale=='lin'){\n", " dat[,3:5]<- rgblinstretch(dat[,3:5])\n", " } else if(scale=='stretch'){\n", " dat[,3:5]<- rgbeqstretch(dat[,3:5])\n", " }\n", " p <- ggplot()+ geom_tile(data=dat, aes(x=x, y=y, fill=rgb(r,g,b))) + scale_fill_identity()\n", "}\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if(nrow(wfs_data) > 0) {\n", "map <- rggbplot(wms_img)+\n", "#borders(\"worldHires\", fill = \"gray\", colour = \"black\", xlim = range(xmin,xmax), ylim = range(ymin,ymax), size = .25) +\n", "coord_quickmap(xlim=range(xmin,xmax),ylim=range(ymin,ymax))+\n", "ggtitle(layer_title)+xlab(\"Longitude\")+ylab(\"Latitude\")\n", "plot(map)\n", "} else {\n", "print(\"No data available for the defined geographical extent\")\n", "}" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Litter abundance per year" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if(nrow(wfs_data) > 0) {\n", "litterabundance <- \"litterabundance\"\n", "abundance <- ggplot() +\n", "theme(legend.position = \"bottom\") +\n", "geom_point(data = wfs_data, aes(x = year, y = litterabundance, color =.data[[map_label]]), size=4) +\n", "ggtitle(\"Litter abundance per year and location\")+xlab(\"Year\")+ylab(\"Litter abundance\")\n", "plot(abundance)\n", "} else {\n", "print(\"No data available for the defined geographical extent\")\n", "}" ] } ],"metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }