Title: R Interface to Get French Heritage Data
Version: 0.1.0
Description: Get spatial vector data from the Atlas du Patrimoine (http://atlas.patrimoines.culture.fr/atlas/trunk/), the official national platform of the French Ministry of Culture, and facilitate its use within R geospatial workflows. The package provides functions to list available heritage datasets, query and retrieve heritage data using spatial queries based on user-provided sf objects, perform spatial filtering operations, and return results as sf objects suitable for spatial analysis, mapping, and integration into heritage management and landscape studies.
License: GPL (≥ 3)
Encoding: UTF-8
RoxygenNote: 7.3.3
Imports: jsonlite, happign, httr2, sf, xml2
Depends: R (≥ 4.1.0)
LazyData: true
Suggests: knitr, rmarkdown, testthat (≥ 3.1.7), httptest2
Config/testthat/edition: 3
NeedsCompilation: no
Packaged: 2026-01-20 18:37:25 UTC; matth
Author: Matthieu Chevereau [aut, cre]
Maintainer: Matthieu Chevereau <matthieu.chevereau@hotmail.fr>
Repository: CRAN
Date/Publication: 2026-01-24 10:40:02 UTC

Available layer datasets

Description

A dataset containing available layer datasets from the French Ministry of Culture's "Atlas du Patrimoine" service.

Usage

all_ids

Format

A data.frame with 5 columns:

id

(character) Layer id.

title

(character) Layer title.

guid

(integer) Layer guid.

code

(character) Internal code, determined from tile.

departement

(character) Layer department

Details

Contains only data available on the 96 departments of metropolitan France.


Check that layer data codes are valid

Description

Validates that provided data codes exist in the heritage layers.

Usage

data_check(data_code)

Arguments

data_code

character vector. Data codes to check.

Details

Value

NULL. Invisibly returns NULL if all codes are valid; otherwise throws an error.

See Also

get_heritage_layernames()


Filter layer IDs by internal code and department

Description

Filters a dataset of layer IDs based on provided data codes and departments.

Usage

data_filter(ids = NULL, department, data_code)

Arguments

ids

data.frame. Input dataset of IDs. NULL as default. If NULL function uses frheritage::all_ids.

department

character vector. Department(s) to filter.

data_code

character vector. Data codes to filter. Must be valid heritage layer codes.

Details

Value

data.frame. Subset of ids filtered by data_code and department, with departement column removed.


Example files for testing

Description

These files are used internally for testing functions.


Cast geometries to simple types

Description

This internal helper converts multi-part geometries to their single-part equivalents: MULTIPOLYGON became POLYGON, MULTILINESTRING became LINESTRING, MULTIPOINT became POINT. If the input contains mixed types, each feature is cast individually.

Usage

geo_cast(x)

Arguments

x

An sf object with geometries to cast.

Value

An sf object with geometries cast to simple types.


Retrieve INSEE department codes for each feature in an sf object

Description

This internal helper retrieves the administrative department (code_insee) intersecting each feature of the input sf object. It uses the Admin Express COG WFS service from IGN via the happign package.

Usage

geo_dep(x)

Arguments

x

An sf object defining the area(s) of interest.

Details

Value

A character vector with the INSEE department code corresponding to each feature of x. Returns NULL if no intersection is found.


Compute bounding boxes for each feature in an sf object

Description

This internal helper computes the bounding box (left, bottom, right, top) for each feature in an sf object.

Usage

geo_extent(x, crs = 4326)

Arguments

x

An sf object (POLYGON or MULTIPOLYGON) for which to compute bounding boxes.

crs

Numeric, Code of the target CRS. Default is 4326.

Details

Value

A data.frame where each row corresponds to a feature's bounding box, with columns left, bottom, right, top.


Check that spatial objects are valid sf layers with expected geometry type

Description

This internal helper verifies that all provided objects:

Usage

geo_object_check(
  ...,
  check_class = TRUE,
  check_valid = TRUE,
  check_empty = TRUE,
  allowed_geom_classes = "POLYGON"
)

Arguments

...

One or several spatial objects to test.

check_class

Logical. Whether to check that each object inherits from class sf. Default is TRUE.

check_valid

Logical. Whether to check that geometries are valid. Default is TRUE.

check_empty

Logical. Whether to check that geometries are not empty. Default is TRUE.

allowed_geom_classes

Character vector of allowed geometry types (e.g., "POLYGON", "LINE", "POINT"). If NULL, the geometry type is not checked. Default is "POLYGON".

Details

The function raises an informative error message if any condition fails. It is intended for internal validation of sf inputs before performing geometric operations or API queries.

Geometry type validation is performed via geo_object_type(), which ensures type consistency across geometries.

Value

Invisibly returns NULL. Throws an error if any object fails validation.

See Also

geo_object_type()


Determine the geometry type of an sf object

Description

This internal helper function identifies and normalizes the geometry type of an sf object. It ensures that all geometries within the object share a consistent type (e.g., "POINT", "LINESTRING", "POLYGON"). If multiple geometry types are found, an error is raised.

Usage

geo_object_type(x)

Arguments

x

An sf object whose geometry type should be determined.

Details

The function retrieves the geometry type of the input using sf::st_geometry_type() and standardizes it according to a simplified classification:

Mixed or inconsistent geometry collections are not supported.

Value

A single character(1) value representing the normalized geometry type. Possible values are: "POINT", "LINE", "POLYGON", or "GEOMETRY".


Prepare and aggregate an sf object

Description

This helper function prepares and aggregates an sf object to produce clean, valid, and unified geometries.

Usage

geo_prepare(x, crs = 2154, buffer = 10)

Arguments

x

An sf object to process.

crs

Integer. Code of the target CRS. Default is 2154.

buffer

Numeric. Buffer distance (in map units) used to merge nearby features. Default is 10.

Details

The function performs a complete geometric preparation workflow:

  1. Geometry validation: ensures that all geometries belong to allowed classes (POINT, LINE, or POLYGON).

  2. Geometry cleaning: invalid features are fixed with sf::st_make_valid(), Z/M dimensions are dropped via sf::st_zm(), and multipart geometries are cast to simple forms.

  3. Coordinate transformation: all geometries are transformed to the specified CRS using sf::st_transform().

  4. Aggregation: small buffer zones (controlled by buffer) are applied to merge adjacent or overlapping features. The buffered geometries are then dissolved with sf::st_union() and cast to POLYGON type.

This process is useful for simplifying feature sets into larger contiguous study zones or analysis areas.

Value

An sf object with cleaned, projected, and aggregated geometries as POLYGONs.


Bind multiple sf objects into a single sf

Description

This internal helper takes a list of sf objects, aligns their columns, and binds them together into a single sf object. It removes NULL or empty objects and handles nested lists of sf.

Usage

geo_sf_bind(sf_list)

Arguments

sf_list

A list of sf objects (can include nested lists).

Value

A single sf object containing all input features, or NULL if the input list is empty or contains no valid sf objects.


Read and transform shapefiles from a ZIP archive

Description

Extracts shapefiles from a ZIP archive, reads them into sf objects, and transform each to CRS:2154 (Lambert-93 projection for France), unless already in the target CRS.

Usage

geo_shapefiles_read(zip_path, crs = 2154)

Arguments

zip_path

character. Path to the ZIP archive to read.

crs

numeric or sf::st_crs object. Source CRS to assume if missing in the shapefiles. Default is 2154 (Lambert-93).

Value

A list of sf objects transform to CRS:2154.


Validate a spatial predicate string

Description

Checks that the provided spatial filter is one of the allowed OGC predicates.

Usage

geo_spatial_check(spatial_filter)

Arguments

spatial_filter

character. Spatial predicate to validate.

Value

character. The validated predicate in uppercase.


Filter an sf layer by spatial relationship

Description

This internal helper function filters features of an sf layer based on a specified spatial relationship with another sf object.

Usage

geo_spatial_filter(layer, x, spatial_filter)

Arguments

layer

An sf object to be filtered.

x

An sf object used as reference for the spatial relationship.

spatial_filter

Character. The spatial predicate to apply. Must be one of: "INTERSECTS", "DISJOINT", "CONTAINS", "WITHIN", "TOUCHES", "BBOX", "CROSSES", "OVERLAPS", "EQUALS".

Details

Value

A subset of layer where features satisfy the specified spatial predicate with respect to x.


Check if an sf object exceeds size or extent thresholds

Description

This internal helper evaluates whether a spatial object is too large based on area (for polygons) and bounding box dimensions.

Usage

geo_too_large(
  x,
  area_threshold = 1e+09,
  extent_threshold = 150000,
  verbose = TRUE
)

Arguments

x

An sf object to check.

area_threshold

Numeric. Maximum allowed area in m2 (for polygon geometries). Default is 1e9.

extent_threshold

Numeric. Maximum allowed bounding box width or height in meters. Default is 1.5e5.

verbose

Logical. If TRUE, prints a message when the object is within limits.

Value

Invisibly returns FALSE if within limits. Throws an error if thresholds are exceeded.


Retrieve and download heritage spatial data for a given sf object

Description

This function retrieves and downloads spatial heritage datasets from the French Ministry of Culture’s "Atlas du Patrimoine" service, based on the spatial extent and department(s) of a given sf object. It first identifies relevant dataset IDs and then downloads corresponding shapefiles for each requested heritage code.

Usage

get_heritage(
  x,
  data_code,
  buffer = 2500,
  crs = 2154,
  spatial_filter = "intersects",
  verbose = TRUE
)

Arguments

x

An sf object defining the area of interest.

data_code

A single character heritage dataset codes to retrieve. Valid codes can be obtained with get_heritage_layernames().

buffer

A numeric value (default = 2500). Buffer distance in meters used to slightly expand geometries before querying.

crs

An integer or sf::st_crs object (default = 2154). Coordinate reference system used for spatial processing.

spatial_filter

A character string (default = "intersects"). Spatial predicate to filter downloaded features.

verbose

Logical. If TRUE (default), prints progress and diagnostic messages.

Details

This functions only works for the 96 departments of metropolitan France.

Internally, the function:

  1. Validates the requested heritage codes.

  2. Checks the spatial filter.

  3. Prepares the geometry and aggregates nearby geometries using buffer input.

  4. Determines the corresponding INSEE department code for each geometry, using happign::get_wfs().

  5. Computes the bounding box of each geometry.

  6. Filters layers ids for the requested data_code by using frheritage::all_ids.

  7. Builds the URL and downloads the zip archive containing the shapefiles.

  8. Reads and merges shapefiles into sf objects.

Value

A single sf object if one heritage code was processed. Returns an empty sf if no matching data is found.

Examples


if (interactive()) {
  # Create a minimal sf object defining the area of interest.
  # The geometry is used to identify relevant departments and
  # to build spatial queries against the Atlas du Patrimoine.
  my_sf_layer <- sf::st_sf(
    geometry = sf::st_sfc(
      sf::st_point(c(2.21, 48.82)),
      crs = 4326
    )
  )

  # Download spatial heritage data for a given heritage code.
  # The function returns an sf object containing the retrieved features.
  heritage_data <- get_heritage(
    x = my_sf_layer,
    data_code = "IMMH",
    buffer = 2000,
    spatial_filter = "intersects"
  )
}



Retrieve heritage layer IDs for a given sf object

Description

This function retrieves available layer identifiers from the French Ministry of Culture's "Atlas du Patrimoine" service feed, based on the spatial extent and department(s) of a given sf object.

Usage

get_heritage_ids(x, buffer = 2500, crs = 2154, verbose = TRUE)

Arguments

x

An sf object defining the area of interest.

buffer

A numeric value (default = 2500). Buffer distance in meters used to slightly expand geometries before querying.

crs

An integer or sf::st_crs object (default = 2154). Coordinate reference system used for spatial processing.

verbose

Logical. If TRUE (default), prints progress and diagnostic messages.

Details

Internally, the function:

  1. Aggregates nearby geometries using buffer input.

  2. Determines the corresponding INSEE department code for each geometry, using happign::get_wfs().

  3. Computes the bounding box of each geometry.

  4. Queries the "Atlas du Patrimoine" service feed for all available metadata records (IDs, titles, GUIDs) within each bounding box.

Progress is shown for each request.

Value

A data.frame with the following columns:

id

Numeric identifier extracted from the record GUID.

title

Record title as published in the service feed.

guid

Full GUID (unique resource identifier).

code

Internal code associated with the layer.

Returns an empty data.frame if no records are found or the request fails.

Examples


if (interactive()) {
  # Create a minimal sf object defining the area of interest.
  # A simple point geometry is sufficient to trigger spatial queries.
  my_sf_layer <- sf::st_sf(
    geometry = sf::st_sfc(
      sf::st_point(c(2.21, 48.82)),
      crs = 4326
    )
  )

  # Retrieve available heritage dataset identifiers intersecting
  # the spatial extent derived from the sf object.
  ids <- get_heritage_ids(my_sf_layer)
}



Get heritage layer codes and labels

Description

This function provides a reference table mapping internal codes to explore heritage layer available datasets from French Ministry of Culture's "Atlas du Patrimoine". Optionally, it can filter the table to return only selected codes.

Usage

get_heritage_layernames(code = NULL)

Arguments

code

Optional character vector of layer codes to filter. If NULL, the full reference table is returned.

Details

The available codes and their meanings are:

Value

A data.frame with two columns:

code

Official heritage layer code (character).

label

Descriptive label (character).

Examples

# Get the full table of heritage layer codes
get_heritage_layernames()

# Filter for specific codes
get_heritage_layernames(c("IMMH", "SICI"))


Download and parse heritage metadata from a URL

Description

This internal helper performs an HTTP GET request to the given URL (typically generated by ids_url_build()) and parses the RSS XML response into a structured data.frame.

Usage

ids_download(url, timeout = 60)

Arguments

url

Character. The complete request URL.

timeout

Numeric. Request timeout in seconds (default = 60).

Value

A data.frame with columns:

id

Numeric identifier extracted from the GUID.

title

Record title as published in the service.

guid

Full GUID (unique resource identifier).

Returns an empty data.frame if the request fails or no items are found.


Determine internal codes from IDs titles

Description

This internal function converts IDs tiles into their corresponding internal codes (e.g., "IMMH", "PAMH"). It matches based on predefined keyword patterns and chooses the most specific code when multiple patterns match.

Usage

ids_to_codes(x)

Arguments

x

character vector of IDs titles.

Value

character vector of internal codes corresponding to each input IDs titles. Returns NA if no pattern matches.


Build a search URL for the RSS service

Description

This internal helper constructs a URL to query for the French Ministry of Culture's "Atlas du Patrimoine" RSS service. The URL filters data using a bounding box (extent) and an INSEE department code (insee_dep).

Usage

ids_url_build(extent, insee_dep)

Arguments

extent

A named list with elements left, bottom, right, top defining the bounding box coordinates.

insee_dep

A character or numeric INSEE department code.

Value

A character string containing the full URL to query the service.


Execute an expression completely silently

Description

This utility executes any R expression or function call and returns its value, while suppressing all printed output (cat, print), warnings, and messages.

Usage

quiet(expr)

Arguments

expr

An R expression or function call to execute silently.

Details

Value

The result of evaluating expr.


Download a ZIP file from a given URL

Description

Downloads a ZIP file from a specified URL and saves it to a temporary file.

Usage

zip_download(url, id)

Arguments

url

character. URL to download the ZIP file from.

id

character or numeric. Identifier of the dataset (used for messages).

Details

Value

character or NULL. Path to the temporary ZIP file, or NULL if download fails.


Build a URL query to download a ZIP

Description

Constructs a URL query to request a ZIP file containing spatial data from the French Ministry of Culture's "Atlas du Patrimoine" service.

Usage

zip_query_build(id, title, guid = NULL, extent_vals, crs = 2154)

Arguments

id

character or numeric. Identifier of the dataset.

title

character. Title of the dataset.

guid

character, optional. GUID of the dataset, if available.

extent_vals

numeric vector of length 4. Bounding box coordinates in order: left, bottom, right, top.

crs

numeric, default 2154. CRS code for the coordinate reference system to use in the query.

Details

Value

character. URL string for downloading the dataset in ZIP format.

mirror server hosted at Truenetwork, Russian Federation.