Skip to contents
library(happign)
#> Please make sure you have an internet connection.
#> Use happign::get_last_news() to display latest geoservice news.
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(sf)
#> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.4.0; sf_use_s2() is TRUE
library(tmap)
tmap_mode("view")
#>  tmap mode set to "view".

Presentation

APIs carto were developed to automatically retrieve certain spatial information required in administrative forms. The main advantage of these APIs is that they can be queried without spatial data. To do this with get_wfs, you would have to use an ECQL query with the ecql_filter argument which can be tricky.

happign implements APIs carto through get_apicarto_* functions.

API carto cadastre

Documentation : https://apicarto.ign.fr/api/doc/cadastre

The API carto cadastre provides following informations :

  • the boundaries of a town (type = "commune")
  • the parcel sections or divisions (type = "section" or type = "division")
  • the cadastral parcels (type = "cadastre")
  • information on non-vectorized parcels (type = "localisant")

At least three parameters must be set :

  • x : An indication about the location. Could be a shape, an insee code or a departement code
  • type : What service do you want to use? (see above)
  • source : The data source "PCI" for “Parcellaire Express” or "BDP" for “BD Parcellaire”. The BD Parcellaire product is a historical product that is no longer updated. It is therefore strongly recommended to use the Parcellaire Express product which is updated every six months.

All other parameters are used to refine the query.

Usage

We’ll start with a simple example : retrieve borders of multiple town. Because get_apicaro_cadastre is a vectorized function, it’s possible to set multiple insee code. If you do not know insee codes, you can consult existing codes from the internal dataframe com_2025.

data("com_2025", package = "happign")

# all town starting with "KER", yes I'm coming from "La Bretagne"
ker_insee_code <- com_2025[startsWith(com_2025$NCC_COM, "KER"), "COM"]
ker_borders <- get_apicarto_cadastre(ker_insee_code, type = "commune")
#> Warning: No data found for : 29092

# result
tm_shape(ker_borders)+
   tm_polygons(col = "black")

Another common case consists in recovering the geometry of the parcels from a “cadastral matrix extract”. The latter lists for each owner all his built and unbuilt properties owned in a commune. It is a private information and to obtain one it is necessary to ask for an extract top the Center of the Land taxes. In this example a false simplified cadastral matrix is used.

cad_mat <- data.frame(CODE_DEP = rep("29", 10),
                      CODE_COM = rep("158", 10),
                      SECTION = rep(c("AX", "AV"), each = 5),
                      N_PARC = c("0001","0002","0003","0004","0005",
                                 "0116","0117","0118","0119","0120"))

parcels <- get_apicarto_cadastre(paste0(cad_mat$CODE_DEP, cad_mat$CODE_COM),
                                 section = cad_mat$SECTION,
                                 numero = cad_mat$N_PARC,
                                 type="parcelle")
#> Warning: No data found for : 29158 - AX - 0005

tm_shape(parcels)+
   tm_borders(col = "black")

API carto urbanism

Documentation : https://apicarto.ign.fr/api/doc/gpu

The API carto GPU allows you to obtain urban planning information intersecting a geometry. Beware, not all municipalities are in the geoportal of urbanism!

RNU

First of all, you can check if a commune is under the National Urbanism Regulation from is insee code. The RNU fully apply in communes that have neither a local map nor a local urban plan (PLU, PLUi) nor a document in replacement of a PLU.

is_rnu <- get_apicarto_gpu("29158", ressource = "municipality")
#> Features downloaded : 1
is_rnu$is_rnu
#> [1] FALSE

# Penmarch is under the RNU and therefore has a document of urbanism

is_rnu <- get_apicarto_gpu("23004", ressource = "municipality")
#> Features downloaded : 1
is_rnu$is_rnu
#> [1] TRUE

# Anzeme is under the RNU and therefore has a town planning document

PLU, PLUi, POS, CC, PSMV

Urban planning documents can take several forms: * PLU : Local Urbanism Plan * PLUi : Intercommunal Local Urbanism Plan * POS : Land use plan * PSMV : Plan of Safeguarding and Development * CC : Communal map

The first step is to find out if urban planning document are available if it is the case, find the document’s partition i.e. its ID :

# find out if documents are available
penmarch <- get_apicarto_cadastre("29158", "commune")
doc <- get_apicarto_gpu(st_centroid(penmarch), "document") 
#> Warning: st_centroid assumes attributes are constant over geometries
# It's better to use centroid instead of borders to avoid conflict with other communes

partition <- doc |>
   filter(grid_title == "PENMARCH") |>
   pull(partition)

Now that the partition is recovered, it is possible to obtain several resources for a specific document. The different resources available are specified in the documentation of the function ?get_apicarto_gpu()

zone_urba <- get_apicarto_gpu(partition, ressource = "zone-urba")

# click on polygon for legend
tm_shape(zone_urba)+
   tm_polygons("libelong", legend.show = FALSE)

Because get_apicarto_gpu is vectorized, many resources can be returned at the same time. This allows to identify all the prescriptions at once for example :

ressources <- c("prescription-surf", "prescription-lin", "prescription-pct")
prescriptions <- get_apicarto_gpu(partition,
                                  ressource = ressources,
                                  dTolerance = 10)
#> Warning: Resources have different attributes and cannot be joined. List is
#> returned.
tm_shape(prescriptions[[1]])+
   tm_polygons("libelle", legend.col.show = FALSE, lwd = 2)+
tm_shape(prescriptions[[2]])+
   tm_lines("libelle", legend.col.show = FALSE, lwd = 2)+
tm_shape(prescriptions[[3]])+
   tm_dots("libelle",  legend.show = FALSE, size = 0.1)