Created
September 12, 2023 04:26
-
-
Save yusufades/637581dfd4814fc721d425a51ff3808a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bs4 import BeautifulSoup | |
scubadoctorFP = 'scuba-doctor-dive-map.kml' | |
outputFP = 'scubadoctorSubsurface.xml' | |
with open(scubadoctorFP, 'r') as kml: | |
kmlSoup = BeautifulSoup(kml.read(), features="xml") | |
kml.close() | |
soup = BeautifulSoup() | |
divesites = soup.new_tag("divesites") | |
soup.append(divesites) | |
for place in kmlSoup("Placemark"): | |
# get the description | |
descSoup = BeautifulSoup(place.find('description').find(text=True)) | |
descContents = descSoup.find('p').find_all(string=True) | |
description = '; '.join([cont.strip() for cont in descContents if "Type:" in cont]) | |
#get coordinates then reverse | |
coords = place.find('coordinates').string.split(',') | |
coords.reverse() | |
gps = ','.join(coords) | |
# create the site | |
site = soup.new_tag("site") | |
site.attrs['description'] = description | |
site.attrs['gps'] = gps | |
site.attrs['name'] = place.find('name').string | |
# create the notes | |
notes = soup.new_tag("notes") | |
notes.string = descSoup.find('a', href=True)['href'] | |
# append everything together | |
site.append(notes) | |
site.append(soup.new_tag("geo", cat='2', origin='0', value='Australia')) | |
divesites.append(site) | |
with open(outputFP, 'w') as outFile: | |
outFile.write(soup.prettify()) | |
outFile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment