Project

General

Profile

1
#run this before running the script:
2
insert into v_entryinfo("creationDate","lastUpdatedDate","dataSource") VALUES (CURRENT_DATE, CURRENT_DATE, 'DUMMY');
3
select * from v_entryinfo order by id desc limit 1;
4
INSERT INTO v_plots ("entryInfo_id") VALUES (2093909);
5
select * from v_plots limit 1;
6
INSERT INTO v_vegx ("entryInfo_id","plots_id") VALUES (2093909,397259);
7
INSERT INTO v_plottype ("entryInfo_id","plots_id_id") values (2093909,397259);
8
INSERT INTO v_geospatialtype ("entryInfo_id") values (2093909);
9
INSERT INTO v_measurementtype ("entryInfo_id") values (2093909);
10

    
11

    
12
import psycopg2
13
import MySQLdb
14
import datetime
15
from xml.dom import minidom
16
from xml.dom.minidom import getDOMImplementation
17
from bien.v.models import *
18

    
19
mysqldb = MySQLdb.connect(host="localhost", user="admin", passwd="lae5eiNa",db="bien2", use_unicode=True)
20
mysqlcursor = mysqldb.cursor()
21

    
22
psqlconn = psycopg2.connect("dbname=bien_test user=bien_dba password=bien")
23
psqlconn.set_client_encoding('LATIN1')
24
psqlcur = psqlconn.cursor()
25

    
26
system = "BIEN2"
27
scope = "system"
28

    
29
entryInfoInsertBeginning = 'INSERT INTO v_entryinfo ("creationDate","lastUpdatedDate","dataSource") VALUES'
30
vegXInsertBeginning = 'INSERT INTO v_vegx ("entryInfo_id","plots_id") VALUES'
31
plotsInsertBeg = 'INSERT INTO v_plots ("entryInfo_id") VALUES'
32
plotInsertBeg = 'INSERT INTO v_plottype ("entryInfo_id","plots_id_id","plotName","plotUniqueIdentifier","shape","locationNarrative","geospatial_id","area_id") VALUES'
33
countryLocationInsertBeg = 'INSERT INTO v_location ("entryInfo_id","PlotType_id_id","placeType","placeName") VALUES'
34
stateLocationInsertBeg = 'INSERT INTO v_location ("entryInfo_id","PlotType_id_id","placeType","placeName") VALUES'
35
countyLocationInsertBeg = 'INSERT INTO v_location ("entryInfo_id","PlotType_id_id","placeType","placeName") VALUES'
36
geospatialInsertBeg = 'INSERT INTO v_geospatialtype ("entryInfo_id","DecimalLatitude","DecimalLongitude","CoordinateUncertaintyInMeters") VALUES'
37
areaInsertBeg = 'INSERT INTO v_measurementtype ("entryInfo_id","value") VALUES'
38

    
39
batchSize = 500
40

    
41
maxEISql = 'select max(id) from v_entryinfo'
42
psqlcur.execute(maxEISql)
43
maxEntryInfo = psqlcur.fetchone()[0]
44

    
45
maxPlotsSQL = 'select max(id) from v_plots'
46
psqlcur.execute(maxPlotsSQL)
47
maxPlots = psqlcur.fetchone()[0]
48
if maxPlots == None:
49
  maxPlots = 0
50

    
51
maxGeospSQL = 'select max(id) from v_geospatialtype'
52
psqlcur.execute(maxGeospSQL)
53
maxGeospatial = psqlcur.fetchone()[0]
54
if maxGeospatial == None:
55
  maxGeospatial = 0
56

    
57
maxPlotSQL = 'select max(id) from v_plottype'
58
psqlcur.execute(maxPlotSQL)
59
maxPlot = psqlcur.fetchone()[0]
60
if maxPlot == None:
61
  maxPlot = 0
62

    
63
maxAreaSQL = 'select max(id) from v_measurementtype'
64
psqlcur.execute(maxAreaSQL)
65
maxArea = psqlcur.fetchone()[0]
66
if maxArea == None:
67
  maxArea = 0
