@@ -128,7 +128,7 @@ def __init__(self, projectFile, mapFile):
128128
129129
130130 # Set the options collected from the GUI
131- def setOptions (self , msUrl , units , image , mapname , width , height , template , header , footer , dump , force , antialias , partials , fontsPath , symbolsPath ):
131+ def setOptions (self , msUrl , units , image , mapname , width , height , template , header , footer , dump , force , antialias , partials , exportLayersOnly , fontsPath , symbolsPath ):
132132 if msUrl .encode ('utf-8' ) != "" :
133133 self .mapServerUrl = msUrl .encode ('utf-8' )
134134
@@ -155,10 +155,11 @@ def setOptions(self, msUrl, units, image, mapname, width, height, template, head
155155 self .footer = footer .encode ('utf-8' )
156156 #print units, image, mapname, width, height, template, header, footer
157157
158- self .dump = bool2str [dump ]
159- self .force = bool2str [force ]
160- self .antialias = bool2str [antialias ]
161- self .partials = bool2str [partials ]
158+ self .dump = bool2str [dump ]
159+ self .force = bool2str [force ]
160+ self .antialias = bool2str [antialias ]
161+ self .partials = bool2str [partials ]
162+ self .exportLayersOnly = exportLayersOnly
162163
163164
164165 ## All real work happens here by calling methods to write the
@@ -167,50 +168,55 @@ def writeMapFile(self):
167168 # open the output file
168169 print "creating the map file"
169170 self .outFile = open (self .mapFile , 'w' )
170- # write the general map and web settings
171- print " --- python : map section "
172- self .writeMapSection ()
173- logmsg = "Wrote map section\n "
174- print " --- python : map section done"
175- # write the projection section
176- print " --- python : proj section "
177- self .writeProjectionSection ()
178- logmsg += "Wrote projection section\n "
179- print " --- python : proj section done"
180- # write the output format section
181- print " --- python : outputformat section "
182- self .writeOutputFormat ()
183- logmsg += "Wrote output format section\n "
184- print " --- python : outputformat section done"
185- # write the legend section
186- print " --- python : legend section"
187- self .writeLegendSection ()
188- logmsg += "Wrote legend section\n "
189- print " --- python : legend section done"
190-
191- # write the WEB section
192- print " --- python : web section "
193- self .writeWebSection ()
194- logmsg += "Wrote web section\n "
195- print " --- python : web section done"
171+ logmsg = "Starting\n "
172+
173+ if self .exportLayersOnly == False :
174+ # write the general map and web settings
175+ print " --- python : map section "
176+ self .writeMapSection ()
177+ logmsg += "Wrote map section\n "
178+ print " --- python : map section done"
179+ # write the projection section
180+ print " --- python : proj section "
181+ self .writeProjectionSection ()
182+ logmsg += "Wrote projection section\n "
183+ print " --- python : proj section done"
184+ # write the output format section
185+ print " --- python : outputformat section "
186+ self .writeOutputFormat ()
187+ logmsg += "Wrote output format section\n "
188+ print " --- python : outputformat section done"
189+ # write the legend section
190+ print " --- python : legend section"
191+ self .writeLegendSection ()
192+ logmsg += "Wrote legend section\n "
193+ print " --- python : legend section done"
194+
195+ # write the WEB section
196+ print " --- python : web section "
197+ self .writeWebSection ()
198+ logmsg += "Wrote web section\n "
199+ print " --- python : web section done"
196200
197201 # write the LAYER sections
198202 print " --- python : layer section "
199- self .writeMapLayers ()
203+ layersMsg = self .writeMapLayers ()
200204 logmsg += "Wrote map layers\n "
205+ logmsg += layersMsg
201206 print " --- python : layer section done"
202207
203- # we use an external synbol set instead
204- # write the symbol defs section
205- # must happen after layers so we can build a symbol queue
206- #print " --- python : symbol section "
207- #self.writeSymbolSection()
208- #logmsg += "Wrote symbol section\n"
209- #print " --- python : symbol section done"
208+ if self .exportLayersOnly == False :
209+ # we use an external synbol set instead
210+ # write the symbol defs section
211+ # must happen after layers so we can build a symbol queue
212+ #print " --- python : symbol section "
213+ #self.writeSymbolSection()
214+ #logmsg += "Wrote symbol section\n"
215+ #print " --- python : symbol section done"
210216
211- # END and close the map file
212- self .outFile .write ("END" )
213- self .outFile .close ()
217+ # END and close the map file
218+ self .outFile .write ("END" )
219+ self .outFile .close ()
214220
215221 logmsg += "Map file completed for " + self .project + "\n "
216222 logmsg += "Map file saved as " + self .mapFile + "\n "
@@ -222,7 +228,7 @@ def writeMapSection(self):
222228 self .outFile .write ("# Edit this file to customize for your map interface\n " )
223229 self .outFile .write ("# (Created with PyQgis MapServer Export plugin)\n " )
224230 self .outFile .write ("MAP\n " )
225- self .outFile .write (" NAME " + self .mapName + "\n " )
231+ self .outFile .write (" NAME \" " + self .mapName + " \ "\n " )
226232 self .outFile .write (" # Map image size\n " )
227233 if self .width == '' or self .height == '' :
228234 self .outFile .write (" SIZE 0 0\n " )
@@ -263,7 +269,7 @@ def getExtentString(self):
263269 # Write the OUTPUTFORMAT section
264270 def writeOutputFormat (self ):
265271 self .outFile .write (" # Background color for the map canvas -- change as desired\n " )
266- self .outFile .write (" IMAGECOLOR 192 192 192 \n " )
272+ self .outFile .write (" IMAGECOLOR 255 255 255 \n " )
267273 self .outFile .write (" IMAGEQUALITY 95\n " )
268274 self .outFile .write (" IMAGETYPE " + self .imageType + "\n " )
269275 self .outFile .write ("\n " )
@@ -368,6 +374,7 @@ def writeWebSection(self):
368374 def writeMapLayers (self ):
369375 # get the list of legend nodes so the layers can be written in the
370376 # proper order
377+ resultMsg = ''
371378 legend_nodes = self .qgs .getElementsByTagName ("legendlayer" )
372379 self .z_order = list ()
373380 for legend_node in legend_nodes :
@@ -384,11 +391,11 @@ def writeMapLayers(self):
384391 # The attributes of the maplayer tag contain the scale dependent settings,
385392 # visibility, and layer type
386393 layer_def = " LAYER\n "
387- # store name of the layer
388- layer_name = lyr .getElementsByTagName ("layername" )[0 ].childNodes [0 ].nodeValue .encode ('utf-8' ).replace ("\" " , "" )
394+ # store name of the layer - replace space with underscore for wms compliance
395+ layer_name = lyr .getElementsByTagName ("layername" )[0 ].childNodes [0 ].nodeValue .encode ('utf-8' ).replace ("\" " , "" ). replace ( " " , "_" )
389396 # first check to see if there is a name
390- if len (lyr . getElementsByTagName ( "layername" )[ 0 ]. childNodes ) > 0 :
391- layer_def += " NAME '" + lyr . getElementsByTagName ( "layername" )[ 0 ]. childNodes [ 0 ]. nodeValue . encode ( 'utf-8' ). replace ( " \" " , "" ) + "' \n "
397+ if len (layer_name ) > 0 :
398+ layer_def += " NAME '%s' \n " % layer_name
392399 else :
393400 # if no name for the layer, manufacture one
394401 layer_def += " NAME 'LAYER%s'\n " % count
@@ -435,6 +442,10 @@ def writeMapLayers(self):
435442 layer_id = lyr .getElementsByTagName ("id" )[0 ].childNodes [0 ].nodeValue .encode ("utf-8" )
436443
437444 uniqueId = self .getPrimaryKey (layer_id , uri .table ())
445+ # %tablename% is returned when no uniqueId is found: inform user
446+ if uniqueId .find ("%" ) >= 0 :
447+ resultMsg += " ! No primary key found for postgres layer '" + layer_name + \
448+ "' \n Make sure you edit the mapfile and change the DATA-string \n containing '" + uniqueId + "'\n "
438449 epsg = self .getEpsg (lyr )
439450
440451 layer_def += " DATA '" + uri .geometryColumn () + " FROM " + uri .quotedTablename () + " USING UNIQUE " + uniqueId + " USING srid=" + epsg + "'\n "
@@ -485,7 +496,6 @@ def writeMapLayers(self):
485496 layer_def += " END\n "
486497
487498 layer_def += " STATUS OFF\n "
488- #layer_def += " STATUS DEFAULT\n"
489499
490500 # turn status in MapServer on or off based on visibility in QGis:
491501# layer_id = lyr.getElementsByTagName("id")[0].childNodes[0].nodeValue.encode("utf-8")
@@ -496,17 +506,14 @@ def writeMapLayers(self):
496506# else:
497507# layer_def += " STATUS OFF\n"
498508
499-
500-
501509 opacity = int ( 100.0 *
502510 float (lyr .getElementsByTagName ("transparencyLevelInt" )[0 ].childNodes [0 ].nodeValue .encode ('utf-8' )) / 255.0 )
503511 layer_def += " TRANSPARENCY " + str (opacity ) + "\n "
504512
505513 layer_def += " PROJECTION\n "
506- # Get the destination srs for this layer and use it to create
507- # the projection section
508- destsrs = self .qgs .getElementsByTagName ("destinationsrs" )[0 ]
509- proj4Text = destsrs .getElementsByTagName ("proj4" )[0 ].childNodes [0 ].nodeValue .encode ('utf-8' )
514+ # Get the data srs for this layer and use it to create the projection section
515+ datasrs = lyr .getElementsByTagName ("srs" )[0 ]
516+ proj4Text = datasrs .getElementsByTagName ("proj4" )[0 ].childNodes [0 ].nodeValue .encode ('utf-8' )
510517 # the proj4 text string needs to be reformatted to make mapserver happy
511518 layer_def += self .formatProj4 (proj4Text )
512519 layer_def += " END\n "
@@ -549,9 +556,10 @@ def writeMapLayers(self):
549556 layer_list [layer_name ] = layer_def
550557 # all layers have been processed, reverse the list and write
551558 # not necessary since z-order is mapped by the legend list order
552- self .z_order .reverse ()
559+ # self.z_order.reverse()
553560 for layer in self .z_order :
554561 self .outFile .write (layer_list [layer ])
562+ return resultMsg
555563
556564
557565 def getEpsg (self , lyr ):
@@ -588,7 +596,7 @@ def getPrimaryKey(self, layerId, tableName):
588596 fidIntegerFields .append (id )
589597
590598 if len (fidIntegerFields ) == 1 :
591- return fields [fidIntegerFields [0 ]].name (). __str__ ( )
599+ return str ( fields [fidIntegerFields [0 ]].name ())
592600
593601 # fid start
594602 fidIntegerFields [:] = []
@@ -597,7 +605,7 @@ def getPrimaryKey(self, layerId, tableName):
597605 fidIntegerFields .append (id )
598606
599607 if len (fidIntegerFields ) == 1 :
600- return fields [fidIntegerFields [0 ]].name (). __str__ ( )
608+ return str ( fields [fidIntegerFields [0 ]].name ())
601609
602610 # id end
603611 idIntegerFields = []
@@ -606,7 +614,7 @@ def getPrimaryKey(self, layerId, tableName):
606614 idIntegerFields .append (id )
607615
608616 if len (idIntegerFields ) == 1 :
609- return fields [idIntegerFields [0 ]].name (). __str__ ( )
617+ return str ( fields [idIntegerFields [0 ]].name ())
610618
611619 # id start
612620 idIntegerFields [:] = []
@@ -615,13 +623,13 @@ def getPrimaryKey(self, layerId, tableName):
615623 idIntegerFields .append (id )
616624
617625 if len (idIntegerFields ) == 1 :
618- return fields [idIntegerFields [0 ]].name (). __str__ ( )
626+ return str ( fields [idIntegerFields [0 ]].name ())
619627
620628 # if we arrive here we have ambiguous or no primary keys
621629 #print "Error: Could not find primary key from field type and field name information.\n"
622630
623631 # using a mapfile pre-processor, the proper id field can be substituted in the following:
624- return "%" + tableName + "_id%"
632+ return str ( "%" + tableName + "_id%" )
625633
626634 # Simple renderer ouput
627635 # We need the layer node and symbol node
0 commit comments