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"?>
|
2023-05-03 00:27:38 +03:00
|
|
|
<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="nmns.place"></gpx>'''
|
2023-05-01 21:58:23 +03:00
|
|
|
kml_root = '''<?xml version="1.0" encoding="UTF-8"?>
|
2023-05-03 00:27:38 +03:00
|
|
|
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" creator="nmns.place"><Document><Folder></Folder></Document></kml>'''
|
2023-05-01 21:58:23 +03:00
|
|
|
|
|
|
|
|
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 )
|
2023-05-22 11:25:46 +03:00
|
|
|
with open(collection[category]["path"] + '.kml', 'w') as xml_file:
|
2023-05-01 21:58:23 +03:00
|
|
|
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 )
|
2023-05-22 11:25:46 +03:00
|
|
|
with open( collection[category]["path"] + '.gpx', 'w') as xml_file:
|
2023-05-01 21:58:23 +03:00
|
|
|
root.writexml(xml_file, indent="", addindent=" ", newl='\n')
|
|
|
|
|
|
|
|
|
|
def geodata(generator):
|
2023-05-22 11:25:46 +03:00
|
|
|
output = generator.settings.get('OUTPUT_PATH')
|
|
|
|
|
filepath = generator.settings.get('CATEGORY_URL')
|
2023-05-01 21:58:23 +03:00
|
|
|
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,
|
2023-05-22 11:25:46 +03:00
|
|
|
"path" : output + '/' + filepath.replace('{slug}', category.slug),
|
2023-05-01 21:58:23 +03:00
|
|
|
"points" : locations
|
|
|
|
|
}})
|
|
|
|
|
|
|
|
|
|
def register():
|
|
|
|
|
signals.article_generator_finalized.connect(geodata)
|
|
|
|
|
signals.finalized.connect(make_files)
|