aboutsummaryrefslogtreecommitdiff
path: root/src/rn.py
blob: 0c6589cadefea7e7fe4118dd8ed353bff52a83cd (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
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>""")