[Tutor] Python loop isn't working
Susana Iraiis Delgado Rodriguez
susana.delgado_s at utzmg.edu.mx
Fri Aug 5 18:14:40 CEST 2011
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?
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]
try:
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))
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"
except IndexError:
print "Tus argumentos no son correctos"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110805/7fbae2d6/attachment.html>
More information about the Tutor
mailing list