From 415830326ad28f4dcff3243ec51a2d9fdb081be5 Mon Sep 17 00:00:00 2001 From: florisvdh Date: Tue, 23 Jun 2020 11:17:33 +0200 Subject: [PATCH 1/8] point-pattern: move files in correct location --- .../tutorials/spatial_point-pattern}/point-pattern-R.Rmd | 0 .../tutorials/spatial_point-pattern}/point-pattern/points.txt | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {source => content/tutorials/spatial_point-pattern}/point-pattern-R.Rmd (100%) rename {source => content/tutorials/spatial_point-pattern}/point-pattern/points.txt (100%) diff --git a/source/point-pattern-R.Rmd b/content/tutorials/spatial_point-pattern/point-pattern-R.Rmd similarity index 100% rename from source/point-pattern-R.Rmd rename to content/tutorials/spatial_point-pattern/point-pattern-R.Rmd diff --git a/source/point-pattern/points.txt b/content/tutorials/spatial_point-pattern/point-pattern/points.txt similarity index 100% rename from source/point-pattern/points.txt rename to content/tutorials/spatial_point-pattern/point-pattern/points.txt From 3bad9d97883cd392f32c8e2263f0a60180320b4a Mon Sep 17 00:00:00 2001 From: florisvdh Date: Tue, 23 Jun 2020 11:20:39 +0200 Subject: [PATCH 2/8] point-pattern: rename Rmd --- .../spatial_point-pattern/{point-pattern-R.Rmd => index.Rmd} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename content/tutorials/spatial_point-pattern/{point-pattern-R.Rmd => index.Rmd} (100%) diff --git a/content/tutorials/spatial_point-pattern/point-pattern-R.Rmd b/content/tutorials/spatial_point-pattern/index.Rmd similarity index 100% rename from content/tutorials/spatial_point-pattern/point-pattern-R.Rmd rename to content/tutorials/spatial_point-pattern/index.Rmd From 312da804c48e4bea6f370a42fd7bccf1921044f0 Mon Sep 17 00:00:00 2001 From: florisvdh Date: Wed, 16 Sep 2020 11:50:44 +0200 Subject: [PATCH 3/8] spatial_point_pattern: underscores in folder --- .../{spatial_point-pattern => spatial_point_pattern}/index.Rmd | 0 .../point-pattern/points.txt | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename content/tutorials/{spatial_point-pattern => spatial_point_pattern}/index.Rmd (100%) rename content/tutorials/{spatial_point-pattern => spatial_point_pattern}/point-pattern/points.txt (100%) diff --git a/content/tutorials/spatial_point-pattern/index.Rmd b/content/tutorials/spatial_point_pattern/index.Rmd similarity index 100% rename from content/tutorials/spatial_point-pattern/index.Rmd rename to content/tutorials/spatial_point_pattern/index.Rmd diff --git a/content/tutorials/spatial_point-pattern/point-pattern/points.txt b/content/tutorials/spatial_point_pattern/point-pattern/points.txt similarity index 100% rename from content/tutorials/spatial_point-pattern/point-pattern/points.txt rename to content/tutorials/spatial_point_pattern/point-pattern/points.txt From 096db6a0094372416e9d3cb20b30bbe7f4ab4df4 Mon Sep 17 00:00:00 2001 From: florisvdh Date: Wed, 16 Sep 2020 11:51:53 +0200 Subject: [PATCH 4/8] spatial_point_pattern: add gfm md_document outputformat in YAML --- content/tutorials/spatial_point_pattern/index.Rmd | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/content/tutorials/spatial_point_pattern/index.Rmd b/content/tutorials/spatial_point_pattern/index.Rmd index 7bb8fa8a6..cff0d32a4 100644 --- a/content/tutorials/spatial_point_pattern/index.Rmd +++ b/content/tutorials/spatial_point_pattern/index.Rmd @@ -2,7 +2,11 @@ title: Estimating densities from a point pattern maintainer: Thierry Onkelinx date: '2017-06-28' -output: html_document +output: + html_document: default + md_document: + preserve_yaml: true + variant: gfm params: cellsize: 20 --- From 9d11dc3b954a6a6bf4208f1ee4f841648efefc82 Mon Sep 17 00:00:00 2001 From: florisvdh Date: Wed, 16 Sep 2020 11:52:33 +0200 Subject: [PATCH 5/8] spatial_point_pattern: use PROJ>=6 compliant CRS --- content/tutorials/spatial_point_pattern/index.Rmd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/tutorials/spatial_point_pattern/index.Rmd b/content/tutorials/spatial_point_pattern/index.Rmd index cff0d32a4..f864ace45 100644 --- a/content/tutorials/spatial_point_pattern/index.Rmd +++ b/content/tutorials/spatial_point_pattern/index.Rmd @@ -34,8 +34,8 @@ The next step is to convert the dataset in to a `SpatialPoints` object with WGS8 ```{r reproject} library(sp) -crs_wgs84 <- CRS("+init=epsg:4326") -crs_lambert <- CRS("+init=epsg:31370") +crs_wgs84 <- CRS(SRS_string = "EPSG:4326") +crs_lambert <- CRS(SRS_string = "EPSG:31370") coordinates(points) <- ~lon + lat points@proj4string <- crs_wgs84 points_lambert <- spTransform(points, crs_lambert) From 934c97364fd302f4a94e29ef02c94520b204ab5b Mon Sep 17 00:00:00 2001 From: florisvdh Date: Wed, 16 Sep 2020 14:06:27 +0200 Subject: [PATCH 6/8] spatial_point_pattern: use proj4string<- function rather than the slot * The use of 'proj4string' in R code is confusing in times where PROJ.4 strings are obsoleted. It can be expected that a more general function name to set an object's CRS will sooner or later be promoted instead of proj4string<-, hence preparing for that already by using the function instead of the slot. I expect the proj4string slot will most probably remain in use for compatibility reasons, but explicitly naming it is better avoided in times where WKT2 strings are actually used to define a CRS. --- content/tutorials/spatial_point_pattern/index.Rmd | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/content/tutorials/spatial_point_pattern/index.Rmd b/content/tutorials/spatial_point_pattern/index.Rmd index f864ace45..d2a12c986 100644 --- a/content/tutorials/spatial_point_pattern/index.Rmd +++ b/content/tutorials/spatial_point_pattern/index.Rmd @@ -37,7 +37,9 @@ library(sp) crs_wgs84 <- CRS(SRS_string = "EPSG:4326") crs_lambert <- CRS(SRS_string = "EPSG:31370") coordinates(points) <- ~lon + lat -points@proj4string <- crs_wgs84 +# proj4string() - still the only available function to set the CRS - may in the +# future get a more general name: +proj4string(points) <- crs_wgs84 points_lambert <- spTransform(points, crs_lambert) ``` @@ -87,7 +89,7 @@ In order to visualise the result, we have to re-project the coordinates back to ```{r ggmap, fig.cap = "Static image of density"} coordinates(dens) <- ~lon + lat -dens@proj4string <- crs_lambert +proj4string(dens) <- crs_lambert dens_wgs <- spTransform(dens, crs_wgs84) %>% as.data.frame() ggmap(map) + From 4ec8f1962aa50693984a386da6e4907557bfd0bc Mon Sep 17 00:00:00 2001 From: florisvdh Date: Wed, 16 Sep 2020 14:47:37 +0200 Subject: [PATCH 7/8] spatial_point_pattern: expand on CRS/proj4string/WKT2 --- .../tutorials/spatial_point_pattern/index.Rmd | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/content/tutorials/spatial_point_pattern/index.Rmd b/content/tutorials/spatial_point_pattern/index.Rmd index d2a12c986..12cbfa8dd 100644 --- a/content/tutorials/spatial_point_pattern/index.Rmd +++ b/content/tutorials/spatial_point_pattern/index.Rmd @@ -30,12 +30,27 @@ ggmap(map) + geom_point(data = points, alpha = 0.1, colour = "blue", shape = 4) ``` -The next step is to convert the dataset in to a `SpatialPoints` object with WGS84 project and re-project it into Lambert72. `sp::CRS()` defines the coordinate systems. `sp::coordinates()<-` is an easy way to convert a `data.frame` into a `SpatialPointsDataFrame`, but without specifying a coordinate system. Therefore we need to override the `proj4string` slot with the correct coordinate system. `sp::spTransform()` converts the spatial object from the current coordinate system to another coordinate system. +The next step is to convert the dataset to a `SpatialPoints` object with WGS84 projection and re-project it to Belgian Lambert72. +`sp::CRS()` defines the coordinate reference systems (CRS). +`sp::coordinates()<-` is an easy way to convert a `data.frame` into a `SpatialPointsDataFrame`, but without specifying a CRS. +Therefore we need to override the CRS with the correct one. -```{r reproject} +`sp` will derive a 'WKT2 string' representation from an EPSG-code [^epsg] that we provide, in order to represent the CRS. +The WKT2 string (well known text) is a recent open standard by the Open Geospatial Consortium to represent a CRS, and it replaces the older (deprecated) 'PROJ.4 string'. +Currently, the function to set the CRS is still called `proj4string()`. +`sp::spTransform()` converts the spatial object from the current CRS to another CRS. + +[^epsg]: Most coordinate reference systems have an [EPSG](https://en.wikipedia.org/wiki/International_Association_of_Oil_%26_Gas_Producers#European_Petroleum_Survey_Group) code which you can find at http://epsg.io/. + +```{r crs-objects} library(sp) crs_wgs84 <- CRS(SRS_string = "EPSG:4326") crs_lambert <- CRS(SRS_string = "EPSG:31370") +``` + +The warning above once again demonstrates that some PROJ.4 information is not supported anymore. + +```{r reproject} coordinates(points) <- ~lon + lat # proj4string() - still the only available function to set the CRS - may in the # future get a more general name: From 0a0f6e323d5ac9edff78dd23660bf1d49babdfba Mon Sep 17 00:00:00 2001 From: florisvdh Date: Wed, 16 Sep 2020 14:48:21 +0200 Subject: [PATCH 8/8] spatial_point_pattern: improve markdown & language --- .../tutorials/spatial_point_pattern/index.Rmd | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/content/tutorials/spatial_point_pattern/index.Rmd b/content/tutorials/spatial_point_pattern/index.Rmd index 12cbfa8dd..b1fb5433a 100644 --- a/content/tutorials/spatial_point_pattern/index.Rmd +++ b/content/tutorials/spatial_point_pattern/index.Rmd @@ -11,9 +11,15 @@ params: cellsize: 20 --- -In this example we focus on a set of 10450 coordinates in a small area. The goal is to estimate the local density of points, expressed as the number of point per unit area. The raw coordinates are given in [WGS84 (EPSG:4326)](https://epsg.io/4326), which is a geodetic coordinate system. That is not suited for calculating distances, so we need to re-project the points into a local projected coordinate system. In this case we use [Lambert72 (EPSG:3170)](https://epsg.io/31370). Next we calculate the density. To visualise the density, we have to transform the results back in to WGS84. +In this example we focus on a set of 10450 coordinates in a small area. +The goal is to estimate the local density of points, expressed as the number of points per unit area. +The raw coordinates are given in [WGS84 (EPSG:4326)](https://epsg.io/4326), which is a geodetic coordinate reference system. +That is not suited for calculating distances, so we need to re-project the points into a local projected coordinate reference system. +In this case we use [Belgian Lambert72 (EPSG:3170)](https://epsg.io/31370). +Next we calculate the density. +To visualise the density, we have to transform the results back to WGS84. -The data used in this example is real data by centred to a different location for privacy reasons. The dataset is available on [GitHub](https://github.com/ThierryO/my_blog/tree/master/data/20170628). +The data used in this example is real data but centred to a different location for privacy reasons. The dataset is available on [GitHub](https://github.com/ThierryO/my_blog/tree/master/data/20170628). First we must read the data into R. Plotting the raw data helps to check errors in the data. @@ -116,7 +122,12 @@ ggmap(map) + ) ``` -Using `leaflet` to generate a map was a bit more laborious. Using the `data.frame dens_wgs`directly failed. So we converted the `data.frame` in a `SpatialPolygonsDataframe`, which is a combination of a `SpatialPolygons` and a `data.frame`. The `SpatialPolygons` consists of a list of `Polygons`, one for each row of the `data.frame`. A `Polygons` object consist of a list of one or more `Polygon` object. In this example a single polygon which represents the grid cell. +Using `leaflet` to generate a map was a bit more laborious. +Using the `data.frame dens_wgs`directly failed. +So we converted the `data.frame` to a `SpatialPolygonsDataframe`, which is a combination of a `SpatialPolygons` and a `data.frame`. +The `SpatialPolygons` consists of a list of `Polygons`, one for each row of the `data.frame`. +A `Polygons` object consists of a list of one or more `Polygon` objects. +In this example it is a single polygon which represents the grid cell. ```{r convert-to-Spatial-Polygons} dens_sp <- lapply( @@ -137,7 +148,10 @@ dens_sp <- lapply( ) ``` -`leaflet` requires a predefined function with a colour pallet. We use `leaflet::colorNumeric()` to get a continuous pallet. Setting `stroke = FALSE` removes the borders of the polygon. `fillOpacity` sets the transparency of the polygons. +`leaflet` requires a predefined function with a colour palette. +We use `leaflet::colorNumeric()` to get a continuous palette. +Setting `stroke = FALSE` removes the borders of the polygon. +`fillOpacity` sets the transparency of the polygons. ```{r leaflet, fig.cap = "Dynamic map of density"} library(leaflet)