blob: 357f7445fe5eab5c0564cf865be5235a6cefa123 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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>""")
|