Private
Public Access
1
0

import courses from kml

This commit is contained in:
Sander Roosendaal
2018-02-16 14:03:18 +01:00
parent d45b674a15
commit 3f56bff2fa
3 changed files with 68 additions and 2 deletions

View File

@@ -12,6 +12,9 @@ from django.conf import settings
from utils import myqueue
from matplotlib import path
import xml.etree.ElementTree as et
ns = {'opengis': 'http://www.opengis.net/kml/2.2'}
import django_rq
queue = django_rq.get_queue('default')
@@ -44,7 +47,7 @@ def polygon_to_path(polygon):
def coordinate_in_polygon(latitude,longitude, polygon):
p = polygon_to_path(polygon)
retun p.contains_points([(latitude,longitude)])[0]
return p.contains_points([(latitude,longitude)])[0]
@@ -71,3 +74,54 @@ def time_in_polygon(df,polygon,maxmin='max'):
return time
def kmltocourse(f):
doc = et.parse(f)
polygonpms = doc.findall('.//opengis:Placemark[opengis:Polygon]',ns)
polygons = []
for pm in polygonpms:
name = pm.findall('.//opengis:name',ns)[0].text
coordinates = pm.findall('.//opengis:coordinates',ns)
if coordinates:
cc = coordinates[0].text
else:
cc = ''
pointstring = cc.split()
points = []
for s in pointstring:
coordinates = s.split(',')
points.append({
'longitude':float(coordinates[0]),
'latitude':float(coordinates[1]),
})
polygons.append({
'name':name,
'points':points
})
return polygons
def createcourse(manager,name,polygons):
c = GeoCourse(manager=manager,name=name)
c.save()
i = 0
for p in polygons:
pp = GeoPolygon(course=c,order_in_course=i,name=p['name'])
pp.save()
j = 0
for point in p['points']:
obj = GeoPoint(
latitude = point['latitude'],
longitude = point['longitude'],
polygon = pp,
order_in_poly = j
)
obj.save()
j += 1
i += 1