Skip to content

Commit

Permalink
replace WKTReader with WKBReader
Browse files Browse the repository at this point in the history
  • Loading branch information
lonvia committed Nov 26, 2015
1 parent 6aaffeb commit 8f065f1
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 64 deletions.
14 changes: 7 additions & 7 deletions expire-tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,14 +464,14 @@ void expire_tiles::from_xnodes_line(const multinodelist_t &xnodes)
from_nodes_line(*it);
}

void expire_tiles::from_wkt(const char * wkt, osmid_t osm_id)
void expire_tiles::from_wkb(const char* wkb, osmid_t osm_id)
{
if (Options->expire_tiles_zoom < 0) return;

multinodelist_t xnodes;
bool polygon;

if (!geometry_builder::parse_wkt(wkt, xnodes, &polygon)) {
if (geometry_builder::parse_wkb(wkb, xnodes, &polygon) == 0) {
if (polygon)
from_xnodes_poly(xnodes, osm_id);
else
Expand All @@ -495,15 +495,15 @@ int expire_tiles::from_db(table_t* table, osmid_t osm_id) {
return -1;

//grab the geom for this id
std::unique_ptr<table_t::wkt_reader> wkts = table->get_wkt_reader(osm_id);
auto wkbs = table->get_wkb_reader(osm_id);

//dirty the stuff
const char* wkt = nullptr;
while((wkt = wkts->get_next()))
from_wkt(wkt, osm_id);
const char* wkb = nullptr;
while((wkb = wkbs.get_next()))
from_wkb(wkb, osm_id);

//return how many rows were affected
return wkts->get_count();
return wkbs.get_count();
}

void expire_tiles::merge_and_destroy(expire_tiles &other) {
Expand Down
2 changes: 1 addition & 1 deletion expire-tiles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct expire_tiles : public boost::noncopyable {
int from_bbox(double min_lon, double min_lat, double max_lon, double max_lat);
void from_nodes_line(const nodelist_t &nodes);
void from_nodes_poly(const nodelist_t &nodes, osmid_t osm_id);
void from_wkt(const char * wkt, osmid_t osm_id);
void from_wkb(const char* wkb, osmid_t osm_id);
int from_db(table_t* table, osmid_t osm_id);

struct tile {
Expand Down
9 changes: 5 additions & 4 deletions geometry-builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#include <geos/geom/MultiLineString.h>
#include <geos/geom/Polygon.h>
#include <geos/geom/MultiPolygon.h>
#include <geos/io/WKTReader.h>
#include <geos/io/WKBReader.h>
#include <geos/io/WKBWriter.h>
#include <geos/util/GEOSException.h>
#include <geos/opLinemerge.h>
Expand Down Expand Up @@ -338,12 +338,13 @@ geometry_builder::maybe_wkts_t geometry_builder::get_wkt_split(const nodelist_t
return wkts;
}

int geometry_builder::parse_wkt(const char * wkt, multinodelist_t &nodes, bool *polygon) {
int geometry_builder::parse_wkb(const char* wkb, multinodelist_t &nodes, bool *polygon) {
GeometryFactory gf;
geos::io::WKTReader reader(&gf);
geos::io::WKBReader reader(gf);

*polygon = false;
geom_ptr geometry(reader.read(wkt));
std::stringstream stream(wkb, std::ios_base::in);
geom_ptr geometry(reader.readHEX(stream));
switch (geometry->getGeometryTypeId()) {
// Single geometries
case GEOS_POLYGON:
Expand Down
2 changes: 1 addition & 1 deletion geometry-builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class geometry_builder
typedef std::shared_ptr<geometry_builder::wkt_t> maybe_wkt_t;
typedef std::shared_ptr<std::vector<geometry_builder::wkt_t> > maybe_wkts_t;

static int parse_wkt(const char *wkt, multinodelist_t &nodes, bool *polygon);
static int parse_wkb(const char *wkb, multinodelist_t &nodes, bool *polygon);
maybe_wkt_t get_wkt_simple(const nodelist_t &nodes, int polygon) const;
maybe_wkts_t get_wkt_split(const nodelist_t &nodes, int polygon, double split_at) const;
maybe_wkts_t build_both(const multinodelist_t &xnodes, int make_polygon,
Expand Down
2 changes: 1 addition & 1 deletion output-multi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ int output_multi_t::process_relation(osmid_t id, const memberlist_t &members,
for (const auto wkt: *wkts) {
//TODO: we actually have the nodes in the m_relation_helper and could use them
//instead of having to reparse the wkt in the expiry code
m_expire->from_wkt(wkt.geom.c_str(), -id);
m_expire->from_wkb(wkt.geom.c_str(), -id);
//what part of the code relies on relation members getting negative ids?
copy_to_table(-id, wkt, outtags, make_polygon);
}
Expand Down
4 changes: 2 additions & 2 deletions output-pgsql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ int output_pgsql_t::pgsql_out_relation(osmid_t id, const taglist_t &rel_tags,

tag_t *areatag = 0;
for (const auto& wkt: *wkts) {
expire->from_wkt(wkt.geom.c_str(), -id);
expire->from_wkb(wkt.geom.c_str(), -id);
/* FIXME: there should be a better way to detect polygons */
if (wkt.is_polygon()) {
if ((wkt.area > 0.0) && m_enable_way_area) {
Expand Down Expand Up @@ -219,7 +219,7 @@ int output_pgsql_t::pgsql_out_relation(osmid_t id, const taglist_t &rel_tags,
tag_t *areatag = 0;
wkts = builder.build_polygons(xnodes, m_options.enable_multi, id);
for (const auto& wkt: *wkts) {
expire->from_wkt(wkt.geom.c_str(), -id);
expire->from_wkb(wkt.geom.c_str(), -id);
if ((wkt.area > 0.0) && m_enable_way_area) {
char tmp[32];
snprintf(tmp, sizeof(tmp), "%g", wkt.area);
Expand Down
43 changes: 7 additions & 36 deletions table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ table_t::table_t(const table_t& other):
if (other.sql_conn) {
connect();
//let postgres cache this query as it will presumably happen a lot
pgsql_exec_simple(sql_conn, PGRES_COMMAND_OK, (fmt("PREPARE get_wkt (" POSTGRES_OSMID_TYPE ") AS SELECT ST_AsText(way) FROM %1% WHERE osm_id = $1") % name).str());
pgsql_exec_simple(sql_conn, PGRES_COMMAND_OK, (fmt("PREPARE get_wkb (" POSTGRES_OSMID_TYPE ") AS SELECT way FROM %1% WHERE osm_id = $1") % name).str());
//start the copy
begin();
pgsql_exec_simple(sql_conn, PGRES_COPY_IN, copystr);
Expand Down Expand Up @@ -184,7 +184,7 @@ void table_t::start()
}

//let postgres cache this query as it will presumably happen a lot
pgsql_exec_simple(sql_conn, PGRES_COMMAND_OK, (fmt("PREPARE get_wkt (" POSTGRES_OSMID_TYPE ") AS SELECT ST_AsText(way) FROM %1% WHERE osm_id = $1") % name).str());
pgsql_exec_simple(sql_conn, PGRES_COMMAND_OK, (fmt("PREPARE get_wkb (" POSTGRES_OSMID_TYPE ") AS SELECT way FROM %1% WHERE osm_id = $1") % name).str());

//generate column list for COPY
string cols = "osm_id,";
Expand Down Expand Up @@ -516,47 +516,18 @@ void table_t::escape_type(const string &value, const string &type, string& dst)
escape(value, dst);
}

std::unique_ptr<table_t::wkt_reader> table_t::get_wkt_reader(const osmid_t id)
table_t::wkb_reader table_t::get_wkb_reader(const osmid_t id)
{
//cant get wkt using the prepared statement without stopping the copy first
//cant get wkb using the prepared statement without stopping the copy first
stop_copy();

char const *paramValues[1];
char tmp[16];
snprintf(tmp, sizeof(tmp), "%" PRIdOSMID, id);
paramValues[0] = tmp;

//the prepared statement get_wkt will behave differently depending on the sql_conn
//the prepared statement get_wkb will behave differently depending on the sql_conn
//each table has its own sql_connection with the get_way referring to the appropriate table
PGresult* res = pgsql_execPrepared(sql_conn, "get_wkt", 1, (const char * const *)paramValues, PGRES_TUPLES_OK);
return std::unique_ptr<wkt_reader>(new wkt_reader(res));
}

table_t::wkt_reader::wkt_reader(PGresult* result):result(result), current(0)
{
count = PQntuples(result);
}

table_t::wkt_reader::~wkt_reader()
{
PQclear(result);
}

const char* table_t::wkt_reader::get_next()
{
if (current < count) {
return PQgetvalue(result, (int) current++, 0);
}
return nullptr;
}

size_t table_t::wkt_reader::get_count() const
{
return count;
}

void table_t::wkt_reader::reset()
{
//NOTE: PQgetvalue doc doesn't say if you can call it multiple times with the same row col
current = 0;
PGresult* res = pgsql_execPrepared(sql_conn, "get_wkb", 1, (const char * const *)paramValues, PGRES_TUPLES_OK);
return wkb_reader(res);
}
47 changes: 35 additions & 12 deletions table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,45 @@ class table_t

std::string const& get_name();

//interface from retrieving well known text geometry from the table
struct wkt_reader
struct pg_result_closer
{
friend class table_t;
void operator() (PGresult* result)
{
PQclear(result);
}

};

//interface from retrieving well known binary geometry from the table
class wkb_reader
{
friend table_t;
public:
virtual ~wkt_reader();
const char* get_next();
size_t get_count() const;
void reset();
const char* get_next()
{
if (m_current < m_count) {
return PQgetvalue(m_result.get(), m_current++, 0);
}
return nullptr;
}

int get_count() const { return m_count; }
void reset()
{
//NOTE: PQgetvalue doc doesn't say if you can call it
// multiple times with the same row col
m_current = 0;
}
private:
wkt_reader(PGresult* result);
PGresult* result;
size_t count;
size_t current;
wkb_reader(PGresult* result)
: m_result(result), m_count(PQntuples(result)), m_current(0)
{}

std::unique_ptr<PGresult, pg_result_closer> m_result;
int m_count;
int m_current;
};
std::unique_ptr<wkt_reader> get_wkt_reader(const osmid_t id);
wkb_reader get_wkb_reader(const osmid_t id);

protected:
void connect();
Expand Down

0 comments on commit 8f065f1

Please sign in to comment.