Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created April 20, 2011 21:17
Show Gist options
  • Save tmcw/932879 to your computer and use it in GitHub Desktop.
Save tmcw/932879 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from osgeo import ogr
import sqlite3, sys, time
def parse_pts(f, lyr):
uuid = 0
con = sqlite3.connect(f)
pts = con.execute('select Latitude, Longitude, HorizontalAccuracy, Timestamp from CellLocation;')
t = pts.fetchone()
while t:
uuid = uuid + 1
add_feature({
'time': time.ctime(t[3] + 978307200),
'coords': [t[0], t[1]],
'accuracy': t[2],
'uuid': uuid
}, lyr)
t = pts.fetchone()
def setup_shapefile(ds):
lyr = ds.CreateLayer('points')
stringfields = ['time']
numfields = ['accuracy', 'uuid']
for i in stringfields:
field_defn = ogr.FieldDefn(i, ogr.OFTString )
field_defn.SetWidth(256)
lyr.CreateField(field_defn)
for i in numfields:
field_defn = ogr.FieldDefn(i, ogr.OFTInteger )
lyr.CreateField(field_defn)
return lyr
def add_feature(feature, lyr):
if (feature.has_key('coords')):
feat = ogr.Feature(lyr.GetLayerDefn())
feat.SetField(0, feature['time'])
feat.SetField(1, feature['accuracy'])
feat.SetField(2, feature['uuid'])
pt = ogr.Geometry(ogr.wkbPoint)
pt.SetPoint_2D(0, feature['coords'][1], feature['coords'][0])
feat.SetGeometry(pt)
lyr.CreateFeature(feat)
def time_code(t):
'''Parse a time & return seconds from epoch'''
y = int(t[:4])
m = int(t[5:7])
day = int(t[8:10])
h = int(t[11:13])
minute = int(t[14:16])
sec = int(t[17:19])
timestamp = y,m,day,h,minute,sec,-1, -1, -1
timeCode = time.mktime(timestamp)
return timeCode
if __name__ == "__main__":
outfile = sys.argv[1]
infiles = sys.argv[2:]
vector = ogr.GetDriverByName('ESRI Shapefile')
ds = vector.CreateDataSource(outfile)
lyr = setup_shapefile(ds)
for infile in infiles:
pts = parse_pts(infile, lyr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment