-
Notifications
You must be signed in to change notification settings - Fork 4
/
OGR2ClassLayer.py
179 lines (167 loc) · 7.65 KB
/
OGR2ClassLayer.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#############################################
# OGR2Layers Plugin (c) for Quantum GIS #
# (c) Copyright Nicolas BOZON - 2008 #
# Authors: Nicolas BOZON, Rene-Luc D'HONT, Michael DOUCHIN, Luca DELUCCHI #
# Email: lucadeluge at gmail dot com #
# #
#############################################
# OGR2Layers Plugin is licensed under the terms of GNU GPL 2 #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. #
# See the GNU General Public License for more details. #
#############################################
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
import os
import string
from OGR2ClassQuery import *
from OGR2ClassStyle import *
class OGR2LayersClassLayer:
"""A class to convert and add layer to the file
layer = QGIS layer
rendering = type of rendering (default, qgis)
query = type of query (none,single,cluster)
outputFormat = output format
outProj = output projection
mydir = directory where save the files
"""
def __init__(self,
layer,
rendering,
query,
outputFormat,
outProj,
mydir,
):
#qgis layer
self.layer = layer
#qgis layer source
self.source = self.layer.source()
#layer name
self.name = self.layer.name()
#layer output format
self.outputFormat = outputFormat
#putput vector name
self.outputName = unicode(self.name + "." + self.outputFormat)
#input epsg
self.inEpsg = self.layer.crs().authid()
#set the output epsg code
self.outputEpsg = outProj
#type of rendering
self.rendering = rendering
#type of query
self.query = query
#type of vector
self.providerName = str(self.layer.dataProvider().name())
#directory where save files
self.pathSave = mydir
#name of file to write
self.destPathName = os.path.abspath(os.path.join(self.pathSave,
self.outputName))
#class query
self.classQuery = OGR2LayersClassQuery(self.layer, self.query)
def convertOGR(self):
"""Convert vector layer and set the variable for html file"""
#TODO fix when WFS url will be correct
# #add WFS
# if (self.providerName == "WFS"):
# #if the input and output epsg have the same projection add WFS
# #like OpenLayers.Layer.WFS
# if self.inEpsg == self.outputEpsg:
# self.outputFormat = "WFS"
# self.outputName = self.source
# #else write shapefile from WFS and reproject it with the right
# #projection and load it like OpenLayers.Layer.GML
# else:
# if self.writeShape():
# return 0
# else:
# err = "A problem occurs during convertion of layer {l}".format(l=self.name)
# raise Exception, err
# #add other vector type
# else:
self.writeShape()
def writeShape(self):
nameFile = os.path.abspath(os.path.join(str(self.pathSave),
str(self.name) + '_temp.shp'))
QgsVectorFileWriter.deleteShapeFile(nameFile)
inputQgsReference = QgsCoordinateReferenceSystem()
inputQgsReference.createFromOgcWmsCrs(self.outputEpsg)
writeShape = QgsVectorFileWriter.writeAsVectorFormat(self.layer,
str(self.destPathName),
"UTF8",
inputQgsReference,
self.outputFormat)
if writeShape == QgsVectorFileWriter.NoError:
return True
else:
err = "A problem occurs during convertion of layer {l}".format(l=self.name)
raise Exception, err
def htmlStyle(self):
"""Create javascript code for style"""
if self.rendering == 'qgis':
try:
#class rendering
self.classStyle = OGR2LayersClassStyle(self.layer,
self.pathSave)
self.image = self.classStyle.svg
return self.classStyle.typeRendering()
except Exception, e:
raise Exception, e
def logStyle(self):
"""Return the log string of style operation for output"""
if self.rendering == 'qgis':
try:
#return the log string
return self.classStyle.retLog()
except Exception, e:
raise Exception, e
def htmlQuery(self):
"""Create javascript code for query"""
return self.classQuery.createQuery()
def htmlLayer(self):
"""Create the Javascript code for vector layer"""
#TODO fix when WFS url will work
if self.outputFormat == 'WFS':
htmlLayer = ['var ' + self.name + ' = new OpenLayers.Layer.Vector'\
+ '("' + self.name + ' ' + self.outputFormat \
+ '", {\n\t\tprotocol: new OpenLayers.Protocol.WFS({\n\t\t\t' \
+ 'url: "' + self.outputName + '",\n\t\t\t' \
+ 'featureType: "' + self.outputName + '",']
#add cluster strategy
htmlLayer.append('\n\t\t})')
htmlLayer.append(',\n\t\tstrategies: [\n\t\t\tnew OpenLayers.Strategy.BBOX()')
else:
htmlLayer = ['var ' + self.name + ' = new OpenLayers.Layer.Vector'\
+ '("' + self.name + ' ' + self.outputFormat \
+ '", {\n\t\tprotocol: new OpenLayers.Protocol.HTTP({\n\t\t\t' \
+ 'url: "' + self.outputName + '",\n\t\t\t']
#if outputFormat is GeoJson
if self.outputFormat == 'GeoJSON':
#add the format
htmlLayer.append('format: new OpenLayers.Format.GeoJSON()')
elif self.outputFormat == 'GML':
#add the format
htmlLayer.append('format: new OpenLayers.Format.GML()')
elif self.outputFormat == 'KML':
#add the format
htmlLayer.append('format: new OpenLayers.Format.KML()')
#add cluster strategy
htmlLayer.append('\n\t\t})')
htmlLayer.append(',\n\t\tstrategies: [\n\t\t\tnew OpenLayers.Strategy.Fixed()')
if self.query == 'cluster':
htmlLayer.append(',\n\t\t\tnew OpenLayers.Strategy.Cluster()')
htmlLayer.append('\n\t\t]')
#if only qgis style it's choosen I must open {
if self.rendering == 'qgis':
htmlLayer.append(',\n\t\tstyleMap: ' + self.name + '_style')
#for all it closes the layer ) and add it to the map
htmlLayer.append('\n\t});\n\tmap.addLayer(' + self.name + ');\n\t')
return htmlLayer