-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_pg.py
52 lines (41 loc) · 1.56 KB
/
upload_pg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from osgeo import ogr, osr
ogr.UseExceptions()
database = 'test'
usr = 'postgres'
pw = 'mysecretpassword'
host = '192.168.1.82'
table = 'example'
inDataSource = ogr.Open("example_wrapper.vrt")
inLayer = inDataSource.GetLayer('example')
connectionString = "PG:dbname='%s' user='%s' password='%s' host='%s'" % (database,usr,pw,host)
ogrds = ogr.Open(connectionString)
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
outLayer = ogrds.CreateLayer(table, srs, ogr.wkbPoint, ['OVERWRITE=YES','LAUNDER=NO'] )
# Add input Layer Fields to the output Layer if it is the one we want
inLayerDefn = inLayer.GetLayerDefn()
for i in range(0, inLayerDefn.GetFieldCount()):
fieldDefn = inLayerDefn.GetFieldDefn(i)
print(inLayerDefn.GetFieldDefn(i).GetNameRef())
#print(fieldDefn)
#fieldName = fieldDefn.GetName()
#print(fieldName)
outLayer.CreateField(fieldDefn)
outLayerDefn = outLayer.GetLayerDefn()
for feat in inLayer:
new_feature = ogr.Feature(outLayerDefn)
# Add field values from input Layer
for i in range(0, outLayerDefn.GetFieldCount()):
fieldDefn = outLayerDefn.GetFieldDefn(i)
#fieldName = fieldDefn.GetName()
#print(fieldName)
#print(outLayerDefn.GetFieldDefn(i).GetNameRef())
new_feature.SetField(outLayerDefn.GetFieldDefn(i).GetNameRef(),
feat.GetField(i))
geom = feat.GetGeometryRef()
new_feature.SetGeometry(geom.Clone())
print(geom.ExportToWkt())
outLayer.StartTransaction()
outLayer.CreateFeature(new_feature)
new_feature = None
outLayer.CommitTransaction()