From 6d215e23057d0f0cf732665e3f05605647bbb1ac Mon Sep 17 00:00:00 2001 From: TheDevKiller Date: Sun, 11 Nov 2018 00:32:33 +0100 Subject: [PATCH] Converted DMS (degrees, minutes, seconds) to DD (decimal degrees) --- fetch.py | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/fetch.py b/fetch.py index a4afed9..f613312 100755 --- a/fetch.py +++ b/fetch.py @@ -10,6 +10,10 @@ import requests import re +# Variables +api = "https://master.apis.dev.openstreetmap.org/" # Testing, it should be replaced by https://api.openstreetmap.org/ when the program is finished + + def runfetch(url): session=requests.Session() req1=session.get(url) # Get the source code @@ -23,30 +27,52 @@ def runfetch(url): #dicformat:{id,commune,site,departement,zone,ministere,aerozone} return diclist +def dms2dd(dms): + coordslst = re.search("(\d{1,3})° (\d{2})' (\d{2},?\d{0,2})(?:\"|”|\'\')", dms).groups() + dd = float(coordslst[0].replace(",", ".")) + float(coordslst[1].replace(",", "."))/60 + float(coordslst[2].replace(",", "."))/3600 + return dd if __name__ == '__main__': - diclist=runfetch('https://www.legifrance.gouv.fr/eli/arrete/2018/10/12/PRMD1824595A/jo/texte') # Fetch all the data from a french article - url='https://www.legifrance.gouv.fr/eli/arrete/2018/10/12/PRMD1824595A/jo/texte' + + diclist = runfetch('https://www.legifrance.gouv.fr/eli/arrete/2018/10/12/PRMD1824595A/jo/texte') # Fetch all the data from a french article + url = 'https://www.legifrance.gouv.fr/eli/arrete/2018/10/12/PRMD1824595A/jo/texte' for index, zone in enumerate(diclist): for element in zone: diclist[index][element] = re.sub("", "\n", diclist[index][element]) # Replace
,
and
by \n + # Make a the area for area in diclist: # Case insensitive area["zone"] = area["zone"].lower() + # Multiple polygons if "polygones" in area["zone"]: lst = [] - tmp = re.compile("zone . : .*").split(area["zone"]) # List of polygons + tmp = re.compile(".*zone.*").split(area["zone"]) # List of polygons + del tmp[0] for polygon in tmp: - lst.append(re.findall(".{0,35} ?:? ?.? ?: ?(\d{1,3}° \d{2}' \d{2},?\d{0,2}(?:\"|”) .) ?\/ ?(\d{1,3}° \d{2}' \d{2},?\d{0,2}(?:\"|”) .)", area["zone"])) - area["zone"] = lst + lst.append(re.findall(".{0,35} ?:? ?.? ?: ?(\d{1,3}° \d{2}' \d{2},?\d{0,2}(?:\"|”) .) ?\/ ?(\d{1,3}° \d{2}' \d{2},?\d{0,2}(?:\"|”) .)", polygon)) + area["zone"] = [] + for index, polygon in enumerate(lst): + area["zone"].append([]) + for index2, coords in enumerate(polygon): + area["zone"][index].append([]) + for index3, point in enumerate(coords): + area["zone"][index][index2].append(dms2dd(point)) + # area["zone"][index][index2][index3] = dms2dd(point) del lst del tmp # Polygons elif "polygone" in area["zone"]: lst = [] lst.append(re.findall(".{0,35} ?:? ?.? ?: ?(\d{1,3}° \d{2}' \d{2},?\d{0,2}(?:\"|”|\'\') ?.) ?\/ ?(\d{1,3}° \d{2}' \d{2},?\d{0,2}(?:\"|”|\'\') ?.)", area["zone"])) - area["zone"] = lst + area["zone"] = [] + for index, polygon in enumerate(lst): + area["zone"].append([]) + for index2, coords in enumerate(polygon): + area["zone"][index].append([]) + for index3, point in enumerate(coords): + area["zone"][index][index2].append([]) + area["zone"][index][index2][index3] = dms2dd(point) del lst # Circles elif "cercle" in area["zone"]: @@ -56,7 +82,9 @@ if __name__ == '__main__': del lst # Atolls elif area["zone"].strip() == "atolls et eaux territoriales incluses": - area["zone"] = [("cercle", "35KM", "138° 54' 8,01\"", "21° 49' 44,64\""), ("cercle", "27KM", "138° 44' 33.01\"", "22° 14' 7.38\"")] + area["zone"] = [("cercle", "35KM", "138.9022", "21.82917"), ("cercle", "27KM", "138.7425", "22.23528A")] + + # Print the dict (keep this code in the end of the file) print("===== Dict =====")