The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

Package {SUNGEO}


Type: Package
Title: Sub-National Geospatial Data Archive: Geoprocessing Toolkit
Version: 1.4.0
Date: 2026-05-28
Description: Integrates spatially-misaligned GIS datasets across incompatible geographic units. Part of the Sub-National Geospatial Data Archive System. For the underlying methods, see Zhukov, Byers, Davidson, and Kollman (2024) "Integrating Data Across Misaligned Spatial Units," Political Analysis, Volume 32, Number 1, pp. 17-33 <doi:10.1017/pan.2023.5>.
URL: https://www.sungeo.org/, https://github.com/zhukovyuri/SUNGEO
License: GPL-2
Encoding: UTF-8
LazyData: TRUE
Depends: R (≥ 3.5.0)
Imports: sf,data.table,dplyr,jsonlite,terra,stringr,stats,methods,purrr,measurements,RANN,cartogram,packcircles,rmapshaper,spdep,Rcpp,httr,rlang,curl
Suggests: lwgeom
Config/roxygen2/version: 8.0.0
NeedsCompilation: no
Packaged: 2026-05-28 17:39:34 UTC; zhukov
Author: Yuri M. Zhukov [aut, cre], Jason Byers [aut], Marty Davidson [aut]
Maintainer: Yuri M. Zhukov <ymz2@georgetown.edu>
Repository: CRAN
Date/Publication: 2026-06-01 13:50:07 UTC

SUNGEO: Sub-National Geospatial Data Archive: Geoprocessing Toolkit

Description

Integrates spatially-misaligned GIS datasets across incompatible geographic units. Part of the Sub-National Geospatial Data Archive System. For the underlying methods, see Zhukov, Byers, Davidson, and Kollman (2024) "Integrating Data Across Misaligned Spatial Units," Political Analysis, Volume 32, Number 1, pp. 17-33 doi:10.1017/pan.2023.5.

Author(s)

Maintainer: Yuri M. Zhukov ymz2@georgetown.edu

Authors:

See Also

Useful links:


SUNGEO

Description

Sub-National Geospatial Data Archive System: Geoprocessing Toolkit

Details

