Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 111 lines (87 sloc) 3.49 KB
#!/usr/bin/env python
from urlparse import urljoin
import json
from django.contrib.gis.gdal import DataSource
from django.contrib.gis.geos import GeometryCollection
import slumber
#SITE_URL = raw_input("Enter URL of LocalWiki instance: ").strip()
#API_KEY = raw_input("Enter API key: ").strip()
#USERNAME = raw_input("Enter API username: ").strip()
#KML_PATH = raw_input("Enter path to KML file: ").strip()
API_KEY = '6f134911a70c953a543b6e4af50024aaadc72a49'
USERNAME = 'philip'
KML_PATH = '/home/philip/Downloads/ghost_signs.kml'
api = slumber.API(urljoin(SITE_URL, '/api/'), format='json')
ds = DataSource(KML_PATH)
def clean_pagename(name):
# Pagenames can't contain a slash with spaces surrounding it.
name = '/'.join([part.strip() for part in name.split('/')])
return name
def get_or_create_tag():
tag_name = raw_input(
"""What tag name would you like to associate with this layer?
(or just press Enter for no tag):\n""").strip()
if tag_name:
# Find or create the tag
if not api.tag.get(name__iexact=tag_name)['objects']:{'name': tag_name},
api_key=API_KEY, username=USERNAME)
# get tag uri
return api.tag.get(name__iexact=tag_name)['objects'][0]['resource_uri']
return None
def get_or_create_page(pagename):
# If the page doesn't exist, create it
if not['objects']:
result ={'name': pagename, 'content': content},
api_key=API_KEY, username=USERNAME)
print ('..created page %s with description %s' %
(pagename, content))
if result:
return result['resource_uri']
# Get page URI
def add_tag_to_page(tag, pagename, page_uri):
# Add page tag
page_tags = api.page_tags.get(page__name__iexact=pagename)['objects']
if page_tags:
page_tags = page_tags[0]
if tag not in page_tags['tags']:
# Update existing page_tags
api_key=API_KEY, username=USERNAME)
# Add a new page_tags
page_tags = {'page': page_uri, 'tags': [tag]}, api_key=API_KEY, username=USERNAME)
def update_map(pagename, page_uri):
map = {
'page': page_uri,
'geom': json.loads(GeometryCollection(feature.geom.geos).geojson),
# Does the page already have a map?
map_data =['objects']
if map_data:
map = map_data[0]
map['geom'] = json.loads(
# Update the map with the new geometry, api_key=API_KEY, username=USERNAME)
# Create new map from the feature's geometry., api_key=API_KEY, username=USERNAME)
for layer in ds:
print 'Importing layer: %s' %
tag = get_or_create_tag()
for feature in layer:
pagename = clean_pagename(feature.get('Name'))
content = feature.get('Description') or 'Describe %s here.' % pagename
page_uri = get_or_create_page(pagename)
add_tag_to_page(tag, pagename, page_uri)
update_map(pagename, page_uri)
print '..imported geometry for %s' % pagename