xdenisx

buoys2spatialite

Jun 5th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.16 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from pysqlite2 import dbapi2 as sqlite
  3. import sys
  4. import glob
  5.  
  6. sql_connection = sqlite.Connection('iapb.sqlite')
  7.  
  8. cursor = sqlite.Cursor(sql_connection)
  9.  
  10. tablename = 'data'
  11. SRID = '4326' # example SRID, as a string
  12. column_names = 'UID INTEGER PRIMARY KEY AUTOINCREMENT, date DATETIME NOT NULL, idx INT NOT NULL, Geometry BLOB NOT NULL'
  13. geometry_column_name = 'Geometry'
  14. geometry_type = 'POINT' # Could be POLYGON, MULTIPOLYGON, LINESTRING, etc
  15.  
  16. def create_point_geometry_table(cursor, sql_connection, tablename, column_names, SRID):
  17.     cursor.execute("CREATE TABLE '%s'(%s) "% (tablename, column_names))
  18.    
  19.     cursor.execute("SELECT RecoverGeometryColumn('%s', '%s', %s, '%s', 2)" % (tablename, geometry_column_name, str(SRID), geometry_type)) # The '2' indicates that this is a two-dimensional geometry
  20.     sql_connection.commit()
  21.  
  22. def ins_function(cursor, sql_connection, tablename,idate,idx,geom_p):
  23.     try:
  24.         ins_test = "INSERT OR REPLACE INTO %s (date,idx,Geometry) VALUES (\'%s\',\'%s\', GeomFromText(\'%s\'))" % (tablename,idate,idx,geom_p)
  25.         print ins_test
  26.         cursor.execute(ins_test)
  27.         sql_connection.commit()
  28.     except:
  29.         print "Insert error!"    
  30.  
  31. def buffer_select(cursor, geometry_column, table, longitude, latitude, wgs84, buffer_distance):
  32.     'Allows one to select features that are within a buffer distance from a point described by the variables longitude and latitude'
  33.     req = '''SELECT date,idx,AsText(%s) FROM %s WHERE Within(%s.Geometry, Buffer(GeomFromText('POINT(%s %s)',%s),%s))''' %\
  34.                    (geometry_column, table, table, longitude, latitude, wgs84, buffer_distance)
  35.    
  36.     rows=cursor.execute(req)
  37.     ch=0              
  38.     for row in rows:
  39.         print u'Date: %s, ID: %s, LatLon: %s' % (row[0],row[1],row[2])
  40.         ch+=1
  41.     print "\n%s records have found" % ch    
  42.     #print req
  43.     return cursor.fetchall() #you could also use fetchone or fetchmany
  44.  
  45. def select_by_id(cursor, geometry_column, table, idx):
  46.     'Allows select byous trajectory by its ID'
  47.     req = '''SELECT date,idx,AsText(%s) FROM %s WHERE (idx=%s AND date LIKE \'%s\') ''' %\
  48.                    (geometry_column, table, idx, '%00:00:00')
  49.    
  50.     rows=cursor.execute(req)
  51.     ch=0              
  52.     for row in rows:
  53.         print u'Date: %s, ID: %s, LatLon: %s' % (row[0],row[1],row[2])
  54.         ch+=1
  55.     print "\n%s records have found" % ch    
  56.     #print req
  57.     return cursor.fetchall() #you could also use fetchone or fetchmany
  58.  
  59. # This is the distance, which will be in the linear value system of the chosen coordinate system (in this case, it is Decimal Degrees)
  60. buffer_distance = '.5'
  61.  
  62. #rows=buffer_select(cursor, geometry_column_name, tablename, '79.0', '0.0', '4326', buffer_distance)
  63.  
  64. select_by_id(cursor, geometry_column_name, tablename,'8363')
  65.  
  66. # create table
  67. #create_point_geometry_table(cursor,sql_connection,tablename,column_names,SRID)
  68.  
  69. # SELECT data
  70. '''
  71. sel = "SELECT date,idx,AsText(Geometry) FROM %s idx = \'%s\'" % (tablename,idx)
  72. rows=cursor.execute(sel)
  73. for row in rows:
  74.    print u'Точка: %s, Имя: %s, Координаты: %s' % (row[0],row[1],row[2])
  75. '''
  76.  
  77. #ffiles=glob.glob('./data/C*')
  78. ffiles=[]
  79. for iffile in ffiles:
  80.     ffile=open(iffile,'r')
  81.     lines=ffile.readlines()
  82.     i=0
  83.     for line in lines:
  84.         try:
  85.             iyear,imon,iday,ihour,idx,ilat,ilon=line.split()
  86.             print u'Год: %s, Месяц: %s, Число: %s, Час: %s, ID: %s, Широта: %s, Долгота: %s' % (iyear,imon,iday,ihour,idx,ilat,ilon)
  87.         except:
  88.             iyear,imon,iday,ihour,idx,ilat,ilon=iyear,imon,iday,ihour,idx,'999','999'
  89.            
  90.         if int(iyear)<1000:
  91.             iyear='19%s' % iyear
  92.         if int(ihour)<10:
  93.             ihour='0%s' % ihour
  94.         if int(iday)<10:
  95.             iday='0%s' % iday
  96.         if int(imon)<10:
  97.             imon='0%s' % imon
  98.          
  99.         idate='%s-%s-%s %s:00:00' % (iyear,imon,iday,ihour)
  100.         geom_p='POINT(%s %s)' % (ilat,ilon)
  101.         ins_function(cursor, sql_connection, tablename, idate, idx, geom_p)
  102.         i+=1
  103.    
  104.     ffile.close()
  105.  
  106.  
  107. sql_connection.close()
Advertisement
Add Comment
Please, Sign In to add comment