sync with 2.3.x branch

This commit is contained in:
Dane Springmeyer 2013-09-25 20:04:31 -07:00
commit 0f4ba4dfd5
25 changed files with 447 additions and 192 deletions

View file

@ -60,6 +60,15 @@ void add_stop5(raster_colorizer_ptr &rc, float v, colorizer_mode_enum m, color c
colorizer_stop stop(v, m, c);
rc->add_stop(stop);
}
mapnik::color get_color(raster_colorizer_ptr &rc, float value) {
unsigned rgba = rc->get_color(value);
unsigned r = (rgba & 0xff);
unsigned g = (rgba >> 8 ) & 0xff;
unsigned b = (rgba >> 16) & 0xff;
unsigned a = (rgba >> 24) & 0xff;
return mapnik::color(r,g,b,a);
}
colorizer_stops const& get_stops(raster_colorizer_ptr & rc)
{
return rc->get_stops();
@ -142,7 +151,7 @@ void export_raster_colorizer()
">>> colorizer = mapnik.RasterColorizer(mapnik.COLORIZER_LINEAR, default_color)\n"
">>> colorizer.add_stop(100, mapnik.COLORIZER_DISCRETE, mapnik.Color(\"#112233\"))\n"
)
.def("get_color", &raster_colorizer::get_color,
.def("get_color", get_color,
"Get the color assigned to a certain value in raster data.\n"
"\n"
"Usage:\n"

View file

@ -203,6 +203,13 @@ inline std::ostream& operator<< (std::ostream& os, invert)
return os;
}
inline std::ostream& operator<< (std::ostream& os, colorize_alpha const& filter)
{
os << "colorize-alpha(TODO)";
return os;
}
template <typename Out>
struct to_string_visitor : boost::static_visitor<void>
{

View file

@ -28,6 +28,9 @@
#include <mapnik/image_data.hpp>
#include <mapnik/noncopyable.hpp>
// boost
#include <boost/optional.hpp>
namespace mapnik {
class raster : private mapnik::noncopyable
{
@ -35,11 +38,22 @@ public:
box2d<double> ext_;
image_data_32 data_;
bool premultiplied_alpha_;
boost::optional<double> nodata_;
raster(box2d<double> const& ext, unsigned width, unsigned height, bool premultiplied_alpha = false)
: ext_(ext),
data_(width,height),
premultiplied_alpha_(premultiplied_alpha)
{}
void set_nodata(double nodata)
{
nodata_ = nodata;
}
boost::optional<double> const& nodata() const
{
return nodata_;
}
};
}

View file

@ -199,7 +199,6 @@ public:
//! \brief Colorize a raster
//!
//! \param[in, out] raster A raster stored in float32 single channel format, which gets colorized in place.
//! \param[in] f The feature used to find 'NODATA' information if available
void colorize(std::shared_ptr<raster> const& raster, feature_impl const& f) const;
@ -207,7 +206,7 @@ public:
//!
//! \param[in] value Input value
//! \return color associated with the value
color get_color(float value) const;
unsigned get_color(float value) const;
//! \brief Set the epsilon value for exact mode

View file

@ -108,6 +108,7 @@ gdal_datasource::gdal_datasource(parameters const& params)
nbands_ = dataset->GetRasterCount();
width_ = dataset->GetRasterXSize();
height_ = dataset->GetRasterYSize();
desc_.add_descriptor(mapnik::attribute_descriptor("nodata", mapnik::Integer));
double tr[6];
bool bbox_override = false;

View file

@ -75,8 +75,7 @@ gdal_featureset::gdal_featureset(GDALDataset& dataset,
nodata_value_(nodata),
first_(true)
{
ctx_->push("value");
ctx_->push("NODATA");
ctx_->push("nodata");
}
gdal_featureset::~gdal_featureset()
@ -115,7 +114,8 @@ feature_ptr gdal_featureset::next()
feature_ptr gdal_featureset::get_feature(mapnik::query const& q)
{
feature_ptr feature = feature_factory::create(ctx_,1);
int raster_has_nodata = 0;
double raster_nodata = 0;
GDALRasterBand * red = 0;
GDALRasterBand * green = 0;
GDALRasterBand * blue = 0;
@ -229,91 +229,60 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q)
{
throw datasource_exception((boost::format("GDAL Plugin: '%d' is an invalid band, dataset only has '%d' bands\n") % band_ % nbands_).str());
}
float* imageData = (float*)image.getBytes();
GDALRasterBand * band = dataset_.GetRasterBand(band_);
int hasNoData(0);
double nodata(0);
if (nodata_value_)
{
hasNoData = 1;
nodata = *nodata_value_;
}
else
{
nodata = band->GetNoDataValue(&hasNoData);
}
raster_nodata = band->GetNoDataValue(&raster_has_nodata);
band->RasterIO(GF_Read, x_off, y_off, width, height,
imageData, image.width(), image.height(),
GDT_Float32, 0, 0);
if (hasNoData)
{
feature->put("NODATA",nodata);
}
}
else // working with all bands
{
for (int i = 0; i < nbands_; ++i)
{
GDALRasterBand * band = dataset_.GetRasterBand(i + 1);
#ifdef MAPNIK_LOG
get_overview_meta(band);
#endif
GDALColorInterp color_interp = band->GetColorInterpretation();
switch (color_interp)
{
case GCI_RedBand:
red = band;
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Found red band";
break;
case GCI_GreenBand:
green = band;
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Found green band";
break;
case GCI_BlueBand:
blue = band;
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Found blue band";
break;
case GCI_AlphaBand:
alpha = band;
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Found alpha band";
break;
case GCI_GrayIndex:
grey = band;
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Found gray band";
break;
case GCI_PaletteIndex:
{
grey = band;
#ifdef MAPNIK_LOG
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Found gray band, and colortable...";
GDALColorTable *color_table = band->GetColorTable();
if (color_table)
{
int count = color_table->GetColorEntryCount();
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Color Table count=" << count;
for (int j = 0; j < count; j++)
{
const GDALColorEntry *ce = color_table->GetColorEntry (j);
if (! ce) continue;
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Color entry RGB=" << ce->c1 << "," <<ce->c2 << "," << ce->c3;
}
}
@ -322,50 +291,33 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q)
}
case GCI_Undefined:
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Found undefined band (assumming gray band)";
grey = band;
break;
default:
MAPNIK_LOG_WARN(gdal) << "gdal_featureset: Band type unknown!";
break;
}
}
if (red && green && blue)
{
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Processing rgb bands...";
int hasNoData = 0;
double nodata = 0.0;
if (nodata_value_)
{
hasNoData = 1;
nodata = *nodata_value_;
}
else
{
nodata = red->GetNoDataValue(&hasNoData);
}
if (hasNoData)
{
feature->put("NODATA",nodata);
}
raster_nodata = red->GetNoDataValue(&raster_has_nodata);
GDALColorTable *color_table = red->GetColorTable();
if (! alpha && hasNoData && ! color_table)
if (!alpha && raster_has_nodata && !color_table)
{
// first read the data in and create an alpha channel from the nodata values
// read the data in and create an alpha channel from the nodata values
// NOTE: we intentionally ignore user supplied nodata value since it only
// works for grayscale images or a single band (as a double)
// TODO - we assume here the nodata value for the red band applies to all band
// but that may not always be the case: http://trac.osgeo.org/gdal/ticket/2734
float* imageData = (float*)image.getBytes();
red->RasterIO(GF_Read, x_off, y_off, width, height,
imageData, image.width(), image.height(),
GDT_Float32, 0, 0);
int len = image.width() * image.height();
for (int i = 0; i < len; ++i)
{
if (nodata == imageData[i])
if (std::fabs(raster_nodata - imageData[i]) < 1e-12)
{
*reinterpret_cast<unsigned *>(&imageData[i]) = 0;
}
@ -374,9 +326,7 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q)
*reinterpret_cast<unsigned *>(&imageData[i]) = 0xFFFFFFFF;
}
}
}
red->RasterIO(GF_Read, x_off, y_off, width, height, image.getBytes() + 0,
image.width(), image.height(), GDT_Byte, 4, 4 * image.width());
green->RasterIO(GF_Read, x_off, y_off, width, height, image.getBytes() + 1,
@ -387,46 +337,31 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q)
else if (grey)
{
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Processing gray band...";
int hasNoData(0);
double nodata(0);
if (nodata_value_)
{
hasNoData = 1;
nodata = *nodata_value_;
}
else
{
nodata = grey->GetNoDataValue(&hasNoData);
}
raster_nodata = grey->GetNoDataValue(&raster_has_nodata);
GDALColorTable* color_table = grey->GetColorTable();
if (hasNoData && ! color_table)
bool has_nodata = nodata_value_ || raster_has_nodata;
if (!color_table && has_nodata)
{
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: No data value for layer=" << nodata;
feature->put("NODATA",nodata);
double apply_nodata = nodata_value_ ? *nodata_value_ : raster_nodata;
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: applying nodata value for layer=" << apply_nodata;
// first read the data in and create an alpha channel from the nodata values
float* imageData = (float*)image.getBytes();
grey->RasterIO(GF_Read, x_off, y_off, width, height,
imageData, image.width(), image.height(),
GDT_Float32, 0, 0);
int len = image.width() * image.height();
for (int i = 0; i < len; ++i)
{
if (nodata == imageData[i])
if (std::fabs(apply_nodata - imageData[i]) < 1e-12)
{
*reinterpret_cast<unsigned *>(&imageData[i]) = 0;
}
else
{
*reinterpret_cast<unsigned *> (&imageData[i]) = 0xFFFFFFFF;
*reinterpret_cast<unsigned *>(&imageData[i]) = 0xFFFFFFFF;
}
}
}
grey->RasterIO(GF_Read, x_off, y_off, width, height, image.getBytes() + 0,
image.width(), image.height(), GDT_Byte, 4, 4 * image.width());
grey->RasterIO(GF_Read,x_off, y_off, width, height, image.getBytes() + 1,
@ -436,38 +371,23 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q)
if (color_table)
{
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Loading colour table...";
unsigned nodata_value = static_cast<unsigned>(std::floor(nodata+.5)); // FIXME: is it realy unsigned ?
if (hasNoData)
{
feature->put("NODATA",static_cast<mapnik::value_integer>(nodata_value));
}
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Loading color table...";
for (unsigned y = 0; y < image.height(); ++y)
{
unsigned int* row = image.getRow(y);
for (unsigned x = 0; x < image.width(); ++x)
{
unsigned value = row[x] & 0xff;
if (hasNoData && (value == nodata_value))
const GDALColorEntry *ce = color_table->GetColorEntry(value);
if (ce)
{
// make no data fully alpha
row[x] = 0;
row[x] = (ce->c4 << 24)| (ce->c3 << 16) | (ce->c2 << 8) | (ce->c1) ;
}
else
{
const GDALColorEntry *ce = color_table->GetColorEntry(value);
if (ce)
{
// TODO - big endian support
row[x] = (ce->c4 << 24)| (ce->c3 << 16) | (ce->c2 << 8) | (ce->c1) ;
}
else
{
// make lacking color entry fully alpha
// note - gdal_translate makes black
row[x] = 0;
}
// make lacking color entry fully alpha
// note - gdal_translate makes black
row[x] = 0;
}
}
}
@ -476,11 +396,28 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q)
if (alpha)
{
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: processing alpha band...";
if (raster_has_nodata)
{
MAPNIK_LOG_ERROR(gdal) << "warning: alpha channel being used instead of nodata value";
}
alpha->RasterIO(GF_Read, x_off, y_off, width, height, image.getBytes() + 3,
image.width(), image.height(), GDT_Byte, 4, 4 * image.width());
}
}
// set nodata value to be used in raster colorizer
if (nodata_value_)
{
raster->set_nodata(*nodata_value_);
}
else
{
raster->set_nodata(raster_nodata);
}
// report actual/original source nodata in feature attributes
if (raster_has_nodata)
{
feature->put("nodata",raster_nodata);
}
return feature;
}
}
@ -514,19 +451,22 @@ feature_ptr gdal_featureset::get_feature_at_point(mapnik::coord2d const& pt)
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: x=" << x << " y=" << y;
GDALRasterBand* band = dataset_.GetRasterBand(band_);
int hasNoData;
double nodata = band->GetNoDataValue(&hasNoData);
int raster_has_nodata;
double nodata = band->GetNoDataValue(&raster_has_nodata);
double value;
band->RasterIO(GF_Read, x, y, 1, 1, &value, 1, 1, GDT_Float64, 0, 0);
if (! hasNoData || value != nodata)
if (! raster_has_nodata || value != nodata)
{
// construct feature
feature_ptr feature = feature_factory::create(ctx_,1);
geometry_type * point = new geometry_type(mapnik::geometry_type::types::Point);
point->move_to(pt.x, pt.y);
feature->add_geometry(point);
feature->put("value",value);
feature->put_new("value",value);
if (raster_has_nodata)
{
feature->put_new("nodata",nodata);
}
return feature;
}
}

