core.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. """!@package grass.script.tgis_core
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS core functions to be used in Python sripts.
  4. Usage:
  5. @code
  6. import grass.temporal as tgis
  7. tgis.create_temporal_database()
  8. ...
  9. @endcode
  10. (C) 2008-2011 by the GRASS Development Team
  11. This program is free software under the GNU General Public
  12. License (>=v2). Read the file COPYING that comes with GRASS
  13. for details.
  14. @author Soeren Gebbert
  15. """
  16. import os
  17. import copy
  18. from datetime import datetime, date, time, timedelta
  19. import grass.script.core as core
  20. import sqlite3 as dbmi
  21. #import psycopg2 as dbmi
  22. # Needed for dictionary like cursors
  23. #import psycopg2.extras
  24. ###############################################################################
  25. def get_grass_location_db_path():
  26. if dbmi.__name__ == "sqlite3":
  27. grassenv = core.gisenv()
  28. dbpath = os.path.join(grassenv["GISDBASE"], grassenv["LOCATION_NAME"])
  29. return os.path.join(dbpath, "grass.db")
  30. elif dbmi.__name__ == "psycopg2":
  31. return "dbname=grass_test user=soeren password=abcdefgh"
  32. ###############################################################################
  33. def get_sql_template_path():
  34. base = os.getenv("GISBASE")
  35. base_etc = os.path.join(base, "etc")
  36. return os.path.join(base_etc, "sql")
  37. ###############################################################################
  38. def test_increment_datetime_by_string():
  39. dt = datetime(2001, 9, 1, 0, 0, 0)
  40. string = "60 seconds, 4 minutes, 12 hours, 10 days, 1 weeks, 5 months, 1 years"
  41. dt1 = datetime(2003,2,18,12,5,0)
  42. dt2 = increment_datetime_by_string(dt, string)
  43. delta = dt1 -dt2
  44. if delta.days != 0 or delta.seconds != 0:
  45. core.fatal("increment computation is wrong")
  46. ###############################################################################
  47. def increment_datetime_by_string(mydate, increment, mult = 1):
  48. """Return a new datetime object incremented with the provided relative dates specified as string.
  49. Additional a multiplier can be specified to multiply the increment bevor adding to the provided datetime object.
  50. @param mydate A datetime object to incremented
  51. @param increment A string providing increment information:
  52. The string may include comma separated values of type seconds, minutes, hours, days, weeks, months and years
  53. Example: Increment the datetime 2001-01-01 00:00:00 with "60 seconds, 4 minutes, 12 hours, 10 days, 1 weeks, 5 months, 1 years"
  54. will result in the datetime 2003-02-18 12:05:00
  55. @param mult A multiplier, default is 1
  56. """
  57. if increment:
  58. seconds = 0
  59. minutes = 0
  60. hours = 0
  61. days = 0
  62. weeks = 0
  63. months = 0
  64. years = 0
  65. inclist = []
  66. # Split the increment string
  67. incparts = increment.split(",")
  68. for incpart in incparts:
  69. inclist.append(incpart.strip().split(" "))
  70. for inc in inclist:
  71. if inc[1].find("seconds") >= 0:
  72. seconds = mult * int(inc[0])
  73. elif inc[1].find("minutes") >= 0:
  74. minutes = mult * int(inc[0])
  75. elif inc[1].find("hours") >= 0:
  76. hours = mult * int(inc[0])
  77. elif inc[1].find("days") >= 0:
  78. days = mult * int(inc[0])
  79. elif inc[1].find("weeks") >= 0:
  80. weeks = mult * int(inc[0])
  81. elif inc[1].find("months") >= 0:
  82. months = mult * int(inc[0])
  83. elif inc[1].find("years") >= 0:
  84. years = mult * int(inc[0])
  85. else:
  86. core.error(_("Wrong increment format: %s") % (increment))
  87. return None
  88. return increment_datetime(mydate, years, months, weeks, days, hours, minutes, seconds)
  89. return mydate
  90. ###############################################################################
  91. def increment_datetime(mydate, years=0, months=0, weeks=0, days=0, hours=0, minutes=0, seconds=0):
  92. """Return a new datetime object incremented with the provided relative dates and times"""
  93. tdelta_seconds = timedelta(seconds=seconds)
  94. tdelta_minutes = timedelta(minutes=minutes)
  95. tdelta_hours = timedelta(hours=hours)
  96. tdelta_days = timedelta(days=days)
  97. tdelta_weeks = timedelta(weeks=weeks)
  98. tdelta_months = timedelta(0)
  99. tdelta_years = timedelta(0)
  100. if months > 0:
  101. # Compute the actual number of days in the month to add as timedelta
  102. year = mydate.year
  103. month = mydate.month
  104. all_months = int(months + month)
  105. years_to_add = int(all_months/12)
  106. residual_months = all_months%12
  107. # Make a deep copy of the datetime object
  108. dt1 = copy.copy(mydate)
  109. # Make sure the montha starts with a 1
  110. if residual_months == 0:
  111. residual_months = 1
  112. dt1 = dt1.replace(year = year + years_to_add, month = residual_months)
  113. tdelta_months = dt1 - mydate
  114. if years > 0:
  115. # Make a deep copy of the datetime object
  116. dt1 = copy.copy(mydate)
  117. # Compute the number of days
  118. dt1 = dt1.replace(year=mydate.year + int(years))
  119. tdelta_years = dt1 - mydate
  120. return mydate + tdelta_seconds + tdelta_minutes + tdelta_hours + \
  121. tdelta_days + tdelta_weeks + tdelta_months + tdelta_years
  122. ###############################################################################
  123. def create_temporal_database():
  124. """This function creates the grass location database structure for raster, vector and raster3d maps
  125. as well as for the space-time datasets strds, str3ds and stvds"""
  126. database = get_grass_location_db_path()
  127. db_exists = False
  128. # Check if the database already exists
  129. if dbmi.__name__ == "sqlite3":
  130. # Check path of the sqlite database
  131. if os.path.exists(database):
  132. db_exists = True
  133. elif dbmi.__name__ == "psycopg2":
  134. # Connect to database
  135. connection = dbmi.connect(database)
  136. cursor = connection.cursor()
  137. # Check for raster_base table
  138. cursor.execute("SELECT EXISTS(SELECT * FROM information_schema.tables WHERE table_name=%s)", ('raster_base',))
  139. db_exists = cursor.fetchone()[0]
  140. connection.commit()
  141. cursor.close()
  142. if db_exists == True:
  143. return
  144. # Read all SQL scripts and templates
  145. map_tables_template_sql = open(os.path.join(get_sql_template_path(), "map_tables_template.sql"), 'r').read()
  146. raster_metadata_sql = open(os.path.join(get_sql_template_path(), "raster_metadata_table.sql"), 'r').read()
  147. raster3d_metadata_sql = open(os.path.join(get_sql_template_path(), "raster3d_metadata_table.sql"), 'r').read()
  148. vector_metadata_sql = open(os.path.join(get_sql_template_path(), "vector_metadata_table.sql"), 'r').read()
  149. stds_tables_template_sql = open(os.path.join(get_sql_template_path(), "stds_tables_template.sql"), 'r').read()
  150. strds_metadata_sql = open(os.path.join(get_sql_template_path(), "strds_metadata_table.sql"), 'r').read()
  151. str3ds_metadata_sql = open(os.path.join(get_sql_template_path(), "str3ds_metadata_table.sql"), 'r').read()
  152. stvds_metadata_sql = open(os.path.join(get_sql_template_path(), "stvds_metadata_table.sql"), 'r').read()
  153. # Create the raster, raster3d and vector tables
  154. raster_tables_sql = map_tables_template_sql.replace("GRASS_MAP", "raster")
  155. vector_tables_sql = map_tables_template_sql.replace("GRASS_MAP", "vector")
  156. raster3d_tables_sql = map_tables_template_sql.replace("GRASS_MAP", "raster3d")
  157. # Create the space-time raster, raster3d and vector dataset tables
  158. strds_tables_sql = stds_tables_template_sql.replace("STDS", "strds")
  159. stvds_tables_sql = stds_tables_template_sql.replace("STDS", "stvds")
  160. str3ds_tables_sql = stds_tables_template_sql.replace("STDS", "str3ds")
  161. # Connect to database
  162. connection = dbmi.connect(database)
  163. cursor = connection.cursor()
  164. if dbmi.__name__ == "sqlite3":
  165. sqlite3_delete_trigger_sql = open(os.path.join(get_sql_template_path(), "sqlite3_delete_trigger.sql"), 'r').read()
  166. # Execute the SQL statements for sqlite
  167. # Create the global tables for the native grass datatypes
  168. cursor.executescript(raster_tables_sql)
  169. cursor.executescript(raster_metadata_sql)
  170. cursor.executescript(vector_tables_sql)
  171. cursor.executescript(vector_metadata_sql)
  172. cursor.executescript(raster3d_tables_sql)
  173. cursor.executescript(raster3d_metadata_sql)
  174. # Create the tables for the new space-time datatypes
  175. cursor.executescript(strds_tables_sql)
  176. cursor.executescript(strds_metadata_sql)
  177. cursor.executescript(stvds_tables_sql)
  178. cursor.executescript(stvds_metadata_sql)
  179. cursor.executescript(str3ds_tables_sql)
  180. cursor.executescript(str3ds_metadata_sql)
  181. cursor.executescript(sqlite3_delete_trigger_sql)
  182. elif dbmi.__name__ == "psycopg2":
  183. # Execute the SQL statements for postgresql
  184. # Create the global tables for the native grass datatypes
  185. cursor.execute(raster_tables_sql)
  186. cursor.execute(raster_metadata_sql)
  187. cursor.execute(vector_tables_sql)
  188. cursor.execute(vector_metadata_sql)
  189. cursor.execute(raster3d_tables_sql)
  190. cursor.execute(raster3d_metadata_sql)
  191. # Create the tables for the new space-time datatypes
  192. cursor.execute(strds_tables_sql)
  193. cursor.execute(strds_metadata_sql)
  194. cursor.execute(stvds_tables_sql)
  195. cursor.execute(stvds_metadata_sql)
  196. cursor.execute(str3ds_tables_sql)
  197. cursor.execute(str3ds_metadata_sql)
  198. connection.commit()
  199. cursor.close()