GetMap.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import mapscript
00004 import os
00005 import sys
00006 import getopt
00007 import math
00008 import signal
00009 import pdb
00010 
00011 
00012 def signal_handler(signal, frame):
00013 
00014         print '\nYou pressed Ctrl+C. Closing application'
00015         sys.exit(0)
00016 
00017 class Map:
00018 
00019         def __init__(self,lat,lng,w,h,output_directory,proj,type,size,zoom,layers):
00020                 self.latitude = lat
00021                 self.longitude=lng
00022                 self.width=w
00023                 self.height=h
00024                 self.output_directory=output_directory
00025                 self.projection=proj
00026                 self.type=type
00027                 self.tile_size=size
00028                 self.zoom=zoom
00029                 self.layers=layers
00030                 self.extent_left=-1
00031                 self.extent_bottom=-1
00032                 self.extent_right=-1
00033                 self.extent_top=-1
00034                 self.nb_tiles_y=-1
00035                 self.nb_tiles_x=-1
00036                 self.nb_level=-1
00037                 self.center_North=-1
00038                 self.center_East=-1
00039                 self.available_extension=available_extension
00040                 self.available_layers=available_layers
00041                 
00042         def CheckLayer(self,layer):
00043                 if layer in self.available_layers:
00044                         return True
00045                 else:
00046                         return False
00047                 
00048         def PreProcess(self):
00049         
00050                 self.ComputeNbtiles()
00051                 self.ComputeExtent()
00052                 self.nb_level=self.find_nb_lvl_pyramid(self.nb_tiles_x*self.nb_tiles_y)
00053                 
00054                 
00055         def GetLayerName(self,layer):
00056                 if layer=="bg":
00057                         return "aerial"
00058                 elif layer =="fg":
00059                         return "info"
00060                 elif layer=="sym":
00061                         return "symbolic"
00062                 else :
00063                         print layer+" is not available on http://map.search.ch"
00064                 
00065         def DownloadTile(self):
00066                 
00067                 current_tile_number=1
00068                 
00069                 for layer in self.layers:
00070                 
00071                         if (os.path.exists(self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer))):
00072         
00073                                 for i in range(-int(math.floor(self.nb_tiles_x/2)),int(math.floor(self.nb_tiles_x/2))+1):
00074                                         for j in range(-int(math.floor(self.nb_tiles_y/2)),int(math.floor(self.nb_tiles_y/2))+1):
00075                                                 
00076                                                 #current tile center
00077                                                 center_East_CH=self.center_East+i*self.tile_size*self.zoom
00078                                                 center_North_CH=self.center_North+j*self.tile_size*self.zoom
00079                 
00080                                                 l=center_East_CH-self.tile_size*self.zoom/2
00081                                                 b=center_North_CH-self.tile_size*self.zoom/2
00082                                                 r=center_East_CH+self.tile_size*self.zoom/2
00083                                                 t=center_North_CH+self.tile_size*self.zoom/2
00084                 
00085                                                 lng_WGS=CHtoWGSlng(center_East_CH,center_North_CH)
00086                                                 lat_WGS=CHtoWGSlat(center_East_CH,center_North_CH)
00087 
00088                                 
00089                                                 if not os.path.isfile(self.output_directory+'/'+str(self.zoom)+'/'+self.GetLayerName(layer)+'/original_tiles/'+layer+'_'+str(l)+'_'+str(b)+'_'+str(r)+'_'+str(t)+'_'+str(self.tile_size)+'.'+self.type):
00090                                                         bashCommand= 'wget -nv --tries=3 -U "SwissMap" --referer="map.search.ch" --output-document='+self.output_directory+'/'+str(self.zoom)+'/'+self.GetLayerName(layer)+'/original_tiles/'+layer+'_'+str(l)+'_'+str(b)+'_'+str(r)+'_'+str(t)+'_'+str(self.tile_size)+'.'+self.type+' "http://map.search.ch/chmap.en.'+self.type+'?x=0m&y=0m&w='+str(self.tile_size)+'&h='+str(self.tile_size)+'&base='+str(lat_WGS)+','+str(lng_WGS)+'&layer='+str(layer)+'&zd='+str(self.zoom)+'&n=0" -o log.txt' 
00091                                                         try:
00092                                                                 os.system(bashCommand)
00093                                                                 print("Download progress :"+str(math.floor(float((current_tile_number*100))/float((self.nb_tiles_x*self.nb_tiles_y*len(self.layers)))*100)/100)+" %")
00094                                                                 current_tile_number+=1
00095                                                         except:
00096                                                                 sys.exit(0)
00097                                                                 return
00098                                                 else:
00099                                                         print("Download progress :"+str(math.floor(float((current_tile_number*100))/float((self.nb_tiles_x*self.nb_tiles_y*len(self.layers)))*100)/100)+" %")
00100                                                         current_tile_number+=1
00101                                                 #else:
00102                                                         #print self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer)+"/original_tiles/"+layer+'_'+str(l)+'_'+str(b)+'_'+str(r)+'_'+str(t)+'_'+str(self.tile_size)+'.'+self.type+" already exists"
00103                                                 
00104                                 print ("All tiles for layer "+str(layer)+" downloaded" )
00105                         else:
00106                                 print (output_directory+"/"+str(zoom)+"/"+GetLayerName(layer)+" does not exist")
00107                 
00108         def ComputeExtent(self):
00109                 
00110                 self.center_North=WGStoCHx(self.latitude,self.longitude)
00111                 self.center_East=WGStoCHy(self.latitude,self.longitude)
00112                 #desired extent
00113                 left=self.center_East-self.width/2
00114                 bottom=self.center_North-self.height/2
00115                 right=self.center_East+self.width/2
00116                 top=self.center_North+self.height/2
00117                 
00118                 #Map extent
00119                 self.extent_left=self.center_East-math.floor(self.nb_tiles_y/2)*self.tile_size*self.zoom-self.tile_size*self.zoom/2
00120                 self.extent_bottom=self.center_North-math.floor(self.nb_tiles_x/2)*self.tile_size*self.zoom-self.tile_size*self.zoom/2
00121                 self.extent_right=self.center_East+math.floor(self.nb_tiles_y/2)*self.tile_size*self.zoom+self.tile_size*self.zoom/2
00122                 self.extent_top=self.center_North+math.floor(self.nb_tiles_x/2)*self.tile_size*self.zoom+self.tile_size*self.zoom/2
00123                 
00124                 
00125         def ComputeNbtiles(self):
00126                 self.nb_tiles_y=int(math.ceil(self.width/(self.tile_size*self.zoom)))
00127                 self.nb_tiles_x=int(math.ceil(self.height/(self.tile_size*self.zoom)))
00128                 #Number of tile defined in order to always have a tile centered on lat,long coordinates
00129                 if not self.nb_tiles_y%2:
00130                         self.nb_tiles_y+=1
00131                 if not self.nb_tiles_x%2:
00132                         self.nb_tiles_x+=1
00133                 
00134         
00135         def BuildMapFile(self):
00136                 #MAP
00137                 empty_map = mapscript.mapObj()
00138                 #Name
00139                 empty_map.name="MAP"
00140                 #Projection
00141                 empty_map.projection=self.projection
00142                 #Extension
00143 #               print([self.extent_left,self.extent_bottom,self.extent_right,self.extent_top])
00144                 empty_map.setExtent(int(self.extent_left),int(self.extent_bottom),int(self.extent_right),int(self.extent_top))
00145                 #Size
00146                 empty_map.setSize(int((self.extent_right-self.extent_left)/self.zoom),int((self.extent_top-self.extent_bottom)/self.zoom));
00147                 #select first ouput format
00148                 empty_map.selectOutputFormat("gif")
00149                 #create second OutputFormat for png
00150                 of2 = mapscript.outputFormatObj('GD/PNG')
00151                 of2.setExtension('png')
00152                 of2.setMimetype('image/png')
00153                 of2.imagemode = mapscript.MS_IMAGEMODE_RGB
00154                 of2.transparent=mapscript.MS_TRUE
00155                 #create third OutputFormat for Gtif
00156                 of3 = mapscript.outputFormatObj('GDAL/GTiff')
00157                 of3.setExtension('tif')
00158                 of3.setMimetype('image/tiff')
00159                 of3.imagemode = mapscript.MS_IMAGEMODE_RGB
00160                 empty_map.appendOutputFormat(of2)
00161                 empty_map.appendOutputFormat(of3)
00162                 
00163                 #Web
00164                 empty_map.web.metadata.set("wms_title","WMS Server")
00165                 empty_map.web.metadata.set("wms_onlineresource","http://localhost/cgi-bin/mapserv?")
00166                 empty_map.web.metadata.set("wms_enable_request","* !GetFeatureInfo")
00167                 empty_map.web.metadata.set("wms_srs",self.projection)
00168                 
00169                 for layer in self.layers:
00170                         if layer=="fg":
00171                                 layerobj=mapscript.layerObj()
00172                                 layerobj.metadata.set("wms_srs",self.projection)
00173                                 layerobj.projection=self.projection
00174                                 layerobj.name=self.GetLayerName(layer)
00175                                 layerobj.status= mapscript.MS_ON
00176                                 layerobj.units= mapscript.MS_METERS
00177                                 layerobj.tileindex=os.path.abspath(self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer)+"/geotiff/0/index.shp")
00178                                 layerobj.type= mapscript.MS_LAYER_RASTER
00179                                 layerobj.maxscaledenom=50000
00180                                 layerobj.minscaledenom=1
00181                                 empty_map.insertLayer(layerobj)
00182                         else:
00183                                 for i in range(0,self.nb_level+1):
00184                                         layerobj=mapscript.layerObj()
00185                                         layerobj.metadata.set("wms_srs",self.projection)
00186                                         layerobj.projection=self.projection
00187                                         layerobj.name=self.GetLayerName(layer)
00188                                         layerobj.status= mapscript.MS_ON
00189                                         layerobj.units= mapscript.MS_METERS
00190                                         layerobj.tileindex=os.path.abspath(self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer)+"/geotiff/"+str(i)+"/index.shp")
00191                                         layerobj.type= mapscript.MS_LAYER_RASTER
00192                                 
00193                                         if i == self.nb_level:
00194                                                 layerobj.maxscaledenom=50000
00195                                         else:
00196                                                 layerobj.maxscaledenom=math.pow(2,i)*2000*self.zoom
00197                                         if i == 0:
00198                                                 layerobj.minscaledenom=1
00199                                         else:
00200                                                 layerobj.minscaledenom=1+math.pow(2,i-1)*2000*self.zoom
00201                                         empty_map.insertLayer(layerobj)
00202                                 
00203                 name=self.output_directory+"/"+str(self.zoom)+"/mapfile_"+str(int(self.extent_left))+"_"+str(int(self.extent_bottom))+"_"+str(int(self.extent_right))+"_"+str(int(self.extent_top))+"_"+self.projection+".map"
00204                 empty_map.save(name)
00205                 print("Mapfile stored in :"+str(os.path.abspath(name)))
00206         
00207         def Listfile(self,src):
00208                 for root, dirs, files in os.walk(src):
00209                         return files
00210         
00211         def CheckFormat(self,ext):
00212                 for ext_def in self.available_extension:
00213                         if ext==ext_def:
00214                                 return True
00215                 return False
00216         
00217         def CreateOutputDirectory(self):
00218                 if os.path.isdir(self.output_directory):
00219                         if os.path.isdir(self.output_directory+"/"+str(self.zoom)):
00220                                 for layer in self.layers:
00221                                         if os.path.isdir(self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer)):
00222                                                 if not os.path.isdir(self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer)+"/original_tiles"):
00223                                                         os.makedirs(self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer)+"/original_tiles")
00224                                                 if not os.path.isdir(self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer)+"/geotiff"):
00225                                                         os.makedirs(self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer)+"/geotiff")
00226                                                         for i in range (0,self.nb_level+1):
00227                                                                 if layer != "fg":
00228                                                                         os.makedirs(self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer)+"/geotiff/"+str(i))
00229                                                 else:
00230                                                         for i in range (0,self.nb_level+1): 
00231                                                                 if not os.path.isdir(self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer)+"/geotiff/"+str(i)):
00232                                                                         os.makedirs(self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer)+"/geotiff/"+str(i))
00233                                         else:
00234                                                 os.makedirs(self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer))
00235                                 
00236                         else:
00237                                 os.makedirs(self.output_directory+"/"+str(self.zoom))
00238                                 for layer in self.layers:
00239                                         os.makedirs(self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer))
00240                                         os.makedirs(self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer)+"/original_tiles")
00241                                         os.makedirs(self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer)+"/geotiff")
00242                                         for i in range (0,self.nb_level+1):
00243                                                 if layer != "fg":
00244                                                         os.makedirs(self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer)+"/geotiff/"+str(i))
00245                 else:
00246                         print (self.output_directory+ " is not a directory")
00247 
00248         def ConvertToGeotiff(self):
00249                 for layer in self.layers:
00250                         geotiff_id=1
00251                         files=self.Listfile(self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer)+"/original_tiles")
00252                         #print(files)
00253                         for file in files:
00254                                 file_without_extension=""
00255                                 extension=""
00256                                 opt_command=""
00257 
00258                                 extension=file.split(".")[len(file.split("."))-1]
00259                                 file_without_extension=""
00260                                 for id in range(len(file.split("."))-1):
00261                                         file_without_extension+=file.split(".")[id]
00262                                         if id !=len(range(len(file.split("."))-1))-1:
00263                                                 file_without_extension+="."
00264                                 if extension=="png":
00265                                         opt_command="-of PNG"
00266                                 if self.CheckFormat(extension):
00267                                         splited_file=file_without_extension.split("_")
00268                                         if len(splited_file)==6:
00269                                                 left=math.floor(float(splited_file[1]))
00270                                                 bottom=math.floor(float(splited_file[2]))
00271                                                 right=math.floor(float(splited_file[3]))
00272                                                 top=math.floor(float(splited_file[4]))
00273                                                 bashCommand = "gdal_translate "+opt_command+" -a_ullr "+str(left)+" "+str(top)+" "+str(right)+" "+str(bottom)+" -a_srs "+self.projection+" "+self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer)+"/original_tiles/"+file+" "+self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer)+"/geotiff/0/"+file_without_extension+"."+extension+""
00274                                         try:
00275                                                 if not os.path.isfile(self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer)+"/geotiff/0/"+file_without_extension+"."+extension): 
00276                                                         os.system(bashCommand +" > /dev/null")
00277                                                         print("Geotiff conversion progress for "+layer+" :"+str(math.floor(float((geotiff_id*100))/float(len(files))*100)/100)+"%")
00278                                                         geotiff_id+=1
00279                                                 else:
00280                                                         print("Geotiff conversion progress for "+layer+" :"+str(math.floor(float((geotiff_id*100))/float(len(files))*100)/100)+"%")
00281                                                         geotiff_id+=1
00282 #                                                       print self.output_directory+"/"+str(self.zoom)+"/"+self.GetLayerName(layer)+"/geotiff/0/"+file_without_extension+"."+extension+" already exists"
00283                                         except:
00284                                                 return
00285                                 
00286         
00287         def BuildPyramid(self,src_dir,target_dir,level=None,format=None):
00288                 
00289                 if level is None:
00290                         level=self.nb_level
00291                 if format is None:
00292                         format=self.type
00293                 bashCommand= 'gdal_retile.py -pyramidOnly -of '+format+' -r bilinear -levels '+str(level)+' -targetDir '+target_dir+' '+src_dir+"/*."+format +"  > /dev/null"
00294                 try:
00295                         os.system(bashCommand)
00296                         print("Pyramid fron "+src_dir+" built")
00297                 except:
00298                         return
00299         
00300         def find_nb_lvl_pyramid(self,nb_tiles):
00301                 nb=math.sqrt(nb_tiles)
00302                 for i in range (0,10):
00303                         if math.pow(2,i)>nb:
00304                                 return i-1
00305                                 
00306         def BuildShapefile(self,directory):
00307                 bashCommand="gdaltindex "+directory+"/index.shp " +directory+"/*."+self.type +" > /dev/null"
00308                 try:
00309                         os.system(bashCommand)
00310                         print("Shapefile for "+directory+" built")
00311                 except:
00312                         return
00313         
00314 def WGStoCHy(lat,lng):
00315         lat = DecToSexAngle(lat)
00316         lng = DecToSexAngle(lng)
00317         # Converts degrees to seconds (sex)
00318         lat = SexAngleToSeconds(lat)
00319         lng = SexAngleToSeconds(lng)
00320         # Axiliary values (% Bern)
00321         lat_aux = (lat - float(169028.66))/10000
00322         lng_aux = (lng - float(26782.5))/10000
00323         #Process Y
00324         y = 600072.37 + 211455.93 * lng_aux - 10938.51 * lng_aux * lat_aux -0.36 * lng_aux * math.pow(lat_aux, 2)-44.54 * math.pow(lng_aux, 3)
00325         return y
00326 
00327 def WGStoCHx(lat,lng):
00328         # Converts degrees dec to sex
00329         lat = DecToSexAngle(lat)
00330         lng = DecToSexAngle(lng)
00331         # Converts degrees to seconds (sex)
00332         lat = SexAngleToSeconds(lat)
00333         lng = SexAngleToSeconds(lng)
00334         # Axiliary values (% Bern)
00335         lat_aux = (lat - float(169028.66))/10000
00336         lng_aux = (lng - float(26782.5))/10000
00337         # Process X
00338         x = 200147.07+ 308807.95 * lat_aux+3745.25 * math.pow(lng_aux, 2)+76.63 * math.pow(lat_aux, 2)-194.56 * math.pow(lng_aux, 2) * lat_aux+119.79 * math.pow(lat_aux, 3)
00339         return x
00340 
00341 def WGStoCHh(lat, lng, h):
00342         # Converts degrees dec to sex
00343         lat = DecToSexAngle(lat)
00344         lng = DecToSexAngle(lng)
00345         # Converts degrees to seconds (sex)
00346         lat = SexAngleToSeconds(lat)
00347         lng = SexAngleToSeconds(lng)
00348         #Axiliary values (% Bern)
00349         lat_aux = (lat - 169028.66)/10000
00350         lng_aux = (lng - 26782.5)/10000
00351         # Process h
00352         h = h - 49.55 + 2.73 * lng_aux +  6.94 * lat_aux
00353         return h
00354 
00355 def CHtoWGSlat(y, x) :
00356         # Converts militar to civil and  to unit = 1000km
00357         # Axiliary values (% Bern)
00358         y_aux = (y - 600000)/1000000
00359         x_aux = (x - 200000)/1000000
00360         # Process lat
00361         lat = 16.9023892+3.238272 * x_aux-0.270978 * math.pow(y_aux, 2)-0.002528 * math.pow(x_aux, 2)-0.0447   * math.pow(y_aux, 2) * x_aux-0.0140   * math.pow(x_aux, 3)
00362         # Unit 10000" to 1 " and converts seconds to degrees (dec)
00363         lat = lat * 100/36
00364         return lat
00365 
00366 def CHtoWGSlng(y,x) :
00367         # Converts militar to civil and  to unit = 1000km
00368         # Axiliary values (% Bern)
00369         y_aux = (y - 600000)/1000000
00370         x_aux = (x - 200000)/1000000
00371         # Process long
00372         lng = 2.6779094+ 4.728982 * y_aux+ 0.791484 * y_aux * x_aux+ 0.1306   * y_aux * math.pow(x_aux, 2)- 0.0436   * math.pow(y_aux, 3)
00373         # Unit 10000" to 1 " and converts seconds to degrees (dec)
00374         lng = lng * 100/36
00375         return lng
00376                 
00377 def CHtoWGSheight(y, x, h) :
00378         #Converts militar to civil and  to unit = 1000km
00379         # Axiliary values (% Bern)
00380         y_aux = (y - 600000)/1000000
00381         x_aux = (x - 200000)/1000000
00382         #Process height
00383         h = h + 49.55 - 12.60 * y_aux - 22.64 * x_aux
00384         return h
00385 
00386 def SexToDecAngle(dms) :
00387         # Extract DMS
00388         # Input: dd.mmss(,)ss
00389         deg=0
00390         min=0
00391         sec=0
00392         deg = int(dms)
00393         min = int((dms-deg)*100)
00394         sec = Decimal((((dms-deg)*100) - min) * 100)
00395         # Result in degrees dec (dd.dddd)
00396         return deg + min/60 + sec/3600
00397         
00398         
00399 def DecToSexAngle(dec) :
00400         deg = float(int(dec));
00401         min = float(int((dec-deg)*60))
00402         sec = (((float(dec)-deg)*60)-min)*60
00403         # Output: dd.mmss(,)ss
00404         return deg + min/100 + sec/10000
00405 
00406 def SexAngleToSeconds(dms) :
00407         deg=0
00408         min=0
00409         sec=0
00410         deg = float(int(dms))
00411         min = float(int((dms-deg)*100))
00412         sec = (((float(dms)-deg)*100) - min) * 100
00413         # Result in degrees sex (dd.mmss)
00414         return sec + min*60 + deg*3600
00415                 
00416 
00417 def usage():
00418         usage = """
00419         USAGE: process.py latitude longitude map_width map_height
00420         -o --output\t output diretory. [Default : "."]
00421         -p --projection\t projection. EPSG Referential where image are projected. [Default : "EPSG:21781"]
00422         -f --format\t Image format. [Default : "png"]
00423         -s --size\t Tile size in px. [Default 256]
00424         -h --help\t Print script usage
00425         -z --zoom\t Zoom level for layer from map.search.ch. [Default 0.25].\n \t 0.25, 0.5, 1, 2, 4,8,32,512 available.
00426         -a --aerial\t "Aerial" layer from map.search.ch. [Default]
00427         -m --map\t "Symbolic" layer from map.search.ch
00428         -i --info\t "Information" layer from map.search.ch
00429         
00430         """        
00431         print usage
00432 import pdb
00433 def CheckLatitude(lat):
00434         try:
00435                 if float(lat) >-90 and float(lat) <90:
00436                         return True
00437                 else:
00438                         return False
00439         except:
00440                 print(lat+" is not a float")
00441 
00442                 
00443 def CheckLongitude(lng):
00444         
00445         try :
00446                 if float(lng) >-180 and float(lng) <180:
00447                         return True
00448                 else:
00449                         return False
00450         except:
00451                 print(lng+" is not a float")
00452         
00453 def CheckWidth(w):
00454         try :
00455                 float(w)
00456                 return True
00457         except:
00458                 print(w+" is not a number")
00459                 return False
00460 
00461 def CheckHeight(h):
00462         try :
00463                 float(h)
00464                 return True
00465         except:
00466                 print(h+" is not a number")
00467                 return False
00468 
00469 
00470 
00471 def main():
00472         
00473         if len(sys.argv)<5:
00474                 usage()
00475                 sys.exit(0)
00476         else:
00477                 signal.signal(signal.SIGINT, signal_handler)
00478                 
00479                 latitude=-1
00480                 longitude=-1
00481                 width=-1
00482                 height=-1
00483                 if CheckLatitude(sys.argv[1]):
00484                         latitude=float(sys.argv[1])
00485                 else:
00486                         print(sys.argv[1]+" is not a latitude in Decimal Degrees format.\nTake a look to : http://en.wikipedia.org/wiki/Decimal_degrees")
00487                         sys.exit(2)
00488                 if CheckLongitude(sys.argv[2]):
00489                         longitude=float(sys.argv[2])
00490                 else:
00491                         sys.argv[2]+" is not a longitude in Decimal Degrees format. Take a look to : http://en.wikipedia.org/wiki/Decimal_degrees"
00492                         sys.exit(2)
00493                 if CheckWidth(sys.argv[3]):
00494                         width=float(sys.argv[3])
00495                 else:
00496                         sys.argv[3]+" is not a width for the map"
00497                         sys.exit(2)
00498                 if CheckHeight(sys.argv[4]):
00499                         height=float(sys.argv[4])
00500                 else:
00501                         sys.argv[4]+" is not a height for the map"
00502                         sys.exit(2)
00503                         
00504                 try:
00505                         opt, args = getopt.getopt(sys.argv[5:], "o:p:f:s:hz:ami", ["output=","projection=","format=","size=","help","zoom=","aerial","map","info"])
00506 #                       print opt
00507 #                       print args
00508                 except getopt.GetoptError, err:
00509                         print str(err)
00510                         usage()
00511                         sys.exit(2)
00512                         
00513                 output=default_output_directory
00514                 projection=default_projection
00515                 format=default_type
00516                 size=default_size
00517                 zoom=default_zoom
00518                 layers=default_layers
00519                 for o, a in opt:
00520                         if o in ("-o", "--output"):
00521                                 if not os.path.isdir(a):
00522                                         sys.exit('ERROR:  %s is not a directory.'%a)
00523                                 else:
00524                                         output=a
00525                         elif o in ("-p", "--projection"):
00526                                 projection=a
00527                                 print("Need to add transform between wgs84 and "+a)
00528                                 sys.exit()
00529                         elif o in ("-f", "--format"):
00530                                 if a in available_extension:
00531                                         format=a
00532                                 else:
00533                                         print ("available formats are 'png', 'gif', 'jpeg'")
00534                                         sys.exit('ERROR:  %s is not a valid format.'%a)
00535                         elif o in ("-s", "--size"):
00536                                 size=int(a)
00537                         elif o in ("-h", "--help"):
00538                                 sys.exit(usage())
00539                         elif o in ("-z", "--zoom"):
00540                                 if zoom in available_zoom:
00541                                         zoom=float(a)
00542                                 else:
00543                                         print('ERROR:  %s is not a valid zoom.'%a)
00544                                         sys.exit(usage())
00545                         elif o in ("-a", "--aerial"):
00546                                 if not "bg" in layers:
00547                                         layers.append("bg")
00548                         elif o in ("-m", "--map"):
00549                                 if not "sym" in layers:
00550                                         layers.append("sym")
00551                         elif o in ("-i", "--info"):
00552                                 if not "fg" in layers:
00553                                         layers.append("fg")
00554                         else:
00555                                 print("False, unhandled option")
00556                                 usage()
00557                                 sys.exit()
00558 
00559 
00560                 
00561                 map_example=Map(latitude,longitude,width,height,output,projection,format,size,zoom,layers)
00562 #               print("Map object created")
00563                 map_example.PreProcess()
00564 #               print("Preprocessing done")
00565                 map_example.CreateOutputDirectory()
00566                 print("Output directories created")
00567                 print("Starting downloading tiles ...")
00568                 map_example.DownloadTile()
00569                 print("Tiles downloaded")
00570                 print("Converting tiles to Geotiff ...")
00571                 map_example.ConvertToGeotiff()
00572                 print("Conversion done")
00573                 if map_example.nb_level>=1:
00574                         print("Building pyramid for each layer ...")
00575                         for layer in map_example.layers:
00576                                 #no need to create pyramid for info layer
00577                                 if layer != "fg":
00578                                         #USAGE BuildPyramid(src_dir,target_dir,level,format)
00579                                         src_dir=map_example.output_directory+"/"+str(map_example.zoom)+"/"+map_example.GetLayerName(layer)+"/geotiff/0"
00580                                         target_dir=map_example.output_directory+"/"+str(map_example.zoom)+"/"+map_example.GetLayerName(layer)+"/geotiff"
00581                                         map_example.BuildPyramid(src_dir,target_dir)
00582                                         #USAGE BuildShapefile(directory)
00583                         print("Building pyramid done")
00584                 else:
00585                         print "Pyramid not build because level 0 does not own enough tiles"
00586                 print("Building shapefile ...")
00587                 for layer in map_example.layers:
00588                         for root, dirs, files in os.walk(map_example.output_directory+"/"+str(map_example.zoom)+"/"+map_example.GetLayerName(layer)+"/geotiff"):
00589                                 if root!=map_example.output_directory+"/"+str(map_example.zoom)+"/"+map_example.GetLayerName(layer)+"/geotiff": 
00590                                         path=os.path.abspath(root)
00591                                         map_example.BuildShapefile(path)
00592                 print("Building shapefile done")
00593                 print("Creating mapfile for mapserver ...")
00594                 map_example.BuildMapFile()
00595                 
00596 
00597 if __name__ == '__main__':
00598         #default parameters
00599 
00600         default_projection="EPSG:21781"
00601         default_output_directory="."
00602         default_type="png"
00603         default_size=256
00604         default_zoom=0.25
00605         default_layers=["bg"]
00606 
00607         #3 formats available from server
00608         available_extension=["png","gif","jpeg"]
00609         available_layers=["bg","fg","sym"]
00610         available_zoom=[0.25,0.5,1,2,4,8,32,512]
00611         #signal.signal(signal.SIGINT, signal_handler) ctrl can not be detected because shell used in python
00612         main()


websocket_gui
Author(s): Benoit Lescot and Stéphane Magnenat
autogenerated on Sat Dec 28 2013 17:46:48