2011-04-19 34 views
0
print 'Content-Type: application/vnd.google-earth.kml+xml' 
print 'Content-Disposition: attachment; filename=BRC_Services.kml\n' 
import _mssql 
import cgi 

fs = cgi.FieldStorage() 

BRCTerritories=str(fs['BRCTerritories'].value) 
Sector=str(fs['Sector'].value) 

if BRCTerritories[1]==".": 
    menusearch = {"BRC_Area":BRCTerritories[0:3],"Sector":str(fs['Sector'].value)} 

else: 
    menusearch = {"BRC_Territory":BRCTerritories[0:1],"Sector":str(fs['Sector'].value)} 

if BRCTerritories[1]=="l": 
    menusearch = {"Sector":str(fs['Sector'].value)} 

if Sector[1]=="l" and BRCTerritories[1]=="-": 
    menusearch = {"BRC_Territory":BRCTerritories[0:1]} 

if Sector[1]=="l" and BRCTerritories[1]==".": 
    menusearch = {"BRC_Area":BRCTerritories[0:3]} 

query='SELECT * FROM dbo.Services' 

conn = _mssql.connect(server='gisdata', user='gisdata', password='gisdata', database='GISData') 
addition=1 

## KML Header 

kmlheader = (
    '<?xml version="1.0" encoding="UTF-8"?>\n' 
    '<kml xmlns="http://www.opengis.net/kml/2.2">\n' 
    '<Document>\n' 
    '<StyleMap id="msn_label">\n' 
    '<Pair>\n' 
    '<key>normal</key>\n' 
    '<styleUrl>#sn_label</styleUrl>\n' 
    '</Pair>\n' 
    '<Pair>\n' 
    '<key>highlight</key>\n' 
    '<styleUrl>#sh_label</styleUrl>\n' 
    '</Pair>\n' 
    '</StyleMap>\n' 

    '<Style id="sn_label">\n' 
    '<LabelStyle>\n' 
    '<scale>1</scale>\n' 
    '</LabelStyle>\n' 
    '<IconStyle><scale>1</scale><Icon><href>http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png</href></Icon></IconStyle>\n' 
    '</Style>\n' 

    '<Style id="sh_label">\n' 
    '<LabelStyle>\n' 
    '<scale>1</scale>\n' 
    '</LabelStyle>\n' 
    '<IconStyle><scale>2</scale><Icon><href>http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png</href></Icon></IconStyle>\n' 
    '</Style>\n' 
    ) 

name="<name>BRC Services Search Results</name>" 

print kmlheader 
print name 

for term in menusearch: 

    if addition==1: 
     addition=0 
     add= (
      " WHERE %s='%s'" 
      ) %(term,menusearch[term]) 
    else: 
     add= (
      " AND %s='%s'" 
      ) %(term,menusearch[term]) 
    query=query+add 

results = [ row for row in conn ] 

print "<Folder><name>BRC Services</name>" 
print "<name><b>Search=</b>" 
print menusearch 
print "</name>" 

for row in results: 

    description = (
     '<h2>%s</h2><br /><br />\n' 
     '<b>Postcode:</b> %s<br />\n' 
     '<b>Sector:</b> %s<br />\n' 
     '<b>Phone:</b> %s<br />\n' 
     '<b>BRC_Territory:</b> %s<br />\n' 
     '<b>BRC_Area:</b> %s<br />\n' 
     '<b>Web:</b> %s<br />\n'  
     ) %(row['Sector'],row['postcode'],row['Sector'],row['phone'],row['BRC_Territory'],row['BRC_Area'],row['web']) 

    placemark = (
     '<Placemark>\n' 
     '<name>%s</name>\n' 
     '<description><![CDATA[%s]]></description>\n' 
     '<styleUrl>#msn_label</styleUrl>\n' 
     '<Style>\n' 
     '<BalloonStyle>\n' 
     '<color>ffffffff</color>' 
     '<text><![CDATA[<font color="Black">$[description]</font>]]></text>' 
     '</BalloonStyle>\n' 
     '</Style>\n' 
     '<Point>\n' 
     '<coordinates>%s,%s</coordinates>\n' 
     '</Point>\n' 
     '</Placemark>\n\n' 
     ) %(row['Sector'],description,row['Lat'],row['Lon']) 
    print placemark 

print "</Folder>" 

kmlfooter = '</Document>\n</kml>' 

print kmlfooter 

conn.close 

回答

0

更改基本字符串到這一點:

query='SELECT * FROM dbo.Services where 1=1' 

現在你不需要基礎上,在那裏初始化「如果相加= 1」邏輯;你可以迭代所有菜單搜索條件爲「和」。

測試標準是否爲給定的標記值,如果是這樣的話,清空menusearch項集合,那麼沒有其他附加操作。

相關問題