Ok Dane!!
I added the lines you suggested me and my script finally worked, thank you
so much.
This is the modification:
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
2011/6/29 Dane Springmeyer <[email protected]>
> HI Susana,
>
> Sounds like all you need to do is add a try/except to catch and skip those
> corrupt shapefiles.
>
> Something like:
>
> try:
> lyr.datasource = mapnik.Shapefile(base=ruta,file=archivo[0])
> except RuntimeError, e:
> print 'Shapefile inválida'
> continue
>
> On Jun 29, 2011, at 8:52 AM, Susana Iraiis Delgado Rodriguez wrote:
>
> Hello Dane!!
>
> I'm developing a script that looks for shapefiles in a specific directory,
> once it finds a file makes an image out of the shapes.
> But I have some shapefiles that are corrupted and its size is 1KB, when the
> scrpits gets to these files it fails showing the next error:
> wrong file code : -1997790976
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "mapnik_punto_sin_duda.py", line 45, in <module>
> lyr.datasource = mapnik.Shapefile(base=ruta,file=archivo[0])
> File "C:\mapnik-0.7.1\python\2.6\site-packages\mapnik\__init__.py", line
> 282,
> in Shapefile
> return CreateDatasource(keywords)
> RuntimeError: wrong file code : -1997790976
>
> My script is:
>
> import mapnik
> import os,fnmatch, sys
> from mapnik import LineSymbolizer,PolygonSymbolizer,PointSymbolizer
> from osgeo import ogr,gdal,osr
>
> file_list = []
> folders = None
> for root, folders, files in os.walk( "c:\\" ):
> for filename in fnmatch.filter(files, '*.shp'):
> 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:
> 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")
> 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."
> else:
> print "Algo fallo y no entro a ninguna de las geometrias"
> if geo == 2:
> 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")
> 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."
> else:
> print "Algo fallo y no entro a ninguna de las geometrias"
> if geo == 1:
> 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")
> 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."
> else:
> print "Algo fallo y no entro a ninguna de las geometrias"
> print "Listo"
>
>
> 2011/6/27 Dane Springmeyer <[email protected]>
>
>>
>> On Jun 20, 2011, at 10:58 AM, Susana Iraiis Delgado Rodriguez wrote:
>>
>> > Hello list!!
>> >
>> > I'm developing a script to create images with mapnik and python, but I
>> have a problem:
>> > Some of my shapefiles are empty and mapnik doesn't want to work on them.
>>
>> Can you explain the problem you are seeing in more detail? Is mapnik
>> throwing an error?
>>
>> > Is there any way to verify when a shapefile is empty or doesn't have
>> information.
>>
>> Depends on what you mean by empty - 0 features perhaps? In that case,
>> Mapnik should still be able to open the Shapefile and then you can, in
>> python, do:
>>
>> ds = mapnik.Shapefile(file="your.shp")
>> if len(ds.all_features()) == 0:
>> print 'this shapefile is empty'
>>
>> >
>> > Thanks!!
>> > _______________________________________________
>> > Mapnik-users mailing list
>> > [email protected]
>> > https://lists.berlios.de/mailman/listinfo/mapnik-users
>>
>>
>
>
_______________________________________________
Mapnik-users mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/mapnik-users