Skip to content

Instantly share code, notes, and snippets.

@rossini-t
Last active December 1, 2017 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rossini-t/2f9fac7f05c844b5c664ad3db55ad422 to your computer and use it in GitHub Desktop.
Save rossini-t/2f9fac7f05c844b5c664ad3db55ad422 to your computer and use it in GitHub Desktop.
A powershell script to fetch french cadastral data from https source
<#
.SYNOPSIS
Télecharge les fichiers édigéos par commune à partir d'une liste au format csv.
Necessite PowerShell 3.0
.DESCRIPTION
La liste est séparée par des ; et contient une ligne d'en tête Departement;CodeCommune.
Le codecommune est le code insee de la commune.
Le second argument de la commande est optionnel et désigne le répertoire de destination.
S'il n'est pas fourni, le repertoire courant est utilisé.
.EXAMPLE
Fetch-Edigeo C:\Path\To\list.csv C:\Path\To\Download\Dir
#>
$file = $args[0];
$f;
try
{
$f = Import-CSV $file -Delimiter ";";
}
catch
{
echo ("Impossible de charger le fichier csv " + $file);
exit 1;
}
$dest = Split-Path $MyInvocation.MyCommand.Path;
if ($args.Length -eq 2)
{
$dest = $args[1];
if (-not (Test-Path $dest))
{
echo ("Repertoire de destination " + $dest + " introuvable.");
exit 1;
}
}
if (-not ($dest.EndsWith("\")))
{
$dest = $dest + "\";
}
$base = "https://cadastre.data.gouv.fr/data/dgfip-pci-vecteur/latest/edigeo/feuilles/";
$client = new-object System.Net.WebClient;
foreach($v in $f)
{
$target = $base + $v.Departement + "/" + $v.Departement + $v.CodeCommune + "/";
foreach ($x in (Invoke-WebRequest $target).Links )
{
if ($x.href -ne "../")
{
$url = $target + $x.href;
$file = $dest + $x.href;
try
{
$client.DownloadFile($url, $file);
echo ($x.href + ": succès");
}
catch
{
echo ($x.href + ": echoué");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment