Skip to content

Commit

Permalink
Merge pull request #57 from MapQuest/tilejson_array
Browse files Browse the repository at this point in the history
Use arrays for center and bounds in tilejson
  • Loading branch information
pnorman committed Dec 11, 2014
2 parents de2561e + 6bdb3f7 commit 30ab719
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
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) {
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>
<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

0 comments on commit 30ab719

Please sign in to comment.