diff options
| author | gdamms <damguillotin@gmail.com> | 2026-07-24 12:44:20 +0200 |
|---|---|---|
| committer | gdamms <damguillotin@gmail.com> | 2026-07-24 12:44:20 +0200 |
| commit | 77aed43a99a295f3202063edefcb7ce01e011958 (patch) | |
| tree | e34a042a75cdb52e2d30afc11dc82660c22f36d5 /src/rn.py | |
| download | dog-friendly-data-77aed43a99a295f3202063edefcb7ce01e011958.tar.gz dog-friendly-data-77aed43a99a295f3202063edefcb7ce01e011958.zip | |
first commit
Diffstat (limited to 'src/rn.py')
| -rw-r--r-- | src/rn.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/rn.py b/src/rn.py new file mode 100644 index 0000000..0c6589c --- /dev/null +++ b/src/rn.py @@ -0,0 +1,81 @@ +import requests +import re + + +def get_coordinates_from_url(url): + response = requests.get(url) + regex = r"\[\[-?\d+\.\d+,-?\d+\.\d+\](?:,\[-?\d+\.\d+,-?\d+\.\d+\])+\]" + matche = re.search(regex, response.text) + if not matche: + return None + coordinates = eval(matche.group(0)) + return coordinates + + +response = requests.get("https://reserves-naturelles.org/reserves-naturelles/") +regex = r"<li class=\"reserve\"[\s\S]*?</li>" +matches = re.findall(regex, response.text) + +kml_file = open("reserves_naturelles.kml", "w", encoding="utf-8") +kml_file.write("""<?xml version="1.0" encoding="UTF-8"?> +<kml xmlns="http://www.opengis.net/kml/2.2"> + <Document> + <name>RN</name> +""") + + +for i, match in enumerate(matches): + + regex = r"title=\"([^\"]+)\"" + full_name = re.search(regex, match) + if not full_name: + print(f"Full name not found in the match: {match}") + continue + full_name = full_name.group(1) + + regex = r"class=\"rsv_nom\">([^<]+)<" + name = re.search(regex, match) + if not name: + print(f"Name not found in the match: {match}") + continue + name = name.group(1) + + regex = r"href=\"([^\"]+)\"" + url = re.search(regex, match) + if not url: + print(f"URL not found in the match: {match}") + continue + url = url.group(1) + + coordinates = get_coordinates_from_url(url) + if not coordinates: + print(f"Coordinates not found for URL: {url}") + continue + + kml_file.write(f""" + <Placemark> + <name>{name}</name> + <description> + {full_name}<br/>{url} + </description> + <Polygon> + <outerBoundaryIs> + <LinearRing> + <coordinates> +""") + for coordinate in coordinates: + kml_file.write(f" {coordinate[0]},{coordinate[1]},0\n") + kml_file.write(""" + </coordinates> + </LinearRing> + </outerBoundaryIs> + </Polygon> + </Placemark> +""") + + print(f"Processed: {name} ({i+1: 3d}/{len(matches)})") + + +kml_file.write(""" + </Document> +</kml>""") |
