Public
Edited
Apr 18, 2024
Importers
Insert cell
Insert cell
## Macro variables
Insert cell
ENDPOINT_URL = "https://minio.lab.sspcloud.fr"
Insert cell
PATH_WITHIN_BUCKET = "production"
Insert cell
BUCKET = "projet-cartiflette"
Insert cell
url_file_available = `${ENDPOINT_URL}/${BUCKET}/${PATH_WITHIN_BUCKET}/available.json`
Insert cell
Insert cell
/**
* Generate a URL for a vector file stored in an S3 bucket based on provided parameters.
*
* @param {Object} options - An object containing parameters for generating the URL.
* @param {string} [options.bucket=BUCKET] - The name of the S3 bucket where the file is stored.
* @param {string} [options.pathWithinBucket='test-download28'] - The path within the bucket where the file is stored.
* @param {string} [options.provider='IGN'] - The provider of the vector file.
* @param {string} [options.dataset_family='ADMINEXPRESS'] - The dataset family.
* @param {string} [options.source='EXPRESS-COG-CARTO-TERRITOIRE'] - The source of the vector file.
* @param {string} [options.vectorfileFormat='geojson'] - The format of the vector file, can be "geojson", "topojson", "gpkg" or "shp".
* @param {string} [options.borders='COMMUNE'] - The administrative level of the tiles within the vector file.
* @param {string} [options.filter_by='REGION'] - The administrative level (supra to 'borders') that will be used to cut the vector file in pieces when writing to S3.
* @param {(string | number)} [options.year='2022'] - The year of the vector file.
* @param {(string | number)} [options.value='11'] - The value of the vector file.
* @param {(string | number | list)} [options.crs=4326] - The coordinate reference system of the vector file.
* @param {string} [options.simplification='0'] - The simplification level of the vector file.
* @returns {string} - The URL of the vector file stored in S3.
*/
function createURLS3({
bucket = BUCKET,
pathWithinBucket = PATH_WITHIN_BUCKET,
provider = 'IGN',
dataset_family = 'ADMINEXPRESS',
source = 'EXPRESS-COG-CARTO-TERRITOIRE',
vectorfileFormat = 'geojson',
borders = 'COMMUNE',
filter_by = 'REGION',
year = '2022',
value = '11',
crs = 4326,
simplification = '0'
} = {}) {
const pathWithin = createPathBucket({
bucket,
path_within_bucket: pathWithinBucket,
provider,
dataset_family,
source,
year,
administrative_level: borders,
crs,
filter_by,
value,
vectorfile_format: vectorfileFormat,
territory: 'metropole',
simplification
});
const url = ENDPOINT_URL + '/' + pathWithin;
return url;
}
Insert cell
/**
* Generate a path for a dataset within a specified bucket based on provided parameters.
*
* @param {Object} options - An object containing parameters for generating the path.
* @param {string} [options.bucket=BUCKET] - The name of the S3 bucket where the dataset is stored.
* @param {string} [options.path_within_bucket='test-download28'] - The path within the bucket where the dataset is stored.
* @param {string} [options.provider='IGN'] - The provider of the dataset.
* @param {string} [options.dataset_family='ADMINEXPRESS'] - The dataset family.
* @param {string} [options.source='EXPRESS-COG-CARTO-TERRITOIRE'] - The source of the dataset.
* @param {string} [options.year='2022'] - The year of the dataset.
* @param {string} [options.administrative_level='COMMUNE_ARRONDISSEMENT'] - The administrative level of the dataset.
* @param {number} [options.crs=4326] - The coordinate reference system code of the dataset.
* @param {string} [options.filter_by='REGION'] - The parameter used for filtering the dataset (e.g., 'REGION' or 'DEPARTMENT').
* @param {(string | number)} [options.value='11'] - The value associated with the filter parameter.
* @param {string} [options.vectorfile_format='geojson'] - The format of the dataset file, such as "geojson", "topojson", "gpkg", or "shp".
* @param {string} [options.territory='metropole'] - The territory covered by the dataset.
* @param {string} [options.simplification='0'] - The simplification level of the dataset.
* @returns {string} - The generated path for the dataset within the S3 bucket.
*/
function createPathBucket({
bucket = BUCKET,
path_within_bucket = PATH_WITHIN_BUCKET,
provider = 'IGN',
dataset_family = 'ADMINEXPRESS',
source = 'EXPRESS-COG-CARTO-TERRITOIRE',
year = '2022',
administrative_level = 'COMMUNE_ARRONDISSEMENT',
crs = 4326,
filter_by = 'REGION',
value = '11',
vectorfile_format = 'geojson',
territory = 'metropole',
simplification = '0.0'
} = {}) {
let writePath = bucket + '/' + path_within_bucket + '/provider=' + provider + '/';
writePath += 'dataset_family=' + dataset_family + '/';
writePath += 'source=' + source + '/';
writePath += 'year=' + year + '/';
writePath += 'administrative_level=' + administrative_level + '/';
writePath += 'crs=' + crs + '/';
writePath += filter_by + '=' + value + '/';
writePath += 'vectorfile_format=' + vectorfile_format + '/';
writePath += 'territory=' + territory + '/';
writePath += 'simplification=' + simplification + '/';
writePath += 'raw.' + vectorfile_format;

return writePath;
}
Insert cell
d3.json(url_test)
Insert cell
url_test = createURLS3()
Insert cell
## Main entry point
Insert cell
carti_download = download_from_cartiflette
Insert cell
/**
* This function downloads a vector file from a specified S3 bucket and returns it.
*
* @param {Object} options - An object containing parameters for downloading the vector file.
* @param {string} [options.value='28'] - The value of the vector file.
* @param {string} [options.borders='COMMUNE'] - The administrative level of the tiles within the vector file.
* @param {string} [options.vectorfileFormat='geojson'] - The format of the vector file, can be "geojson", "topojson", "gpkg" or "shp".
* @param {string} [options.filter_by='REGION'] - The administrative level (supra to 'borders') that will be used to cut the vector file in pieces when writing to S3.
* @param {(string | number)} [options.year=2022] - The year of the vector file.
* @param {string} [options.bucket=BUCKET] - The name of the bucket where the file is stored.
* @param {string} [options.pathWithinBucket='test-download28'] - The path within the bucket where the file is stored.
* @param {string} [options.provider='IGN'] - The provider of the vector file.
* @param {string} [options.source='EXPRESS-COG-CARTO-TERRITOIRE'] - The source of the vector file.
* @param {(string | number | list)} [options.crs=4326] - The coordinate reference system of the vector.
* @param {string} [options.simplification='0'] - The simplification level of the vector file.
* @returns {Promise} - A Promise that resolves to the downloaded vector file data.
*/
async function download_from_cartiflette(options) {
const {
value = '28',
borders = 'COMMUNE',
vectorfileFormat = 'geojson',
filter_by = 'REGION',
year = 2022,
bucket = BUCKET,
pathWithinBucket = PATH_WITHIN_BUCKET,
provider = 'IGN',
source = 'EXPRESS-COG-CARTO-TERRITOIRE',
crs = 4326,
simplification = '0'
} = options;

const url = createURLS3({
bucket,
pathWithinBucket,
provider,
source,
vectorfileFormat,
borders,
filter_by,
year,
value,
crs,
simplification
});

try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch vector file. Status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error('Error fetching vector file:', error);
throw error;
}
}
Insert cell
download_from_cartiflette({value: "75", borders: "COMMUNE_ARRONDISSEMENT", filter_by: "DEPARTEMENT"})
Insert cell
liste_departements = [
...Array(95).keys()
].map(i => (i + 1).toString().padStart(2, '0')).concat(["2A","2B", "971", "972", "973", "974"]);
Insert cell
Insert cell
/**
* Merge all features of an array of geojson and return a single geojson
*/
function to_single_geojson(geojsons) {
// extract features of each geojson
const all_features = geojsons.map((d) => d.features).flat();
// keep first geojson as model and replace it's features with all_features
return { ...geojsons[0], features: all_features };
}
Insert cell
Insert cell
createURLS3(
{"value": "75", "borders": "COMMUNE_ARRONDISSEMENT", "vectorfileFormat": "geojson", "year": 2022,
crs: 4326, filter_by: "DEPARTEMENT", source: "EXPRESS-COG-CARTO-TERRITOIRE"
})
Insert cell
get_multiple_departement(
["01", "02"],
"COMMUNE", "geojson", 2022
)
Insert cell
function get_multiple_departement(
list_departements,
level_arrondissement, vectorfileFormat, year
){
let data_multiple_departement = Promise.all(
list_departements.map(
departement => createURLS3(
{"value": departement, "borders": level_arrondissement, "vectorfileFormat": vectorfileFormat, "year": year,
crs: 4326, filter_by: "DEPARTEMENT", source: "EXPRESS-COG-CARTO-TERRITOIRE"
})
)
.map(url => d3.json(url))
);
return data_multiple_departement
}
Insert cell
d3.json(
create_url_departement("31", "COMMUNE_ARRONDISSEMENT", "topojson", 2022)
)
Insert cell
function create_url_departement(departement, selectedlevel, format, year, simplification = 0){
const filter_by = "DEPARTEMENT";

let url = createURLS3(
{"value": departement, "borders": selectedlevel, "vectorfileFormat": format, "year": year,
crs: 4326, filter_by: filter_by, simplification: simplification, source: "EXPRESS-COG-CARTO-TERRITOIRE"}
) ;
return url
}
Insert cell
create_url_france("BASSIN_VIE", "topojson", 2022)
Insert cell
function create_url_france(selectedlevel, format, year, drom_rapproches = false, simplification = 0){
const filter_by = drom_rapproches ? "FRANCE_ENTIERE_DROM_RAPPROCHES" : "FRANCE_ENTIERE";

let url=createURLS3({
value: "France", borders: selectedlevel, vectorfileFormat: format, year: year,
crs: 4326, filter_by: filter_by, simplification: simplification,
source: "EXPRESS-COG-CARTO-TERRITOIRE"
})
return url
}
Insert cell
get_france("AIRE_ATTRACTION_VILLES", "topojson", 2022, false)
Insert cell
function get_france(
selectedlevel, format, year, drom_rapproches = false, simplification = 0
){
const filter_by = (drom_rapproches) ? "FRANCE_ENTIERE_DROM_RAPPROCHES" : "FRANCE_ENTIERE";
let obj = download_from_cartiflette({
value: "France", borders: selectedlevel, vectorfileFormat: format, year: year,
crs: 4326, filter_by: filter_by, simplification: simplification,
source: "EXPRESS-COG-CARTO-TERRITOIRE"})
return obj
}
Insert cell
get_departement("75", "COMMUNE_ARRONDISSEMENT", "geojson", 2022)
Insert cell
function get_departement(
departement, selectedlevel, format, year, simplification = 0
){
let obj = download_from_cartiflette({
"value": departement, "borders": selectedlevel, "vectorfileFormat": format, "year": year,
crs: 4326, filter_by: "DEPARTEMENT", simplification: simplification,
source: "EXPRESS-COG-CARTO-TERRITOIRE"})
return obj
}
Insert cell
function download_button(url) {
return `
<form method="get" action="${url}" target="_blank" rel="noopener noreferrer">
<button class="btn" type="submit">
<i class="fa fa-download"></i> Download !
</button>
</form>
`;
}
Insert cell
Insert cell
import { style as faStyle, fa, fab, fas } from "@airbornemint/fontawesome"
Insert cell
faStyle({regular: true, solid: true, brands: true})
Insert cell
create_url_departement("01", "COMMUNE_ARRONDISSEMENT", "topojson", 2022)
Insert cell
button = {
let api_stars=`https://ghbtns.com/github-btn.html?user=inseefrlab&repo=cartiflette&type=star&count=true&size=large`
let button_like = html`<iframe class="githubStarButton_10T9" src=${api_stars} title="GitHub Stars" width="160" height="30" style="border: none;"></iframe>`
return button_like
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more