Private
Public Access
1
0

Merge branch 'feature/coursetokml' into develop

This commit is contained in:
Sander Roosendaal
2018-06-06 14:23:53 +02:00
6 changed files with 133 additions and 0 deletions
+52
View File
@@ -14,6 +14,16 @@ import geocoder
from matplotlib import path from matplotlib import path
import xml.etree.ElementTree as et import xml.etree.ElementTree as et
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
from xml.dom import minidom
def prettify(elem):
"""Return a pretty-printed XML string for the Element.
"""
rough_string = tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
import pandas as pd import pandas as pd
import numpy as np import numpy as np
from timezonefinder import TimezoneFinder from timezonefinder import TimezoneFinder
@@ -108,6 +118,48 @@ def get_polygons(polygonpms):
return polygons return polygons
def coursetokml(course):
top = Element('kml')
document = SubElement(top,'Document')
name = SubElement(document, 'name')
name.text = 'Courses.kml'
folder = SubElement(document,'Folder')
foldername = SubElement(folder,'name')
foldername.text = 'Courses'
folder2 = SubElement(folder,'Folder')
coursename = SubElement(folder2,'name')
coursename.text = course.name
open = SubElement(folder2,'open')
open.text = '1'
polygons = GeoPolygon.objects.filter(course=course).order_by("order_in_course")
polygonsxml = []
for polygon in polygons:
placemark = SubElement(folder2,'Placemark')
polygonname = SubElement(placemark,'name')
polygonname.text = polygon.name
p = SubElement(placemark,'Polygon')
tessellate = SubElement(p,'tessellate')
tessellate.text = '1'
boundary = SubElement(p,'outerBoundaryIs')
ring = SubElement(boundary,'LinearRing')
coordinates = SubElement(ring,'coordinates')
coordinates.text = ''
points = GeoPoint.objects.filter(polygon=polygon).order_by("order_in_poly")
for point in points:
coordinates.text += '{lon},{lat},0 '.format(
lat = point.latitude,
lon = point.longitude,
)
coordinates.text += '{lon},{lat},0'.format(
lat = points[0].latitude,
lon = points[0].longitude,
)
return prettify(top)
def kmltocourse(f): def kmltocourse(f):
doc = et.parse(f) doc = et.parse(f)
courses = doc.findall('.//opengis:Folder[opengis:Placemark]',ns) courses = doc.findall('.//opengis:Folder[opengis:Placemark]',ns)
+21
View File
@@ -749,6 +749,27 @@ def handle_sendemail_unrecognizedowner(useremail, userfirstname,
return 1 return 1
@app.task
def handle_sendemailkml(first_name, last_name, email, kmlfile,**kwargs):
# send email with attachment
fullemail = first_name + " " + last_name + " " + "<" + email + ">"
subject = "File from Rowsandall.com"
d = {'first_name':first_name,
'siteurl':siteurl,
}
from_email = 'Rowsandall <info@rowsandall.com>'
res = send_template_email(from_email,[fullemail],
subject,'kmlemail.html',d,
attach_file=kmlfile,**kwargs)
os.remove(kmlfile)
return 1
# Send email with TCX attachment # Send email with TCX attachment
@app.task @app.task
+3
View File
@@ -26,6 +26,9 @@
<div class="grid_2"> <div class="grid_2">
<a class="button small gray" href="/rowers/list-courses">Courses</a> <a class="button small gray" href="/rowers/list-courses">Courses</a>
</div> </div>
<div class="grid_2">
<a class="button small gray" href="/rowers/courses/{{ course.id }}/emailkml">Export to KML</a>
</div>
</div> </div>
<div class="grid_12 alpha"> <div class="grid_12 alpha">
+13
View File
@@ -0,0 +1,13 @@
{% extends "emailbase.html" %}
{% block body %}
<p>Dear <strong>{{ first_name }}</strong>,</p>
<p>
Please find attached the requested KML course file. You can open this file in Google Earth
</p>
<p>
Best Regards, the Rowsandall Team
</p>
{% endblock %}
+1
View File
@@ -518,6 +518,7 @@ urlpatterns = [
url(r'^courses/(?P<id>\d+)/edit$',views.course_edit_view, url(r'^courses/(?P<id>\d+)/edit$',views.course_edit_view,
name='course_edit_view'), name='course_edit_view'),
url(r'^courses/(?P<id>\d+)/delete$',views.course_delete_view), url(r'^courses/(?P<id>\d+)/delete$',views.course_delete_view),
url(r'^courses/(?P<id>\d+)/emailkml$',views.course_kmlemail_view),
url(r'^courses/(?P<id>\d+)/replace$',views.course_replace_view), url(r'^courses/(?P<id>\d+)/replace$',views.course_replace_view),
url(r'^courses/(?P<id>\d+)$',views.course_view), url(r'^courses/(?P<id>\d+)$',views.course_view),
url(r'^courses/(?P<id>\d+)/map$',views.course_map_view), url(r'^courses/(?P<id>\d+)/map$',views.course_map_view),
+43
View File
@@ -132,6 +132,7 @@ from rowers.tasks import handle_makeplot,handle_otwsetpower,handle_sendemailtcx,
from rowers.tasks import ( from rowers.tasks import (
handle_sendemail_unrecognized,handle_sendemailnewcomment, handle_sendemail_unrecognized,handle_sendemailnewcomment,
handle_sendemailsummary, handle_sendemailsummary,
handle_sendemailkml,
handle_sendemailnewresponse, handle_updatedps, handle_sendemailnewresponse, handle_updatedps,
handle_updatecp,long_test_task,long_test_task2, handle_updatecp,long_test_task,long_test_task2,
handle_zip_file,handle_getagegrouprecords, handle_zip_file,handle_getagegrouprecords,
@@ -1847,6 +1848,48 @@ def workout_tcxemail_view(request,id=0):
return response return response
@login_required()
def course_kmlemail_view(request,id=0):
r = getrower(request.user)
if r.emailbounced:
message = "Please check your email address first. Email to this address bounced."
messages.error(request,message)
return HttpResponseRedirect(
reverse(course_view,
kwargs = {
'id':str(id),
})
)
course = GeoCourse.objects.get(id=id)
kmlstring = courses.coursetokml(course)
kmlfilename = 'course_{id}.kml'.format(id=id)
with open(kmlfilename,'w') as fop:
fop.write(kmlstring)
res = myqueue(queuehigh,handle_sendemailkml,
r.user.first_name,
r.user.last_name,
r.user.email,kmlfilename,
emailbounced = r.emailbounced
)
successmessage = "The KML file was sent to you per email"
messages.info(request,successmessage)
url = reverse(course_view,
kwargs = {
'id':str(id),
})
response = HttpResponseRedirect(url)
return response
# Export workout to GPX and send to user's email address # Export workout to GPX and send to user's email address
@login_required() @login_required()
def workout_gpxemail_view(request,id=0): def workout_gpxemail_view(request,id=0):