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/n2000.py | |
| download | dog-friendly-data-77aed43a99a295f3202063edefcb7ce01e011958.tar.gz dog-friendly-data-77aed43a99a295f3202063edefcb7ce01e011958.zip | |
first commit
Diffstat (limited to 'src/n2000.py')
| -rw-r--r-- | src/n2000.py | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/n2000.py b/src/n2000.py new file mode 100644 index 0000000..357f744 --- /dev/null +++ b/src/n2000.py @@ -0,0 +1,96 @@ +import re +import requests + + +reponse = requests.get("https://www.natura2000.fr/carte-natura2000") +regex = r"\"entity_id\":\"(\d+)\"" +matches = re.findall(regex, reponse.text) + + +kml_file = open("natura_2000.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>Natura 2000</name> +""") + + +for i, match in enumerate(matches): + url = f"https://www.natura2000.fr/leaflet-ajax-popup/node/{match}/pop_up_n2000_field/und?_wrapper_format=drupal_ajax" + response = requests.get(url) + + regex = r"href=\\u0022\\/site-natura\\/(.*?)\\u0022" + match = re.search(regex, response.text) + + if not match: + print(f"No match found for entity_id {match}.") + continue + + site_url = f"https://www.natura2000.fr/site-natura/{match.group(1)}" + response = requests.get(site_url) + + regex = r"<title>(.*?) \| Natura 2000</title>" + match = re.search(regex, response.text) + if not match: + print(f"No name found for {site_url}.") + continue + name = match.group(1) + + regex = r"\"type\":\"(multipolygon|polygon)\"" + match = re.search(regex, response.text) + if not match: + print(f"No geometry type found for {site_url}.") + continue + geometry_type = match.group(1) + + if geometry_type == "polygon": + regex = r"\"points\":(\[\[.*?\]\])" + elif geometry_type == "multipolygon": + regex = r"\"points\":(\[\[\[.*?\]\]\])" + else: + print(f"Unsupported geometry type {geometry_type} for {site_url}.") + continue + + match = re.search(regex, response.text) + if not match: + print(f"No coordinates found for {site_url}.") + continue + polygons = eval(match.group(1)) + + if geometry_type == "polygon": + # Wrap single polygon in a list to treat it as multipolygon + polygons = [polygons] + + kml_file.write(f""" + <Placemark> + <name>{name}</name> + <description> + {name}<br/>{site_url} + </description> + <MultiGeometry> + """) + + for polygon in polygons: + kml_file.write(""" + <Polygon> + <outerBoundaryIs> + <LinearRing> + <coordinates> + """) + for point in polygon[0]: + kml_file.write(f"{point['lon']},{point['lat']},0\n") + kml_file.write(""" + </coordinates> + </LinearRing> + </outerBoundaryIs> + </Polygon> + """) + kml_file.write(f""" + </MultiGeometry> + </Placemark> + """) + + print(f"Processed: {name} ({i+1: 5d}/{len(matches)})") + +kml_file.write(""" </Document> +</kml>""") |
