Skip to content
Newer
Older
100644 196 lines (183 sloc) 8.43 KB
6351e1c @lucadelu Refactoring of plugin, create different class and rebuild ogr2layers.py
authored Nov 10, 2010
1 #############################################
0423c9d @lucadelu add plugin to webmenu for 1.8; reindentation
authored Feb 29, 2012
2 # OGR2Layers Plugin (c) for Quantum GIS #
3 # (c) Copyright Nicolas BOZON - 2008 #
4 # Authors: Nicolas BOZON, Rene-Luc D'HONT, Michael DOUCHIN, Luca DELUCCHI #
5 # Email: lucadeluge at gmail dot com #
6 # #
6351e1c @lucadelu Refactoring of plugin, create different class and rebuild ogr2layers.py
authored Nov 10, 2010
7 #############################################
0423c9d @lucadelu add plugin to webmenu for 1.8; reindentation
authored Feb 29, 2012
8 # OGR2Layers Plugin is licensed under the terms of GNU GPL 2 #
9 # This program is free software; you can redistribute it and/or modify #
10 # it under the terms of the GNU General Public License as published by #
11 # the Free Software Foundation; either version 2 of the License, or #
12 # (at your option) any later version. #
13 # This program is distributed in the hope that it will be useful, #
14 # but WITHOUT ANY WARRANTY; without even implied warranty of #
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. #
16 # See the GNU General Public License for more details. #
6351e1c @lucadelu Refactoring of plugin, create different class and rebuild ogr2layers.py
authored Nov 10, 2010
17 #############################################
18
19 from PyQt4.QtCore import *
20 from PyQt4.QtGui import *
21 from qgis.core import *
22 from qgis.gui import *
23
24 import sys
25 import os
26 import string
27
28 import OGR2ogr
29 from OGR2ClassQuery import *
30 from OGR2ClassStyle import *
31
32
33 class OGR2LayersClassLayer:
0423c9d @lucadelu add plugin to webmenu for 1.8; reindentation
authored Feb 29, 2012
34 """A class to convert and add layer to the file
35 layer = QGIS layer
36 rendering = type of rendering (default, qgis)
37 query = type of query (none,single,cluster)
38 outputFormat = output format
39 outProj = output projection
40 mydir = directory where save the files
41 """
42 def __init__(self,
43 layer,
44 rendering,
45 query,
46 outputFormat,
47 outProj,
48 mydir,
49 ):
50 #qgis layer
51 self.layer = layer
52 #qgis layer source
53 self.source = self.layer.source()
54 self.source.remove(QRegExp('\|layerid=[\d]+$'))
55 #layer name
56 self.name = self.layer.name()
57 #layer output format
58 self.outputFormat = outputFormat
59 #putput vector name
60 self.outputName = unicode(self.name + "." + self.outputFormat)
61 #input epsg
62 self.inEpsg = self.layer.crs().epsg()
63 #set the output epsg code
64 if outProj == "EPSG:900913":
65 self.outputEpsg = 900913
66 elif outProj == "EPSG:4326":
67 self.outputEpsg = 4326
68 #type of rendering
69 self.rendering = rendering
70 #type of query
71 self.query = query
72 #type of vector
73 self.providerName = str(self.layer.dataProvider().name())
74 #directory where save files
75 self.pathSave = mydir
76 #name of file to write
77 self.destPathName = os.path.abspath(os.path.join(self.pathSave,self.outputName))
78 #class query
79 self.classQuery = OGR2LayersClassQuery(self.layer, self.query)
6351e1c @lucadelu Refactoring of plugin, create different class and rebuild ogr2layers.py
authored Nov 10, 2010
80
0423c9d @lucadelu add plugin to webmenu for 1.8; reindentation
authored Feb 29, 2012
81 def convertOGR(self):
82 """Convert vector layer and set the variable for html file"""
83 #add WFS
84 if (self.providerName == "WFS"):
85 #if the input and output epsg have the same projection add WFS
86 #like OpenLayers.Layer.WFS
87 if self.inEpsg == self.outputEpsg:
88 self.OpenLayersFormat = "WFS"
89 self.outputFormat = "WFS"
90 self.outputName = self.source
91 #else write shapefile from WFS and reproject it with the right
92 #projection and load it like OpenLayers.Layer.GML
93 else:
94 if self.writeShape():
95 return 0
96 #nameFileWFS = os.path.abspath(os.path.join(self.pathSave,
97 #str(self.name) + "_temp.shp"))
98 #QgsVectorFileWriter.deleteShapeFile(nameFileWFS)
99 #inputQgsReference = QgsCoordinateReferenceSystem()
100 #inputQgsReference.createFromEpsg(self.inEpsg)
101 #writeShape = QgsVectorFileWriter.writeAsShapefile(self.layer,
102 #nameFileWFS, "UTF8", inputQgsReference)
103 #if writeShape == QgsVectorFileWriter.NoError:
104 #OGR2ogr.Ogr2Ogr(nameFileWFS, str(self.destPathName), self.outputEpsg,
105 #self.inEpsg, self.outputFormat)
106 #self.OpenLayersFormat = "GML"
107 else:
108 raise Exception, "Some problem with convertion from WFS"
109 #add other vector type
110 else :
111 #spatialite
112 if (self.providerName == "spatialite" or self.providerName == "postgres"):
113 if self.writeShape():
114 return 0
115 else:
116 raise Exception, "Some problem with convertion from database"
117 #raise Exception, "There is a bug with OGR and Spatialite and " \
118 #+ "now it isn't possible support Spatialite\n" # WORK
119 #mysource_temp = self.source.split(' ')[0]
120 #mysource_temp = str(mysource_temp.split('=')[1])
121 #self.source = mysource_temp.replace("'","")
122 #grass
123 elif (self.providerName == "grass"): #grass
124 mysource_temp = str(self.source).split('/')[0:-1]
125 mysource_temp.insert(-1,"vector")
126 mysource_temp.append("head")
127 self.source = string.join(mysource_temp,'/')
128 #other type of vector like Esri shapefile, GML, KML, GeoJson
129 OGR2ogr.Ogr2Ogr(str(self.source), str(self.destPathName), self.outputEpsg,
130 self.inEpsg, self.outputFormat)
131 self.OpenLayersFormat = "GML"
132 return 0
6870c56 @lucadelu Update files with some bugs closed and add comments
authored Nov 15, 2010
133
0423c9d @lucadelu add plugin to webmenu for 1.8; reindentation
authored Feb 29, 2012
134 def writeShape(self):
135 nameFile = os.path.abspath(os.path.join(str(self.pathSave),
136 str(self.name) + '_temp.shp'))
137 QgsVectorFileWriter.deleteShapeFile(nameFile)
138 inputQgsReference = QgsCoordinateReferenceSystem()
139 inputQgsReference.createFromEpsg(self.inEpsg)
140 writeShape = QgsVectorFileWriter.writeAsShapefile(self.layer,
141 str(nameFile), "UTF8", inputQgsReference)
142 if writeShape == QgsVectorFileWriter.NoError:
143 OGR2ogr.Ogr2Ogr(nameFile, str(self.destPathName), self.outputEpsg,
144 self.inEpsg, self.outputFormat)
145 self.OpenLayersFormat = "GML"
146 QgsVectorFileWriter.deleteShapeFile(nameFile)
147 return True
4a21abf @lucadelu add support to spatialite
authored Nov 21, 2010
148
0423c9d @lucadelu add plugin to webmenu for 1.8; reindentation
authored Feb 29, 2012
149 def htmlStyle(self):
150 """Create javascript code for style"""
151 if self.rendering == 'qgis':
152 try:
153 #class rendering
154 self.classStyle = OGR2LayersClassStyle(self.layer,self.pathSave)
155 self.image = self.classStyle.svg
156 return self.classStyle.typeRendering()
157 except Exception, e:
158 raise Exception, e
8068a26 @lucadelu add a proof of support for new symbology
authored Dec 4, 2011
159
0423c9d @lucadelu add plugin to webmenu for 1.8; reindentation
authored Feb 29, 2012
160 def logStyle(self):
161 """Return the log string of style operation for output"""
162 if self.rendering == 'qgis':
163 try:
164 #return the log string
165 return self.classStyle.retLog()
166 except Exception, e:
167 raise Exception, e
8068a26 @lucadelu add a proof of support for new symbology
authored Dec 4, 2011
168
0423c9d @lucadelu add plugin to webmenu for 1.8; reindentation
authored Feb 29, 2012
169 def htmlQuery(self):
170 """Create javascript code for query"""
171 return self.classQuery.createQuery()
6351e1c @lucadelu Refactoring of plugin, create different class and rebuild ogr2layers.py
authored Nov 10, 2010
172
0423c9d @lucadelu add plugin to webmenu for 1.8; reindentation
authored Feb 29, 2012
173 def htmlLayer(self):
174 """Create the Javascript code for vector layer"""
175 htmlLayer = ['var ' + self.name + ' = new OpenLayers.Layer.' \
176 + self.OpenLayersFormat + '("' + self.name + ' ' + self.outputFormat \
177 + '", "' + self.outputName + '", {']
178 #if outputFormat is GeoJson
179 if self.outputFormat == 'GeoJSON':
180 #add the format
181 htmlLayer.append('format: OpenLayers.Format.GeoJSON')
182 #add cluster strategy
183 if self.query == 'cluster':
184 htmlLayer.append(', strategies: [new OpenLayers.Strategy.Cluster()]')
185 #if qgis style it's choosen add it, I use this indentation for {
186 #because it is already open with strategy cluster
187 if self.rendering == 'qgis':
188 htmlLayer.append(', styleMap: ' + self.name + '_style')
189 #if only qgis style it's choosen I must open {
190 if self.query != 'cluster' and self.rendering == 'qgis':
191 htmlLayer.append(', styleMap: ' + self.name + '_style')
192 #for all it closes the layer ) and add it to the map
193 htmlLayer.append('});\n\tmap.addLayer('+ self.name +');\n\t')
8068a26 @lucadelu add a proof of support for new symbology
authored Dec 4, 2011
194
0423c9d @lucadelu add plugin to webmenu for 1.8; reindentation
authored Feb 29, 2012
195 return htmlLayer
Something went wrong with that request. Please try again.