99import sys , string
1010from xml .dom import minidom , Node
1111
12+ # symbol map
13+ qgisSymbols = {'hard:circle' : 'CIRCLE' }
1214class Qgis2Map :
1315 def __init__ (self , projectFile , mapFile ):
1416 self .project = projectFile
@@ -61,7 +63,8 @@ def writeMapFile(self):
6163 # write the LAYER sections
6264 self .writeMapLayers ()
6365
64- # close the map file
66+ # END and close the map file
67+ self .outFile .write ("END" )
6568 self .outFile .close ()
6669
6770 ret = "Writing the map file using " + self .project + " " + self .mapFile
@@ -77,7 +80,7 @@ def writeMapSection(self):
7780 self .outFile .write (" NAME " + self .mapName + "\n " )
7881 self .outFile .write (" # Map image size\n " )
7982 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 " )
8184 self .outFile .write ("\n " )
8285 # extents
8386 xmin = self .qgs .getElementsByTagName ("xmin" )
@@ -97,7 +100,7 @@ def writeMapSection(self):
97100# Write the OUTPUTFORMAT section
98101 def writeOutputFormat (self ):
99102 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 " )
101104 self .outFile .write (" IMAGETYPE " + self .imageType + "\n " )
102105 self .outFile .write (" OUTPUTFORMAT\n " )
103106 self .outFile .write (" NAME " + self .imageType + "\n " )
@@ -155,30 +158,34 @@ def writeWebSection(self):
155158 self .outFile .write (" # Template and header/footer settings\n " )
156159 self .outFile .write (" # Only the template parameter is required to display a map. See MapServer documentation\n " )
157160
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 " )
161164 self .outFile .write (" END\n " )
162165
163166# Write the map layers
164167 def writeMapLayers (self ):
165168 # get the list of maplayer nodes
166169 maplayers = self .qgs .getElementsByTagName ("maplayer" )
170+ print "Processing " , len (maplayers ), " layers"
171+ count = 0
167172 for lyr in maplayers :
173+ count += 1
174+ print "Processing layer " , count
168175 # The attributes of the maplayer tag contain the scale dependent settings,
169176 # visibility, and layer type
170177
171178 self .outFile .write (" LAYER\n " )
172179 # write the name of the layer
173180 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 " )
175184 self .outFile .write (" STATUS ON\n " )
176185 self .outFile .write (" PROJECTION\n " )
177186 proj4Text = lyr .getElementsByTagName ("proj4" )[0 ].childNodes [0 ].nodeValue .encode ()
178187 self .outFile .write (self .formatProj4 (proj4Text ))
179-
180-
181-
188+ self .outFile .write (" END\n " )
182189 scaleDependent = lyr .getAttribute ("scaleBasedVisibilityFlag" ).encode ()
183190 if scaleDependent == '1' :
184191 # get the min and max scale settings
@@ -188,10 +195,70 @@ def writeMapLayers(self):
188195 self .outFile .write (" MINSCALE " + minscale + "\n " )
189196 if maxscale > '' :
190197 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
192208 self .outFile .write (" END\n " )
193209
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 " )
194252
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+
195262# Utility method to format a proj4 text string into mapserver format
196263 def formatProj4 (self , proj4text ):
197264 parms = proj4text .split (" " )
0 commit comments