added csv2kml.py
This commit is contained in:
parent
33e912c2de
commit
39c2ed076f
112
indianrails/trains/csv2kml.py
Normal file
112
indianrails/trains/csv2kml.py
Normal file
|
@ -0,0 +1,112 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
import csv
|
||||
import urllib
|
||||
import urllib2
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
import sqlite3
|
||||
try:
|
||||
import xml.etree.ElementTree as ET
|
||||
except:
|
||||
import elementtree.ElementTree as ET
|
||||
|
||||
|
||||
def geo_lookup(address):
|
||||
path = os.path.expanduser('~/.ox')
|
||||
if not os.path.exists(path):
|
||||
os.makedirs(path)
|
||||
path = os.path.join(path, 'geo.sqlite')
|
||||
|
||||
conn = sqlite3.connect(path, timeout=10)
|
||||
conn.text_factory = str
|
||||
|
||||
c = conn.cursor()
|
||||
c.execute('''CREATE TABLE IF NOT EXISTS geo (query varchar(1024) unique, result text)''')
|
||||
conn.commit()
|
||||
|
||||
c.execute('SELECT result FROM geo WHERE query = ?', (address, ))
|
||||
for row in c:
|
||||
return json.loads(row[0])
|
||||
|
||||
url = 'http://maps.google.com/maps/geo?q=%s&output=json&sensor=false'%urllib.quote(address.encode('utf8'))
|
||||
|
||||
#print url
|
||||
u = urllib2.urlopen(url)
|
||||
data = json.load(u)
|
||||
u.close()
|
||||
if data[u'Status'][u'code'] == 620:
|
||||
print "to fast"
|
||||
print data
|
||||
c.execute(u"INSERT OR REPLACE INTO geo VALUES (?, ?)", (address, json.dumps(data)))
|
||||
conn.commit()
|
||||
c.close()
|
||||
conn.close()
|
||||
time.sleep(0.3)
|
||||
return data
|
||||
|
||||
def get_coordinates(address):
|
||||
return geo_lookup(address).get("Placemark", [{}])[0].get("Point", {}).get("coordinates")
|
||||
|
||||
def save_kml(addresses, filename):
|
||||
kml = ET.Element('kml')
|
||||
kml.attrib['xmlns'] = 'http://www.opengis.net/kml/2.2'
|
||||
d = ET.SubElement(kml, "Document")
|
||||
name = ET.SubElement(d, "name")
|
||||
name.text = os.path.splitext(filename)[0]
|
||||
for data in addresses:
|
||||
address = '' #FIXME FORMAT csv data here
|
||||
info = '' #FIXME FORMAT data to be display in kml
|
||||
c = get_coordinates(address)
|
||||
if c:
|
||||
place = ET.SubElement(d, "Placemark")
|
||||
name = ET.SubElement(place, "name")
|
||||
name.text = person
|
||||
description = ET.SubElement(place, "description")
|
||||
description.text = info
|
||||
point = ET.SubElement(place, "Point")
|
||||
coordinates = ET.SubElement(point, "coordinates")
|
||||
coordinates.text = ', '.join(map(str, c))
|
||||
else:
|
||||
print address, "failed"
|
||||
f = open(filename, "w")
|
||||
data = '<?xml version="1.0" encoding="UTF-8"?>'+ET.tostring(kml)
|
||||
f.write(data.encode('utf-8'))
|
||||
f.close()
|
||||
|
||||
def load_csv(filename):
|
||||
reader = csv.reader(open(filename, 'rb'))
|
||||
keys = []
|
||||
data = []
|
||||
for row in reader:
|
||||
if not keys:
|
||||
keys = row
|
||||
else:
|
||||
r = {}
|
||||
i = 0
|
||||
for i in range(0, len(row)):
|
||||
r[keys[i].decode('utf-8')] = row[i].decode('utf-8')
|
||||
data.append(r)
|
||||
return data
|
||||
|
||||
if __name__ == '__main__':
|
||||
from optparse import OptionParser
|
||||
import sys
|
||||
|
||||
usage = "Usage: %prog [options] csv [kml]"
|
||||
parser = OptionParser(usage=usage)
|
||||
(opts, args) = parser.parse_args()
|
||||
|
||||
if len(args) < 1:
|
||||
parser.print_help()
|
||||
sys.exit(-1)
|
||||
|
||||
csvf = args[0]
|
||||
if len(args) == 2:
|
||||
kml = args[1]
|
||||
else:
|
||||
kml = "%s.kml" % os.path.splitext(csvf)[0]
|
||||
addresses = load_csv(csvf)
|
||||
save_kml(addresses, kml)
|
||||
|
|
@ -250,7 +250,7 @@ def import_schedule():
|
|||
|
||||
def geolocate_stations():
|
||||
errors = []
|
||||
for s in Station.objects.filter(point=None)[0:10]:
|
||||
for s in Station.objects.filter(point=None)[0:1000]:
|
||||
coords = csv2kml.get_coordinates(s.address)
|
||||
if coords is not None:
|
||||
pt = Point(coords[0], coords[1])
|
||||
|
|
Loading…
Reference in New Issue
Block a user