GreenR
greenR is an R package that enables the quantification, analysis, and visualization of urban greenness within city networks. It leverages data from OpenStreetMap (www.openstreetmap.org) and is implemented as both an R package and a Shiny web application for user-friendly interaction.
Install / Use
/learn @sachit27/GreenRREADME
greenR: Urban Environmental Analytics for R
Quantify. Analyze. Visualize.
greenR is an open-source R package for quantifying, analyzing, and visualizing urban greenness. It uses OpenStreetMap (OSM) and other geospatial sources to assign green indices to street segments and urban areas in a scalable way. The package enables greenness measurement, equity assessment, and interpretive mapping, and includes interactive Mapbox visualizations for exploring results directly on the map. Beyond basic greenery metrics, greenR supports canopy height modeling, Green View Index (street-level vegetation visibility), and accessibility to parks and green spaces. It also incorporates urban heat island analysis, allowing researchers to examine how greenness, built density, and micro-climate interact across the urban fabric.
Key Features
| Feature | Description | |---------|-------------| | 🌳 Green Index Quantification | Street-segment level greenness scores using OSM data with configurable distance decay functions | | 🌲 Canopy Height Modeling | 1m resolution tree canopy analysis using Meta/WRI ALS GEDI v6 global dataset | | 🚶 Accessibility Analysis | Isochrone-based green space accessibility with walking/cycling network routing via Mapbox | | 🌡️ Urban Heat Island Analysis | Satellite-based thermal mapping with ECOSTRESS/Landsat, Getis-Ord Gi* hotspot detection, and green-heat correlations | | 📊 Spatial Inequality Metrics | Gini coefficients, Lorenz curves, and H3 hexagonal binning for equity analysis | | 🧭 Directional Analysis | Orientation-aware green index calculation to assess greenness exposure by direction | | 🔗 Open Data Integration | Seamless integration with OpenStreetMap, Microsoft Planetary Computer, and GHSL data | | 🗺️ Interactive Visualizations | Leaflet maps, 3D Mapbox visualizations, and publication-ready ggplot2 outputs | | 🖥️ Shiny Application | No-code web interface for interactive greenness analysis without programming |
For more detailed information about the motivation, methodology, and validation, please have a look at this paper published in Ecological Indicators
✨ greenR was selected as the Winner of the Prix Carto 2025 – Edu category.
At the celebration of 100 years of the Institute of Cartography and Geoinformation at ETH Zurich, the Swiss Society of Cartography awarded this prize.
Installation
It can be installed via CRAN
install.packages("greenR")
In case there are difficulties in installing through CRAN, you can do it directly via GitHub by running the command below in R.
# install.packages("remotes") # Uncomment if you do not have the 'remotes' package installed
remotes::install_github("sachit27/greenR", dependencies = TRUE)
Or you can also use devtools to install the package.
library(devtools)
devtools::install_github("sachit27/greenR", dependencies = TRUE)
If you're updating from a previous version and encounter any issues, try clearing your package cache:
remove.packages("greenR")
.rs.restartR()
devtools::install_github("sachit27/greenR", dependencies = TRUE, force = TRUE)
NOTE: If you encounter any issues with the installation of the "osmdata" R package as a dependency, you may bypass CRAN and install it directly from the Github Repository. This alternative installation method can be useful if the standard CRAN installation is not working as expected. Here is how you can install it.
remotes::install_github ("ropensci/osmdata")
After installing the greenR package, you can load the package into the R session using the following command.
library(greenR)
Specify the location and download the data
The first step is to acquire data. This provides a systematic approach to collecting the requisite geospatial data from OSM, thereby serving as the foundation for all subsequent analyses. The users can simply specify any city or neighborhood (that has data available in OSM database). This function looks in the database and finds any city and downloads OSM data for the specified spatial area with regard to three key environmental features: highways, green areas, and trees. Here green areas include all the areas with the following tags: "forest", "vineyard", "plant_nursery", "orchard", "greenfield", "recreation_ground", "allotments", "meadow","village_green","flowerbed", "grass", "farmland", "garden", "dog_park","nature_reserve", and "park".
Option 1: City or neighborhood name
data <- get_osm_data("City of London, United Kingdom")
Or
data <- get_osm_data("Fulham, London, United Kingdom")
Handling Overpass API Timeouts
OpenStreetMap data is retrieved via the Overpass API. Occasionally, you may encounter an error such as: HTTP 504 Gateway Timeout This typically occurs when the default Overpass server is temporarily overloaded.
If this happens, you can switch to an alternative Overpass mirror before running get_osm_data():
library(osmdata)
# Set an alternative Overpass server
set_overpass_url("https://lambert.openstreetmap.de/api/interpreter")
# Then retry
data <- get_osm_data("Basel, Switzerland")
Option 2: Bounding box coordinates
For more precise control over the area of interest, users can provide a bounding box using coordinates. This is particularly useful when you need to define a specific area around a point of interest.
# Define bounding box: (left, bottom, right, top)
bbox <- c(-0.1, 51.5, 0.1, 51.7) # Example bounding box for central London
data <- get_osm_data(bbox)
Visualise green spaces and clustering
The visualize_green_spaces() function is designed to aid in the visual assessment of green space data. Utilizing an integrated leaflet map, users can explore the distribution and mapping quality of green spaces within a specified area. By plotting this data on an interactive leaflet map, users gain insights into the extent and accuracy of green space representation. After visualizing the green spaces within the desired area, users may wish to contribute to the OpenStreetMap project to enhance the data quality or add unrepresented areas. Additionally, the green_space_clustering() function initially transforms the green spaces into an equal-area projection to calculate the areas accurately. Post-transformation, the K-means algorithm is applied to these areas, clustering the green spaces based on the number of clusters specified.
green_areas_data <- data$green_areas
visualize_green_spaces(green_areas_data)
green_space_clustering(green_areas_data, num_clusters = 3)

