Guest User

Untitled

a guest
Mar 14th, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. ##Author: Steve S. Steves
  2.  
  3. import arcpy
  4. import os
  5. import pyodbc
  6. from pyodbc import Error
  7. import time
  8. import shutil
  9.  
  10.  
  11. def compareLists(listOne, listTwo,):
  12.  
  13. notInList = [item for item in listOne if (item not in listTwo)]
  14. print "Lists compared wp"
  15. assert notInList[0] not in listTwo
  16. return notInList
  17.  
  18.  
  19. def createConnection(dbFile):
  20. print "In create_Connection function"
  21. for x in pyodbc.drivers():
  22. if x.startswith('Microsoft Access Driver'):
  23. driver = x
  24. connStr = r'DRIVER={}; DBQ={}'.format(driver,dbFile)
  25. conn = pyodbc.connect(connStr)
  26. print("Connection Created")
  27. return conn
  28.  
  29. def executeSql(conn, sqlStatement):
  30. c = conn.cursor()
  31. c.execute(sqlStatement)
  32. print "query executed"
  33.  
  34.  
  35. def populateTable(conn, populateTableSql, values):
  36. c = conn.cursor()
  37. c.executemany(populateTableSql, values)
  38.  
  39.  
  40. def buildingTables(dbFile, sqlStatement, populateTableSql, values):
  41. print "creating connection"
  42. conn = createConnection(dbFile)
  43.  
  44. try:
  45. print "Calling functions"
  46. executeSql(conn, sqlStatement)
  47. populateTable(conn, populateTableSql, values)
  48. conn.commit()
  49. print "transaction committed"
  50.  
  51. except Error as e:
  52. print(e)
  53.  
  54. finally:
  55. #Checks to see if conn is a variable and then closes it if it is
  56. if conn:
  57. print "closing connection"
  58. conn.close()
  59.  
  60. #Making a FC consisting of only some things in other things
  61. Directory = r""
  62.  
  63. # Calculate date, time, and FGDB name
  64. date = time.strftime('%Y %m %d %H %M %S')
  65. GDB_Name = date + '_NewFootprints.gdb'
  66.  
  67. # Create a new FileGDB
  68. arcpy.CreateFileGDB_management(Directory, GDB_Name)
  69.  
  70. # Set to path of created FileGDB
  71. GDB = os.path.join(Directory, GDB_Name)
  72.  
  73. connection_name = ""
  74. database_platform = ""
  75. instance = ""
  76. authentication = ""
  77. username = ""
  78. password = ""
  79. savePW = ""
  80. database = ""
  81.  
  82. # Look for folder to put y Connection into
  83. if not os.path.isdir(Directory):
  84. os.path.makedirs(Directory)
  85.  
  86. # Look for y connection and create if absent
  87. if not os.path.isfile(os.path.join(Directory, connection_name)):
  88. print ("Making connection file")
  89. arcpy.CreateDatabaseConnection_management(Directory,
  90. connection_name,
  91. database_platform,
  92. instance,
  93. authentication,
  94. username,
  95. password,
  96. savePW,
  97. database)
  98.  
  99. yFDS = r""
  100. FeatureClass = r""
  101. FullPathFC = os.path.join(Directory, connection_name, yFDS, FeatureClass)
  102. sr = arcpy.Describe(FullPathFC).spatialReference
  103.  
  104. # Create new FDS for footprint fc
  105. arcpy.CreateFeatureDataset_management(GDB,
  106. "",
  107. sr)
  108.  
  109. # Get file path of FDS in created FileGDB
  110. newFDS = os.path.join(GDB, "")
  111.  
  112. print ("New File GDB and feature dataset have been created")
  113.  
  114. xFC = os.path.join(r"",r"")
  115. ysFC = os.path.join(r"",r"")
  116. cityLimitsFC = os.path.join(r"",r"",r"")
  117.  
  118.  
  119. inFeaturesList = [xFC, cityLimitsFC]
  120. outFeatureClass = os.path.join(newFDS,"")
  121.  
  122. arcpy.Intersect_analysis(inFeaturesList, outFeatureClass)
  123. print "Intersect ran"
  124.  
  125. #list and dict comprehensions
  126. ysFC = os.path.join(r"",r"")
  127. ysFields = ["","","",""]
  128. #Not using owner number
  129. ysFieldsDict = ["","","", "", "", "", "", "", "", "", "", ""]
  130. yList = [[str(row[0]),str(row[2]), str(row[3]), str(row[4]),
  131. str(row[5]), str(row[6]), str(row[7]),str(row[8]),
  132. str(row[9]), str(row[10]),str(row[11])]
  133. for row in arcpy.da.SearchCursor(ysFC,
  134. ysFieldsDict)]
  135. print "Done with y Parcels List"
  136.  
  137. #make a function for making these lists out of the queries.
  138. xFC = outFeatureClass
  139. xFields = ["","","",""]
  140. xFieldsDict = ["","","",""]
  141. xList = [str(row[0]) for row in arcpy.da.SearchCursor(xFC, xFields)]
  142. z = r""
  143. zFields = ["", "", "", "", "", "", "", "", "", "", "", ""]
  144. zList = [[str(row[0]), str(row[2]), str(row[11]) ,str(row[3]), str(row[4]),
  145. str(row[5]), str(row[6]), str(row[7]), str(row[8]),
  146. str(row[9]), str(row[10])] for row in
  147. arcpy.da.SearchCursor(z, zFields)]
  148. print "z db loaded"
  149.  
  150.  
  151. #using z db to get records for xList
  152. moreFieldsxList = [item for item in zList if item[0] in xList]
  153.  
  154. print "moreFieldsxList made"
  155. print len(moreFieldsxList)
  156. #getting as many aas from the thing as we can.
  157. #faster to load entire database and use python to manipulate it rather
  158. #than using looping search queires.
  159.  
  160. aaFC = os.path.join(r"",r"")
  161. #This is a for a lookup
  162. aaFields = ["",""]
  163.  
  164. aaCodeDict = {row[1]: row[0] for row in arcpy.da.SearchCursor(
  165. aaFC, aaFields)}
  166.  
  167. print "aa Code dictionary built"
  168.  
  169. for item in moreFieldsxList:
  170. if item[2] in aaCodeDict:
  171. item[2] = aaCodeDict[item[2]]
  172.  
  173. print "Done with adding information to moreFieldsxList"
  174.  
  175. listOne = moreFieldsxList
  176. listTwo = yList
  177. listOne = tuple(listOne)
  178. listTwo = tuple(listTwo)
  179. print "Running compare functions"
  180.  
  181. queryListOne = compareLists(listOne, listTwo)
  182. queryListTwo = compareLists(listTwo, listOne)
  183. print "List comparisons successful"
  184.  
  185. #Writing to access database. There are in fact plans to make the
  186. #comparison functions two left joins including only results that
  187. #don't match like it should have been in the first place.
  188. dbFile = "U:FileMcFileFace.mdb"
  189. sqlStatement1 = """ CREATE TABLE In_y_Not_x(
  190. ONE TEXT,
  191. TWO TEXT,
  192. THREE TEXT,
  193. FOUR TEXT,
  194. FIVE TEXT,
  195. SIX TEXT,
  196. SEVEN TEXT,
  197. EIGHT TEXT,
  198. NINE TEXT,
  199. TEN TEXT,
  200. TENTY-ONE TEXT
  201. )
  202. """
  203. sqlStatement2 = """ CREATE TABLE In_x_Not_y(
  204. ONE TEXT,
  205. TWO TEXT,
  206. THREE TEXT,
  207. FOUR TEXT,
  208. FIVE TEXT,
  209. SIX TEXT,
  210. SEVEN TEXT,
  211. EIGHT TEXT,
  212. NINE TEXT,
  213. TEN TEXT,
  214. ELEVEN TEXT
  215. )
  216. """
  217. populateTableSql1 = """ INSERT INTO In_y_Not_x VALUES(
  218. ?,?,?,?,?,?,?,?,?,?,?)"""
  219. populateTableSql2 = """ INSERT INTO In_x_Not_y VALUES(
  220. ?,?,?,?,?,?,?,?,?,?,?)"""
  221. values1 = queryListOne
  222. values2 = queryListTwo
  223.  
  224. buildingTables(dbFile, sqlStatement1, populateTableSql1, values1)
  225. buildingTables(dbFile, sqlStatement2, populateTableSql2, values2)
  226.  
  227.  
  228. shutil.rmtree(GDB)
  229.  
  230. print "Finished, and without any deaths too!"
Add Comment
Please, Sign In to add comment