Title: | Calculating Spatial Risk |
---|---|
Description: | Methods for spatial risk calculations. It offers an efficient approach to determine the sum of all observations within a circle of a certain radius. This might be beneficial for insurers who are required (by a recent European Commission regulation) to determine the maximum value of insured fire risk policies of all buildings that are partly or fully located within a circle of a radius of 200m. See Church (1974) <doi:10.1007/BF01942293> for a description of the problem. |
Authors: | Martin Haringa [aut, cre] |
Maintainer: | Martin Haringa <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.7.1.9000 |
Built: | 2025-02-19 05:16:30 UTC |
Source: | https://github.com/mharinga/spatialrisk |
Takes an object produced by points_to_polygon()
, and
creates the corresponding choropleth map. The given clustering is according
to the Fisher-Jenks algorithm. This commonly used method for choropleths
seeks to reduce the variance within classes and maximize the variance
between classes.
choropleth( sf_object, value = "output", id_name = "areaname", mode = "plot", n = 7, legend_title = "Clustering", palette = "viridis" )
choropleth( sf_object, value = "output", id_name = "areaname", mode = "plot", n = 7, legend_title = "Clustering", palette = "viridis" )
sf_object |
object of class sf |
value |
column name to shade the polygons |
id_name |
column name of ids to plot |
mode |
choose between static ('plot' is default) and interactive map ('view') |
n |
number of clusters (default is 7) |
legend_title |
title of legend |
palette |
palette name or a vector of colors. See
|
tmap
Martin Haringa
test <- points_to_polygon(nl_provincie, insurance, sum(amount, na.rm = TRUE)) choropleth(test) choropleth(test, id_name = "areaname", mode = "view")
test <- points_to_polygon(nl_provincie, insurance, sum(amount, na.rm = TRUE)) choropleth(test) choropleth(test, id_name = "areaname", mode = "view")
Takes an object produced by choropleth_sf()
, and creates
the correspoding choropleth map.
choropleth_ggplot2( sf_object, value = output, n = 7, dig.lab = 2, legend_title = "Class", option = "D", direction = 1 )
choropleth_ggplot2( sf_object, value = output, n = 7, dig.lab = 2, legend_title = "Class", option = "D", direction = 1 )
sf_object |
object of class sf |
value |
column to shade the polygons |
n |
number of clusters (default is 7) |
dig.lab |
number of digits in legend (default is 2) |
legend_title |
title of legend |
option |
a character string indicating the colormap option to use. Four options are available: "magma" (or "A"), "inferno" (or "B"), "plasma" (or "C"), "viridis" (or "D", the default option) and "cividis" (or "E"). |
direction |
Sets the order of colors in the scale. If |
ggplot map
Martin Haringa
test <- points_to_polygon(nl_postcode2, insurance, sum(amount, na.rm = TRUE)) choropleth_ggplot2(test)
test <- points_to_polygon(nl_postcode2, insurance, sum(amount, na.rm = TRUE)) choropleth_ggplot2(test)
Calculates the concentration, which is the sum of all observations within a circle of a certain radius.
concentration( sub, full, value, lon_sub = lon, lat_sub = lat, lon_full = lon, lat_full = lat, radius = 200, display_progress = TRUE )
concentration( sub, full, value, lon_sub = lon, lat_sub = lat, lon_full = lon, lat_full = lat, radius = 200, display_progress = TRUE )
sub |
data.frame of target points to calculate concentration risk for, including at least columns for longitude and latitude. |
full |
data.frame containing reference points, where the function finds locations within a radius from the target points. Should include at least columns for longitude, latitude, and the value of interest to summarize. |
value |
column name with value of interest to summarize in |
lon_sub |
column name in |
lat_sub |
column name in |
lon_full |
column name in |
lat_full |
column name in |
radius |
numeric. Radius of the circle in meters (default is 200). |
display_progress |
boolean indicating whether to show progress bar (TRUE/FALSE). Defaults to TRUE. |
A data.frame equal to sub
including an additional
column concentration
.
Martin Haringa
df <- data.frame(location = c("p1", "p2"), lon = c(6.561561, 6.561398), lat = c(53.21369, 53.21326)) concentration(df, Groningen, value = amount, radius = 100)
df <- data.frame(location = c("p1", "p2"), lon = c(6.561561, 6.561398), lat = c(53.21369, 53.21326)) concentration(df, Groningen, value = amount, radius = 100)
Convert Coordinate Reference System (CRS) of a data.frame from one CRS to another.
convert_crs_df( df, crs_from = 3035, crs_to = 4326, lon_from = "x", lat_from = "y", lon_to = "lon", lat_to = "lat" )
convert_crs_df( df, crs_from = 3035, crs_to = 4326, lon_from = "x", lat_from = "y", lon_to = "lon", lat_to = "lat" )
df |
data.frame to be converted. |
crs_from |
CRS code of the original coordinate system (default: 3035). |
crs_to |
CRS code of the target coordinate system (default: 4326). |
lon_from |
column name of longitude values in |
lat_from |
column name of latitude values in |
lon_to |
column name for longitude values in the converted data frame (default: "lon"). |
lat_to |
column name for latitude values in the converted data frame (default: "lat"). |
data.frame with converted coordinates
Martin Haringa
Determines the central coordinates of a circle with a constant radius that maximizes the coverage of demand points.
find_highest_concentration( df, value, top_n = 1, radius = 200, cell_size = 100, grid_precision = 1, lon = "lon", lat = "lat", crs_metric = 3035, print_progress = TRUE )
find_highest_concentration( df, value, top_n = 1, radius = 200, cell_size = 100, grid_precision = 1, lon = "lon", lat = "lat", crs_metric = 3035, print_progress = TRUE )
df |
data.frame. Should include at least columns for longitude, latitude, and the value of interest to summarize. |
value |
column name with value of interest to summarize in |
top_n |
positive integer value greater or equal to 1 (default is 1). |
radius |
numeric. Radius of the circle in meters (default is 200). |
cell_size |
numeric. Size of cell in meters (default is 100). |
grid_precision |
numeric. Precision of grid in meters (default is 1). |
lon |
column name in |
lat |
column name in |
crs_metric |
numeric. The metric Coordinate Reference System (CRS) is used solely in the background calculations. For European coordinates, EPSG:3035 (default) is recommended. For the United States, EPSG:6317 can be utilized. For Asia and the Pacific regions, EPSG:8859 is recommended. |
print_progress |
print progress iteration steps. |
A recent regulation by the European Commission mandates insurance companies to report the maximum value of insured fire risk policies for all buildings partially or fully situated within a circle with a radius of 200 meters (see Article 132 - fire risk sub-module - of the Delegated Regulation). This article captures the risk of catastrophic fire or explosion, including as a result of terrorist attacks. The sub-module is based on the scenario that the insurance or reinsurance undertaking incurs a loss equal to the capital insured for each building located partly or fully within a radius of 200 meters.
This problem resembles a Maximal Covering Location Problem (MCLP) with a fixed radius, belonging to the category of facility location problems. The main aim is to select the best locations for a predetermined number of facilities to achieve maximum coverage of demand points within a specified radius of each facility. In essence, the objective is to identify optimal facility locations to cover as many demand points as feasible, while ensuring that each demand point falls within the designated distance (radius) of at least one facility.
A list with two elements:
A data.frame containing the top_n
concentrations as specified
by top_n
.
A data.frame containing the rows from df
that correspond to the
top_n
concentrations.
Martin Haringa
Commission Delegated Regulation (EU) (2015). Solvency II Delegated Act 2015/35. Official Journal of the European Union, 58:124.
x <- find_highest_concentration(Groningen, "amount") plot(x) y <- find_highest_concentration( Groningen, "amount", top_n = 2, cell_size = 50 ) plot(y)
x <- find_highest_concentration(Groningen, "amount") plot(x) y <- find_highest_concentration( Groningen, "amount", top_n = 2, cell_size = 50 ) plot(y)
A dataset of postal codes and the corresponding spatial locations in terms of a latitude and a longitude.
Groningen
Groningen
A data frame with 25000 rows and 8 variables:
Name of street
Number of house
Letter of house
Suffix to number of house
Postal code of house
The name of the city
Longitude (in degrees)
Latitude (in degrees)
Random value
The BAG is the Dutch registry for Buildings and adresses (Basisregistratie adressen en gebouwen).
Calculates the shortest distance between two points on the Earth's surface using the Haversine formula, also known as the great-circle distance or "as the crow flies".
haversine(lat_from, lon_from, lat_to, lon_to, r = 6378137)
haversine(lat_from, lon_from, lat_to, lon_to, r = 6378137)
lat_from |
Latitude of the starting point. |
lon_from |
Longitude of the starting point. |
lat_to |
Latitude of the destination point. |
lon_to |
Longitude of the destination point. |
r |
Radius of the Earth in meters (default = 6378137). |
The Haversine ('half-versed-sine') formula was published by R.W. Sinnott in 1984, although it has been known for much longer.
Vector of distances in the same unit as r
(default in meters).
Martin Haringa
Sinnott, R.W, 1984. Virtues of the Haversine. Sky and Telescope 68(2): 159.
haversine(53.24007, 6.520386, 53.24054, 6.520386)
haversine(53.24007, 6.520386, 53.24054, 6.520386)
A dataset of postal codes with their sum insured, population and the corresponding spatial locations in terms of a latitude and a longitude.
insurance
insurance
A data frame with 29,990 rows and 5 variables:
6-digit postal code
Population per 4-digit postal code
Sum insured
Longitude (in degrees) of the corresponding 6-digit postal code
Latitude (in degrees) of the corresponding 6-digit postal code
Martin Haringa
Spline interpolation and smoothing on the sphere.
interpolate_spline( observations, targets, value, lon_obs = lon, lat_obs = lat, lon_targets = lon, lat_targets = lat, k = 50 )
interpolate_spline( observations, targets, value, lon_obs = lon, lat_obs = lat, lon_targets = lon, lat_targets = lat, k = 50 )
observations |
data.frame of observations. |
targets |
data.frame of locations to calculate the interpolated and smoothed values for (target points). |
value |
Column with values in |
lon_obs |
Column in |
lat_obs |
Column in |
lon_targets |
Column in |
lat_targets |
Column in |
k |
(default 50) is the basis dimension. For small data sets reduce
|
observations
should include at least columns for longitude
and latitude.
targets
should include at least columns for longitude,
latitude and value of interest to interpolate and smooth.
A smooth of the general type discussed in Duchon (1977) is used: the sphere is embedded in a 3D Euclidean space, but smoothing employs a penalty based on second derivatives (so that locally as the smoothing parameter tends to zero we recover a "normal" thin plate spline on the tangent space). This is an unpublished suggestion of Jean Duchon.
Object equal to object targets
including an extra column
with predicted values.
Martin Haringa
## Not run: target <- sf::st_drop_geometry(nl_postcode3) obs <- dplyr::sample_n(insurance, 1000) pop_df <- interpolate_spline(obs, target, population_pc4, k = 20) pop_sf <- dplyr::left_join(nl_postcode3, pop_df) choropleth(pop_sf, value = "population_pc4_pred", n = 13) ## End(Not run)
## Not run: target <- sf::st_drop_geometry(nl_postcode3) obs <- dplyr::sample_n(insurance, 1000) pop_df <- interpolate_spline(obs, target, population_pc4, k = 20) pop_sf <- dplyr::left_join(nl_postcode3, pop_df) choropleth(pop_sf, value = "population_pc4_pred", n = 13) ## End(Not run)
This function retrieves historic weather data collected by the official KNMI weather stations. See spatialrisk::knmi_stations for a list of the official KNMI weather stations.
knmi_historic_data(startyear, endyear)
knmi_historic_data(startyear, endyear)
startyear |
start year for historic weather data. |
endyear |
end year for historic weather data. |
The returned data frame contains the following columns:
station = ID of measurement station;
date = Date;
FH = Hourly mean wind speed (in 0.1 m/s);
FX = Maximum wind gust (in 0.1 m/s) during the hourly division;
DR = Precipitation duration (in 0.1 hour) during the hourly division;
RH = Hourly precipitation amount (in 0.1 mm) (-1 for <0.05 mm);
city = City where the measurement station is located;
lon = Longitude of station (crs = 4326);
lat = Latitude of station (crs = 4326).
Data frame containing weather data and meta data for weather station locations.
Martin Haringa
## Not run: knmi_historic_data(2015, 2019) ## End(Not run)
## Not run: knmi_historic_data(2015, 2019) ## End(Not run)
A data frame containing the IDs and meta-data on the official KNMI weather stations.
knmi_stations
knmi_stations
A data frame with 50 rows and 7 variables:
ID of the station (209-391)
City where the station is located
Longitude of station (crs = 4326)
Latitude of the station (crs = 4326)
Altitude of the station (in meters)
X coordinate of the station (crs = 32631)
Y coordinate of the station (crs = 32631)
Martin Haringa
sf
for COROP regions in the NetherlandsAn object of class sf
(simple feature) for COROP regions
in the Netherlands.
nl_corop
nl_corop
A simple feature object with 40 rows and 5 variables:
corop number
corop name
geometry object of COROP region
longitude of the corop centroid
latitude of the corop centroid
A COROP region is a regional area within the Netherlands. These regions are used for analytical purposes by, among others, Statistics Netherlands. The Dutch abbreviation stands for Coordinatiecommissie Regionaal Onderzoeksprogramma, literally the Coordination Commission Regional Research Programme.
Martin Haringa
sf
for municipalities in the NetherlandsAn object of class sf
(simple feature) for municipalities
(Dutch: gemeentes) in the Netherlands in the year 2021.
nl_gemeente
nl_gemeente
A simple feature object with 380 rows and 6 variables:
id of gemeente
code of gemeente
name of gemeente
longitude of the gemeente centroid
latitude of the gemeente centroid
geometry object of gemeente
Martin Haringa
sf
for 2-digit postcode regions in the NetherlandsAn object of class sf
(simple feature) for 2-digit postal
codes (Dutch: postcode) regions in the Netherlands.
nl_postcode2
nl_postcode2
A simple feature object with 90 rows and 4 variables:
2-digit postal code
geometry object of postal code
longitude of the 2-digit postal code centroid
latitude of the 2-digit postal code centroid
Postal codes in the Netherlands, known as postcodes, are alphanumeric, consisting of four digits followed by two uppercase letters. The first two digits indicate a city and a region, the second two digits and the two letters indicate a range of house numbers, usually on the same street.
Martin Haringa
sf
for 3-digit postcode regions in the NetherlandsAn object of class sf
(simple feature) for 3-digit postal
codes (Dutch: postcode) regions in the Netherlands.
nl_postcode3
nl_postcode3
A simple feature object with 799 rows and 3 variables:
3-digit postal code
geometry object of postal code
longitude of the 3-digit postal code centroid
latitude of the 3-digit postal code centroid
Postal codes in the Netherlands, known as postcodes, are alphanumeric, consisting of four digits followed by two uppercase letters. The first two digits indicate a city and a region, the second two digits and the two letters indicate a range of house numbers, usually on the same street.
Martin Haringa
sf
for 4-digit postcode regions in the NetherlandsAn object of class sf
(simple feature) for 4-digit postal
codes (Dutch: postcode) regions in the Netherlands.
nl_postcode4
nl_postcode4
A simple feature object with 4053 rows and 7 variables:
4-digit postal code
name of corresponding 4-digit postal code
name of city
pc4 is in one of the following twenty (biggest) cities in the Netherlands: Amsterdam, Rotterdam, 's-Gravenhage, Utrecht, Eindhoven, Tilburg, Groningen, Almere, Breda, Nijmegen, Enschede, Apeldoorn, Haarlem, Amersfoort, Arnhem, 's-Hertogenbosch, Zoetermeer, Zwolle, Maastricht, Leiden.
geometry object of postal code
longitude of the 4-digit postal code centroid
latitude of the 4-digit postal code centroid
Postal codes in the Netherlands, known as postcodes, are alphanumeric, consisting of four digits followed by two uppercase letters. The first two digits indicate a city and a region, the second two digits and the two letters indicate a range of house numbers, usually on the same street.
Martin Haringa
sf
for provinces in the NetherlandsAn object of class sf
(simple feature) for provinces
(Dutch: provincies) in the Netherlands.
nl_provincie
nl_provincie
A simple feature object with 12 rows and 4 variables:
province name
geometry object of province
longitude of the province centroid
latitude of the province centroid
Martin Haringa
Create map for a data.frame containing points.
plot_points(df, value, lon = "lon", lat = "lat", crs = 4326, at = NULL)
plot_points(df, value, lon = "lon", lat = "lat", crs = 4326, at = NULL)
df |
data.frame containing columns for longitude and latitude. |
value |
column in |
lon |
column in |
lat |
column in |
crs |
crs code for the coordinate reference system (default is 4326). |
at |
the breakpoints used for visualisation. |
## Not run: plot_points(Groningen, value = "amount") ## End(Not run)
## Not run: plot_points(Groningen, value = "amount") ## End(Not run)
find_highest_concentration()
Automatically create a plot for objects obtained from
find_highest_concentration()
.
## S3 method for class 'concentration' plot( x, type = c("concentration", "focal", "rasterized", "updated_focal"), color1 = NULL, max.rad = 20, ... )
## S3 method for class 'concentration' plot( x, type = c("concentration", "focal", "rasterized", "updated_focal"), color1 = NULL, max.rad = 20, ... )
x |
x object of class |
type |
is one of "concentration" (default), "rasterized", "focal", "updated_focal". See details for more information. |
color1 |
color when one concentration is plotted (default is "#4B0055"). |
max.rad |
maximal radius for size of circles in plot (default is 20). |
... |
additional arguments. |
More info for type:
"concentration": this is..
"focal": this is..
"rasterized": this is..
"updated_focal": this is..
Martin Haringa
x <- find_highest_concentration(Groningen, "amount") plot(x, "concentration") plot(x, "rasterized") plot(x, "focal") plot(x, "updated_focal")
x <- find_highest_concentration(Groningen, "amount") plot(x, "concentration") plot(x, "rasterized") plot(x, "focal") plot(x, "updated_focal")
Filter all observations in a data.frame that fall within a circle of a specified radius drawn around a given latitude and longitude point.
points_in_circle( data, lon_center, lat_center, lon = lon, lat = lat, radius = 200 )
points_in_circle( data, lon_center, lat_center, lon = lon, lat = lat, radius = 200 )
data |
data.frame with at least columns for longitude and latitude. |
lon_center |
numeric. Representing the longitude of the circle's center. |
lat_center |
numeric. Representing the latitude of the circle's center. |
lon |
column name in |
lat |
column name in |
radius |
radius of the circle in meters (default is 200m). |
A subset of the input data.frame containing only the observations that fall within the specified circle.
Martin Haringa
points_in_circle(Groningen, lon_center = 6.571561, lat_center = 53.21326, radius = 60)
points_in_circle(Groningen, lon_center = 6.571561, lat_center = 53.21326, radius = 60)
Filter all observations in a data.frame that fall within a circle of a specified radius drawn around a given latitude and longitude point.
points_in_circle_vec( data, lon_center, lat_center, lon = lon, lat = lat, radius = 200 )
points_in_circle_vec( data, lon_center, lat_center, lon = lon, lat = lat, radius = 200 )
data |
data.frame with at least columns for longitude and latitude. |
lon_center |
numeric. Representing the longitude of the circle's center. |
lat_center |
numeric. Representing the latitude of the circle's center. |
lon |
column name in |
lat |
column name in |
radius |
radius of the circle in meters (default is 200m). |
A subset of the input data.frame containing only the observations that fall within the specified circle.
Martin Haringa
points_in_circle_vec(Groningen, lon_center = c(6.571561, 6.56561), lat_center = c(53.21326, 53.20326), radius = 60)
points_in_circle_vec(Groningen, lon_center = c(6.571561, 6.56561), lat_center = c(53.21326, 53.20326), radius = 60)
Join a data.frame containing coordinates (longitude and latitude) to polygon geometries. Arithmetic operations are then applied to the attributes of the joined coordinates to obtain aggregated values for each polygon.
points_to_polygon(sf_map, df, oper, crs = 4326, outside_print = FALSE)
points_to_polygon(sf_map, df, oper, crs = 4326, outside_print = FALSE)
sf_map |
object of class sf representing the polygon geometries. |
df |
data.frame containing coordinates (column names should be 'lon' and 'lat') |
oper |
arithmetic operation to be applied on the polygon level. |
crs |
coordinate reference system (default is 4326). |
outside_print |
logical indicating whether to print points that are not within a polygon (default is FALSE). |
An object of class sf
Martin Haringa
points_to_polygon(nl_postcode2, insurance, sum(amount, na.rm = TRUE)) ## Not run: shp_read <- sf::st_read("~/path/to/file.shp") points_to_polygon(shp_read, insurance, sum(amount, na.rm = TRUE)) ## End(Not run)
points_to_polygon(nl_postcode2, insurance, sum(amount, na.rm = TRUE)) ## Not run: shp_read <- sf::st_read("~/path/to/file.shp") points_to_polygon(shp_read, insurance, sum(amount, na.rm = TRUE)) ## End(Not run)