nfc-web/web/plugins/geodata/geodata.py

73 lines
3.3 KiB
Python
Raw Normal View History

2023-05-01 21:58:23 +03:00
from pelican import signals
from xml.dom import minidom
collection = {}
gpx_root = '''<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" version="1.1" creator="nfc.flux.vision"></gpx>'''
kml_root = '''<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2"><Document><Folder></Folder></Document></kml>'''
def make_files(void):
makeGPX()
makeKML()
def makeKML():
if len(collection) > 0:
for category in collection:
root = minidom.parseString(kml_root)
folder = root.createElement('name')
foldername = root.createTextNode( collection[category]["name"] )
folder.appendChild( foldername )
root.childNodes[0].childNodes[0].childNodes[0].appendChild( folder )
for place in collection[category]["points"]:
placemark = root.createElement('Placemark')
name = root.createElement('name')
title = root.createTextNode( place['name'] )
name.appendChild( title )
placemark.appendChild( name )
point = root.createElement('Point')
coordinates = root.createElement('coordinates')
lat, lon = place['coordinates'].replace(' ', '').split(',')
latlon = root.createTextNode( lon + ',' + lat)
coordinates.appendChild( latlon )
point.appendChild( coordinates )
placemark.appendChild( point )
root.childNodes[0].childNodes[0].childNodes[0].appendChild( placemark )
with open('output/projects/' + category + '.kml', 'w') as xml_file:
root.writexml(xml_file, indent="", addindent=" ", newl='\n')
def makeGPX():
if len(collection) > 0:
for category in collection:
root = minidom.parseString(kml_root)
for place in collection[category]["points"]:
lat, lon = place['coordinates'].replace(' ', '').split(',')
wpt = root.createElement('wpt')
wpt.setAttribute('lat', lat)
wpt.setAttribute('lon', lon)
name = root.createElement('name')
title = root.createTextNode( place['name'] )
name.appendChild( title )
wpt.appendChild( name )
root.childNodes[0].appendChild( wpt )
with open('output/projects/' + category + '.gpx', 'w') as xml_file:
root.writexml(xml_file, indent="", addindent=" ", newl='\n')
def geodata(generator):
for category, articles in generator.categories:
locations = []
for article in articles:
if "location" in article.metadata:
locations.append({
"name": article.title,
"coordinates": article.metadata["location"]
})
collection.update({
category.slug : {
"name" : category.name,
"points" : locations
}})
def register():
signals.article_generator_finalized.connect(geodata)
signals.finalized.connect(make_files)