68

    
69
getSql = 'SELECT PlotName, PlotCD, DBSourceName, Locality, Country, StateProvince, County, PlotArea, PlotAreaUnits, PlotDecimalLatitude, PlotDecimalLongitude, CoordinateUncertainty, PlotShape FROM PlotMetaDataDimension where (PlotName is not null and PlotName <> "") or (PlotCD is not null and PlotCD <> "") order by DBPlotID, DBSourceName'
70
mysqlcursor.execute(getSql)
71
result = mysqlcursor.fetchall()
72

    
73
eii = entryInfoInsertBeginning
74
vxi = vegXInsertBeginning
75
psi = plotsInsertBeg
76
pi = plotInsertBeg
77
cli = countryLocationInsertBeg
78
sli = stateLocationInsertBeg
79
cnli = countyLocationInsertBeg
80
gsi = geospatialInsertBeg
81
ai = areaInsertBeg
82

    
83
iterCt = 0
84
plotName = ''
85
coordinateUncertanity = ''
86
lat = ''
87
long = ''
88
for record in result:
89
  iterCt = iterCt + 1
90
  if record[0] is None or record[0] == '':
91
    plotName = str(record[1])
92
  else:
93
    plotName = str(record[0])
94
  eii  = eii  + "('" + datetime.date.today().strftime("%Y-%m-%d") + "','" + datetime.date.today().strftime("%Y-%m-%d") + "','BIEN2'), "
95
  psi  = psi  + '(' + str(maxEntryInfo + iterCt) + '), '
96
  vxi  = vxi  + '(' + str(maxEntryInfo + iterCt) + ',' + str(maxPlots + iterCt) + '), '
97
  if record[11] is None:
98
    coordinateUncertanity = "NULL"
99
  else:
100
    coordinateUncertanity = str(int(record[11]))
101
  if record[9] is None:
102
    lat = 'NULL'
103
  else:
104
    lat = str(record[9])
105
  if record[10] is None:
106
    long = 'NULL'
107
  else:
108
    long = str(record[9])
109
  gsi  = gsi  + '(' + str(maxEntryInfo + iterCt) + ',' + lat + ',' + long + ',' + coordinateUncertanity + '), '
110
  ai   = ai   + '(' + str(maxEntryInfo + iterCt) + ",'" + str(record[7]) + ' ' + str(record[8]) + "'), "
111
  pi   = pi   + '(' + str(maxEntryInfo + iterCt) + "," + str(maxPlots + iterCt) + ",'" + plotName + "','" + plotName + "_" + str(record[2]) + "','" + str(record[12]) + "','" + str(record[3]) + "'," + str(maxGeospatial + iterCt) + "," + str(maxArea + iterCt) + "), "
112
  cli  = cli  + "(" + str(maxEntryInfo + iterCt) + "," + str(maxPlot + iterCt) + ",'Country','" + str(record[4]) + "'), "
113
  sli  = sli  + "(" + str(maxEntryInfo + iterCt) + "," + str(maxPlot + iterCt) + ",'State','" + str(record[5]) + "'), "
114
  cnli = cnli + "(" + str(maxEntryInfo + iterCt) + "," + str(maxPlot + iterCt) + ",'County','" + str(record[6]) + "'), "
115
  if iterCt % batchSize == 0 or iterCt == len(result):
116
    psqlcur.execute(eii[:(len(eii)-2)])
117
    psqlconn.commit()
118
    psqlcur.execute(psi[:(len(psi)-2)])
119
    psqlconn.commit()
120
    psqlcur.execute(vxi[:(len(vxi)-2)])
121
    psqlconn.commit()
122
    psqlcur.execute(gsi[:(len(gsi)-2)])
123
    psqlconn.commit()
124
    psqlcur.execute(ai[:(len(ai)-2)])
125
    psqlconn.commit()
126
    psqlcur.execute(pi[:(len(pi)-2)])
127
    psqlconn.commit()
128
    psqlcur.execute(cli[:(len(cli)-2)])
129
    psqlconn.commit()
130
    psqlcur.execute(sli[:(len(sli)-2)])
131
    psqlconn.commit()
132
    psqlcur.execute(cnli[:(len(cnli)-2)])
133
    psqlconn.commit()
134
    eii = entryInfoInsertBeginning
135
    vxi = vegXInsertBeginning
136
    psi = plotsInsertBeg
137
    pi = plotInsertBeg
138
    cli = countryLocationInsertBeg
139
    sli = stateLocationInsertBeg
140
    cnli = countyLocationInsertBeg
141
    gsi = geospatialInsertBeg
142
    ai = areaInsertBeg
143

    
144
psqlconn.rollback()
145

    
146

    
(10-10/21)