See the README on [GitHub](https://github.com/zhukovyuri/SUNGEO#readme)


Data availability through SUNGEO API

Description

Census of geospatial and processed data files available to download using SUNGEO::get_data().

Usage

data(available_data)

Format

List of 42 data.table objects Geoset:GADM :Classes ‘data.table’ and 'data.frame': 249 obs. of 4 variables Geoset:GAUL :Classes ‘data.table’ and 'data.frame': 242 obs. of 4 variables Geoset:geoBoundaries :Classes ‘data.table’ and 'data.frame': 197 obs. of 4 variables Geoset:GRED :Classes ‘data.table’ and 'data.frame': 74 obs. of 4 variables Geoset:HEXGRID :Classes ‘data.table’ and 'data.frame': 199 obs. of 4 variables Geoset:MPIDR :Classes ‘data.table’ and 'data.frame': 52 obs. of 4 variables Geoset:NHGIS :Classes ‘data.table’ and 'data.frame': 1 obs. of 4 variables Geoset:PRIOGRID :Classes ‘data.table’ and 'data.frame': 199 obs. of 4 variables Geoset:SHGIS :Classes ‘data.table’ and 'data.frame': 68 obs. of 4 variables

country_iso3

Codes for available countries (ISO 3166-1 alpha-3). Character string.

country_name

Names of available countries. Character string.

geoset_years

Years of available historical boundary files. Character string.

space_units

Available spatial units of analysis. Character string.

Elections:LowerHouse:CLEA :Classes ‘data.table’ and 'data.frame': 168 obs. of 6 variables Demographics:Ethnicity:EPR :Classes ‘data.table’ and 'data.frame': 180 obs. of 6 variables Demographics:Ethnicity:GREG :Classes ‘data.table’ and 'data.frame': 234 obs. of 6 variables Demographics:Population:GHS :Classes ‘data.table’ and 'data.frame': 257 obs. of 6 variables Events:PoliticalViolence:ABADarfur :Classes ‘data.table’ and 'data.frame': 1 obs. of 6 variables Events:PoliticalViolence:ACLED :Classes ‘data.table’ and 'data.frame': 100 obs. of 6 variables Events:PoliticalViolence:BeissingerProtest :Classes ‘data.table’ and 'data.frame': 15 obs. of 6 variables Events:PoliticalViolence:BeissingerRiot :Classes ‘data.table’ and 'data.frame': 15 obs. of 6 variables Events:PoliticalViolence:BeissingerUkraine :Classes ‘data.table’ and 'data.frame': 1 obs. of 6 variables Events:PoliticalViolence:COCACW :Classes ‘data.table’ and 'data.frame': 1 obs. of 6 variables Events:PoliticalViolence:ESOCAfghanistanWITS :Classes ‘data.table’ and 'data.frame': 1 obs. of 6 variables Events:PoliticalViolence:ESOCIraqSIGACT :Classes ‘data.table’ and 'data.frame': 1 obs. of 6 variables Events:PoliticalViolence:ESOCIraqWITS :Classes ‘data.table’ and 'data.frame': 1 obs. of 6 variables Events:PoliticalViolence:ESOCMexicoDrugRelatedMurders :Classes ‘data.table’ and 'data.frame': 1 obs. of 6 variables Events:PoliticalViolence:ESOCMexicoHomicide :Classes ‘data.table’ and 'data.frame': 1 obs. of 6 variables Events:PoliticalViolence:ESOCPakistanBFRS :Classes ‘data.table’ and 'data.frame': 1 obs. of 6 variables Events:PoliticalViolence:ESOCPakistanWITS :Classes ‘data.table’ and 'data.frame': 1 obs. of 6 variables Events:PoliticalViolence:GED :Classes ‘data.table’ and 'data.frame': 121 obs. of 6 variables Events:PoliticalViolence:Lankina :Classes ‘data.table’ and 'data.frame': 1 obs. of 6 variables Events:PoliticalViolence:NIRI :Classes ‘data.table’ and 'data.frame': 12 obs. of 6 variables Events:PoliticalViolence:NVMS :Classes ‘data.table’ and 'data.frame': 1 obs. of 6 variables Events:PoliticalViolence:PITF :Classes ‘data.table’ and 'data.frame': 133 obs. of 6 variables Events:PoliticalViolence:SCAD :Classes ‘data.table’ and 'data.frame': 60 obs. of 6 variables Events:PoliticalViolence:yzCaucasus2000 :Classes ‘data.table’ and 'data.frame': 1 obs. of 6 variables Events:PoliticalViolence:yzChechnya :Classes ‘data.table’ and 'data.frame': 1 obs. of 6 variables Events:PoliticalViolence:yzLibya :Classes ‘data.table’ and 'data.frame': 1 obs. of 6 variables Events:PoliticalViolence:yzUkraine2014 :Classes ‘data.table’ and 'data.frame': 1 obs. of 6 variables Infrastructure:Roads:gRoads :Classes ‘data.table’ and 'data.frame': 240 obs. of 6 variables Infrastructure:NightLights:DMSP :Classes ‘data.table’ and 'data.frame': 257 obs. of 6 variables PublicHealth:Covid19:JHUCSSEC19 :Classes ‘data.table’ and 'data.frame': 207 obs. of 6 variables Terrain:Elevation:ETOPO1 :Classes ‘data.table’ and 'data.frame': 256 obs. of 6 variables Terrain:LandCover:GLCC :Classes ‘data.table’ and 'data.frame': 257 obs. of 6 variables Weather:AirTemperatureAndPrecipitation:NOAA :Classes ‘data.table’ and 'data.frame': 209 obs. of 6 variables

country_iso3

Codes for available countries (ISO 3166-1 alpha-3). Character string.

country_name

Names of available countries. Character string.

year_range

Range of available years for data topic. Character string.

time_units

Available time units. Character string.

space_units

Available spatial units. Character string.

geosets

Names of available geographic boundary data sources. Character string.

Source

Sub-National Geospatial Data Archive System: Geoprocessing Toolkit (updated March 17, 2023).


Country code dictionary

Description

Reference table of country names and ISO-3166 codes, adapted from countrycode package.

Usage

data(cc_dict)

Format

data.table object, with 8626 obs. of 3 variables:

country_name

Country names. Character string.

country_name_alt

Alternative spellings of country names, ASCII characters only. Character string.

country_iso3

Country codes (ISO 3166-1 alpha-3). Character string.

Source

Vincent Arel-Bundock. Package countrycode: Convert Country Names and Country Code, version 1.40. CRAN (October 12, 2022).


Constituency level results for lower chamber legislative elections, Germany 2009.

Description

A simple feature collection containing the spatial geometries of electoral constituency borders, and data on turnout levels, votes shares and other attributes of lower chamber legislative elections.

Usage

data(clea_deu2009)

Format

Simple feature collection with 16 features and 10 fields. geometry type: MULTIPOLYGON. dimension: XY. bbox: xmin: 5.867281 ymin: 47.27096 xmax: 15.04388 ymax: 55.05902. epsg (SRID): 4326. proj4string: +proj=longlat +datum=WGS84 +no_defs.

cst

Constituency number. Numeric.

cst_n

Constituency name. Character.

ctr

Country number. Numeric.

ctr_n

Country name. Character.

yrmo

Year and month of election (YYYYMM). Character.

to1

Turnout in first round. Numeric.

vv1

Number of valid votes in first round. Numeric.

pvs1_margin

Popular vote share margin in first round. Numeric.

incumb_pty_n

Incumbent party name.

win1_pty_n

Party name of popular vote share winner in first round. Character.

Source

Constituency-Level Elections Archive (CLEA) https://electiondataarchive.org/


Constituency level results for lower chamber legislative elections, Germany 2009.

Description

A data.frame object containing the geographic centroids of electoral contituencies, and data on turnout levels, votes shares and other attributes of lower chamber legislative elections.

Usage

data(clea_deu2009_df)

Format

data.frame with 16 observations and 12 variables.

cst

Constituency number. Numeric.

cst_n

Constituency name. Character.

ctr

Country number. Numeric.

ctr_n

Country name. Character.

yrmo

Year and month of election (YYYYMM). Character.

to1

Turnout in first round. Numeric.

vv1

Number of valid votes in first round. Numeric.

pvs1_margin

Popular vote share margin in first round. Numeric.

incumb_pty_n

Incumbent party name.

win1_pty_n

Party name of popular vote share winner in first round. Character.

longitude

Longitude of constituency centroid. Numeric.

latitude

Latitude of constituency centroid. Numeric.

Source

Constituency-Level Elections Archive (CLEA) https://electiondataarchive.org/


Constituency level results for lower chamber legislative elections, Germany 2009.

Description

A simple feature collection containing the geographic centroids of electoral contituencies, and data on turnout levels, votes shares and other attributes of lower chamber legislative elections.

Usage

data(clea_deu2009_pt)

Format

Simple feature collection with 16 features and 10 fields. geometry type: POINT. dimension: XY. bbox: xmin: 6.953882 ymin: 48.54535 xmax: 13.40315 ymax: 54.18635. epsg (SRID): 4326. proj4string: +proj=longlat +datum=WGS84 +no_defs.

cst

Constituency number. Numeric.

cst_n

Constituency name. Character.

ctr

Country number. Numeric.

ctr_n

Country name. Character.

yrmo

Year and month of election (YYYYMM). Character.

to1

Turnout in first round. Numeric.

vv1

Number of valid votes in first round. Numeric.

pvs1_margin

Popular vote share margin in first round. Numeric.

incumb_pty_n

Incumbent party name.

win1_pty_n

Party name of popular vote share winner in first round. Character.

Source

Constituency-Level Elections Archive (CLEA) https://electiondataarchive.org/


Convert data.frame object into simple features object

Description

Function takes in x-, y-coordinates, and a data.frame of variables (optional) and returns an SFC object

Usage

df2sf(
  x_coord,
  y_coord,
  input_data = NULL,
  file = NULL,
  n_max = Inf,
  start = 0,
  projection_input = "EPSG:4326",
  zero.policy = FALSE,
  show_removed = FALSE
)

Arguments

x_coord

Numeric vector with longitude or easting projected coordinates. When input_data or file is supplied, can be either column name or numeric vector of the same length as nrow(input_data).

y_coord

Numeric vector with latitude or northing projected coordinates. Must be equal to the vector length of x_coord. When input_data or file is supplied, can be either column name or numeric vector of the same length as nrow(input_data).

input_data

Optional data frame object, containing x_coord and y_coord. nrow(input_data) must be equal to the vector length of x_coord. NOTE: Rows corresponding to non-usable coordinates are removed from the final output.

file

Optional path to csv file. Overrides input_data.

n_max

Maximum number of rows to read in file. Default is Inf.

start

Number of rows to skip in file. Default is 0 (start on first row).

projection_input

Projection string associated with x_coord and y_coord. Default is '+proj=longlat'.

zero.policy

If TRUE, removes rows where corresponding coordinates equals (0,0). Default is FALSE.

show_removed

If TRUE, returns a vector of indices corresponding to non-usable coordinates. Default is FALSE.

Value

If show_removed==FALSE, returns an sf object, with rows corresponding to non-usable coordinates removed. If show_removed==TRUE, returns a list, with an sf object (Spatial_Coordinates), and a vector of indices corresponding to non-usable coordinates removed (Removed_Rows).

Examples

# Coordinates supplied as vectors
data(clea_deu2009_df)
out_1 <- df2sf(x_coord=clea_deu2009_df$longitude,y_coord = clea_deu2009_df$latitude)
class(out_1)
plot(out_1$geometry)

# Coordinates supplied as column mames
out_2 <- df2sf(x_coord="longitude",y_coord ="latitude", input_data = clea_deu2009_df)
plot(out_2["geometry"])

# Load from external file

tmp <- tempfile()
write.csv(clea_deu2009_df,file=tmp)
out_3 <- df2sf(x_coord="longitude",y_coord ="latitude", file=tmp)
plot(out_3["geometry"])


Fix polygon geometries

Description

Function to check validity and fix broken geometries in simple features polygon objects

Usage

fix_geom(x, n_it = 10)

Arguments

x

Polygon layer to be checked and fixed. sf object.

n_it

Number of iterations. Default is 10. Numeric..

Value

Returns a sf polygon object, with self-intersections and other geometry problems fixed.

Examples

# Fix geometries for a single dataset
data(clea_deu2009)
out_1 <- fix_geom(clea_deu2009)
all(sf::st_is_valid(out_1))

Geocode addresses and coordinates with OpenStreetMap

Description

Finds geographic coordinates of addresses and place names (forward geocoding), or converts longitude/latitude coordinates to place names and administrative units (reverse geocoding), using OpenStreetMap's Nominatim API.

Usage

geocode_osm(
  query,
  match_num = 1,
  return_all = FALSE,
  details = FALSE,
  reverse = FALSE,
  lon = NULL,
  lat = NULL,
  zoom = 10,
  user_agent = NULL
)

Arguments

query

Address or place name to be geocoded. Character string. Ignored if reverse=TRUE.

match_num

If query matches multiple locations, which match to return? Default is 1 (highest-ranking match, by relevance). Numeric.

return_all

Should all matches be returned? Overrides match_num if TRUE. Default is FALSE. Logical.

details

Should detailed results be returned? Default is FALSE. Logical. Ignored if reverse=TRUE.

reverse

Should reverse geocoding be performed (coordinates to address)? Default is FALSE. Logical.

lon

Longitude of the point to reverse geocode. Required if reverse=TRUE. Numeric.

lat

Latitude of the point to reverse geocode. Required if reverse=TRUE. Numeric.

zoom

Zoom level for reverse geocoding, controlling the level of detail returned (0 = country, 10 = city, 18 = building). Default is 10. Numeric.

user_agent

Valid User-Agent identifying the application for OSM Nominatim. If none supplied, function will attempt to auto-detect. Character string.

Details

Note that the Nominatim Usage Policy stipulates an absolute maximum of 1 request per second (https://operations.osmfoundation.org/policies/nominatim/). For batch geocoding of multiple addresses, please use geocode_osm_batch.

Value

A data.frame object.

For forward geocoding (reverse=FALSE):

If details=TRUE, also contains:

For reverse geocoding (reverse=TRUE):

Examples

# Geocode an address (top match only)
geocode_osm("Michigan Stadium")

# Return detailed results for top match
geocode_osm("Michigan Stadium", details = TRUE)

# Return detailed results for all matches
geocode_osm("Michigan Stadium", details = TRUE, return_all = TRUE)

# Reverse geocode a coordinate pair
geocode_osm(reverse = TRUE, lon = -83.74868, lat = 42.26587, zoom = 18)


Batch geocode addresses and coordinates with OpenStreetMap

Description

Finds geographic coordinates of multiple addresses and place names (forward geocoding), or converts multiple longitude/latitude coordinate pairs to place names and administrative units (reverse geocoding), using OpenStreetMap's Nominatim API.

Usage

geocode_osm_batch(
  query = NULL,
  delay = 1,
  return_all = FALSE,
  match_num = 1,
  details = FALSE,
  reverse = FALSE,
  lon = NULL,
  lat = NULL,
  zoom = 10,
  user_agent = NULL,
  verbose = FALSE
)

Arguments

query

Addresses or place names to be geocoded. Character string. Ignored if reverse=TRUE.

delay

Delay between requests, in seconds. Default is 1. Numeric.

return_all

Should all matches be returned? Overrides match_num if TRUE. Default is FALSE. Logical.

match_num

If query matches multiple locations, which match to return? Default is 1 (highest-ranking match, by relevance). Numeric.

details

Should detailed results be returned? Default is FALSE. Logical. Ignored if reverse=TRUE.

reverse

Should reverse geocoding be performed (coordinates to address)? Default is FALSE. Logical.

lon

Longitudes of points to reverse geocode. Required if reverse=TRUE. Must be the same length as lat. Numeric vector.

lat

Latitudes of points to reverse geocode. Required if reverse=TRUE. Must be the same length as lon. Numeric vector.

zoom

Zoom level for reverse geocoding, controlling the level of detail returned (0 = country, 10 = city, 18 = building). Default is 10. Numeric.

user_agent

Valid User-Agent identifying the application for OSM Nominatim. If none supplied, function will attempt to auto-detect. Character string.

verbose

Print status messages and progress? Default is FALSE. Logical.

Details

Wrapper function for geocode_osm. Because the Nominatim Usage Policy stipulates an absolute maximum of 1 request per second, this function facilitates batch geocoding by adding a small delay between queries (https://operations.osmfoundation.org/policies/nominatim/).

Value

A data.frame object.

For forward geocoding (reverse=FALSE):

If details=TRUE, also contains:

For reverse geocoding (reverse=TRUE):

Examples


# Geocode multiple addresses (top matches only)
geocode_osm_batch(c("Ann Arbor", "East Lansing", "Columbus"))


# With progress reports
geocode_osm_batch(c("Ann Arbor", "East Lansing", "Columbus"), verbose = TRUE)


# Return detailed results for all matches
geocode_osm_batch(c("Ann Arbor", "East Lansing", "Columbus"),
  details = TRUE, return_all = TRUE)


# Reverse geocode multiple coordinates (city level)
geocode_osm_batch(
  reverse = TRUE,
  lon = c(-83.743, -84.556, -82.999),
  lat = c(42.278,  42.701,  39.961),
  zoom = 10
)


Download data from SUNGEO server

Description

Function to download data files through the SUNGEO API. Function produces a data.table object, corresponding to the user's choice of countries, topics, sources, and spatial and temporal units.

Usage

get_data(
  country_names = NULL,
  country_iso3 = NULL,
  geoset = "GADM",
  geoset_yr = 2018,
  space_unit = "adm1",
  time_unit = "year",
  topics = NULL,
  year_min = 1990,
  year_max = 2017,
  print_url = TRUE,
  print_time = TRUE,
  error_stop = FALSE,
  by_topic = TRUE,
  skip_missing = TRUE,
  cache_param = FALSE,
  short_message = TRUE
)

Arguments

country_names

Country name(s). Character string (single country) or vector of character strings (multiple countries).

country_iso3

Country code (ISO 3166-1 alpha-3). Character string (single country) or vector of character strings (multiple countries).

geoset

Name of geographic boundary set. Can be one of "GADM" (Database of Global Administrative Areas), "GAUL" (Global Administrative Unit Layers), "geoBoundaries", "GRED" (GeoReferenced Electoral Districts Datasets), "HEXGRID" (SUNGEO Hexagonal Grid), "MPIDR" (Max Planck Institute for Demographic Research Population History GIS Collection), "NHGIS" (National Historical Geographic Information System), "PRIOGRID" (PRIO-GRID 2.0), "SHGIS" (SUNGEO Historical GIS). Default is "GADM". Character string.

geoset_yr

Year of geographic boundaries. See get_info()['geosets'] for availability. Default is 2018. Integer.

space_unit

Geographic level of analysis. Can be one of "adm0" (country), "adm1" (province), "adm2" (district), "cst" (GRED electoral constituency), "hex05" (SUNGEO Hexagonal Grid cell), "prio" (PRIO-GRID cell). See get_info()['geosets'] for availability by geoset, country and topic. Default is "adm1". Character string.

time_unit

Temporal level of analysis. Can be one of "year", "month", "week". See get_info()['topics'] for availability by topic. Default is "year". Character string.

topics

Data topics. See get_info()['summary'] for full list. Character string (single topic) or vector of character strings (multiple topics).

year_min

Time range of requested data: start year. See get_info()['topics'] for availability by topic. Default is 1990. Integer.

year_max

Time range of requested data: end year. See get_info()['topics'] for availability by topic. Default is 2017. Integer.

print_url

Print url string of requested data to console? Default is TRUE. Logical.

print_time

Print processing time for API query to console? Default is TRUE. Logical.

error_stop

Error handling. If TRUE, function terminates request if an error is encountered. If FALSE, error is skipped and error message is recorded in a new message column. Default is FALSE. Logical.

by_topic

Break query down by topic and country? If TRUE, a separate request is sent to the API for each country and topic, and the results are combined on the client side. This ensures that data that are available for some, but not all countries are returned, rather than resulting in a failed request. If FALSE, a single request is sent to the API for all countries and topics, and the results are combined on the server side. Only data that are available for all countries are returned. Default is TRUE. Logical.

skip_missing

Skip missing data topics? If TRUE, missing data topics are skipped, columns are populated with NAs, and corresponding error message is recorded in a new message column. If FALSE, returns NULL results for missing topics. Default is TRUE. Logical.

cache_param

Store cached query on server? This can speed up processing for repeated queries. Default is FALSE. Logical.

short_message

Shorten error messages? If TRUE, a short, informative error message is recorded in the message column. If FALSE, full error message is recorded. Default is TRUE. Logical.

Value

data.table object, with requested data from SUNGEO API.

See Also

get_info

Examples


# Single country, single topic
out_1 <- get_data(country_name="Afghanistan",topics="Demographics:Population:GHS")
out_1



out_2 <- get_data(
	country_name=c("Afghanistan","Moldova"),
	topics=c("Demographics:Ethnicity:EPR","Demographics:Population:GHS"))
out_2



# Other boundary sets, spatial and time units
out_3 <- get_data(
	country_name="Albania",
	topics="Weather:AirTemperatureAndPrecipitation:NOAA",
	geoset="GAUL",geoset_yr=1990,space_unit="adm2",time_unit="month",
	year_min=1990,year_max=1991)
out_3


Information on available SUNGEO data files

Description

This function reports the availability of data files on the SUNGEO server, searchable by country and topic.

Usage

get_info(country_names = NULL, country_iso3s = NULL, topics = NULL)

Arguments

country_names

Country name(s). Character string (single country) or vector of character strings (multiple countries).

country_iso3s

Country code (ISO 3166-1 alpha-3). Character string (single country) or vector of character strings (multiple countries).

topics

Data topics. See get_info() for full list. Character string (single topic) or vector of character strings (multiple topics).

Value

list object, with three slots: 'summary', 'topics', and 'geoset'.

See Also

get_data

Examples

# Get list of all available data
out_1 <- get_info()
out_1["summary"]
out_1["topics"]
out_1["geosets"]

# Get list of available data for a single country
out_2 <- get_info(country_names="Afghanistan")
out_2

# Get list of available data for a single topic
out_3 <- get_info(topics="Elections:LowerHouse:CLEA")
out_3

# Get list of available data for a multiple countries and topics
out_4 <- get_info(
                 country_names=c("Afghanistan","Zambia"),
                 topics=c("Elections:LowerHouse:CLEA","Events:PoliticalViolence:GED"))
out_4


Population count raster for Germany, 2010.

Description

2.5 arc-minute resolution raster of estimates of human population (number of persons per pixel), consistent with national censuses and population registers, for the year 2010.

Usage

data(gpw4_deu2010)

Format

class : SpatRaster dimensions : 186, 220, 1 (nrow, ncol, nlyr) resolution : 0.04166667, 0.04166667 (x, y) extent : 5.875, 15.04167, 47.29167, 55.04167 (xmin, xmax, ymin, ymax) coord. ref. : lon/lat WGS 84 (EPSG:4326) source(s) : memory name : gpw_v4_population_count_rev11_2010_2pt5_min min value : 0.00 max value : 92915.66

Source

Gridded Population of the World (GPW) v4: Population Count, v4.11 doi:10.7927/H4JW8BX5.


Hexagonal grid for Germany.

Description

Regular hexagonal grid of 0.5 degree diameter cells, covering territory of Germany (2020 borders).

Usage

data(hex_05_deu)

Format

Simple feature collection with 257 features and 3 fields. geometry type: POLYGON. dimension: XY. bbox: xmin: 5.375001 ymin: 46.76568 xmax: 15.375 ymax: 55.13726. epsg (SRID): 4326. proj4string: +proj=longlat +datum=WGS84 +no_defs.

HEX_ID

Unique cell identifier. Character.

HEX_X

Longitude of cell centroid. Numeric.

HEX_Y

Latitude of cell centroid. Numeric.

Source

SUNGEO


Roads polylines for Germany, 1992

Description

Roads thematic layer from Digital Chart of the World. Subset: divided multi-lane highways.

Usage

data(highways_deu1992)

Format

Simple feature collection with 1741 features and 5 fields. geometry type: MULTILINESTRING. dimension: XY. bbox: xmin: 5.750933 ymin: 47.58799 xmax: 14.75109 ymax: 54.80712 epsg (SRID): 4326. proj4string: +proj=longlat +datum=WGS84 +no_defs.

MED_DESCRI

Is the road a divided multi-lane highway with a median? Character string.

RTT_DESCRI

Primary or secondary route? Character string.

F_CODE_DES

Feature code description (road or trail). Character string.

ISO

ISO 3166-1 alpha-3 country code. Character string.

ISOCOUNTRY

Country name. Character string.

Source

Defense Mapping Agency (DMA), 1992. Digital Chart of the World. Defense Mapping Agency, Fairfax, Virginia. (Four CD-ROMs). Available through DIVA-GIS: https://diva-gis.org/data.html (accessed May 19, 2026).


Automatically calculate Local G hot spot intensity

Description

Function automatically calculates the Local G hot spot intensity measure for spatial points or spatial polygons. Uses RANN for efficient nearest neighbor calculation (spatial points only); users can specify the number of neighbors (k). Users can specify the neighborhood style (see spdep::nb2listw) with default being standardized weight matrix (W).

Usage

hot_spot(
  insert,
  variable = NULL,
  style = "W",
  k = 9,
  remove_missing = TRUE,
  NA_Value = 0,
  include_Moran = FALSE
)

Arguments

insert

Spatial point or spatial polygon object. Acceptable formats include sf, SpatialPolygonsDataFrame, SpatialPointsDataFrame.

variable

Column name or numeric vector containing the variable from which the local G statistic will be calculated. Must possess a natural scale that orders small and large observations (i.e. number, percentage, ratio and not model residuals).

style

Style can take values 'W', 'B', 'C', 'U', 'mimax', 'S' (see nb2listw). Character string.

k

Number of neighbors. Default is 9. Numeric.

remove_missing

Whether to calculate statistic without missing values. If FALSE, substitute value must be supplied to NA_Value.

NA_Value

Substitute for missing values. Default value is 0. Numeric.

include_Moran

Calculate local Moran's I statistics. Default is FALSE. Logical.

Value

If input is sf, SpatialPolygonsDataFrame or SpatialPointsDataFrame object, returns sf object with same geometries and columns as input, appended with additional column containing Local G estimates (LocalG). If input is RasterLayer object, returns RasterBrick object containing original values (Original) and Local G estimates (LocalG).

Examples

# Calculate Local G for sf point layer
data(clea_deu2009_pt)
out_1 <- hot_spot(insert=clea_deu2009_pt, variable = clea_deu2009_pt$to1)
class(out_1)
plot(out_1["LocalG"])

# Calculate Local G for sf polygon layer (variable as numeric vector)
data(clea_deu2009)
out_2 <- hot_spot(insert=clea_deu2009, variable = clea_deu2009$to1)
summary(out_2$LocalG)
plot(out_2["LocalG"])


# Calculate Local G for sf polygon layer (variable as column name)
out_3 <- hot_spot(insert=clea_deu2009, variable = "to1")
summary(out_3$LocalG)
plot(out_3["LocalG"])



# Calculate Local G for a SpatialPolygonsDataFrame object (variable as column name)
out_4 <- hot_spot(insert=as(clea_deu2009,"Spatial"), variable = "to1")
summary(out_4$LocalG)
plot(out_4["LocalG"])



Line-in-polygon analysis

Description

Function for basic geometry calculations on polyline features, within an overlapping destination polygon layer.

Usage

line2poly(
  linez,
  polyz,
  poly_id,
  measurez = c("length", "density", "distance"),
  outvar_name = "line",
  unitz = "km",
  reproject = TRUE,
  na_val = NA,
  verbose = TRUE
)

Arguments

linez

Source polyline layer. sf object.

polyz

Destination polygon layer. Must have identical CRS to linez. sf object.

poly_id

Name of unique ID column for destination polygon layer. Character string.

measurez

Desired measurements. Could be any of "length" (sum of line lengths by polygon), "density" (sum of line lengths divided by area of polygon) and/or "distance" (distance from each polygon to nearest line feature). Default is to report all three. Character string or vector of character strings.

outvar_name

Name (root) to be given to output variable. Default is "line". Character string.

unitz

Units of measurement (linear). Defaul is "km". Character string.

reproject

Temporarily reproject layers to planar projection for geometric operations? Defaul is TRUE. Logical.

na_val

Value to be assigned to missing values (line lengths and densities only). Defaul is NA. Logical or list.

verbose

Print status messages and progress? Default is TRUE. Logical.

Value

An sf polygon object, with summary statisics of linez features aggregated to the geometries of polyz.

If measurez = "lengths", contains fields with suffixes

If measurez = "density", contains fields with suffixes

If measurez = "distance", contains fields with suffixes

If measurez = c("length","density","distance") (default), contains all of the above.

Examples

# Road lengths, densities and distance from polygon to nearest highway
data(hex_05_deu)
data(highways_deu1992)
out_1 <- line2poly(linez = highways_deu1992,
                   polyz = hex_05_deu,
                   poly_id = "HEX_ID")
plot(out_1["line_length"])
plot(out_1["line_density"])
plot(out_1["line_distance"])


# Replace missing road lengths and densities with 0's, rename variables
out_2 <- line2poly(linez = highways_deu1992,
                   polyz = hex_05_deu,
                   poly_id = "HEX_ID",
                   outvar_name = "road",
                   na_val = 0)
plot(out_2["road_length"])
plot(out_2["road_density"])
plot(out_2["road_distance"])


Make date ticker

Description

Function to create a table of consecutive dates, in SUNGEO-compliant format.

Usage

make_ticker(
  date_min = 19000101,
  date_max = as.integer(gsub("-", "", as.Date(Sys.Date())))
)

Arguments

date_min

Start date, in YYYYMMDD format. Default is 19000101. Integer.

date_max

End date, in YYYYMMDD format. Default is today. Integer.

Value

data.table object, with seven columns:

Examples

# All dates from January 1, 1900 to today
out_1 <- make_ticker()
out_1

# All dates from January 1, 1200 to today
out_2 <- make_ticker(date_min=12000101)
out_2

# All dates from January 1, 1500 to December 31, 1899
out_3 <- make_ticker(date_min=15000101, date_max=18991231)
out_3


Merge list of tables on common variable(s)

Description

Function that finds a set of common columns in a list of tables, and merges the tables on these columns.

Usage

merge_list(lst)

Arguments

lst

List of tables to be merged. List object.

Value

data.table object

Examples

# Merge list of three tables with different common variables
A <- data.table::data.table(month=month.name,year=rep(1991:1992,each=12),A=rnorm(24))
B <- data.table::data.table(year=c(1991,1992),B=rbeta(2,1,1))
C <- data.table::data.table(month=month.name,C=runif(12))

out_1 <- merge_list(list(A,B,C))
out_1


Relative scale and nesting coefficients

Description

Function to calculate relative scale and nesting metrics for changes of support from a source polygon layer to an overlapping (but spatially misaligned) destination polygon layer.

Usage

nesting(
  poly_from = NULL,
  poly_to = NULL,
  metrix = "all",
  tol_ = 0.001,
  by_unit = FALSE
)

Arguments

poly_from

Source polygon layer. sf object (polygon or multipolygon).

poly_to

Destination polygon layer. Must have identical CRS to poly_from. sf object (polygon or multipolygon).

metrix

Requested scaling and nesting metrics. See "details". Default is "all". Character string or vector of character strings.

tol_

Minimum area of polygon intersection, in square meters. Default is 0.001. Numeric.

by_unit

Include a by-unit decomposition of requested nesting metrics (if available)? Default is FALSE. Logical.

Details

Currently supported metrics (metrix) include:

It is possible to pass multiple arguments to metrix (e.g. metrix=c("rn","rs")). The default (metrix="all") returns all of the above metrics.

The function automatically reprojects source and destination geometries to Lambert Equal Area prior to calculation, with map units in meters.

Values of tol_ can be adjusted to increase or decrease the sensitivity of these metrics to small border misalignments. The default value discards polygon intersections smaller than 0.001 square meters in area.

Value

Named list, with numeric values for each requested metric in metrix. If by_unit==TRUE, last element of list is a data.table, with nesting metrics disaggregated by source unit, where the first column is a row index for the source polygon layer.

Examples

# Calculate all scale and nesting metrics for two sets of polygons
data(clea_deu2009)
data(hex_05_deu)
nest_1 <- nesting(
              poly_from = clea_deu2009,
              poly_to = hex_05_deu
              )
nest_1

# Calculate just Relative Nesting, in the opposite direction
nest_2 <- nesting(
              poly_from = hex_05_deu,
              poly_to = clea_deu2009,
              metrix = "rn"
              )
nest_2

Point-to-polygon interpolation, simple overlay method

Description

Function for assigning values from a source point layer to a destination polygon layer, using simple point-in-polygon overlays

Usage

point2poly_simp(
  pointz,
  polyz,
  varz,
  char_varz = NULL,
  funz = list(function(x) {
     sum(x, na.rm = TRUE)
 }),
  na_val = NA,
  drop_na_cols = FALSE
)

Arguments

pointz

Source points layer. sf object.

polyz

Destination polygon layer. Must have identical CRS to pointz. sf object.

varz

Names of variable(s) to be assigned from source polygon layer to destination polygons. Character string or vector of character strings.

char_varz

Names of character string variable(s) in varz. Character string or vector of character strings.

funz

Aggregation function to be applied to variables specified in varz. Must take as an input a vector x. Function or list of functions.

na_val

Value to be assigned to missing values. Defaul is NA. Logical or list.

drop_na_cols

Drop columns with completely missing values. Defaul is FALSE. Logical.

Details

Assignment procedures are the same for numeric and character string variables. All variables supplied in varz are passed directly to the function specified in funz. If different sets of variables are to be aggregated with different functions, both varz and funz should be specified as lists (see examples below).

Value

Returns a sf polygon object, with variables from pointz assigned to the geometries of polyz.

Examples

# Assignment of a single variable (sums)
data(hex_05_deu)
data(clea_deu2009_pt)
out_1 <- point2poly_simp(pointz=clea_deu2009_pt,
                         polyz=hex_05_deu,
                         varz="vv1")
plot(out_1["vv1"])

# Replace NA's with 0's
out_2 <- point2poly_simp(pointz = clea_deu2009_pt,
                         polyz = hex_05_deu,
                         varz = "vv1",
                         na_val = 0)
plot(out_2["vv1"])


# Multiple variables, with different assignment functions
out_3 <- point2poly_simp(pointz = clea_deu2009_pt,
                         polyz = hex_05_deu,
                         varz = list(
                           c("to1","pvs1_margin"),
                           c("vv1"),
                           c("incumb_pty_n","win1_pty_n")),
                         funz = list(
                           function(x){mean(x,na.rm=TRUE)},
                           function(x){sum(x,na.rm=TRUE)},
                           function(x){paste0(unique(na.omit(x)),collapse=" | ") }),
                         na_val = list(NA_real_,0,NA_character_))
plot(out_3["pvs1_margin"])



Point-to-polygon interpolation, tessellation method

Description

Function for interpolating values from a source point layer to a destination polygon layer, using Voronoi tessellation and area/population weights.

Usage

point2poly_tess(
  pointz,
  polyz,
  poly_id,
  char_methodz = "aw",
  methodz = "aw",
  pop_raster = NULL,
  varz = NULL,
  pycno_varz = NULL,
  char_varz = NULL,
  char_assign = "biggest_overlap",
  funz = function(x, w) {
     stats::weighted.mean(x, w, na.rm = TRUE)
 },
  return_tess = FALSE,
  seed = 1
)

Arguments

pointz

Source points layer. sf object.

polyz

Destination polygon layer. Must have identical CRS to pointz. sf object.

poly_id

Name of unique ID column for destination polygon layer. Character string.

char_methodz

Interpolation method(s) for character strings. Could be either of "aw" (areal weighting, default) or "pw" (population weighting). See "details". Character string.

methodz

Interpolation method(s) for numeric covariates. Could be either of "aw" (areal weighting, default) and/or "pw" (population weighting). See "details". Character string or vector of character strings.

pop_raster

Population raster to be used for population weighting, Must be supplied if methodz="pw". Must have identical CRS to poly_from. raster or SpatRaster object.

varz

Names of numeric variable(s) to be interpolated from source polygon layer to destination polygons. Character string or list of character strings.

pycno_varz

Names of spatially extensive numeric variables for which the pycnophylactic (mass-preserving) property should be preserved. Character string or vector of character strings.

char_varz

Names of character string variables to be interpolated from source polygon layer to destination polygons. Character string or vector of character strings.

char_assign

Assignment rule to be used for variables specified in char_varz. Could be either "biggest_overlap" (default) or "all_overlap". See "details". Character string or vector of character strings.

funz

Aggregation function to be applied to variables specified in varz. Must take as an input a numeric vector x and vector of weights w. Function or list of functions.

return_tess

Return Voronoi polygons, in addition to destinaton polygon layer? Default is FALSE. Logical.

seed

Seed for generation of random numbers. Default is 1. Numeric.

Details

This function interpolates point data to polygons with a two-step process. In the first step (tessellation), each point is assigned a Voronoi cell, drawn such that (a) the distance from its borders to the focal point is less than or equal to the distance to any other point, and (b) no gaps between cells remain. The second step (interpolation) performs a polygon-in-polygon interpolation, using the Voronoi cells as source polygons.

Currently supported integration methods in the second step (methodz) include:

When a list of variables are supplied and one methods argument specified, then the chosen method will be applied to all variables.

When a list of of variables are supplied and multiple methods arguments specified, then weighting methods will be applied in a pairwise order. For example, specifying varz = list(c("to1","pvs1_margin"), c("vv1")) and methodz = c('aw', 'pw') will apply areal weighting to the the first set of variables (to1 and pvs1_margin) and population weighing to the second set (vv1).

Interpolation procedures are handled somewhat differently for numeric and character string variables. For numeric variables supplied in varz, "aw" and/or "pw" weights are passed to the function specified in funz. If different sets of numeric variables are to be aggregated with different functions, both varz and funz should be specified as lists (see examples below).

For character string (and any other) variables supplied in char_varz, "aw" and/or "pw" weights are passed to the assignment rule(s) specified in char_assign. Note that the char_varz argument may include numerical variables, but varz cannot include character string variables.

Currently supported assignment rules for character strings (char_assign) include:

It is possible to pass multiple arguments to char_assign (e.g. char_assign=c("biggest_overlap","all_overlap")), in which case the function will calculate both, and append the resulting columns to the output.

Value

If return_tess=FALSE, returns a sf polygon object, with variables from pointz interpolated to the geometries of polyz.

If return_tess=TRUE, returns a list, containing

Examples

# Interpolation of a single variable, with area weights
data(hex_05_deu)
data(clea_deu2009_pt)
out_1 <- point2poly_tess(pointz = clea_deu2009_pt,
                             polyz = hex_05_deu,
                             poly_id = "HEX_ID",
                             varz = "to1")
plot(out_1["to1_aw"])

# Extract and inspect tessellation polygons
out_2 <- point2poly_tess(pointz = clea_deu2009_pt,
                             polyz = hex_05_deu,
                             poly_id = "HEX_ID",
                             varz = "to1",
                             return_tess = TRUE)
plot(out_2$tess["to1"])
plot(out_2$result["to1_aw"])


# Interpolation of multiple variables, with area and population weights
data(gpw4_deu2010)
gpw4_deu2010 <- terra::rast(gpw4_deu2010) # unwrap PackedSpatRaster
out_3 <- point2poly_tess(pointz = clea_deu2009_pt,
                         polyz = hex_05_deu,
                         poly_id = "HEX_ID",
                         methodz = c("aw","pw"),
                         varz = list(
                           c("to1","pvs1_margin"),
                           c("vv1")
                         ),
                         pycno_varz = "vv1",
                         funz = list(
                           function(x,w){stats::weighted.mean(x,w)},
                           function(x,w){sum(x*w)}
                           ),
                         char_varz = c("incumb_pty_n","win1_pty_n"),
                         pop_raster = gpw4_deu2010)
plot(out_3["vv1_pw"])


Area and population weighted polygon-to-polygon interpolation

Description

Function for interpolating values from a source polygon layer to an overlapping (but spatially misaligned) destination polygon layer, using area and/or population weights.

Usage

poly2poly_ap(
  poly_from,
  poly_to,
  poly_to_id,
  geo_vor = NULL,
  methodz = "aw",
  char_methodz = "aw",
  pop_raster = NULL,
  varz = NULL,
  pycno_varz = NULL,
  char_varz = NULL,
  char_assign = "biggest_overlap",
  funz = function(x, w) {
     stats::weighted.mean(x, w, na.rm = TRUE)
 },
  seed = 1
)

Arguments

poly_from

Source polygon layer. sf object.

poly_to

Destination polygon layer. Must have identical CRS to poly_from. sf object.

poly_to_id

Name of unique ID column for destination polygon layer. Character string.

geo_vor

Voronoi polygons object (used internally by point2poly_tess). sf object.

methodz

Area interpolation method(s). Could be either of "aw" (areal weighting, default) and/or "pw" (population weighting). See "details". Character string or vector of character strings.

char_methodz

Interpolation method(s) for character strings. Could be either of "aw" (areal weighting, default) or "pw" (population weighting). See "details". Character string.

pop_raster

Population raster to be used for population weighting, Must be supplied if methodz="pw". Must have identical CRS to poly_from. raster or SpatRaster object.

varz

Names of numeric variable(s) to be interpolated from source polygon layer to destination polygons. Character string or vector of character strings.

pycno_varz

Names of spatially extensive numeric variables for which the pycnophylactic (mass-preserving) property should be preserved. Character string or vector of character strings.

char_varz

Names of character string variables to be interpolated from source polygon layer to destination polygons. Character string or vector of character strings.

char_assign

Assignment rule to be used for variables specified in char_varz. Could be either "biggest_overlap" (default) or "all_overlap". See "details". Character string or vector of character strings.

funz

Aggregation function to be applied to variables specified in varz. Must take as an input a numeric vector x and vector of weights w. Function or list of functions.

seed

Seed for generation of random numbers. Default is 1. Numeric.

Details

Currently supported integration methods (methodz) include:

It is possible to pass multiple arguments to methodz (e.g. methodz=c("aw","pw")), in which case the function will calculate both sets of weights, and append the resulting columns to the output.

Interpolation procedures are handled somewhat differently for numeric and character string variables. For numeric variables supplied in varz, "aw" and/or "pw" weights are passed to the function specified in funz. If different sets of numeric variables are to be aggregated with different functions, both varz and funz should be specified as lists (see examples below).

For character string (and any other) variables supplied in char_varz, "aw" and/or "pw" weights are passed to the assignment rule(s) specified in char_assign. Note that the char_varz argument may include numerical variables, but varz cannot include character string variables.

Currently supported assignment rules for character strings (char_assign) include:

It is possible to pass multiple arguments to char_assign (e.g. char_assign=c("biggest_overlap","all_overlap")), in which case the function will calculate both, and append the resulting columns to the output.

Value

sf polygon object, with variables from poly_from interpolated to the geometries of poly_to.

Examples

# Interpolation of a single variable, with area weights
data(clea_deu2009)
data(hex_05_deu)
out_1 <- poly2poly_ap(poly_from = clea_deu2009,
              poly_to = hex_05_deu,
              poly_to_id = "HEX_ID",
              varz = "to1"
             )
plot(out_1["to1_aw"])


# Interpolation of multiple variables, with area weights
out_2 <- poly2poly_ap(
              poly_from = clea_deu2009,
              poly_to = hex_05_deu,
              poly_to_id = "HEX_ID",
              varz = list(
                c("to1","pvs1_margin"),
                c("vv1") ),
              pycno_varz = "vv1",
              funz = list(
                function(x,w){stats::weighted.mean(x,w)},
                function(x,w){sum(x*w)} ),
              char_varz = c("incumb_pty_n","win1_pty_n")
             )
plot(out_2["vv1_aw"])



# Interpolation of a single variable, with population weights
data(gpw4_deu2010)
gpw4_deu2010 <- terra::rast(gpw4_deu2010) # unwrap PackedSpatRaster
out_3 <- poly2poly_ap(poly_from = clea_deu2009,
                         poly_to = hex_05_deu,
                         poly_to_id = "HEX_ID",
                         varz = "to1",
                         methodz = "pw",
                         pop_raster = gpw4_deu2010)
plot(out_3["to1_pw"])



# Interpolation of a single variable, with area and population weights
out_4 <- poly2poly_ap(poly_from = clea_deu2009,
                         poly_to = hex_05_deu,
                         poly_to_id = "HEX_ID",
                         varz = "to1",
                         methodz = c("aw","pw"),
                         pop_raster = gpw4_deu2010)
plot(out_4["to1_aw"])


Convert simple features object into regularly spaced raster

Description

This function takes in an sf spatial object (polygon or point) and returns a regularly spaced RasterLayer. Reverse translation option allows users to create an sf polygon object from the regularly spaced RasterLayer. This function can also conver the sf object into a cartogram with a user-specified variable name.

Usage

sf2raster(
  polyz_from = NULL,
  pointz_from = NULL,
  input_variable = NULL,
  reverse = FALSE,
  poly_to = NULL,
  return_output = NULL,
  return_field = NULL,
  aggregate_function = list(function(x) mean(x, na.rm = TRUE)),
  reverse_function = list(function(x) mean(x, na.rm = TRUE)),
  grid_dim = c(1000, 1000),
  cartogram = FALSE,
  carto_var = NULL,
  message_out = TRUE,
  return_list = FALSE
)

Arguments

polyz_from

Source polygon layer. sf object.

pointz_from

Source point layer. sf object.

input_variable

Name of input variable from source layer. Character string.

reverse

Reverse translation from raster layer to sf polygon object (polygon features only). Default is FALSE.

poly_to

Destination polygon layer for reverse conversion. Must be specified if reverse=TRUE. sf object.

return_output

Return output for reverse conversion. Must be specified if reverse=TRUE.

return_field

Return field for reverse conversion. Must be specified if reverse=TRUE.

aggregate_function

Aggregation function to be applied to variables specified in input_variable. Must take as an input a numeric vector x. Function or list of functions. Default is mean.

reverse_function

Aggregation function for reverse conversion. Must be specified if reverse=TRUE. Function or list of functions. Default is mean.

grid_dim

Dimensions of raster grid. Numerical vector of length 2 (number of rows, number of columns). Default is c(1000,1000).

cartogram

Cartogram transformation. Logical. Default is FALSE.

carto_var

Input variable for cartogram transformation. Must be specified if cartogram=TRUE. Character string.

message_out

Print informational messages. Logical. Default is TRUE.

return_list

Return full set of results, including input polygons, centroids and field raster. Default is FALSE. Logical.

Value

If return_list=FALSE (default) and reverse=FALSE (default), returns RasterLayer object, with cell values corresponding to input_variable.

If return_list=TRUE and input layer is polygon, returns a list containing

If return_list=TRUE and input layer is points, returns a list containing

If reverse=TRUE, returns an sf polygon layer, with columns corresponding to input_variable and auto-generated numerical ID Field.

Examples

# Rasterization of polygon layer.
data(clea_deu2009)
out_1 <- sf2raster(polyz_from = utm_select(clea_deu2009),
                   input_variable = "to1")
terra::plot(out_1)


# Rasterization of point layer
data(clea_deu2009_pt)
out_2 <- sf2raster(pointz_from = utm_select(clea_deu2009_pt),
                   input_variable = "to1",
                   grid_dim = c(25,25))
terra::plot(out_2)


# Cartogram (vote turnout scaled by number of valid votes)
out_3 <- sf2raster(polyz_from = utm_select(clea_deu2009),
                   input_variable = "to1",
                   cartogram = TRUE,
                   carto_var = "vv1")
terra::plot(out_3)


# Polygonization of cartogram raster
out_4a <- sf2raster(polyz_from = utm_select(clea_deu2009),
                    input_variable = "to1",
                    cartogram = TRUE,
                    carto_var = "vv1",
                    return_list = TRUE)
out_4 <- sf2raster(reverse = TRUE,
                   poly_to = out_4a$poly_to,
                   return_output = out_4a$return_output,
                   return_field = out_4a$return_field)
terra::plot(out_4)


Smart numerical rounding function

Description

Function to round numerical values with minimal information loss (e.g. to avoid "0.000" values in tables).

Usage

smart_round(x, rnd = 0, return_char = TRUE)

Arguments

x

Vector of values to be rounded. Numeric.

rnd

Requested number of decimal places. Default is 0. Non-negative integer.

return_char

Return rounded values as character string? Default is TRUE. Logical.

Details

Rounds the values in its first argument to the specified number of decimal places (default 0). If brute-force rounding produces zero values (e.g. "0.00"), the number of decimal places is expanded to include the first significant digit.

Value

If return_char=TRUE, returns a character string of same length as x. If return_char=FALSE, returns a numerical vector of same length as x.

Examples

# Round a vector of numbers, character string output (best for tables)
out_1 <- smart_round(c(.0013,2.3,-1,pi),rnd=2)
out_1

# Round a vector of numbers, numerical output
out_2 <- smart_round(c(.0013,2.3,-1,pi),rnd=2,return_char=FALSE)
out_2

Update bounding box of sf object

Description

Function to update the coordinates of the bounding box of sf vector data objects (e.g. after cropping or subsetting).

Usage

update_bbox(sfobj)

Arguments

sfobj

Layer to be updated. sf object.

Value

sf object, with corrected bounds.

Examples

# Update bbox for subset of sf object
data(clea_deu2009)
out_1 <- update_bbox(clea_deu2009[clea_deu2009$cst_n%in%c("Berlin"),])
out_1

# Bounding box of full dataset
data.table::as.data.table(clea_deu2009)[,sf::st_bbox(geometry)]

# Bounding box of subset (incorrect)
data.table::as.data.table(clea_deu2009)[cst_n%in%c("Berlin"),sf::st_bbox(geometry)]

# Corrected bounding box
data.table::as.data.table(out_1)[,sf::st_bbox(geometry)]


Automatically convert geographic (degree) to planar coordinates (meters)

Description

Function to automatically convert simple feature, spatial and raster objects with geographic coordinates (longitude, latitude / WGS 1984, EPSG:4326) to planar UTM coordinates. If the study region spans multiple UTM zones, defaults to Albers Equal Area.

Usage

utm_select(x, max_zones = 5, return_list = FALSE)

Arguments

x

Layer to be reprojected. sf, sp, SpatRaster or RasterLayer object.

max_zones

Maximum number of UTM zones for single layer. Default is 5. Numeric.

return_list

Return list object instead of reprojected layer (see Details). Default is FALSE. Logical.

Details

Optimal map projection for the object x is defined by matching its horizontal extent with that of the 60 UTM zones. If object spans multiple UTM zones, uses either the median zone (if number of zones is equal to or less than max_zones) or Albers Equal Area projection with median longitude as projection center (if number of zones is greater than max_zones).

Value

Re-projected layer. sf or RasterLayer object, depending on input.

If return_list=TRUE, returns a list object containing

Examples

# Find a planar projection for an unprojected (WSG 1984) hexagonal grid of Germany
data(hex_05_deu)
sf::st_crs(hex_05_deu)
out_1 <- utm_select(hex_05_deu)
sf::st_crs(out_1)

# Find a planar projection for a raster
data(gpw4_deu2010)
gpw4_deu2010 <- terra::rast(gpw4_deu2010) # unwrap PackedSpatRaster
sf::st_crs(gpw4_deu2010)
out_2 <- utm_select(gpw4_deu2010)
sf::st_crs(out_2)

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.