Guest User

Untitled

a guest
Nov 20th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. import psycopg2
  2. import subprocess
  3. import os
  4.  
  5. path = "D:\\"
  6. resolution = 231.65
  7.  
  8. for raster_lc in os.listdir(path):
  9. if raster_lc.endswith(".tif"):
  10.  
  11. raster_lc = os.path.join(path, raster_lc)
  12. name = raster_lc.split('\\')[7]
  13. file_name = name.split('.tif')[0]
  14.  
  15. from Global import parameters
  16. db, user, password, resolution = parameters()
  17.  
  18. os.environ['PATH'] = r';C:\Program Files\PostgreSQL\9.4\bin'
  19. os.environ['PGHOST'] = 'localhost'
  20. os.environ['PGPORT'] = '5432'
  21. os.environ['PGUSER'] = user
  22. os.environ['PGPASSWORD'] = password
  23. os.environ['PGDATABASE'] = db
  24.  
  25. rastername = str(file_name)
  26. rasterlayer = rastername.lower()
  27.  
  28. connection = psycopg2.connect(database=db, user=user, host="localhost", password=password)
  29.  
  30. # Check if raster table already exists
  31. cursor_check = connection.cursor()
  32. cursor_check.execute("SELECT COUNT(*) FROM information_schema.tables WHERE table_name = '" + rasterlayer + "' ;")
  33. check = cursor_check.fetchone()[0]
  34.  
  35. if check > 0:
  36. connection.close()
  37. pass
  38.  
  39. else:
  40.  
  41. cmds = 'raster2pgsql -s 4326 -I "' + raster_lc + '" |psql'
  42. subprocess.call(cmds, shell=True)
  43.  
  44. """
  45. # Project raster Albers, resample to 250m and transform to WGS84
  46. rql = "UPDATE " + rasterlayer + " SET rast = ST_Transform(ST_SetSRID(rast,4326),102025); \
  47. UPDATE " + rasterlayer + " SET rast = ST_Rescale(rast, 250, 'Near'); \
  48. UPDATE " + rasterlayer + " SET rast = ST_Transform(ST_SetSRID(rast,102025),4326); "
  49. cursor.execute(rql)
  50. connection.commit()
  51. """
  52.  
  53. connection.close()
  54.  
  55. ########################################################################################################
  56.  
  57. calculation = "((" + resolution + " * " + resolution + ") / 10000)"
  58.  
  59. table_list = ["gadm_0","gadm_1","gadm_2","gadm_3"]
  60.  
  61. for table in table_list:
  62.  
  63. connection = psycopg2.connect(database=db, user=user, host="localhost", password=password)
  64. cursor = connection.cursor()
  65.  
  66. geom = table + "_geom"
  67. year = file_name[-9:].split("_")[0]
  68. clid = file_name[-9:].split("_")[1]
  69.  
  70. cid = int(clid)
  71. rasteryear = int(year)
  72.  
  73. temp = "ca_aggregation"
  74.  
  75. ffl = "CREATE TABLE " + temp + " AS TABLE " + table + "; \
  76. DELETE FROM " + temp + "; \
  77. ALTER SEQUENCE seq_aggregation RESTART WITH 1; \
  78. ALTER TABLE " + temp + " ALTER COLUMN idx SET DEFAULT NEXTVAL('seq_aggregation');"
  79. cursor.execute(ffl)
  80. connection.commit()
  81.  
  82. # Crop statistics: calculate per aggregation unit
  83. bql = "INSERT INTO " + temp + "(gid, year) SELECT \
  84. gid, " + year + " \
  85. FROM " + geom + ";"
  86. cursor.execute(bql)
  87. connection.commit()
  88.  
  89. if cid == 1:
  90. ha = "cereals"
  91. if cid == 2:
  92. ha = "maize"
  93. if cid == 3:
  94. ha = "grassland"
  95. if cid == 4:
  96. ha = "forest"
  97. if cid == 5:
  98. ha = "shrubs"
  99. if cid == 6:
  100. ha = "urban"
  101. if cid == 7:
  102. ha = "industrial"
  103. if cid == 8:
  104. ha = "bare_soil"
  105. if cid == 9:
  106. ha = "other"
  107.  
  108. crop = ha
  109. years = str(year)
  110.  
  111. sql = "UPDATE " + temp + " SET " + crop + " = zonal_stat." + crop + " \
  112. FROM (SELECT gid, (sum(sum)/100) * " + calculation + " AS " + crop + " \
  113. FROM " + rasterlayer + ", " + geom + ", ST_SummaryStats(ST_Clip(rast,1,geom,true), 1) \
  114. GROUP BY gid) AS zonal_stat WHERE " + temp + ".gid = zonal_stat.gid AND year = " + years + "; "
  115. cursor.execute(sql)
  116. connection.commit()
  117.  
  118. oql = "UPDATE " + temp + " SET " + crop + " = 0 WHERE " + crop + " IS NULL;"
  119. cursor.execute(oql)
  120. connection.commit()
  121.  
  122. # Check year
  123. check_id = connection.cursor()
  124. check_id.execute("SELECT COUNT(*) FROM " + table + " WHERE year = " + years + " ;")
  125. check_code = check_id.fetchone()[0]
  126. checker = int(check_code)
  127.  
  128. if checker == 0:
  129.  
  130. bql = "INSERT INTO " + table + "(gid, year) SELECT \
  131. gid, " + year + " \
  132. FROM " + geom + ";"
  133. cursor.execute(bql)
  134. connection.commit()
  135.  
  136. # Pass data
  137. www = "UPDATE " + table + " SET " + crop + " = " + temp + "." + crop + " FROM " + temp + " \
  138. #WHERE " + table + ".gid = " + temp + ".gid \
  139. #AND " + table + ".year = " + temp + ".year ; \
  140. #DROP TABLE " + temp + "; "
  141. cursor.execute(www)
  142. connection.commit()
  143.  
  144. else:
  145.  
  146. # Pass data and drop temporary table
  147. www = "DROP TABLE " + temp + "; "
  148. cursor.execute(www)
  149. connection.commit()
  150.  
  151. connection.close()
Add Comment
Please, Sign In to add comment