Optionally output JSON data with pixel coordinates.

This commit is contained in:
Hermann Kraus 2011-07-16 12:24:52 +00:00
parent abcba2bc34
commit 8bb6df1a07
6 changed files with 27 additions and 31 deletions

View file

@ -81,7 +81,7 @@ public:
* \param box Area (in pixel coordinates)
* \param feature The feature being processed
* \param prj_trans Projection transformation
* \param t Cooridnate transformation
* \param t Coordinate transformation
* \param properties List of properties to output
*/
virtual void add_box(box2d<double> const& box, Feature const& feature,
@ -107,7 +107,6 @@ public:
* \param properties metawriter_property_map object with userdefined values.
* Useful for setting filename etc.
*/
virtual void start(metawriter_property_map const& properties)
{
boost::ignore_unused_variable_warning(properties);
@ -122,7 +121,7 @@ public:
*/
void set_size(int width, int height) { width_ = width; height_ = height; }
/** Set Map object's srs. */
virtual void set_map_srs(projection const& proj) = 0;
virtual void set_map_srs(projection const& proj) { /* Not required when working with image coordinates. */ }
/** Return the list of default properties. */
metawriter_properties const& get_default_properties() const { return dflt_properties_;}
protected:

View file

@ -80,8 +80,6 @@ public:
metawriter_properties const& properties);
virtual void start(metawriter_property_map const& properties);
virtual void stop();
virtual void set_map_srs(projection const& proj);
/**
* An instance of a rendered feature. The box represents the image

View file

@ -70,6 +70,8 @@ public:
void set_output_empty(bool output_empty) { output_empty_ = output_empty; }
/** See set_output_empty(). */
bool get_output_empty() { return output_empty_; }
void set_pixel_coordinates(bool on) { pixel_coordinates_ = on; }
bool get_pixel_coordinates() { return pixel_coordinates_; }
virtual void set_map_srs(projection const& proj);
protected:
enum {
@ -80,6 +82,7 @@ protected:
/** Features written. */
int count_;
bool output_empty_;
bool pixel_coordinates_;
/** Transformation from map srs to output srs. */
proj_transform *trans_;
projection output_srs_;
@ -94,15 +97,17 @@ protected:
if (count_ == HEADER_NOT_WRITTEN) write_header();
if (count_++) *f_ << ",\n";
*f_ << "{ \"type\": \"Feature\",\n \"geometry\": { \"type\": \""<<type<<"\",\n \"coordinates\":";
*f_ << "{ \"type\": \"Feature\",\n \"geometry\": { \"type\": \"" << type << "\",\n \"coordinates\":";
}
void write_properties(Feature const& feature, metawriter_properties const& properties);
inline void write_point(CoordTransform const& t, double x, double y, bool last = false)
{
double z = 0.0;
t.backward(&x, &y);
trans_->forward(x, y, z);
*f_ << "["<<x<<","<<y<<"]";
if (!pixel_coordinates_) {
t.backward(&x, &y);
trans_->forward(x, y, z);
}
*f_ << "[" << x << "," << y << "]";
if (!last) {
*f_ << ",";
}

View file

@ -104,7 +104,7 @@ void metawriter_json_stream::write_properties(Feature const& feature, metawriter
{
*f_ << "}," << //Close coordinates object
"\n \"properties\": {";
std::map<std::string, value> fprops = feature.props();
std::map<std::string, value> const &fprops = feature.props();
int i = 0;
BOOST_FOREACH(std::string p, properties)
{
@ -143,14 +143,21 @@ void metawriter_json_stream::add_box(box2d<double> const &box, Feature const& fe
{
/* Check if feature is in bounds. */
if (box.maxx() < 0 || box.maxy() < 0 || box.minx() > width_ || box.miny() > height_) return;
double minx, miny, maxx, maxy;
if (pixel_coordinates_) {
minx = box.minx();
miny = box.miny();
maxx = box.maxx();
maxy = box.maxy();
} else {
//t_ coord transform has transform for box2d combined with proj_transform
box2d<double> transformed = t.backward(box, *trans_);
//t_ coord transform has transform for box2d combined with proj_transform
box2d<double> transformed = t.backward(box, *trans_);
double minx = transformed.minx();
double miny = transformed.miny();
double maxx = transformed.maxx();
double maxy = transformed.maxy();
minx = transformed.minx();
miny = transformed.miny();
maxx = transformed.maxx();
maxy = transformed.maxy();
}
write_feature_header("Polygon");

View file

@ -41,9 +41,9 @@ metawriter_create(const boost::property_tree::ptree &pt) {
metawriter_ptr writer;
string type = get_attr<string>(pt, "type");
optional<string> properties = get_opt_attr<string>(pt, "default-output");
if (type == "json") {
string file = get_attr<string>(pt, "file");
optional<string> properties = get_opt_attr<string>(pt, "default-output");
metawriter_json_ptr json = metawriter_json_ptr(new metawriter_json(properties, parse_path(file)));
optional<boolean> output_empty = get_opt_attr<boolean>(pt, "output-empty");
if (output_empty) {
@ -52,10 +52,8 @@ metawriter_create(const boost::property_tree::ptree &pt) {
writer = json;
} else if (type == "inmem") {
optional<string> properties = get_opt_attr<string>(pt, "default-output");
metawriter_inmem_ptr inmem = metawriter_inmem_ptr(new metawriter_inmem(properties));
writer = inmem;
} else {
throw config_error(string("Unknown type '") + type + "'");
}

View file

@ -57,7 +57,6 @@ namespace mapnik {
metawriter_inmem::metawriter_inmem(metawriter_properties dflt_properties)
: metawriter(dflt_properties) {
// ???
}
metawriter_inmem::~metawriter_inmem() {
@ -130,16 +129,6 @@ metawriter_inmem::start(metawriter_property_map const& /*properties*/) {
instances_.clear();
}
void
metawriter_inmem::stop() {
}
void
metawriter_inmem::set_map_srs(projection const& /*proj*/) {
// currently unused, since the inmem metawriter keeps everything in
// image coordinates.
}
const std::list<metawriter_inmem::meta_instance> &
metawriter_inmem::instances() const {
return instances_;