View file

@ -127,25 +127,19 @@ void raster_colorizer::colorize(raster_ptr const& raster, feature_impl const& f)
unsigned *imageData = raster->data_.getData();
int len = raster->data_.width() * raster->data_.height();
bool hasNoData = false;
float noDataValue = 0;
//std::map<std::string,value>::const_iterator fi = Props.find("NODATA");
if (f.has_key("NODATA"))
{
hasNoData = true;
noDataValue = static_cast<float>(f.get("NODATA").to_double());
}
boost::optional<double> const& nodata = raster->nodata();
for (int i=0; i<len; ++i)
{
// the GDAL plugin reads single bands as floats
float value = *reinterpret_cast<float *> (&imageData[i]);
if (hasNoData && noDataValue == value)
imageData[i] = color(0,0,0,0).rgba();
if (nodata && (std::fabs(value - *nodata) < epsilon_))
{
imageData[i] = 0;
}
else
imageData[i] = get_color(value).rgba();
{
imageData[i] = get_color(value);
}
}
}
@ -154,14 +148,14 @@ inline unsigned interpolate(unsigned start, unsigned end, float fraction)
return static_cast<unsigned>(fraction * ((float)end - (float)start) + start);
}
color raster_colorizer::get_color(float value) const
unsigned raster_colorizer::get_color(float value) const
{
int stopCount = stops_.size();
//use default color if no stops
if(stopCount == 0)
{
return default_color_;
return default_color_.rgba();
}
//1 - Find the stop that the value is in
@ -284,7 +278,7 @@ color raster_colorizer::get_color(float value) const
MAPNIK_LOG_DEBUG(raster_colorizer) << "\toutputColor: " << outputColor.to_string();
*/
return outputColor;
return outputColor.rgba();
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.1 KiB

View file

@ -9,22 +9,6 @@ def setup():
# from another directory we need to chdir()
os.chdir(execution_path('.'))
def test_gen_map():
mapxmlfile = '../data/good_maps/raster_colorizer.xml'
mapxmloutputfile = 'raster_colorizer_test_save.xml'
outputfile = 'raster_colorizer_test.png'
m = mapnik.Map(800, 600)
try:
mapnik.load_map(m, mapxmlfile)
mapnik.save_map(m, mapxmloutputfile)
m.zoom_all()
mapnik.render_to_file(m, outputfile)
except RuntimeError,e:
# only test datasources that we have installed
if not 'Could not create datasource' in str(e):
raise RuntimeError(str(e))
#test discrete colorizer mode
def test_get_color_discrete():
#setup

View file

@ -1,40 +0,0 @@
#coding=utf8
import os
import mapnik
from utilities import execution_path, run_all
from nose.tools import *
def setup():
# All of the paths used are relative, if we run the tests
# from another directory we need to chdir()
os.chdir(execution_path('.'))
if 'gdal' in mapnik.DatasourceCache.plugin_names():
def test_vrt_rendering():
m = mapnik.Map(512,512)
mapnik.load_map(m,'../data/good_maps/vrt_colortable.xml')
m.zoom_all()
im = mapnik.Image(512,512)
mapnik.render(m,im)
actual = '/tmp/vrt_colortable.png'
expected = 'images/support/vrt_colortable.png'
im.save(actual)
expected_im = mapnik.Image.open(expected)
eq_(im.tostring(),expected_im.tostring(), 'failed comparing actual (%s) and expected(%s)' % (actual,'tests/python_tests/'+ expected))
def test_tif_rendering_nodata():
m = mapnik.Map(512,512)
mapnik.load_map(m,'../data/good_maps/tiff_colortable.xml')
m.zoom_all()
im = mapnik.Image(512,512)
mapnik.render(m,im)
actual = '/tmp/tif_colortable.png'
expected = 'images/support/tif_colortable.png'
im.save(actual)
expected_im = mapnik.Image.open(expected)
eq_(im.tostring(),expected_im.tostring(), 'failed comparing actual (%s) and expected(%s)' % (actual,'tests/python_tests/'+ expected))
if __name__ == "__main__":
setup()
run_all(eval(x) for x in dir() if x.startswith("test_"))

View file

@ -90,7 +90,7 @@ def test_dataraster_query_point():
def test_load_save_map():
map = mapnik.Map(256,256)
in_map = "../data/good_maps/raster_symbolizer.xml"
in_map = "../visual_tests/styles/raster_symbolizer.xml"
try:
mapnik.load_map(map, in_map)

View file

@ -0,0 +1,136 @@
{
"keys": [
""
],
"data": {},
"grid": [
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" "
]
}

View file

@ -0,0 +1,136 @@
{
"keys": [
""
],
"data": {},
"grid": [
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" "
]
}

View file

@ -0,0 +1,72 @@
{
"keys": [
""
],
"data": {},
"grid": [
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" "
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

View file

@ -62,7 +62,7 @@
<Layer name="dataraster" srs="+init=epsg:32630">
<StyleName>DISCRETE RAINBOW</StyleName>
<Datasource>
<Parameter name="file">../raster/dataraster.tif</Parameter>
<Parameter name="file">../../data/raster/dataraster.tif</Parameter>
<Parameter name="type">gdal</Parameter>
<Parameter name="band">1</Parameter>
</Datasource>

View file

@ -26,7 +26,7 @@
<StyleName>ramped</StyleName>
<Datasource>
<!-- get stats on this file with gdalinfo ../raster/dataraster.tif -stats -->
<Parameter name="file">../raster/dataraster.tif</Parameter>
<Parameter name="file">../../data/raster/dataraster.tif</Parameter>
<Parameter name="type">gdal</Parameter>
<Parameter name="band">1</Parameter>
</Datasource>

View file

@ -8,7 +8,7 @@
<Layer name="2011_5km_vrt_nodata" srs="+init=epsg:32630">
<StyleName>2011_5km_vrt_nodata_style</StyleName>
<Datasource>
<Parameter name="file">../raster/dataraster.vrt</Parameter>
<Parameter name="file">../../data/raster/dataraster.vrt</Parameter>
<Parameter name="type">gdal</Parameter>
</Datasource>
</Layer>

View file

@ -176,6 +176,9 @@ files = {
'dst-over-compositing':{'sizes':[(512,512)]},
'tiff_colortable':{'sizes':[(256,256)]},
'tiff_colortable_custom_nodata':{'sizes':[(256,256)]},
'vrt_colortable':{'sizes':[(256,256)]},
'raster_colorizer':{'sizes':[(512,512)]},
'raster_symbolizer':{'sizes':[(512,512)]},
}
class Reporting: