⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ms_export.py

📁 一个非常好的GIS开源新版本
💻 PY
📖 第 1 页 / 共 2 页
字号:
#***************************************************************************#    ms_export.py#    --------------------------------------#   Date                 : Sun Sep 16 12:33:46 AKDT 2007#   Copyright            : (C) 2008 by Gary E. Sherman#   Email                : sherman at mrcc dot com#***************************************************************************#*                                                                         *#*   This program is free software; you can redistribute it and/or modify  *#*   it under the terms of the GNU General Public License as published by  *#*   the Free Software Foundation; either version 2 of the License, or     *#*   (at your option) any later version.                                   *#*                                                                         *#***************************************************************************/# This class exports a QGIS project file to a mapserver .map file.# All the work is done in the writeMapFile method. The msexport binary# presents a Qt based GUI that collects the needed information for this# script. ## CHANGES SHOULD NOT BE MADE TO THE writeMapFile METHOD UNLESS YOU# ARE CHANGING THE QgsMapserverExport CLASS AND YOU KNOW WHAT YOU ARE# DOINGimport sys import osfrom string import *from xml.dom import minidom, Node# symbol mapqgisSymbols = {'hard:circle'   : 'CIRCLE',               'hard:triangle' : 'TRIANGLE'}class Qgis2Map:  def __init__(self, projectFile, mapFile):    self.project = projectFile    self.mapFile = mapFile    # create the DOM     self.qgs = minidom.parse(projectFile)    # init the other members that are not set by the constructor    self.units = ''    self.imageType = ''    self.mapName = ''    self.width = ''    self.height = ''    self.minScale = ''    self.maxScale = ''    self.template = ''    self.header = ''    self.footer = ''    self.symbolQueue = {}      # Set the options collected from the GUI  def setOptions(self, units, image, mapname, width, height, template, header, footer):    self.units = units    self.imageType = str(image)    self.mapName = mapname    self.width = width    self.height = height    #self.minScale = minscale    #self.maxScale = maxscale    self.template = template    self.header = header    self.footer = footer    print units, image, mapname, width, height, template, header, footer  ## All real work happens here by calling methods to write the  ## various sections of the map file  def writeMapFile(self):    # open the output file    print "creating the map file"    self.outFile = open(self.mapFile, 'w')    # write the general map and web settings    print " --- python : map section "    self.writeMapSection()    logmsg =  "Wrote map section\n"    print " --- python : map section done"    # write the projection section    print " --- python : proj section "    self.writeProjectionSection()    logmsg += "Wrote projection section\n"    print " --- python : proj section done"    # write the output format section    print " --- python : outputformat section "    self.writeOutputFormat()    logmsg += "Wrote output format section\n"    print " --- python : outputformat section done"    # write the legend section    print " --- python : legend section"    self.writeLegendSection()    logmsg += "Wrote legend section\n"    print " --- python : legend section done"    # write the WEB section    print " --- python : web section "    self.writeWebSection()    logmsg += "Wrote web section\n"    print " --- python : web section done"    # write the LAYER sections    print " --- python : layer section "    self.writeMapLayers()    logmsg += "Wrote map layers\n"    print " --- python : layer section done"    # write the symbol defs section    # must happen after layers so we can build a symbol queue    print " --- python : symbol section "    self.writeSymbolSection()    logmsg += "Wrote symbol section\n"    print " --- python : symbol section done"    # END and close the map file    self.outFile.write("END")    self.outFile.close()    logmsg += "Map file completed for " + self.project + "\n"    logmsg += "Map file saved as " + self.mapFile + "\n"    return logmsg  # Write the general parts of the map section  def writeMapSection(self):    self.outFile.write("# Map file created from QGIS project file " + self.project + "\n")    self.outFile.write("# Edit this file to customize for your map interface\n")    self.outFile.write("# (Created with PyQgis MapServer Export plugin)\n")    self.outFile.write("MAP\n")    self.outFile.write("  NAME " + self.mapName + "\n")    self.outFile.write("  # Map image size\n")    self.outFile.write("  SIZE " + self.width + " " + self.height + "\n")    self.outFile.write("  UNITS %s\n" % (self.units))    self.outFile.write("\n")    # extents    xmin = self.qgs.getElementsByTagName("xmin")    self.outFile.write("  EXTENT ")    self.outFile.write(xmin[0].childNodes[0].nodeValue.encode('utf-8'))    self.outFile.write(" ")    ymin = self.qgs.getElementsByTagName("ymin")    self.outFile.write(ymin[0].childNodes[0].nodeValue.encode('utf-8'))    self.outFile.write(" ")    xmax = self.qgs.getElementsByTagName("xmax")    self.outFile.write(xmax[0].childNodes[0].nodeValue.encode('utf-8'))    self.outFile.write(" ")    ymax = self.qgs.getElementsByTagName("ymax")    self.outFile.write(ymax[0].childNodes[0].nodeValue.encode('utf-8'))    self.outFile.write("\n")  # Write the OUTPUTFORMAT section  def writeOutputFormat(self):    self.outFile.write("  # Background color for the map canvas -- change as desired\n")    self.outFile.write("  IMAGECOLOR 192 192 192\n")    self.outFile.write("  IMAGEQUALITY 95\n")    self.outFile.write("  IMAGETYPE " + self.imageType + "\n")    self.outFile.write("  OUTPUTFORMAT\n")    self.outFile.write("    NAME " + self.imageType + "\n")    self.outFile.write("    DRIVER 'GD/" + self.imageType.upper() + "'\n")    self.outFile.write("    MIMETYPE 'image/" + lower(self.imageType) + "'\n")    self.outFile.write("    #IMAGEMODE PC256\n")    self.outFile.write("    EXTENSION '" + lower(self.imageType) + "'\n")    self.outFile.write("  END\n")      # Write Projection section  def writeProjectionSection(self):    # Need to get the destination srs from one of the map layers since    # the project file doesn't contain the epsg id or proj4 text for     # the map apart from that defined in each layer    self.outFile.write("  PROJECTION\n")    # Get the proj4 text from the first map layer's destination SRS    destsrs = self.qgs.getElementsByTagName("destinationsrs")[0]     proj4Text = destsrs.getElementsByTagName("proj4")[0].childNodes[0].nodeValue.encode('utf-8')     # the proj4 text string needs to be reformatted to make mapserver happy    self.outFile.write(self.formatProj4(proj4Text))    self.outFile.write("  END\n\n")  # Write the LEGEND section  def writeLegendSection(self):    self.outFile.write("  # Legend\n")    self.outFile.write("  LEGEND\n")    self.outFile.write("      IMAGECOLOR 255 255 255\n")    self.outFile.write("    STATUS ON\n")    self.outFile.write("    KEYSIZE 18 12\n")    self.outFile.write("    LABEL\n")    self.outFile.write("      TYPE BITMAP\n")    self.outFile.write("      SIZE MEDIUM\n")    self.outFile.write("      COLOR 0 0 89\n")    self.outFile.write("    END\n")    self.outFile.write("  END\n\n")      # Write the symbol definitions  def writeSymbolSection(self):    for symbol in self.symbolQueue.keys():      self.outFile.write( self.symbolQueue[symbol] )      self.outFile.write( "\n" )  # Write the WEB section of the map file  def writeWebSection(self):    self.outFile.write("  # Web interface definition. Only the template parameter\n")    self.outFile.write("  # is required to display a map. See MapServer documentation\n")    self.outFile.write("  WEB\n")    self.outFile.write("    # Set IMAGEPATH to the path where MapServer should\n")    self.outFile.write("    # write its output.\n")    self.outFile.write("    IMAGEPATH '/tmp/'\n")    self.outFile.write("\n")    self.outFile.write("    # Set IMAGEURL to the url that points to IMAGEPATH\n")    self.outFile.write("    # as defined in your web server configuration\n")    self.outFile.write("    IMAGEURL '/tmp/'\n")    self.outFile.write("\n")    # TODO allow user to configure this    self.outFile.write("    # WMS server settings\n")    self.outFile.write("    METADATA\n")    self.outFile.write("      'wms_title'           '" + self.mapName + "'\n")    self.outFile.write("      'wms_onlineresource'  'http://my.host.com/cgi-bin/mapserv?map=wms.map&'\n")    self.outFile.write("      'wms_srs'             'EPSG:4326'\n")    self.outFile.write("    END\n\n")    self.outFile.write("    #Scale range at which web interface will operate\n")    if self.minScale != "":      self.outFile.write("    MINSCALE " + self.minScale + "\n")     if self.maxScale != "":      self.outFile.write("    MAXSCALE " + self.maxScale + "\n")     self.outFile.write("    # Template and header/footer settings\n")    self.outFile.write("    # Only the template parameter is required to display a map. See MapServer documentation\n")        if self.template != "":      self.outFile.write("    TEMPLATE '" + self.template + "'\n")    if self.header != "":      self.outFile.write("    HEADER '" + self.header + "'\n")    if self.footer != "":      self.outFile.write("    FOOTER '" + self.footer + "'\n")    self.outFile.write("  END\n\n")  def parsePostgisConnection( self, dataString ):    pg = {}    pg['host'] = 'localhost'    pg['dbname'] = 'gisdata'    pg['user'] = ''    pg['password'] = ''    pg['table'] = ''    pg['geom'] = 'the_geom'            whereCondition = dataString.split("sql")[1][1:]    cmp = dataString.split("sql")[0].split(" ")        for c in cmp:      if c[:1] == "(":        pg['geom'] = c[1:][:-1]      else:        kvp = c.split("=")        if (len(kvp) >= 2):          pg[kvp[0]] =  kvp[1]        connString = 'host=' + pg['host'] + " user=" + pg['user']        if (len(pg['password'].replace("\'", "")) > 0):      connString += " password=" + pg['password'].replace("'", "")        connString += " dbname=" + pg['dbname']         dataString = pg['geom'] + " FROM " + pg['table'].replace("\"", "")    filterString = whereCondition.replace("\"", "")    return (connString, dataString, filterString)         # Write the map layers  def writeMapLayers(self):    # get the list of maplayer nodes    maplayers = self.qgs.getElementsByTagName("maplayer")    print "Processing ", len(maplayers), " layers"    count = 0    for lyr in maplayers:      count += 1      print "Processing layer ", count       # The attributes of the maplayer tag contain the scale dependent settings,      # visibility, and layer type      self.outFile.write("  LAYER\n")      # write the name of the layer      # first check to see if there is a name      if len(lyr.getElementsByTagName("layername")[0].childNodes) > 0:        self.outFile.write("    NAME '" + lyr.getElementsByTagName("layername")[0].childNodes[0].nodeValue.encode('utf-8').replace("\"", "") + "'\n")      else:        self.outFile.write("    NAME 'LAYER%s'\n" % count)      if lyr.getAttribute("type").encode('utf-8') == 'vector':          self.outFile.write("    TYPE " + lyr.getAttribute("geometry").encode('utf-8').upper() + "\n")      elif lyr.getAttribute("type").encode('utf-8') == 'raster':          self.outFile.write("    TYPE " + lyr.getAttribute("type").encode('utf-8').upper() + "\n")       # Set min/max scales      if lyr.getAttribute('scaleBasedVisibilityFlag').encode('utf-8') == 1:        self.outFile.write("    MINSCALE " + lyr.getAttribute('minScale').encode('utf-8') + "\n")        self.outFile.write("    MAXSCALE " + lyr.getAttribute('maxScale').encode('utf-8') + "\n")      # data      dataString = lyr.getElementsByTagName("datasource")[0].childNodes[0].nodeValue.encode('utf-8')      # test if it is a postgis, grass or WMS layer      # is there a better way to do this? probably.      try:        providerString = lyr.getElementsByTagName("provider")[0].childNodes[0].nodeValue.encode('utf-8')      except:        # if providerString is null        providerString = ''      if providerString == 'postgres':        # it's a postgis layer        (pgConnString, sqlData, sqlFilter) = self.parsePostgisConnection(dataString)        self.outFile.write("    CONNECTIONTYPE postgis\n")        self.outFile.write("    CONNECTION '" + pgConnString + "'\n")        self.outFile.write("    DATA '" + sqlData + "'\n")        if sqlFilter:          self.outFile.write("    FILTER '" + sqlFilter + "'\n")      elif providerString == 'wms' and lyr.getAttribute("type").encode('utf-8').upper() == 'RASTER':        # it's a WMS layer         self.outFile.write("    CONNECTIONTYPE WMS\n")        self.outFile.write("    CONNECTION '" + dataString + "'\n")        rasterProp = lyr.getElementsByTagName("rasterproperties")[0]        # loop thru wmsSubLayers          wmsSubLayers = rasterProp.getElementsByTagName('wmsSublayer')        wmsNames = []        wmsStyles = []        for wmsLayer in wmsSubLayers:           wmsNames.append( wmsLayer.getElementsByTagName('name')[0].childNodes[0].nodeValue.encode('utf-8').replace("\"", "") )          try:             wmsStyles.append( wmsLayer.getElementsByTagName('style')[0].childNodes[0].nodeValue.encode('utf-8') )          except:            wmsStyles.append( '' )        # Create necesssary wms metadata        format = rasterProp.getElementsByTagName('wmsFormat')[0].childNodes[0].nodeValue.encode('utf-8')        self.outFile.write("    METADATA\n")        self.outFile.write("      'wms_name' '" + ','.join(wmsNames) + "'\n")        self.outFile.write("      'wms_server_version' '1.1.1'\n")        try:          ct = lyr.getElementsByTagName('coordinatetransform')[0]          srs = ct.getElementsByTagName('sourcesrs')[0].getElementsByTagName('spatialrefsys')[0]          epsg = srs.getElementsByTagName('epsg')[0].childNodes[0].nodeValue.encode('utf-8')          self.outFile.write("      'wms_srs' 'EPSG:4326 EPSG:" + epsg + "'\n")        except:	  pass        self.outFile.write("      'wms_format' '" + format + "'\n")        self.outFile.write("      'wms_style' '" + ','.join(wmsStyles) + "'\n")        self.outFile.write("    END\n")      else:         # its a standard ogr, gdal or grass layer        self.outFile.write("    DATA '" + dataString + "'\n")            # WMS settings for all layers      self.outFile.write("    METADATA\n")      if len(lyr.getElementsByTagName("layername")[0].childNodes) > 0:        self.outFile.write("      'wms_title' '"            + lyr.getElementsByTagName("layername")[0].childNodes[0].nodeValue.encode('utf-8').replace("\"", "") + "'\n")      else:        self.outFile.write("      'wms_title' 'LAYER%s'\n"  % count)      self.outFile.write("    END\n")      self.outFile.write("    STATUS DEFAULT\n")

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -