Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use arrays for center and bounds in tilejson #57

Merged
merged 2 commits into from
Dec 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/tilejson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <mapnik/feature_layer_desc.hpp>
#include <mapnik/datasource.hpp>

#include <unordered_set>

namespace bpt = boost::property_tree;
namespace bal = boost::algorithm;

Expand Down Expand Up @@ -177,6 +179,7 @@ mapnik::parameters make_default_parameters() {
std::string make_tilejson(const mapnik::Map &map,
const std::string &base_url) {
static const mapnik::parameters defaults = make_default_parameters();
static const std::unordered_set<std::string> array_keys = {"center", "bounds"};

// TODO: remove super-hacky hard-coded 'JSON' serialiser and use
// a proper one.
Expand Down Expand Up @@ -216,7 +219,11 @@ std::string make_tilejson(const mapnik::Map &map,

for (auto const &row : params) {
out << "\"" << row.first << "\":";
mapnik::util::apply_visitor(json_converter(out), row.second);
if (array_keys.count(row.first) > 0) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this better than array_keys.find(row.first) == array_keys.end?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's effectively the same (well, with !=), but I think it's more readable.

out << "[" << row.second << "]";
} else {
mapnik::util::apply_visitor(json_converter(out), row.second);
}
out << ",";
}

Expand Down
16 changes: 16 additions & 0 deletions test/tilejson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ void test_tilejson_generate_numeric() {
}
}

// Some params need to be arrays of integers
for (auto &param : {"center", "bounds"}) {
// The same check as above for presense
sregex is_present = as_xpr('"') >> param >> '"' >> *space >> as_xpr(':');
if (regex_search(json.begin(), json.end(), is_present)) {
sregex is_array = as_xpr('"') >> param >> '"' >> *space >> as_xpr(':')
>> *space >> as_xpr('[');

if (!regex_search(json.begin(), json.end(), is_array)) {
throw std::runtime_error(
(boost::format("Parameter \"%1%\" should be an array of numbers, but is not "
"in TileJSON: %2%") % param % json).str());
}
}
}

} catch (const std::exception &e) {
std::throw_with_nested(
std::runtime_error(
Expand Down
3 changes: 2 additions & 1 deletion test/tilejson_params.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
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 +no_defs +over">
<Parameters>
<Parameter name="metatile">1</Parameter>
<Parameter name="center">-75.6038,40.0799,11</Parameter>
<Parameter name="center">-75,40,11</Parameter>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any significance to making the center values all integers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No - just a shorter output in the tests

<Parameter name="bounds">-180,-85,180,85</Parameter>
<Parameter name="maxzoom">16</Parameter>
<Parameter name="minzoom">0</Parameter>
<Parameter name="name"><![CDATA[MQ Carto Vector]]></Parameter>
Expand Down