format utils

This commit is contained in:
Mathis Logemann 2022-01-26 20:41:37 +01:00
parent e7c3d04309
commit d5a873e81c
14 changed files with 630 additions and 595 deletions

View file

@ -20,7 +20,6 @@
*
*****************************************************************************/
#include <iostream>
#include <string>
@ -31,10 +30,8 @@
#include <mapnik/datasource_cache.hpp>
#include <mapnik/util/geometry_to_wkb.hpp>
int main(int argc, char** argv)
{
if (argc != 2)
{
std::cerr << "Usage: " << argv[0] << " <path-to-shapefile>\n";
@ -57,8 +54,7 @@ int main (int argc, char ** argv )
try
{
ds = mapnik::datasource_cache::instance().create(p);
}
catch ( ... )
} catch (...)
{
std::cerr << "Can't create datasource!\n";
return EXIT_FAILURE;
@ -96,7 +92,5 @@ int main (int argc, char ** argv )
}
}
return EXIT_SUCCESS;
}

View file

@ -42,21 +42,21 @@ MAPNIK_DISABLE_WARNING_POP
const int DEFAULT_DEPTH = 8;
const double DEFAULT_RATIO = 0.55;
namespace mapnik { namespace detail {
namespace mapnik {
namespace detail {
bool is_csv(std::string const& filename)
{
return boost::iends_with(filename,".csv")
|| boost::iends_with(filename,".tsv");
return boost::iends_with(filename, ".csv") || boost::iends_with(filename, ".tsv");
}
bool is_geojson(std::string const& filename)
{
return boost::iends_with(filename,".geojson")
|| boost::iends_with(filename,".json");
return boost::iends_with(filename, ".geojson") || boost::iends_with(filename, ".json");
}
}}
} // namespace detail
} // namespace mapnik
int main(int argc, char** argv)
{
@ -76,6 +76,7 @@ int main (int argc, char** argv)
try
{
po::options_description desc("Mapnik CSV/GeoJSON index utility");
// clang-format off
desc.add_options()
("help,h", "Produce usage message")
("version,V","Print version string")
@ -89,14 +90,15 @@ int main (int argc, char** argv)
("validate-features", "Validate GeoJSON features")
("bbox,b", po::value<std::string>(), "Only index features within bounding box: --bbox=minx,miny,maxx,maxy")
;
// clang-format on
po::positional_options_description p;
p.add("files", -1);
po::store(po::command_line_parser(argc, argv)
.options(desc)
.style(po::command_line_style::unix_style | po::command_line_style::allow_long_disguise)
.positional(p)
.run(), vm);
.run(),
vm);
po::notify(vm);
if (vm.count("version"))
@ -145,8 +147,7 @@ int main (int argc, char** argv)
{
use_bbox = true;
}
}
catch (std::exception const& ex)
} catch (std::exception const& ex)
{
std::clog << "Error: " << ex.what() << std::endl;
return EXIT_FAILURE;
@ -221,9 +222,9 @@ int main (int argc, char** argv)
for (auto const& item : boxes)
{
auto ext_f = std::get<0>(item);
if (use_bbox && !bbox.intersects(ext_f)) continue;
mapnik::util::index_record rec =
{std::get<1>(item).first, std::get<1>(item).second, ext_f};
if (use_bbox && !bbox.intersects(ext_f))
continue;
mapnik::util::index_record rec = {std::get<1>(item).first, std::get<1>(item).second, ext_f};
tree.insert(rec, ext_f);
}
@ -231,8 +232,7 @@ int main (int argc, char** argv)
std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary);
if (!file)
{
std::clog << "cannot open index file for writing file \""
<< (filename + ".index") << "\"" << std::endl;
std::clog << "cannot open index file for writing file \"" << (filename + ".index") << "\"" << std::endl;
}
else
{

View file

@ -36,10 +36,12 @@
#include <iostream>
#include <sstream>
namespace mapnik { namespace detail {
namespace mapnik {
namespace detail {
template<typename T>
std::pair<bool,typename T::value_type::first_type> process_csv_file(T & boxes, std::string const& filename, std::string const& manual_headers, char separator, char quote)
std::pair<bool, typename T::value_type::first_type>
process_csv_file(T& boxes, std::string const& filename, std::string const& manual_headers, char separator, char quote)
{
using box_type = typename T::value_type::first_type;
csv_utils::csv_file_parser p;
@ -47,14 +49,12 @@ std::pair<bool,typename T::value_type::first_type> process_csv_file(T & boxes, s
p.separator_ = separator;
p.quote_ = quote;
util::mapped_memory_file csv_file{filename};
try
{
p.parse_csv_and_boxes(csv_file.file(), boxes);
return std::make_pair(true, box_type(p.extent_));
}
catch (std::exception const& ex)
} catch (std::exception const& ex)
{
std::clog << ex.what() << std::endl;
return std::make_pair(false, box_type(p.extent_));
@ -66,4 +66,5 @@ using item_type = std::pair<box_type, std::pair<std::uint64_t, std::uint64_t>>;
using boxes_type = std::vector<item_type>;
template std::pair<bool, box_type> process_csv_file(boxes_type&, std::string const&, std::string const&, char, char);
}}
} // namespace detail
} // namespace mapnik

View file

@ -26,11 +26,17 @@
#include <utility>
#include <mapnik/geometry/box2d.hpp>
namespace mapnik { namespace detail {
namespace mapnik {
namespace detail {
template<typename T>
std::pair<bool, typename T::value_type::first_type> process_csv_file(T & boxes, std::string const& filename, std::string const& manual_headers, char separator, char quote);
std::pair<bool, typename T::value_type::first_type> process_csv_file(T& boxes,
std::string const& filename,
std::string const& manual_headers,
char separator,
char quote);
}}
}
} // namespace mapnik
#endif // MAPNIK_UTILS_PROCESS_CSV_FILE_HPP

View file

@ -43,8 +43,7 @@ MAPNIK_DISABLE_WARNING_POP
namespace {
constexpr mapnik::json::well_known_names feature_properties[] = {
mapnik::json::well_known_names::type,
constexpr mapnik::json::well_known_names feature_properties[] = {mapnik::json::well_known_names::type,
mapnik::json::well_known_names::geometry,
mapnik::json::well_known_names::properties}; // sorted
@ -56,7 +55,6 @@ constexpr mapnik::json::well_known_names geometry_collection_properties[] = {
mapnik::json::well_known_names::type,
mapnik::json::well_known_names::geometries}; // sorted
template<typename Keys>
std::string join(Keys const& keys)
{
@ -64,7 +62,8 @@ std::string join(Keys const& keys)
bool first = true;
for (auto const& key : keys)
{
if (!first) result += ",";
if (!first)
result += ",";
result += "\"" + std::string(mapnik::json::wkn_to_string(key)) + "\"";
first = false;
}
@ -91,18 +90,19 @@ bool validate_geojson_feature(mapnik::json::geojson_value & value, Keys const& k
{
if (!value.is<mapnik::json::geojson_object>())
{
if (verbose) std::clog << "Expecting an GeoJSON object" << std::endl;
if (verbose)
std::clog << "Expecting an GeoJSON object" << std::endl;
return false;
}
mapnik::json::geojson_object& feature = mapnik::util::get<mapnik::json::geojson_object>(value);
std::sort(feature.begin(), feature.end(), [](auto const& e0, auto const& e1)
{
std::sort(feature.begin(), feature.end(), [](auto const& e0, auto const& e1) {
return std::get<0>(e0) < std::get<0>(e1);
});
if (!has_keys(feature.begin(), feature.end(), feature_properties))
{
if (verbose) std::clog << "Expecting one of " << join(feature_properties) << std::endl;
if (verbose)
std::clog << "Expecting one of " << join(feature_properties) << std::endl;
return false;
}
@ -114,21 +114,22 @@ bool validate_geojson_feature(mapnik::json::geojson_value & value, Keys const& k
auto& geom_value = std::get<1>(elem);
if (!geom_value.is<mapnik::json::geojson_object>())
{
if (verbose) std::clog << "\"geometry\": xxx <-- expecting an JSON object here" << std::endl;
if (verbose)
std::clog << "\"geometry\": xxx <-- expecting an JSON object here" << std::endl;
return false;
}
auto& geometry = mapnik::util::get<mapnik::json::geojson_object>(geom_value);
// sort by property name
std::sort(geometry.begin(), geometry.end(), [](auto const& e0, auto const& e1)
{
std::sort(geometry.begin(), geometry.end(), [](auto const& e0, auto const& e1) {
return std::get<0>(e0) < std::get<0>(e1);
});
if (!has_keys(geometry.begin(), geometry.end(), geometry_properties)
&& !has_keys(geometry.begin(), geometry.end(), geometry_collection_properties))
if (!has_keys(geometry.begin(), geometry.end(), geometry_properties) &&
!has_keys(geometry.begin(), geometry.end(), geometry_collection_properties))
{
if (verbose) std::clog << "\"geometry\": xxx <-- expecting one of " << join(geometry_properties)
<< " or " << join(geometry_collection_properties) << std::endl;
if (verbose)
std::clog << "\"geometry\": xxx <-- expecting one of " << join(geometry_properties) << " or "
<< join(geometry_collection_properties) << std::endl;
return false;
}
@ -142,13 +143,16 @@ bool validate_geojson_feature(mapnik::json::geojson_value & value, Keys const& k
auto const& geom_type_value = std::get<1>(elem2);
if (!geom_type_value.is<mapnik::geometry::geometry_types>())
{
if (verbose) std::clog << "\"type\": xxx <-- expecting an GeoJSON geometry type here" << std::endl;
if (verbose)
std::clog << "\"type\": xxx <-- expecting an GeoJSON geometry type here" << std::endl;
return false;
}
geom_type = mapnik::util::get<mapnik::geometry::geometry_types>(geom_type_value);
if (geom_type == mapnik::geometry::geometry_types::GeometryCollection)
{
if (verbose) std::clog << "GeometryCollections are not allowed" << std::endl;;
if (verbose)
std::clog << "GeometryCollections are not allowed" << std::endl;
;
return false;
}
}
@ -157,7 +161,8 @@ bool validate_geojson_feature(mapnik::json::geojson_value & value, Keys const& k
auto const& coordinates_value = std::get<1>(elem2);
if (!coordinates_value.is<mapnik::json::positions>())
{
if (verbose) std::clog << "\"coordinates\": xxx <-- expecting an GeoJSON positions here" << std::endl;
if (verbose)
std::clog << "\"coordinates\": xxx <-- expecting an GeoJSON positions here" << std::endl;
return false;
}
coordinates = &mapnik::util::get<mapnik::json::positions>(coordinates_value);
@ -168,7 +173,8 @@ bool validate_geojson_feature(mapnik::json::geojson_value & value, Keys const& k
// expecting single position
if (!coordinates->is<mapnik::json::point>())
{
if (verbose) std::clog << "Expecting single position in Point" << std::endl;
if (verbose)
std::clog << "Expecting single position in Point" << std::endl;
return false;
}
}
@ -177,7 +183,8 @@ bool validate_geojson_feature(mapnik::json::geojson_value & value, Keys const& k
// expecting
if (!coordinates->is<mapnik::json::ring>())
{
if (verbose) std::clog << "Expecting sequence of positions (ring) in LineString" << std::endl;
if (verbose)
std::clog << "Expecting sequence of positions (ring) in LineString" << std::endl;
return false;
}
else
@ -185,7 +192,8 @@ bool validate_geojson_feature(mapnik::json::geojson_value & value, Keys const& k
auto const& ring = mapnik::util::get<mapnik::json::ring>(*coordinates);
if (ring.size() < 2)
{
if (verbose) std::clog << "Expecting at least two coordinates in LineString" << std::endl;
if (verbose)
std::clog << "Expecting at least two coordinates in LineString" << std::endl;
return false;
}
}
@ -195,7 +203,8 @@ bool validate_geojson_feature(mapnik::json::geojson_value & value, Keys const& k
// expecting
if (!coordinates->is<mapnik::json::rings>())
{
if (verbose) std::clog << "Expecting an array of rings in Polygon" << std::endl;
if (verbose)
std::clog << "Expecting an array of rings in Polygon" << std::endl;
return false;
}
else
@ -203,14 +212,16 @@ bool validate_geojson_feature(mapnik::json::geojson_value & value, Keys const& k
auto const& rings = mapnik::util::get<mapnik::json::rings>(*coordinates);
if (rings.size() < 1)
{
if (verbose) std::clog << "Expecting at least one ring in Polygon" << std::endl;
if (verbose)
std::clog << "Expecting at least one ring in Polygon" << std::endl;
return false;
}
for (auto const& ring : rings)
{
if (ring.size() < 4)
{
if (verbose) std::clog << "Expecting at least four coordinates in Polygon ring" << std::endl;
if (verbose)
std::clog << "Expecting at least four coordinates in Polygon ring" << std::endl;
return false;
}
}
@ -227,20 +238,20 @@ using base_iterator_type = char const*;
auto const& geojson_value = mapnik::json::grammar::geojson_value;
}
namespace mapnik { namespace detail {
} // namespace
namespace mapnik {
namespace detail {
template<typename T>
std::pair<bool,typename T::value_type::first_type> process_geojson_file_x3(T & boxes, std::string const& filename, bool validate_features, bool verbose)
std::pair<bool, typename T::value_type::first_type>
process_geojson_file_x3(T& boxes, std::string const& filename, bool validate_features, bool verbose)
{
using box_type = typename T::value_type::first_type;
box_type extent;
#if defined(MAPNIK_MEMORY_MAPPED_FILE)
mapnik::mapped_region_ptr mapped_region;
boost::optional<mapnik::mapped_region_ptr> memory =
mapnik::mapped_memory_cache::instance().find(filename, true);
boost::optional<mapnik::mapped_region_ptr> memory = mapnik::mapped_memory_cache::instance().find(filename, true);
if (!memory)
{
std::clog << "Error : cannot memory map " << filename << std::endl;
@ -269,17 +280,16 @@ std::pair<bool,typename T::value_type::first_type> process_geojson_file_x3(T & b
try
{
mapnik::json::extract_bounding_boxes(itr, end, boxes);
}
catch (boost::spirit::x3::expectation_failure<base_iterator_type> const& ex)
} catch (boost::spirit::x3::expectation_failure<base_iterator_type> const& ex)
{
std::clog << ex.what() << std::endl;
std::clog << "Expected: " << ex.which();
std::clog << " Got: \"" << std::string(ex.where(), ex.where() + 200) << '"' << std::endl;
return std::make_pair(false, extent);
}
catch (std::exception const& ex)
} catch (std::exception const& ex)
{
std::clog << "mapnik-index (GeoJSON) : could not extract bounding boxes from : '" << filename << "'" << std::endl;
std::clog << "mapnik-index (GeoJSON) : could not extract bounding boxes from : '" << filename << "'"
<< std::endl;
return std::make_pair(false, extent);
}
@ -287,18 +297,18 @@ std::pair<bool,typename T::value_type::first_type> process_geojson_file_x3(T & b
using space_type = mapnik::json::grammar::space_type;
auto keys = mapnik::json::get_keys();
#if BOOST_VERSION >= 106700
auto feature_grammar = x3::with<mapnik::json::grammar::keys_tag>(keys)
[ geojson_value ];
auto feature_grammar = x3::with<mapnik::json::grammar::keys_tag>(keys)[geojson_value];
#else
auto feature_grammar = x3::with<mapnik::json::grammar::keys_tag>(std::ref(keys))
[ geojson_value ];
auto feature_grammar = x3::with<mapnik::json::grammar::keys_tag>(std::ref(keys))[geojson_value];
#endif
for (auto const& item : boxes)
{
if (item.first.valid())
{
if (!extent.valid()) extent = item.first;
else extent.expand_to_include(item.first);
if (!extent.valid())
extent = item.first;
else
extent.expand_to_include(item.first);
if (validate_features)
{
base_iterator_type feat_itr = start + item.second.first;
@ -309,30 +319,36 @@ std::pair<bool,typename T::value_type::first_type> process_geojson_file_x3(T & b
bool result = x3::phrase_parse(feat_itr, feat_end, feature_grammar, space_type(), feature_value);
if (!result || feat_itr != feat_end)
{
if (verbose) std::clog << "Failed to parse: offset=" << item.second.first << " size=" << item.second.second << std::endl;
if (verbose)
std::clog << "Failed to parse: offset=" << item.second.first
<< " size=" << item.second.second << std::endl;
return std::make_pair(false, extent);
}
}
catch (x3::expectation_failure<std::string::const_iterator> const& ex)
} catch (x3::expectation_failure<std::string::const_iterator> const& ex)
{
if (verbose) std::clog << ex.what() << std::endl;
if (verbose)
std::clog << ex.what() << std::endl;
return std::make_pair(false, extent);
}
catch (...)
} catch (...)
{
if (verbose) std::clog << "Failed to parse: offset=" << item.second.first << " size=" << item.second.second << std::endl;
if (verbose)
std::clog << "Failed to parse: offset=" << item.second.first << " size=" << item.second.second
<< std::endl;
return std::make_pair(false, extent);
}
if (!validate_geojson_feature(feature_value, keys, verbose))
{
if (verbose) std::clog << "Failed to validate: [" << std::string(start + item.second.first, feat_end ) << "]" << std::endl;
if (verbose)
std::clog << "Failed to validate: [" << std::string(start + item.second.first, feat_end) << "]"
<< std::endl;
return std::make_pair(false, extent);
}
}
}
else if (validate_features)
{
if (verbose) std::clog << "Invalid bbox encountered " << item.first << std::endl;
if (verbose)
std::clog << "Invalid bbox encountered " << item.first << std::endl;
return std::make_pair(false, extent);
}
}
@ -341,4 +357,5 @@ std::pair<bool,typename T::value_type::first_type> process_geojson_file_x3(T & b
template std::pair<bool, box_type> process_geojson_file_x3(boxes_type&, std::string const&, bool, bool);
}}
} // namespace detail
} // namespace mapnik

View file

@ -26,11 +26,14 @@
#include <utility>
#include <string>
namespace mapnik { namespace detail {
namespace mapnik {
namespace detail {
template<typename T>
std::pair<bool, typename T::value_type::first_type> process_geojson_file_x3(T & boxes, std::string const& filename, bool validate_features, bool verbose);
std::pair<bool, typename T::value_type::first_type>
process_geojson_file_x3(T& boxes, std::string const& filename, bool validate_features, bool verbose);
}}
}
} // namespace mapnik
#endif // MAPNIK_UTILS_PROCESS_GEOJSON_FILE_X3_HPP

View file

@ -36,6 +36,7 @@ int main (int argc,char** argv)
try
{
po::options_description desc("mapnik-render utility");
// clang-format off
desc.add_options()
("help,h", "produce usage message")
("version,V","print version string")
@ -48,7 +49,7 @@ int main (int argc,char** argv)
("map-height",po::value<int>(),"map height in pixels")
("variables","make map parameters available as render-time variables")
;
// clang-format on
po::positional_options_description p;
p.add("xml", 1);
p.add("img", 1);
@ -108,11 +109,13 @@ int main (int argc,char** argv)
params_as_variables = true;
}
if (vm.count("map-width")) {
if (vm.count("map-width"))
{
map_width = vm["map-width"].as<int>();
}
if (vm.count("map-height")) {
if (vm.count("map-height"))
{
map_height = vm["map-height"].as<int>();
}
@ -170,8 +173,7 @@ int main (int argc,char** argv)
{
std::clog << "rendered to: " << img_file << "\n";
}
}
catch (std::exception const& ex)
} catch (std::exception const& ex)
{
std::clog << "Error " << ex.what() << std::endl;
return -1;

View file

@ -20,7 +20,6 @@
*
*****************************************************************************/
#include <iostream>
#include <vector>
#include <string>
@ -64,6 +63,7 @@ int main (int argc,char** argv)
try
{
po::options_description desc("ogrindex utility");
// clang-format off
desc.add_options()
("help,h", "produce usage message")
("version,V", "print version string")
@ -72,7 +72,7 @@ int main (int argc,char** argv)
("ratio,r", po::value<double>(), "split ratio (default 0.55)")
("ogr_files", po::value<vector<string> >(), "ogr supported files to index: file1 file2 ...fileN")
;
// clang-format on
po::positional_options_description p;
p.add("ogr_files", -1);
po::variables_map vm;
@ -101,8 +101,7 @@ int main (int argc,char** argv)
{
ogr_files = vm["ogr_files"].as<vector<string>>();
}
}
catch (...)
} catch (...)
{
std::clog << "Exception of unknown type!" << std::endl;
return -1;
@ -132,7 +131,8 @@ int main (int argc,char** argv)
// TODO - layer names don't match dataset name, so this will break for
// any layer types of ogr than shapefiles, etc
size_t breakpoint = ogrname.find_last_of(".");
if (breakpoint == string::npos) breakpoint = ogrname.length();
if (breakpoint == string::npos)
breakpoint = ogrname.length();
std::string ogrlayername(ogrname.substr(0, breakpoint));
if (mapnik::util::exists(ogrlayername + ".ogrindex"))
@ -151,7 +151,6 @@ int main (int argc,char** argv)
{
ogr_datasource ogr(params);
box2d<double> extent = ogr.envelope();
quadtree<int> tree(extent, depth, ratio);
int count = 0;
@ -174,7 +173,8 @@ int main (int argc,char** argv)
box2d<double> item_ext = fp->envelope();
tree.insert(count, item_ext);
if (verbose) {
if (verbose)
{
std::clog << "record number " << (count + 1) << " box=" << item_ext << std::endl;
}
@ -185,10 +185,13 @@ int main (int argc,char** argv)
std::fstream file((ogrlayername + ".ogrindex").c_str(),
std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary);
if (!file) {
std::clog << "cannot open ogrindex file for writing file \""
<< (ogrlayername+".ogrindex") << "\"" << std::endl;
} else {
if (!file)
{
std::clog << "cannot open ogrindex file for writing file \"" << (ogrlayername + ".ogrindex") << "\""
<< std::endl;
}
else
{
tree.trim();
std::clog << " number nodes=" << tree.count() << std::endl;
file.exceptions(std::ios::failbit | std::ios::badbit);
@ -206,10 +209,10 @@ int main (int argc,char** argv)
catch (...)
{
std::clog << "unknown exception..." << "\n";
std::clog << "unknown exception..."
<< "\n";
return -1;
}
}
std::clog << "done!" << std::endl;

View file

@ -42,13 +42,13 @@ MAPNIK_DISABLE_WARNING_POP
int main(int argc, char** argv)
{
namespace po = boost::program_options;
po::options_description desc("Postgresql/PostGIS to SQLite3 converter\n Options");
std::string usage = "usage: pgsql2sqlite --dbname db --table planet_osm_line --file osm.sqlite --query \"select * from planet_osm_line\"";
std::string usage = "usage: pgsql2sqlite --dbname db --table planet_osm_line --file osm.sqlite --query \"select * "
"from planet_osm_line\"";
try
{
// clang-format off
desc.add_options()
("help,?","Display this help screen.")
("host,h",po::value<std::string>(),"Allows you to specify connection to a database on a machine other than the default.")
@ -61,7 +61,7 @@ int main ( int argc, char** argv)
("file,f",po::value<std::string>(),"Use this option to specify the name of the file to create.")
;
// clang-format on
// po::positional_options_description p;
// p.add("table",1);
@ -98,20 +98,20 @@ int main ( int argc, char** argv)
std::shared_ptr<Connection> conn(creator());
std::string query = vm["query"].as<std::string>();
std::string output_table_name = vm.count("table") ? vm["table"].as<std::string>() : mapnik::sql_utils::table_from_sql(query);
std::string output_table_name =
vm.count("table") ? vm["table"].as<std::string>() : mapnik::sql_utils::table_from_sql(query);
std::string output_file = vm["file"].as<std::string>();
std::cout << "output_table : " << output_table_name << "\n";
mapnik::pgsql2sqlite(conn, query, output_table_name, output_file);
}
catch (mapnik::datasource_exception & ex)
} catch (mapnik::datasource_exception& ex)
{
std::cerr << ex.what() << "\n";
return EXIT_FAILURE;
}
}
catch(std::exception& e) {
} catch (std::exception& e)
{
std::cerr << desc << "\n";
std::cout << usage << "\n";
std::cerr << e.what() << "\n";

View file

@ -62,7 +62,8 @@ static std::string numeric2string(const char* buf)
std::ostringstream ss;
if (sign == 0x4000) ss << "-";
if (sign == 0x4000)
ss << "-";
int i = std::max(weight, std::int16_t(0));
int d = 0;
@ -133,22 +134,26 @@ static std::string numeric2string(const char* buf)
value = 0;
// Output up to 4 decimal digits for this value
if (dscale > 0) {
if (dscale > 0)
{
ss << (value / 1000);
value %= 1000;
dscale--;
}
if (dscale > 0) {
if (dscale > 0)
{
ss << (value / 100);
value %= 100;
dscale--;
}
if (dscale > 0) {
if (dscale > 0)
{
ss << (value / 10);
value %= 10;
dscale--;
}
if (dscale > 0) {
if (dscale > 0)
{
ss << value;
dscale--;
}
@ -159,7 +164,6 @@ static std::string numeric2string(const char* buf)
return ss.str();
}
namespace mapnik {
template<typename Connection>
@ -180,7 +184,8 @@ void pgsql2sqlite(Connection conn,
for (int i = 0; i < count; ++i)
{
if (i!=0) select_sql << ",";
if (i != 0)
select_sql << ",";
select_sql << "\"" << rs->getFieldName(i) << "\"";
}
@ -226,7 +231,9 @@ void pgsql2sqlite(Connection conn,
// add AsBinary(<geometry_column>) modifier
std::string select_sql_str = select_sql.str();
boost::algorithm::replace_all(select_sql_str, "\"" + geom_col + "\"","ST_AsBinary(" + geom_col+") as " + geom_col);
boost::algorithm::replace_all(select_sql_str,
"\"" + geom_col + "\"",
"ST_AsBinary(" + geom_col + ") as " + geom_col);
#ifdef MAPNIK_DEBUG
std::cout << select_sql_str << "\n";
@ -235,19 +242,22 @@ void pgsql2sqlite(Connection conn,
std::ostringstream cursor_sql;
std::string cursor_name("my_cursor");
cursor_sql << "DECLARE " << cursor_name << " BINARY INSENSITIVE NO SCROLL CURSOR WITH HOLD FOR " << select_sql_str << " FOR READ ONLY";
cursor_sql << "DECLARE " << cursor_name << " BINARY INSENSITIVE NO SCROLL CURSOR WITH HOLD FOR " << select_sql_str
<< " FOR READ ONLY";
conn->execute(cursor_sql.str());
std::shared_ptr<CursorResultSet> cursor(new CursorResultSet(conn, cursor_name, 10000));
unsigned num_fields = cursor->getNumFields();
if (num_fields == 0) return;
if (num_fields == 0)
return;
std::string feature_id = "fid";
std::ostringstream create_sql;
create_sql << "create table if not exists " << output_table_name << " (" << feature_id << " INTEGER PRIMARY KEY AUTOINCREMENT,";
create_sql << "create table if not exists " << output_table_name << " (" << feature_id
<< " INTEGER PRIMARY KEY AUTOINCREMENT,";
int geometry_oid = -1;
@ -289,7 +299,6 @@ void pgsql2sqlite(Connection conn,
create_sql << "' TEXT";
break;
}
}
}
@ -297,25 +306,24 @@ void pgsql2sqlite(Connection conn,
output_table_insert_sql += ")";
std::cout << "client_encoding=" << conn->client_encoding() << "\n";
std::cout << "geometry_column=" << geom_col << "(" << geom_type
<< ") srid=" << srid << " oid=" << geometry_oid << "\n";
std::cout << "geometry_column=" << geom_col << "(" << geom_type << ") srid=" << srid << " oid=" << geometry_oid
<< "\n";
db.execute("begin;");
// output table sql
db.execute(create_sql.str());
// spatial index sql
std::string spatial_index_sql = "create virtual table idx_" + output_table_name
+ "_" + geom_col + " using rtree(pkid, xmin, xmax, ymin, ymax)";
std::string spatial_index_sql =
"create virtual table idx_" + output_table_name + "_" + geom_col + " using rtree(pkid, xmin, xmax, ymin, ymax)";
db.execute(spatial_index_sql);
// blob_to_hex hex;
int pkid = 0;
std::string spatial_index_insert_sql = "insert into idx_" + output_table_name + "_"
+ geom_col + " values (?,?,?,?,?)" ;
std::string spatial_index_insert_sql =
"insert into idx_" + output_table_name + "_" + geom_col + " values (?,?,?,?,?)";
sqlite::prepared_statement spatial_index(db, spatial_index_insert_sql);
@ -345,8 +353,7 @@ void pgsql2sqlite(Connection conn,
{
case 25:
case 1042:
case 1043:
{
case 1043: {
std::string text(buf);
boost::algorithm::replace_all(text, "'", "''");
output_rec.push_back(sqlite::value_type(text));
@ -358,22 +365,19 @@ void pgsql2sqlite(Connection conn,
case 21:
output_rec.emplace_back(int(int2net(buf)));
break;
case 700:
{
case 700: {
float val;
float4net(val, buf);
output_rec.emplace_back(double(val));
break;
}
case 701:
{
case 701: {
double val;
float8net(val, buf);
output_rec.emplace_back(val);
break;
}
case 1700:
{
case 1700: {
std::string str = numeric2string(buf);
double val;
if (mapnik::util::string2double(str, val))
@ -383,8 +387,7 @@ void pgsql2sqlite(Connection conn,
break;
}
default:
{
default: {
if (oid == geometry_oid)
{
mapnik::feature_impl feat(ctx, pkid);
@ -420,7 +423,8 @@ void pgsql2sqlite(Connection conn,
}
}
if (!empty_geom) output_table.insert_record(output_rec);
if (!empty_geom)
output_table.insert_record(output_rec);
if (pkid % 1000 == 0)
{
@ -440,4 +444,4 @@ void pgsql2sqlite(Connection conn,
std::cout << "\r vacumming";
std::cout << "\n Done!" << std::endl;
}
}
} // namespace mapnik

View file

@ -22,7 +22,8 @@
#include "sqlite.hpp"
namespace mapnik { namespace sqlite {
namespace mapnik {
namespace sqlite {
database::database(std::string const& name)
{
@ -54,5 +55,5 @@ namespace mapnik { namespace sqlite {
}
return true;
}
}
}
} // namespace sqlite
} // namespace mapnik

View file

@ -40,7 +40,8 @@ MAPNIK_DISABLE_WARNING_POP
#include <string>
#include <vector>
namespace mapnik { namespace sqlite {
namespace mapnik {
namespace sqlite {
class database : private util::noncopyable
{
@ -66,11 +67,14 @@ namespace mapnik { namespace sqlite {
bool execute(std::string const& sql);
};
struct null_type {};
struct null_type
{};
struct blob
{
blob(const char* buf, unsigned size)
: buf_(buf), size_(size) {}
: buf_(buf)
, size_(size)
{}
const char* buf_;
unsigned size_;
@ -84,7 +88,9 @@ namespace mapnik { namespace sqlite {
struct binder
{
binder(sqlite3_stmt* stmt, unsigned index)
: stmt_(stmt), index_(index) {}
: stmt_(stmt)
, index_(index)
{}
bool operator()(null_type)
{
@ -139,9 +145,11 @@ namespace mapnik { namespace sqlite {
sqlite3_stmt* stmt_;
unsigned index_;
};
public:
prepared_statement(database& db, std::string const& sql)
: db_(db.db_.get()), stmt_(0)
: db_(db.db_.get())
, stmt_(0)
{
const char* tail;
// char * err_msg;
@ -189,5 +197,5 @@ namespace mapnik { namespace sqlite {
sqlite3* db_;
sqlite3_stmt* stmt_;
};
}
}
} // namespace sqlite
} // namespace mapnik

View file

@ -61,6 +61,7 @@ int main (int argc,char** argv)
try
{
po::options_description desc("shapeindex utility");
// clang-format off
desc.add_options()
("help,h", "produce usage message")
("version,V","print version string")
@ -70,7 +71,7 @@ int main (int argc,char** argv)
("ratio,r",po::value<double>(),"split ratio (default 0.55)")
("shape_files",po::value<std::vector<std::string> >(),"shape files to index: file1 file2 ...fileN")
;
// clang-format on
po::positional_options_description p;
p.add("shape_files", -1);
po::variables_map vm;
@ -117,8 +118,7 @@ int main (int argc,char** argv)
{
shape_files = vm["shape_files"].as<std::vector<std::string>>();
}
}
catch (std::exception const& ex)
} catch (std::exception const& ex)
{
std::clog << "Error: " << ex.what() << std::endl;
return EXIT_FAILURE;
@ -174,7 +174,6 @@ int main (int argc,char** argv)
box2d<double> extent;
shx.read_envelope(extent);
std::clog << "length=" << file_length << std::endl;
std::clog << "version=" << version << std::endl;
std::clog << "type=" << shape_type << std::endl;
@ -216,19 +215,20 @@ int main (int argc,char** argv)
}
shape_type = shp.read_ndr_integer();
if (shape_type == shape_io::shape_null) continue;
if (shape_type == shape_io::shape_null)
continue;
if (shape_type==shape_io::shape_point
|| shape_type==shape_io::shape_pointm
|| shape_type == shape_io::shape_pointz)
if (shape_type == shape_io::shape_point || shape_type == shape_io::shape_pointm ||
shape_type == shape_io::shape_pointz)
{
double x = shp.read_double();
double y = shp.read_double();
item_ext = box2d<double>(x, y, x, y);
}
else if (index_parts &&
(shape_type == shape_io::shape_polygon || shape_type == shape_io::shape_polygonm || shape_type == shape_io::shape_polygonz
|| shape_type == shape_io::shape_polyline || shape_type == shape_io::shape_polylinem || shape_type == shape_io::shape_polylinez))
(shape_type == shape_io::shape_polygon || shape_type == shape_io::shape_polygonm ||
shape_type == shape_io::shape_polygonz || shape_type == shape_io::shape_polyline ||
shape_type == shape_io::shape_polylinem || shape_type == shape_io::shape_polylinez))
{
shp.read_envelope(item_ext);
int num_parts = shp.read_ndr_integer();
@ -240,8 +240,10 @@ int main (int argc,char** argv)
{
int start = parts[k];
int end;
if (k == num_parts - 1) end = num_points;
else end = parts[k + 1];
if (k == num_parts - 1)
end = num_points;
else
end = parts[k + 1];
mapnik::geometry::linear_ring<double> ring;
ring.reserve(end - start);
@ -300,8 +302,8 @@ int main (int argc,char** argv)
#endif
if (!file)
{
std::clog << "cannot open index file for writing file \""
<< (shapename+".index") << "\"" << std::endl;
std::clog << "cannot open index file for writing file \"" << (shapename + ".index") << "\""
<< std::endl;
}
else
{

View file

@ -53,17 +53,14 @@ MAPNIK_DISABLE_WARNING_PUSH
#include "agg_scanline_u.h"
MAPNIK_DISABLE_WARNING_POP
struct main_marker_visitor
{
main_marker_visitor(std::string const& svg_name,
double scale_factor,
bool verbose,
bool auto_open)
: svg_name_(svg_name),
scale_factor_(scale_factor),
verbose_(verbose),
auto_open_(auto_open) {}
main_marker_visitor(std::string const& svg_name, double scale_factor, bool verbose, bool auto_open)
: svg_name_(svg_name)
, scale_factor_(scale_factor)
, verbose_(verbose)
, auto_open_(auto_open)
{}
int operator()(mapnik::marker_svg const& marker) const
{
@ -112,12 +109,9 @@ struct main_marker_visitor
mapnik::svg::vertex_stl_adapter<mapnik::svg::svg_path_storage> stl_storage(marker.get_data()->source());
mapnik::svg::svg_path_adapter svg_path(stl_storage);
mapnik::svg::renderer_agg<
mapnik::svg_path_adapter,
mapnik::svg_attribute_type,
renderer_solid,
agg::pixfmt_rgba32_pre > svg_renderer_this(svg_path,
marker.get_data()->attributes());
mapnik::svg::
renderer_agg<mapnik::svg_path_adapter, mapnik::svg_attribute_type, renderer_solid, agg::pixfmt_rgba32_pre>
svg_renderer_this(svg_path, marker.get_data()->attributes());
svg_renderer_this.render(ras_ptr, sl, renb, mtx, opacity, bbox);
@ -172,6 +166,7 @@ int main (int argc,char** argv)
try
{
po::options_description desc("svg2png utility");
// clang-format off
desc.add_options()
("help,h", "produce usage message")
("version,V","print version string")
@ -181,7 +176,7 @@ int main (int argc,char** argv)
("scale-factor", po::value<double>(), "provide scaling factor (default: 1.0)")
("svg",po::value<std::vector<std::string> >(),"svg file to read")
;
// clang-format on
po::positional_options_description p;
p.add("svg", -1);
po::variables_map vm;
@ -242,17 +237,16 @@ int main (int argc,char** argv)
{
std::clog << "found: " << svg_name << "\n";
}
std::shared_ptr<mapnik::marker const> marker = mapnik::marker_cache::instance().find(svg_name, false, strict);
std::shared_ptr<mapnik::marker const> marker =
mapnik::marker_cache::instance().find(svg_name, false, strict);
main_marker_visitor visitor(svg_name, scale_factor, verbose, auto_open);
status = mapnik::util::apply_visitor(visitor, *marker);
}
}
catch (std::exception const& ex)
} catch (std::exception const& ex)
{
std::clog << "Exception caught:" << ex.what() << std::endl;
return -1;
}
catch (...)
} catch (...)
{
std::clog << "Exception of unknown type!" << std::endl;
return -1;