[Tutor] Python loop isn't working

Emile van Sebille emile at fenx.com
Fri Aug 5 18:40:48 CEST 2011


On 8/5/2011 9:14 AM Susana Iraiis Delgado Rodriguez said...
>
> Hello list!
> I have a python script which works with other libraries in order to
> create maps. The script looks for an extension in a specific directory
> and with mapnik makes the thumbnails.
> The module works fine from Python command line and Pyton IDLE, but I
> need this program to run from MS-DOS, when I added the try statement it
> started to fail. Module runs but doesn't do what I need. Console shows:
>
> C:\Python26>shp2png.py C:\\ .png
> Iniciando...
> Listo
>
> What am I doing wrong?
>

First, get rid of the encompassing try: except block so you can see any 
actual errors instead of trapping them.  Place print statements at 
various points to test and confirm values.  Read up on pdb.set_trace() 
to assist when prints don't quite help enough. Start your diagnostics 
from there and post the the actual traceback results when you get stuck.

Emile


> import mapnik
> import os,fnmatch, sys
> from mapnik import LineSymbolizer,PolygonSymbolizer,PointSymbolizer
> from osgeo import ogr,gdal,osr
>
> directorio = sys.argv[1]
> extension = sys.argv[2]


drop this...
> try:


outdent this...
>      if len(sys.argv) == 3:
>          print 'Iniciando...'
>          file_list = []
>          folders = None
>          for root, folders, files in os.walk( directorio+'\\' ):
>              for filename in fnmatch.filter(files, extension):
>                  file_list.append(os.path.join(root, filename))

throw a print statement here to confirm that file_list has valid 
contents (I suspect not)

>          for row, filepath in enumerate(file_list, start=1):
>              dir(LineSymbolizer().stroke)
>              shapeData = ogr.Open(filepath)
>              shp = 'Error al abrir el archivo' +filepath
>              if shapeData is None:
>                  print shp
>                  continue
>              layer = shapeData.GetLayer()
>              defn = layer.GetLayerDefn()
>              geo = defn.GetGeomType()
>              (ruta, filename) = os.path.split(filepath)
>              archivo = os.path.splitext(filename)
>              i = archivo[0]+'.png'
>
>              m = mapnik.Map(800,500,"+proj=latlong +datum=WGS84")
>              m.background = mapnik.Color('#EBEBEB')
>              s = mapnik.Style()
>              r=mapnik.Rule()
>
>              if geo == 3 or geo == -2147483645:
>                  print "Trabajando mapa "+ruta+"\\"+filename+" con
> geometria "+ str(geo)
>
> r.symbols.append(mapnik.PolygonSymbolizer(mapnik.Color('#EB784B')))
>
> r.symbols.append(mapnik.LineSymbolizer(mapnik.Color('rgb(170%,170%,170%)'),0.9))
>                  s.rules.append(r)
>                  m.append_style('My Style',s)
>                  lyr = mapnik.Layer('world',"+proj=latlong +datum=WGS84")
>                  try:
>                      lyr.datasource =
> mapnik.Shapefile(base=ruta,file=archivo[0])
>                      lyr.styles.append('My Style')
>                      m.layers.append(lyr)
>                      m.zoom_to_box(lyr.envelope())
>                      mapnik.render_to_file(m,i, 'png')
>                      print "La imagen " +i+ " fue creada."
>                  except RuntimeError, e:
>                      print 'Shapefile invalida'
>                      continue
>
>              elif geo == 2 or geo == -2147483646:
>                  print "Trabajando mapa "+ruta+"\\"+filename+" con
> geometria "+ str(geo)
>
> r.symbols.append(mapnik.LineSymbolizer(mapnik.Color('#EB784B'),0.9))
>                  s.rules.append(r)
>                  m.append_style('My Style',s)
>                  lyr = mapnik.Layer('world',"+proj=latlong +datum=WGS84")
>                  try:
>                      lyr.datasource =
> mapnik.Shapefile(base=ruta,file=archivo[0])
>                      lyr.styles.append('My Style')
>                      m.layers.append(lyr)
>                      m.zoom_to_box(lyr.envelope())
>                      mapnik.render_to_file(m,i, 'png')
>                      print "La imagen " +i+ " fue creada."
>                  except RuntimeError, e:
>                      print 'Shapefile invalida'
>                      continue
>
>              elif geo == 1 or geo == -2147483647:
>                  print "Trabajando mapa "+ruta+"\\"+filename+" con
> geometria "+ str(geo)
>                  blue =
> mapnik.PointSymbolizer('C:\Python26\icono.png','png',50,50)
>                  blue.allow_overlap = True
>                  s=mapnik.Style()
>                  r=mapnik.Rule()
>                  r.symbols.append(blue)
>                  s.rules.append(r)
>                  m.append_style('My Style',s)
>                  lyr = mapnik.Layer('world',"+proj=latlong +datum=WGS84")
>                  try:
>                      lyr.datasource =
> mapnik.Shapefile(base=ruta,file=archivo[0])
>                      lyr.styles.append('My Style')
>                      m.layers.append(lyr)
>                      m.zoom_to_box(lyr.envelope())
>                      mapnik.render_to_file(m,i, 'png')
>                      print "La imagen " +i+ " fue creada."
>                  except RuntimeError, e:
>                      print 'Shapefile invalida'
>                      continue
>              else:
>                  print "Algo fallo y no entro a ninguna de las geometrias"
>          print "Listo"
>      else:
>              print "Tus argumentos no son correctos"

... and drop this
> except IndexError:
>      print "Tus argumentos no son correctos"
>
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list