diff --git a/core/views/caves.py b/core/views/caves.py index b5a13f4..82e9d64 100644 --- a/core/views/caves.py +++ b/core/views/caves.py @@ -1,10 +1,15 @@ import os import re import subprocess +import tempfile +import zipfile +import urllib +from bs4 import BeautifulSoup + from pathlib import Path from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist -from django.http import HttpResponse, HttpResponseNotFound, HttpResponseRedirect +from django.http import HttpResponse, HttpResponseNotFound, HttpResponseRedirect, FileResponse from django.shortcuts import render from django.urls import NoReverseMatch, reverse @@ -17,6 +22,8 @@ from troggle.core.views import expo from troggle.settings import CAVEDESCRIPTIONS, ENTRANCEDESCRIPTIONS from troggle.parsers.caves import read_cave, read_entrance +from django.template import loader +from django.utils.safestring import mark_safe from .auth import login_required_if_public @@ -628,3 +635,45 @@ def expo_kml(request): }, content_type = "application/vnd.google-earth.kml+xml" ) + +def expo_kmz(request): + notablecaves = set(getnotablecaves()) + #Zip file written to a file, to save this function using too much memory + with tempfile.TemporaryDirectory() as tmpdirname: + zippath = os.path.join(tmpdirname, 'expo.kmz') + with zipfile.ZipFile(zippath, 'w', compression=zipfile.ZIP_DEFLATED) as myzip: + entrances = [] + for e in Entrance.objects.all(): + html = loader.get_template("entrance_html.kml").render({"entrance": e}, request) + soup=BeautifulSoup(html) + for img in soup.find_all("img"): + #src_orig = img['src'] + src = urllib.parse.urljoin(e.cavelist()[0].url.rpartition("/")[0] + "/", img['src']) + img['src'] = src + p = os.path.join(settings.EXPOWEB, src) + #print(e.cavelist()[0].url, e.cavelist()[0].url.rpartition("/")[0] + "/", src_orig, p) + if os.path.isfile(p): + myzip.write(p, src) + for a in soup.find_all("a"): + try: + ao = a['href'] + aa = urllib.parse.urljoin(e.cavelist()[0].url.rpartition("/")[0] + "/", ao) + a['href'] = urllib.parse.urljoin("https://expo.survex.com/", aa) + print(e.cavelist()[0].url.rpartition("/")[0] + "/", ao, a['href']) + except: + pass + html = mark_safe(soup.prettify("utf-8").decode("utf-8")) + + size = {True: "large", False:"small"}[bool(set(e.cavelist()) & notablecaves)] + + entrances.append(loader.get_template("entrance.kml").render({"entrance": e, "html": html, "size": size}, request)) + + s = loader.get_template("expo.kml").render({"entrances": entrances}, request) + myzip.writestr("expo.kml", s) + for f in os.listdir(settings.KMZ_ICONS_PATH): + p = os.path.join(settings.KMZ_ICONS_PATH, f) + if os.path.isfile(p): + myzip.write(p, os.path.join("icons", f)) + return FileResponse(open(zippath, 'rb'), content_type="application/vnd.google-earth.kmz .kmz") + + diff --git a/kmz_icons/camp.png b/kmz_icons/camp.png new file mode 100644 index 0000000..57dd2c6 Binary files /dev/null and b/kmz_icons/camp.png differ diff --git a/kmz_icons/large_cave.png b/kmz_icons/large_cave.png new file mode 100644 index 0000000..06b7d2c Binary files /dev/null and b/kmz_icons/large_cave.png differ diff --git a/kmz_icons/small_cave.png b/kmz_icons/small_cave.png new file mode 100644 index 0000000..2d5a8ed Binary files /dev/null and b/kmz_icons/small_cave.png differ diff --git a/templates/entrance.kml b/templates/entrance.kml new file mode 100644 index 0000000..77bb014 --- /dev/null +++ b/templates/entrance.kml @@ -0,0 +1,10 @@ + {% if entrance.latlong %} + + {{ entrance.slug }} + + #{{ size }}_cave + + {{ entrance.latlong.1 }},{{ entrance.latlong.0 }},0 + + + {% endif %} diff --git a/templates/entrance_html.kml b/templates/entrance_html.kml new file mode 100644 index 0000000..b45b11b --- /dev/null +++ b/templates/entrance_html.kml @@ -0,0 +1,55 @@ +
+ Cave: {{entrance.cavelist.0.name }} + {% if entrance.marking %} +
Marking
{{ entrance.marking_val|safe }}
+ {% endif %} + {% if entrance.marking_comment %} +
Marking Comment
{{ entrance.marking_comment|safe }}
+ {% endif %} + {% if entrance.findability %} +
Findability
{{ entrance.findability_val|safe }}
+ {% endif %} + {% if entrance.findability_comment %} +
Findability Comment
{{ entrance.findability_comment|safe }}
+ {% endif %} + {% if entrance.location_description %} +
Location
{{ entrance.location_description|safe }}
+ {% endif %} + {% if entrance.approach %} +
Approach
{{ entrance.approach|safe }}
+ {% endif %} + {% if entrance.map_description %} +
Map
{{ entrance.map_description|safe }}
+ {% endif %} + {% if entrance.underground_description %} +
Underground
{{ entrance.underground_description|safe }}
+ {% endif %} + {% if entrance.photo %} +
Photo
{{ entrance.photo|safe }}
+ {% endif %} + {% if entrance.entrance_description %} +
Description
{{ entrance.entrance_description|safe }}
+ {% endif %} + {% if entrance.explorers %} +
Explorers
{{ entrance.explorers|safe }}
+ {% endif %} + {% if entrance.northing %} +
Location
?BMN? Northing: {{ entrance.northing|safe }}, Easting: {{ entrance.easting|safe }}, {{ entrance.alt|safe }}m
+ {% endif %} + {% if entrance.tag_station %} +
Tag Location
{{ entrance.tag_station }} {{ entrance.tag.latlong.0|floatformat:5 }}N {{ entrance.tag.latlong.1|floatformat:5 }}E - UTM33 {{ entrance.tag.y|floatformat:0 }}, {{ entrance.tag.x|floatformat:0 }}, {{ entrance.tag.z|floatformat:0 }}m
+ {% endif %} + {% if entrance.bearings %} +
Bearings
{{ entrance.bearings|safe }}
+ {% endif %} + {% if entrance.exact_station %} +
Exact Station
{{ entrance.exact_station|safe }} {{ entrance.exact_location.latlong.0|floatformat:5 }}N {{ entrance.exact_location.latlong.1|floatformat:5 }}E - UTM33 {{ entrance.exact_location.y|floatformat:0 }}, {{ entrance.exact_location.x|floatformat:0 }}, {{ entrance.exact_location.z|floatformat:0 }}m
+ {% endif %} + {% if entrance.other_station %} +
Other Station
{{ entrance.other_station|safe }} + {% if entrance.other_description %} + - {{ entrance.other_description|safe }} + {% endif %} {{ entrance.other_location.latlong.0|floatformat:5 }}N {{ entrance.other_location.latlong.1|floatformat:5 }}E - UTM33 {{ entrance.other_location.y|floatformat:0 }}, {{ entrance.other_location.x|floatformat:0 }}, {{ entrance.other_location.z|floatformat:0 }}m +
+ {% endif %} +
diff --git a/templates/expo.kml b/templates/expo.kml index 9179ee9..3861276 100644 --- a/templates/expo.kml +++ b/templates/expo.kml @@ -2,104 +2,75 @@ Expo.KML - - - + normal - #default0 + #small_cave_ highlight - #hl + #small_cave_hl + + + + + + + + normal + #large_cave_ + + + highlight + #large_cave_hl Expo.KML {% for entrance in entrances %} - {% if entrance.latlong %} - - {{ entrance.slug }} - - {% if entrance.marking %} -
Marking
{{ entrance.marking_val|safe }}
- {% endif %} - {% if entrance.marking_comment %} -
Marking Comment
{{ entrance.marking_comment|safe }}
- {% endif %} - {% if entrance.findability %} -
Findability
{{ entrance.findability_val|safe }}
- {% endif %} - {% if entrance.findability_comment %} -
Findability Comment
{{ entrance.findability_comment|safe }}
- {% endif %} - {% if entrance.location_description %} -
Location
{{ entrance.location_description|safe }}
- {% endif %} - {% if entrance.approach %} -
Approach
{{ entrance.approach|safe }}
- {% endif %} - {% if entrance.map_description %} -
Map
{{ entrance.map_description|safe }}
- {% endif %} - {% if entrance.underground_description %} -
Underground
{{ entrance.underground_description|safe }}
- {% endif %} - {% if entrance.photo %} -
Photo
{{ entrance.photo|safe }}
- {% endif %} - {% if entrance.entrance_description %} -
Description
{{ entrance.entrance_description|safe }}
- {% endif %} - {% if entrance.explorers %} -
Explorers
{{ entrance.explorers|safe }}
- {% endif %} - {% if entrance.northing %} -
Location
?BMN? Northing: {{ entrance.northing|safe }}, Easting: {{ entrance.easting|safe }}, {{ entrance.alt|safe }}m
- {% endif %} - {% if entrance.tag_station %} -
Tag Location
{{ entrance.tag_station }} UTM33 {{ entrance.tag.y|floatformat:0 }}, {{ entrance.tag.x|floatformat:0 }}, {{ entrance.tag.z|floatformat:0 }}m
- {% endif %} - {% if entrance.bearings %} -
Bearings
{{ entrance.bearings|safe }}
- {% endif %} - {% if entrance.exact_station %} -
Exact Station
{{ entrance.exact_station|safe }} UTM33 {{ entrance.exact_location.y|floatformat:0 }}, {{ entrance.exact_location.x|floatformat:0 }}, {{ entrance.exact_location.z|floatformat:0 }}m
- {% endif %} - {% if entrance.other_station %} -
Other Station
{{ entrance.other_station|safe }} - {% if entrance.other_description %} - - {{ entrance.other_description|safe }} - {% endif %} UTM33 {{ entrance.other_location.y|floatformat:0 }}, {{ entrance.other_location.x|floatformat:0 }}, {{ entrance.other_location.z|floatformat:0 }}m -
- {% endif %} - ]]>
- #default - - {{ entrance.latlong.1 }},{{ entrance.latlong.0 }},0 - -
- {% endif %} + {{ entrance }} {% endfor %}
diff --git a/urls.py b/urls.py index 1fca988..c7fc81c 100644 --- a/urls.py +++ b/urls.py @@ -8,7 +8,7 @@ from troggle.core.views import statistics, survex from troggle.core.views.auth import expologin, expologout from troggle.core.views.caves import (cave3d, caveEntrance, caveindex, cavepage, caveQMs, edit_cave, cave_debug, - edit_entrance, get_entrances, qm, expo_kml) + edit_entrance, get_entrances, qm, expo_kml, expo_kmz) from troggle.core.views.drawings import dwgallfiles, dwgfilesingle from troggle.core.views.editor_helpers import image_selector, new_image_form from troggle.core.views.expo import (editexpopage, expofiles_redirect, @@ -232,6 +232,7 @@ trogglepatterns = [ re_path(r'^new_image_form/(?P.*)', new_image_form, name = 'new_image_form'), re_path(r'^expo.kml', expo_kml, name = 'expo.kml'), + re_path(r'^expo.kmz', expo_kmz, name = 'expo.kmz'), # Final catchall which also serves expoweb handbook pages and imagestiny re_path(r'^(.*)$', expopage, name="expopage"), # CATCHALL assumed relative to EXPOWEB