mapnik / python-mapnik

Python bindings for mapnik
GNU Lesser General Public License v2.1
157 stars 91 forks source link

RuntimeError: GDAL Plugin: 1191448000 is an invalid band, dataset only has 3bands #186

Open winsento opened 6 years ago

winsento commented 6 years ago

There are several problems when rendering an image:

  1. The image is not for the entire image size (the rest is transparent).
  2. Image is blurry
  3. If ` <! [CDATA [auto]]> </ Parameter> is set, then I periodically get an error like this:
ERROR 5: raster.tif: GDALDataset::GetRasterBand(1259353168) - Illegal band #

Traceback (most recent call last):
  File "map.py", line 36, in <module>
    mapnik.render(m, image)
RuntimeError: GDAL Plugin: 1259353168 is an invalid band, dataset only has 3bands

On version mapnik=3.0.13 and python-mapnik=3.0.x - all OK. Tested on Mac and Ubuntu.

Code example:

import mapnik

PROJ = '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs'

style_xml = '<?xml version="1.0" encoding="utf-8"?> \
<!DOCTYPE Map[]> \
<Map srs="+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs"> \
<Style name="raster" filter-mode="first"> \
  <Rule> \
    <RasterSymbolizer opacity="1" scaling="bilinear" /> \
  </Rule> \
</Style> \
<Layer name="raster" \
  srs="+proj=latlong +ellps=WGS84 +datum=WGS84 +no_defs"> \
    <StyleName>raster</StyleName> \
    <Datasource> \
       <Parameter name="nodata"><![CDATA[0]]></Parameter> \
       <Parameter name="file"><![CDATA[raster.tif]]></Parameter> \
       <Parameter name="band"><![CDATA[auto]]></Parameter> \
       <Parameter name="type"><![CDATA[gdal]]></Parameter> \
    </Datasource> \
  </Layer> \
</Map>'

m = mapnik.Map(256,256)
mapnik.load_map_from_string(m, style_xml.encode('utf-8'))
proj = mapnik.Projection(PROJ)
bbox = proj.forward(mapnik.Box2d(37.35145568847656, 55.68958466999194, 37.35179901123047, 55.68977819238148))
m.zoom_to_box(bbox)

image = mapnik.Image(m.width, m.height)
mapnik.render(m, image)
image.save('image.png')

image

winsento commented 6 years ago

After last update 3.0.20 i get this error. Ping @talaj @flippmoke

talaj commented 6 years ago

There is no such option as

<Parameter name="band">auto</Parameter>

The band parameter needs to be an integer - band number. It should give some sensible error however.

My only idea now is that some regression happened after the changes with overviews.

Can you offer the raster.tif file so I can reproduce it easily?

winsento commented 6 years ago

@talaj here archive with geotif and python script (138Mb) https://drive.google.com/open?id=1wF84PnNM8Tud6NvfCpY44eEkvU0_w_j3

UPD:

2018-05-21_18-45-19

talaj commented 6 years ago

Thank you @winsento. I'm able to reproduce it now.

talaj commented 6 years ago

I've found and fixed the problem in https://github.com/mapycz/mapnik/commit/9a57592d7ba19686ea1a72d143fa675742a51d75. I haven't made a pull request to Mapnik yet as it needs some test coverage.

@winsento, can you build it with this patch yourself or should I try to merge it into Mapnik asap?

winsento commented 6 years ago

@talaj oh cool. I'll try to build and test.

winsento commented 6 years ago

@talaj it became better, but not completely fixed and rasters on small zoom became cloudy (before was sharp).

image

talaj commented 6 years ago

OK, I now noticed that the tiff is being reprojected "on the fly". I think I'm able to reproduce it:

issue-overviews-1024-1024-4x4-1 0-agg

Output seems fine when I reprojected the tiff to the Mercator by GDAL prior to rendering, including overviews:

issue-overviews-1024-1024-4x4-1 0-agg_merc

winsento commented 6 years ago

@talaj yes the tiff reprojected "on the fly" from EPSG:4326 to EPSG:3857. If something else is needed you for the tests - let me know.

talaj commented 6 years ago

@winsento, I hopefully figured it out finally. Please, cherry-pick additional commits from https://github.com/mapycz/mapnik/commits/fix-overviews.

winsento commented 6 years ago

@talaj cool. I'm going to try now.

winsento commented 6 years ago

@talaj i confirm now everything is ok.

talaj commented 6 years ago

Great, I will try to merge it to Mapnik.

winsento commented 6 years ago

@talaj Excellent. when I'm will be in the Czech republic, pint's on me)

talaj commented 6 years ago

@winsento, fine, looking forward!

It would be also great reward if you can offer that tiff file as a test case for Mapnik. In much smaller resolution or a part of it.

winsento commented 6 years ago

@talaj maybe for not increase the amount of test data, to the existing geotiff (in test data) add overview via gdaladdo (http://www.gdal.org/gdaladdo.html)?

talaj commented 6 years ago

@winsento, you are right, that will be better.

yddcode commented 2 years ago

GDALDataset::GetRasterBand(4) - Illegal band Nir = data.GetRasterBand(4).ReadAsArray().astype(np.float32) # near infrared AttributeError: 'NoneType' object has no attribute 'ReadAsArray'