9
9
import sys , string
10
10
from xml .dom import minidom , Node
11
11
12
+ # symbol map
13
+ qgisSymbols = {'hard:circle' : 'CIRCLE' }
12
14
class Qgis2Map :
13
15
def __init__ (self , projectFile , mapFile ):
14
16
self .project = projectFile
@@ -61,7 +63,8 @@ def writeMapFile(self):
61
63
# write the LAYER sections
62
64
self .writeMapLayers ()
63
65
64
- # close the map file
66
+ # END and close the map file
67
+ self .outFile .write ("END" )
65
68
self .outFile .close ()
66
69
67
70
ret = "Writing the map file using " + self .project + " " + self .mapFile
@@ -77,7 +80,7 @@ def writeMapSection(self):
77
80
self .outFile .write (" NAME " + self .mapName + "\n " )
78
81
self .outFile .write (" # Map image size\n " )
79
82
self .outFile .write (" SIZE " + self .width + " " + self .height + "\n " )
80
- self .outFile .write (" UNITS ' " + self .units + "' \n " )
83
+ self .outFile .write (" UNITS " + self .units . lower () + "\n " )
81
84
self .outFile .write ("\n " )
82
85
# extents
83
86
xmin = self .qgs .getElementsByTagName ("xmin" )
@@ -97,7 +100,7 @@ def writeMapSection(self):
97
100
# Write the OUTPUTFORMAT section
98
101
def writeOutputFormat (self ):
99
102
self .outFile .write (" # Background color for the map canvas -- change as desired\n " )
100
- self .outFile .write (" IMAGECOLOR 255 255 255 \n " )
103
+ self .outFile .write (" IMAGECOLOR 192 192 192 \n " )
101
104
self .outFile .write (" IMAGETYPE " + self .imageType + "\n " )
102
105
self .outFile .write (" OUTPUTFORMAT\n " )
103
106
self .outFile .write (" NAME " + self .imageType + "\n " )
@@ -155,30 +158,34 @@ def writeWebSection(self):
155
158
self .outFile .write (" # Template and header/footer settings\n " )
156
159
self .outFile .write (" # Only the template parameter is required to display a map. See MapServer documentation\n " )
157
160
158
- self .outFile .write (" TEMPLATE " + self .template + "\n " )
159
- self .outFile .write (" HEADER " + self .header + "\n " )
160
- self .outFile .write (" FOOTER " + self .footer + "\n " )
161
+ self .outFile .write (" TEMPLATE ' " + self .template + "' \n " )
162
+ self .outFile .write (" HEADER ' " + self .header + "' \n " )
163
+ self .outFile .write (" FOOTER ' " + self .footer + "' \n " )
161
164
self .outFile .write (" END\n " )
162
165
163
166
# Write the map layers
164
167
def writeMapLayers (self ):
165
168
# get the list of maplayer nodes
166
169
maplayers = self .qgs .getElementsByTagName ("maplayer" )
170
+ print "Processing " , len (maplayers ), " layers"
171
+ count = 0
167
172
for lyr in maplayers :
173
+ count += 1
174
+ print "Processing layer " , count
168
175
# The attributes of the maplayer tag contain the scale dependent settings,
169
176
# visibility, and layer type
170
177
171
178
self .outFile .write (" LAYER\n " )
172
179
# write the name of the layer
173
180
self .outFile .write (" NAME '" + lyr .getElementsByTagName ("layername" )[0 ].childNodes [0 ].nodeValue .encode () + "'\n " )
174
- self .outFile .write (" TYPE '" + lyr .getAttribute ("geometry" ).encode () + "'\n " )
181
+ self .outFile .write (" TYPE " + lyr .getAttribute ("geometry" ).encode ().upper () + "\n " )
182
+ # data
183
+ self .outFile .write (" DATA '" + lyr .getElementsByTagName ("datasource" )[0 ].childNodes [0 ].nodeValue .encode () + "'\n " )
175
184
self .outFile .write (" STATUS ON\n " )
176
185
self .outFile .write (" PROJECTION\n " )
177
186
proj4Text = lyr .getElementsByTagName ("proj4" )[0 ].childNodes [0 ].nodeValue .encode ()
178
187
self .outFile .write (self .formatProj4 (proj4Text ))
179
-
180
-
181
-
188
+ self .outFile .write (" END\n " )
182
189
scaleDependent = lyr .getAttribute ("scaleBasedVisibilityFlag" ).encode ()
183
190
if scaleDependent == '1' :
184
191
# get the min and max scale settings
@@ -188,10 +195,70 @@ def writeMapLayers(self):
188
195
self .outFile .write (" MINSCALE " + minscale + "\n " )
189
196
if maxscale > '' :
190
197
self .outFile .write (" MAXSCALE " + maxscale + "\n " )
191
-
198
+ # write the CLASS section for rendering
199
+ # First see if there is a single symbol renderer
200
+ if lyr .getElementsByTagName ("singlesymbol" ).length > 0 :
201
+ symbolNode = lyr .getElementsByTagName ("singlesymbol" )[0 ].getElementsByTagName ('symbol' )[0 ]
202
+ self .simpleRenderer (lyr , symbolNode )
203
+ elif lyr .getElementsByTagName ("graduatedsymbol" ).length > 0 :
204
+ self .graduatedRenderer (lyr , lyr .getElementsByTagName ("graduatedsymbol" )[0 ].getElementsByTagName ('symbol' )[0 ] )
205
+ # end of CLASS
206
+ self .outFile .write (" END\n " )
207
+ # end of LAYER
192
208
self .outFile .write (" END\n " )
193
209
210
+ # Simple renderer ouput
211
+ # We need the layer node and symbol node
212
+ def simpleRenderer (self , layerNode , symbolNode ):
213
+ # symbology depends on the feature type and the .qgs file
214
+ # contains the same markup for a layer regardless of type
215
+ # so we infer a symbol type based on the geometry
216
+ geometry = layerNode .getAttribute ("geometry" ).encode ().upper ()
217
+ if geometry == 'POLYGON' :
218
+ symbol = '0'
219
+ elif geometry == 'LINE' :
220
+ symbol = '0'
221
+ elif geometry == 'POINT' :
222
+ symbol = qgisSymbols [symbolNode .getElementsByTagName ('pointsymbol' )[0 ].childNodes [0 ].nodeValue .encode ()]
223
+
224
+ self .outFile .write (" CLASS\n " )
225
+ # use the point symbol map to lookup the mapserver symbol type
226
+ self .outFile .write (" SYMBOL " + symbol + " \n " )
227
+ self .outFile .write (" SIZE "
228
+ + symbolNode .getElementsByTagName ('pointsize' )[0 ].childNodes [0 ].nodeValue .encode ()
229
+ + " \n " )
230
+ # outline color
231
+ outlineNode = symbolNode .getElementsByTagName ('outlinecolor' )[0 ]
232
+ self .outFile .write (" OUTLINECOLOR "
233
+ + outlineNode .getAttribute ('red' ) + ' '
234
+ + outlineNode .getAttribute ('green' ) + ' '
235
+ + outlineNode .getAttribute ('blue' )
236
+ + "\n " )
237
+ # color
238
+ colorNode = symbolNode .getElementsByTagName ('fillcolor' )[0 ]
239
+ self .outFile .write (" COLOR "
240
+ + colorNode .getAttribute ('red' ) + ' '
241
+ + colorNode .getAttribute ('green' ) + ' '
242
+ + colorNode .getAttribute ('blue' )
243
+ + "\n " )
244
+
245
+
246
+ # Graduated symbol renderer output
247
+ def graduatedRenderer (self , layerNode , symbolNode ):
248
+ # get the renderer field for building up the classes
249
+ classField = layerNode .getElementsByTagName ('classificationattribute' )[0 ].childNodes [0 ].nodeValue .encode ()
250
+ # write the render item
251
+ self .outFile .write (" CLASSITEM " + classField + "\n " )
194
252
253
+ # write the rendering info for each class
254
+ classes = layerNode .getElementsByTagName ('symbol' )
255
+ for cls in classes :
256
+ self .outFile .write (cls .getElementsByTagName ('lowervalue' )[0 ].childNodes [0 ].nodeValue .encode ())
257
+
258
+
259
+
260
+ # do something here
261
+
195
262
# Utility method to format a proj4 text string into mapserver format
196
263
def formatProj4 (self , proj4text ):
197
264
parms = proj4text .split (" " )
0 commit comments