-
Notifications
You must be signed in to change notification settings - Fork 3
/
ogrTovrt.py
142 lines (133 loc) · 4.17 KB
/
ogrTovrt.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
################################################################################
#
# MODULE: r.planet.py
#
# AUTHOR(S): Massimo Di Stefano 2010-02-7 - [email protected]
# (original code : ogr2vrt.py - Frank Warmerdam, [email protected])
#
#
# PURPOSE: Create OGR VRT from source datasource and
# OMD associated style-file
#
# COPYRIGHT: (c) 2010 by Massimo Di Stefano
#
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.
#
# REQUIRES: Gdal - python
#
#
#
################################################################################
try:
from osgeo import osr, ogr, gdal
except ImportError:
import osr, ogr, gdal
import string
import sys
def GeomType2Name( type ):
if type == ogr.wkbUnknown:
return 'wkbUnknown'
elif type == ogr.wkbPoint:
return 'wkbPoint'
elif type == ogr.wkbLineString:
return 'wkbLineString'
elif type == ogr.wkbPolygon:
return 'wkbPolygon'
elif type == ogr.wkbMultiPoint:
return 'wkbMultiPoint'
elif type == ogr.wkbMultiLineString:
return 'wkbMultiLineString'
elif type == ogr.wkbMultiPolygon:
return 'wkbMultiPolygon'
elif type == ogr.wkbGeometryCollection:
return 'wkbGeometryCollection'
elif type == ogr.wkbNone:
return 'wkbNone'
elif type == ogr.wkbLinearRing:
return 'wkbLinearRing'
else:
return 'wkbUnknown'
def Esc(x):
return gdal.EscapeString( x, gdal.CPLES_XML )
def makestile(outfile, brush, pen, size, fill, thickness):
brush = brush.split(',')
pen = pen.split(',')
size = size.split(',')
outfile = outfile.replace('.vrt','')
outfile = outfile+'.omd'
omd = '// vector file rendering options\n'
omd += 'brush_color: %s %s %s \n' % (brush[0], brush[1], brush[2])
omd += 'pen_color: %s %s %s \n' % (pen[0], pen[1], pen[2])
omd += 'point_width_height: %s %s \n' % (size[0], size[1])
omd += 'fill_flag: %s \n' % (fill)
omd += 'thickness: %s \n' % (thickness)
open(outfile,'w').write(omd)
def ogrvrt(infile,outfile):
layer_list = []
relative = "0"
schema=0
print infile
src_ds = ogr.Open( infile, update = 0 )
if len(layer_list) == 0:
for layer in src_ds:
layer_list.append( layer.GetLayerDefn().GetName() )
vrt = '<OGRVRTDataSource>\n'
for name in layer_list:
layer = src_ds.GetLayerByName(name)
layerdef = layer.GetLayerDefn()
vrt += ' <OGRVRTLayer name="%s">\n' % Esc(name)
vrt += ' <SrcDataSource relativeToVRT="%s" shared="%d">%s</SrcDataSource>\n' \
% (relative,not schema,Esc(infile))
if schema:
vrt += ' <SrcLayer>@dummy@</SrcLayer>\n'
else:
vrt += ' <SrcLayer>%s</SrcLayer>\n' % Esc(name)
vrt += ' <GeometryType>%s</GeometryType>\n' \
% GeomType2Name(layerdef.GetGeomType())
srs = layer.GetSpatialRef()
if srs is not None:
vrt += ' <LayerSRS>%s</LayerSRS>\n' \
% (Esc(srs.ExportToWkt()))
# Process all the fields.
for fld_index in range(layerdef.GetFieldCount()):
src_fd = layerdef.GetFieldDefn( fld_index )
if src_fd.GetType() == ogr.OFTInteger:
type = 'Integer'
elif src_fd.GetType() == ogr.OFTString:
type = 'String'
elif src_fd.GetType() == ogr.OFTReal:
type = 'Real'
elif src_fd.GetType() == ogr.OFTStringList:
type = 'StringList'
elif src_fd.GetType() == ogr.OFTIntegerList:
type = 'IntegerList'
elif src_fd.GetType() == ogr.OFTRealList:
type = 'RealList'
elif src_fd.GetType() == ogr.OFTBinary:
type = 'Binary'
elif src_fd.GetType() == ogr.OFTDate:
type = 'Date'
elif src_fd.GetType() == ogr.OFTTime:
type = 'Time'
elif src_fd.GetType() == ogr.OFTDateTime:
type = 'DateTime'
else:
type = 'String'
vrt += ' <Field name="%s" type="%s"' \
% (Esc(src_fd.GetName()), type)
if not schema:
vrt += ' src="%s"' % Esc(src_fd.GetName())
if src_fd.GetWidth() > 0:
vrt += ' width="%d"' % src_fd.GetWidth()
if src_fd.GetPrecision() > 0:
vrt += ' precision="%d"' % src_fd.GetPrecision()
vrt += '/>\n'
vrt += ' </OGRVRTLayer>\n'
vrt += '</OGRVRTDataSource>\n'
file = open(outfile,'w')
file.write(vrt)
file.close()
print 'vrt wroted'