@@ -128,7 +128,7 @@ def __init__(self, projectFile, mapFile):
128
128
129
129
130
130
# 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 ):
132
132
if msUrl .encode ('utf-8' ) != "" :
133
133
self .mapServerUrl = msUrl .encode ('utf-8' )
134
134
@@ -155,10 +155,11 @@ def setOptions(self, msUrl, units, image, mapname, width, height, template, head
155
155
self .footer = footer .encode ('utf-8' )
156
156
#print units, image, mapname, width, height, template, header, footer
157
157
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
162
163
163
164
164
165
## All real work happens here by calling methods to write the
@@ -167,50 +168,55 @@ def writeMapFile(self):
167
168
# open the output file
168
169
print "creating the map file"
169
170
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"
196
200
197
201
# write the LAYER sections
198
202
print " --- python : layer section "
199
- self .writeMapLayers ()
203
+ layersMsg = self .writeMapLayers ()
200
204
logmsg += "Wrote map layers\n "
205
+ logmsg += layersMsg
201
206
print " --- python : layer section done"
202
207
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"
210
216
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 ()
214
220
215
221
logmsg += "Map file completed for " + self .project + "\n "
216
222
logmsg += "Map file saved as " + self .mapFile + "\n "
@@ -222,7 +228,7 @@ def writeMapSection(self):
222
228
self .outFile .write ("# Edit this file to customize for your map interface\n " )
223
229
self .outFile .write ("# (Created with PyQgis MapServer Export plugin)\n " )
224
230
self .outFile .write ("MAP\n " )
225
- self .outFile .write (" NAME " + self .mapName + "\n " )
231
+ self .outFile .write (" NAME \" " + self .mapName + " \ "\n " )
226
232
self .outFile .write (" # Map image size\n " )
227
233
if self .width == '' or self .height == '' :
228
234
self .outFile .write (" SIZE 0 0\n " )
@@ -263,7 +269,7 @@ def getExtentString(self):
263
269
# Write the OUTPUTFORMAT section
264
270
def writeOutputFormat (self ):
265
271
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 " )
267
273
self .outFile .write (" IMAGEQUALITY 95\n " )
268
274
self .outFile .write (" IMAGETYPE " + self .imageType + "\n " )
269
275
self .outFile .write ("\n " )
@@ -368,6 +374,7 @@ def writeWebSection(self):
368
374
def writeMapLayers (self ):
369
375
# get the list of legend nodes so the layers can be written in the
370
376
# proper order
377
+ resultMsg = ''
371
378
legend_nodes = self .qgs .getElementsByTagName ("legendlayer" )
372
379
self .z_order = list ()
373
380
for legend_node in legend_nodes :
@@ -384,11 +391,11 @@ def writeMapLayers(self):
384
391
# The attributes of the maplayer tag contain the scale dependent settings,
385
392
# visibility, and layer type
386
393
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 ( " " , "_" )
389
396
# 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
392
399
else :
393
400
# if no name for the layer, manufacture one
394
401
layer_def += " NAME 'LAYER%s'\n " % count
@@ -435,6 +442,10 @@ def writeMapLayers(self):
435
442
layer_id = lyr .getElementsByTagName ("id" )[0 ].childNodes [0 ].nodeValue .encode ("utf-8" )
436
443
437
444
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 "
438
449
epsg = self .getEpsg (lyr )
439
450
440
451
layer_def += " DATA '" + uri .geometryColumn () + " FROM " + uri .quotedTablename () + " USING UNIQUE " + uniqueId + " USING srid=" + epsg + "'\n "
@@ -485,7 +496,6 @@ def writeMapLayers(self):
485
496
layer_def += " END\n "
486
497
487
498
layer_def += " STATUS OFF\n "
488
- #layer_def += " STATUS DEFAULT\n"
489
499
490
500
# turn status in MapServer on or off based on visibility in QGis:
491
501
# layer_id = lyr.getElementsByTagName("id")[0].childNodes[0].nodeValue.encode("utf-8")
@@ -496,17 +506,14 @@ def writeMapLayers(self):
496
506
# else:
497
507
# layer_def += " STATUS OFF\n"
498
508
499
-
500
-
501
509
opacity = int ( 100.0 *
502
510
float (lyr .getElementsByTagName ("transparencyLevelInt" )[0 ].childNodes [0 ].nodeValue .encode ('utf-8' )) / 255.0 )
503
511
layer_def += " TRANSPARENCY " + str (opacity ) + "\n "
504
512
505
513
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' )
510
517
# the proj4 text string needs to be reformatted to make mapserver happy
511
518
layer_def += self .formatProj4 (proj4Text )
512
519
layer_def += " END\n "
@@ -549,9 +556,10 @@ def writeMapLayers(self):
549
556
layer_list [layer_name ] = layer_def
550
557
# all layers have been processed, reverse the list and write
551
558
# not necessary since z-order is mapped by the legend list order
552
- self .z_order .reverse ()
559
+ # self.z_order.reverse()
553
560
for layer in self .z_order :
554
561
self .outFile .write (layer_list [layer ])
562
+ return resultMsg
555
563
556
564
557
565
def getEpsg (self , lyr ):
@@ -588,7 +596,7 @@ def getPrimaryKey(self, layerId, tableName):
588
596
fidIntegerFields .append (id )
589
597
590
598
if len (fidIntegerFields ) == 1 :
591
- return fields [fidIntegerFields [0 ]].name (). __str__ ( )
599
+ return str ( fields [fidIntegerFields [0 ]].name ())
592
600
593
601
# fid start
594
602
fidIntegerFields [:] = []
@@ -597,7 +605,7 @@ def getPrimaryKey(self, layerId, tableName):
597
605
fidIntegerFields .append (id )
598
606
599
607
if len (fidIntegerFields ) == 1 :
600
- return fields [fidIntegerFields [0 ]].name (). __str__ ( )
608
+ return str ( fields [fidIntegerFields [0 ]].name ())
601
609
602
610
# id end
603
611
idIntegerFields = []
@@ -606,7 +614,7 @@ def getPrimaryKey(self, layerId, tableName):
606
614
idIntegerFields .append (id )
607
615
608
616
if len (idIntegerFields ) == 1 :
609
- return fields [idIntegerFields [0 ]].name (). __str__ ( )
617
+ return str ( fields [idIntegerFields [0 ]].name ())
610
618
611
619
# id start
612
620
idIntegerFields [:] = []
@@ -615,13 +623,13 @@ def getPrimaryKey(self, layerId, tableName):
615
623
idIntegerFields .append (id )
616
624
617
625
if len (idIntegerFields ) == 1 :
618
- return fields [idIntegerFields [0 ]].name (). __str__ ( )
626
+ return str ( fields [idIntegerFields [0 ]].name ())
619
627
620
628
# if we arrive here we have ambiguous or no primary keys
621
629
#print "Error: Could not find primary key from field type and field name information.\n"
622
630
623
631
# using a mapfile pre-processor, the proper id field can be substituted in the following:
624
- return "%" + tableName + "_id%"
632
+ return str ( "%" + tableName + "_id%" )
625
633
626
634
# Simple renderer ouput
627
635
# We need the layer node and symbol node
0 commit comments