Green and Tree Count Density Analysis
The analyze_green_and_tree_count_density() function quantifies the spatial distribution and inequality of green areas or tree locations within an urban area using hexagonal spatial bins. Users can select either green area polygons or tree point data from OpenStreetMap as the input.
- Method: The function divides the study area into hexagons (using the H3 spatial indexing system) and counts either the number of green polygons or trees in each hexagon. Multiple classification schemes (quantile, Jenks, fixed thresholds) are available to assign each hexagon to low, medium, or high density classes, automatically selecting a fallback if the data are sparse.Spatial metrics are calculated, including counts per km² over the hexagonal area.
- Analytics: The function produces a comprehensive set of summary statistics and inequality measures, including the total, mean, and median counts per hexagon, as well as the Gini index to quantify spatial inequality in the distribution of green areas or trees. It also calculates skewness and kurtosis to characterize the shape of the distribution, and generates the Lorenz curve with its area-under-curve to visualize and measure inequality. Additionally, the function reports the classification method and the bin thresholds used to categorize density across the study area.
Interactive maps (Leaflet) and plots (such as the Lorenz curve) are generated automatically. Results and metrics can also be exported as GeoJSON and JSON files for further analysis.
osm_data <- get_osm_data("Zurich, Switzerland")
result <- analyze_green_and_tree_count_density(
osm_data = osm_data,
mode = "tree_density", #or you can use "green_area"
h3_res = 8,
save_lorenz = TRUE
)
result$map # Interactive Leaflet map
result$analytics # Summary statistics and Gini index
result$lorenz_plot # Lorenz curve plot (if saved)
Accessibility analysis
Mapbox Version (Dynamic): The 'accessibility_mapbox' function creates an accessibility map using Mapbox GL JS. This map shows green areas and allows users to generate isochrones for walking times. The resulting HTML file includes interactive features for changing the walking time and moving the location marker dynamically.
mapbox_token <- "your_mapbox_access_token_here"
accessibility_mapbox(green_areas_data, mapbox_token)
.
qqbot-media
343.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
