Permalink
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()
SITE_URL = 'http://127.0.0.1:8000'
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']:
api.tag.post({'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 api.page.get(name__iexact=pagename)['objects']:
try:
result = api.page.post({'name': pagename, 'content': content},
api_key=API_KEY, username=USERNAME)
except:
pass
print ('..created page %s with description %s' %
(pagename, content))
if result:
return result['resource_uri']
# Get page URI
return api.page.get(
name__iexact=pagename)['objects'][0]['resource_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
page_tags['tags'].append(tag)
api.page_tags(pagename).put(page_tags,
api_key=API_KEY, username=USERNAME)
else:
# Add a new page_tags
page_tags = {'page': page_uri, 'tags': [tag]}
api.page_tags.post(page_tags, 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 = api.map.get(page__name__iexact=pagename)['objects']
if map_data:
map = map_data[0]
map['geom'] = json.loads(
GeometryCollection(feature.geom.geos).geojson)
# Update the map with the new geometry
api.map(pagename).put(map, api_key=API_KEY, username=USERNAME)
else:
# Create new map from the feature's geometry.
api.map.post(map, api_key=API_KEY, username=USERNAME)
for layer in ds:
print 'Importing layer: %s' % layer.name
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