1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
|
/* Implements the mid-layer processing for osm2pgsql
* using several PostgreSQL tables
*
* This layer stores data read in from the planet.osm file
* and is then read by the backend processing code to
* emit the final geometry-enabled output formats
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <libpq-fe.h>
#include "osmtypes.h"
#include "output.h"
#include "reprojection.h"
#include "output-pgsql.h"
#include "build_geometry.h"
#include "middle-pgsql.h"
/* Set if the output is in lat/lon */
#define SRID (project_getprojinfo()->srs)
enum table_id {
t_point, t_line, t_poly, t_roads
};
/* Tables to output */
static struct {
//enum table_id table;
const char *name;
const char *type;
} tables [] = {
{ name: "%s_point", type: "POINT" },
{ name: "%s_line", type: "LINESTRING"},
{ name: "%s_polygon", type: "POLYGON" },
{ name: "%s_roads", type: "LINESTRING"}
};
static const unsigned int num_tables = sizeof(tables)/sizeof(*tables);
/* Table columns, representing key= tags */
static struct {
const char *name;
const char *type;
const int polygon;
} exportTags[] = {
{"access", "text", 0},
{"admin_level", "text", 0},
{"aeroway", "text", 1},
{"amenity", "text", 1},
{"bicycle", "text", 0},
{"bridge", "text", 0},
{"boundary", "text", 0},
{"building", "text", 1},
{"cutting", "text", 0},
{"embankment","text", 0},
{"foot", "text", 0},
{"highway", "text", 0},
{"horse", "text", 0},
{"junction", "text", 0},
{"landuse", "text", 1},
{"layer", "text", 0},
{"learning", "text", 0},
{"leisure", "text", 1},
{"man_made", "text", 1},
{"military", "text", 1},
{"motorcar", "text", 0},
{"name", "text", 0},
{"natural", "text", 1},
{"oneway", "text", 0},
{"power", "text", 1},
{"place", "text", 0},
{"railway", "text", 0},
{"ref", "text", 0},
{"religion", "text", 0},
{"residence","text", 0},
{"route", "text", 0},
{"sport", "text", 1},
{"tourism", "text", 1},
{"tunnel", "text", 0},
{"waterway", "text", 0},
{"width", "text", 0},
{"wood", "text", 0},
{"z_order", "int4", 0},
{"way_area", "real", 0}
};
static const unsigned int numTags = sizeof(exportTags) / sizeof(*exportTags);
/* Data to generate z-order column and road table */
static struct {
int offset;
const char *highway;
int roads;
} layers[] = {
{ 9, "motorway", 1 },
{ 9, "motorway_link", 1 },
{ 8, "trunk", 1 },
{ 8, "trunk_link", 1 },
{ 7, "primary", 1 },
{ 7, "primary_link", 1 },
{ 6, "secondary", 1 },
{ 6, "secondary_link",1 },
// 5 = railway
{ 4, "tertiary", 0 },
{ 4, "tertiary_link", 0 },
{ 3, "residential", 0 },
{ 3, "unclassified", 0 },
{ 3, "minor", 0 }
};
static const unsigned int nLayers = (sizeof(layers)/sizeof(*layers));
static PGconn **sql_conns;
void sql_out(PGconn *sql_conn, const char *sql, int len)
{
unsigned int r;
r = PQputCopyData(sql_conn, sql, len);
if (r != 1) {
fprintf(stderr, "bad result %d, line '%.*s'\n", r, len, sql);
exit_nicely();
}
}
static int add_z_order_polygon(struct keyval *tags, int *roads)
{
const char *natural = getItem(tags, "natural");
const char *layer = getItem(tags, "layer");
#if 0
const char *landuse = getItem(tags, "landuse");
const char *leisure = getItem(tags, "leisure");
#endif
int z_order, l;
char z[13];
/* Discard polygons with the tag natural=coastline */
if (natural && !strcmp(natural, "coastline"))
return 1;
l = layer ? strtol(layer, NULL, 10) : 0;
z_order = 10 * l;
*roads = 0;
#if 0
/* - New scheme uses layer + way area to control render order, not tags */
/* landuse & leisure tend to cover large areas and we want them under other polygons */
if (landuse) z_order -= 2;
if (leisure) z_order -= 1;
#endif
snprintf(z, sizeof(z), "%d", z_order);
addItem(tags, "z_order", z, 0);
return 0;
}
static int add_z_order_line(struct keyval *tags, int *roads)
{
const char *layer = getItem(tags, "layer");
const char *highway = getItem(tags, "highway");
const char *bridge = getItem(tags, "bridge");
const char *tunnel = getItem(tags, "tunnel");
const char *railway = getItem(tags, "railway");
int z_order = 0;
int l;
unsigned int i;
char z[13];
l = layer ? strtol(layer, NULL, 10) : 0;
z_order = 10 * l;
*roads = 0;
if (highway) {
for (i=0; i<nLayers; i++) {
if (!strcmp(layers[i].highway, highway)) {
z_order += layers[i].offset;
*roads = layers[i].roads;
break;
}
}
}
if (railway && strlen(railway)) {
z_order += 5;
*roads = 1;
}
if (bridge && (!strcmp(bridge, "true") || !strcmp(bridge, "yes") || !strcmp(bridge, "1")))
z_order += 10;
if (tunnel && (!strcmp(tunnel, "true") || !strcmp(tunnel, "yes") || !strcmp(tunnel, "1")))
z_order -= 10;
snprintf(z, sizeof(z), "%d", z_order);
addItem(tags, "z_order", z, 0);
return 0;
}
static int add_z_order(struct keyval* tags, int polygon, int *roads)
{
return polygon ? add_z_order_polygon(tags, roads) : add_z_order_line(tags, roads);
}
static void fix_motorway_shields(struct keyval *tags)
{
const char *highway = getItem(tags, "highway");
const char *name = getItem(tags, "name");
const char *ref = getItem(tags, "ref");
/* The current mapnik style uses ref= for motorway shields but some roads just have name= */
if (!highway || strcmp(highway, "motorway"))
return;
if (name && !ref)
addItem(tags, "ref", name, 0);
}
/* Append all alternate name:xx on to the name tag with space sepatators.
* name= always comes first, the alternates are in no particular order
* Note: A new line may be better but this does not work with Mapnik
*
* <tag k="name" v="Ben Nevis" />
* <tag k="name:gd" v="Ben Nibheis" />
* becomes:
* <tag k="name" v="Ben Nevis Ben Nibheis" />
*/
void compress_tag_name(struct keyval *tags)
{
const char *name = getItem(tags, "name");
struct keyval *name_ext = getMatches(tags, "name:");
struct keyval *p;
char out[2048];
if (!name_ext)
return;
out[0] = '\0';
if (name) {
strncat(out, name, sizeof(out)-1);
strncat(out, " ", sizeof(out)-1);
}
while((p = popItem(name_ext)) != NULL) {
/* Exclude name:source = "dicataphone" and duplicates */
if (strcmp(p->key, "name:source") && !strstr(out, p->value)) {
strncat(out, p->value, sizeof(out)-1);
strncat(out, " ", sizeof(out)-1);
}
freeItem(p);
}
free(name_ext);
// Remove trailing space
out[strlen(out)-1] = '\0';
//fprintf(stderr, "*** New name: %s\n", out);
updateItem(tags, "name", out);
}
static void pgsql_out_cleanup(void)
{
unsigned int i;
if (!sql_conns)
return;
for (i=0; i<num_tables; i++) {
if (sql_conns[i]) {
PQfinish(sql_conns[i]);
sql_conns[i] = NULL;
}
}
free(sql_conns);
sql_conns = NULL;
}
/* example from: pg_dump -F p -t planet_osm gis
COPY planet_osm (osm_id, name, place, landuse, leisure, "natural", man_made, waterway, highway, railway, amenity, tourism, learning, building, bridge, layer, way) FROM stdin;
17959841 \N \N \N \N \N \N \N bus_stop \N \N \N \N \N \N -\N 0101000020E610000030CCA462B6C3D4BF92998C9B38E04940
17401934 The Horn \N \N \N \N \N \N \N \N pub \N \N \N \N -\N 0101000020E6100000C12FC937140FD5BFB4D2F4FB0CE04940
...
mine - 01 01000000 48424298424242424242424256427364
psql - 01 01000020 E6100000 30CCA462B6C3D4BF92998C9B38E04940
01 01000020 E6100000 48424298424242424242424256427364
0x2000_0000 = hasSRID, following 4 bytes = srid, not supported by geos WKBWriter
Workaround - output SRID=4326;<WKB>
*/
static int pgsql_out_node(int id, struct keyval *tags, double node_lat, double node_lon)
{
char sql[2048], *v;
unsigned int i, export = 0;
PGconn *sql_conn = sql_conns[t_point];
for (i=0; i < numTags; i++) {
if ((v = getItem(tags, exportTags[i].name))) {
export = 1;
break;
}
}
if (!export)
return 0;
//compress_tag_name(tags);
sprintf(sql, "%d\t", id);
sql_out(sql_conn, sql, strlen(sql));
for (i=0; i < numTags; i++) {
if ((v = getItem(tags, exportTags[i].name)))
escape(sql, sizeof(sql), v);
else
sprintf(sql, "\\N");
sql_out(sql_conn, sql, strlen(sql));
sql_out(sql_conn, "\t", 1);
}
sprintf(sql, "SRID=%d;POINT(%.15g %.15g)", SRID, node_lon, node_lat);
sql_out(sql_conn, sql, strlen(sql));
sql_out(sql_conn, "\n", 1);
return 0;
}
static void write_wkts(int id, struct keyval *tags, const char *wkt, PGconn *sql_conn)
{
unsigned int j;
char sql[2048];
const char*v;
sprintf(sql, "%d\t", id);
sql_out(sql_conn, sql, strlen(sql));
for (j=0; j < numTags; j++) {
if ((v = getItem(tags, exportTags[j].name)))
escape(sql, sizeof(sql), v);
else
sprintf(sql, "\\N");
sql_out(sql_conn, sql, strlen(sql));
sql_out(sql_conn, "\t", 1);
}
sprintf(sql, "SRID=%d;", SRID);
sql_out(sql_conn, sql, strlen(sql));
sql_out(sql_conn, wkt, strlen(wkt));
sql_out(sql_conn, "\n", 1);
}
void add_parking_node(int id, struct keyval *tags, double node_lat, double node_lon)
{
// insert into planet_osm_point(osm_id,name,amenity,way) select osm_id,name,amenity,centroid(way) from planet_osm_polygon where amenity='parking';
const char *access = getItem(tags, "access");
const char *amenity = getItem(tags, "amenity");
const char *name = getItem(tags, "name");
struct keyval nodeTags;
if (!amenity || strcmp(amenity, "parking"))
return;
// Do not add a 'P' symbol if access is defined and something other than public.
if (access && strcmp(access, "public"))
return;
initList(&nodeTags);
addItem(&nodeTags, "amenity", amenity, 0);
if (name)
addItem(&nodeTags, "name", name, 0);
if (access)
addItem(&nodeTags, "access", access, 0);
pgsql_out_node(id, &nodeTags, node_lat, node_lon);
resetList(&nodeTags);
}
unsigned int pgsql_filter_tags(struct keyval *tags, int *polygon)
{
const char *v;
unsigned int i, filter = 1;
*polygon = 0;
for (i=0; i < numTags; i++) {
if ((v = getItem(tags, exportTags[i].name))) {
filter = 0;
*polygon |= exportTags[i].polygon;
if (*polygon)
break;
}
}
return filter;
}
/*
COPY planet_osm (osm_id, name, place, landuse, leisure, "natural", man_made, waterway, highway, railway, amenity, tourism, learning, bu
ilding, bridge, layer, way) FROM stdin;
198497 Bedford Road \N \N \N \N \N \N residential \N \N \N \N \N \N \N 0102000020E610000004000000452BF702B342D5BF1C60E63BF8DF49406B9C4D470037D5BF5471E316F3DF4940DFA815A6EF35D5BF9AE95E27F5DF4940B41EB
E4C1421D5BF24D06053E7DF4940
212696 Oswald Road \N \N \N \N \N \N minor \N \N \N \N \N \N \N 0102000020E610000004000000467D923B6C22D5BFA359D93EE4DF4940B3976DA7AD11D5BF84BBB376DBDF4940997FF44D9A06D5BF4223D8B8FEDF49404D158C4AEA04D
5BF5BB39597FCDF4940
*/
static int pgsql_out_way(int id, struct keyval *tags, struct osmNode *nodes, int count)
{
int polygon = 0, roads = 0;
char *wkt;
double area, interior_lat, interior_lon;
const char *multipolygon = getItem(tags, "multipolygon");
// If the way has been part of a multipolygon then skip
if (multipolygon)
return 0;
if (pgsql_filter_tags(tags, &polygon) || add_z_order(tags, polygon, &roads))
return 0;
//compress_tag_name(tags);
fix_motorway_shields(tags);
wkt = get_wkt_simple(nodes, count, polygon, &area, &interior_lon, &interior_lat);
if (wkt && strlen(wkt)) {
/* FIXME: there should be a better way to detect polygons */
if (!strncmp(wkt, "POLYGON", strlen("POLYGON"))) {
if (area > 0.0) {
char tmp[32];
snprintf(tmp, sizeof(tmp), "%f", area);
addItem(tags, "way_area", tmp, 0);
}
write_wkts(id, tags, wkt, sql_conns[t_poly]);
add_parking_node(id, tags, interior_lat, interior_lon);
} else {
write_wkts(id, tags, wkt, sql_conns[t_line]);
if (roads)
write_wkts(id, tags, wkt, sql_conns[t_roads]);
}
}
free(wkt);
return 0;
}
static int pgsql_out_relation(int id, struct keyval *rel_tags, struct osmNode **xnodes, struct keyval **xtags, int *xcount)
{
unsigned int i, wkt_size;
double interior_lat, interior_lon;
int polygon = 0, roads = 0;
struct keyval tags, *p;
#if 0
fprintf(stderr, "Got relation with counts:");
for (i=0; xcount[i]; i++)
fprintf(stderr, " %d", xcount[i]);
fprintf(stderr, "\n");
#endif
// Aggregate tags from all polygons with the relation,
// no idea is this is a good way to deal with differing tags across the ways or not
initList(&tags);
p = rel_tags->next;
while (p != rel_tags) {
addItem(&tags, p->key, p->value, 1);
p = p->next;
}
for (i=0; xtags[i]; i++) {
p = xtags[i]->next;
while (p != xtags[i]) {
addItem(&tags, p->key, p->value, 1);
p = p->next;
}
}
if (pgsql_filter_tags(&tags, &polygon) || add_z_order(&tags, polygon, &roads)) {
resetList(&tags);
return 0;
}
wkt_size = build_geometry(id, xnodes, xcount);
if (!wkt_size) {
resetList(&tags);
return 0;
}
for (i=0;i<wkt_size;i++)
{
char *wkt = get_wkt(i);
if (strlen(wkt)) {
/* FIXME: there should be a better way to detect polygons */
if (!strncmp(wkt, "POLYGON", strlen("POLYGON"))) {
double area = get_area(i);
if (area > 0.0) {
char tmp[32];
snprintf(tmp, sizeof(tmp), "%f", area);
addItem(&tags, "way_area", tmp, 0);
}
write_wkts(id, &tags, wkt, sql_conns[t_poly]);
get_interior(i, &interior_lat, &interior_lon);
add_parking_node(id, &tags, interior_lat, interior_lon);
} else {
write_wkts(id, &tags, wkt, sql_conns[t_line]);
if (roads)
write_wkts(id, &tags, wkt, sql_conns[t_roads]);
}
}
free(wkt);
}
clear_wkts();
// Mark each member so that we can skip them during iterate_ways
for (i=0; xcount[i]; i++)
addItem(xtags[i], "multipolygon", "1", 0);
resetList(&tags);
return 0;
}
static int pgsql_out_start(const char *conninfo, const char *prefix, int append)
{
char sql[1024], tmp[128];
PGresult *res;
unsigned int i,j;
/* We use a connection per table to enable the use of COPY_IN */
sql_conns = calloc(num_tables, sizeof(PGconn *));
assert(sql_conns);
for (i=0; i<num_tables; i++) {
PGconn *sql_conn;
/* Substitute prefix into name of table */
{
char *temp = malloc( strlen(prefix) + strlen(tables[i].name) + 1 );
sprintf( temp, tables[i].name, prefix );
tables[i].name = temp;
}
fprintf(stderr, "Setting up table: %s\n", tables[i].name);
sql_conn = PQconnectdb(conninfo);
/* Check to see that the backend connection was successfully made */
if (PQstatus(sql_conn) != CONNECTION_OK) {
fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(sql_conn));
exit_nicely();
}
sql_conns[i] = sql_conn;
if (!append) {
sprintf( sql, "DROP TABLE %s;", tables[i].name);
res = PQexec(sql_conn, sql);
PQclear(res); /* Will be an error if table does not exist */
}
res = PQexec(sql_conn, "BEGIN");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "BEGIN %s failed: %s\n", tables[i].name, PQerrorMessage(sql_conn));
PQclear(res);
exit_nicely();
}
PQclear(res);
if (!append) {
sprintf(sql, "CREATE TABLE %s ( osm_id int4", tables[i].name );
for (j=0; j < numTags; j++) {
sprintf(tmp, ",\"%s\" %s", exportTags[j].name, exportTags[j].type);
strcat(sql, tmp);
}
strcat(sql, " );\n");
sprintf( sql + strlen(sql), "SELECT AddGeometryColumn('%s', 'way', %d, '%s', 2 );\n",
tables[i].name, SRID, tables[i].type );
sprintf( sql + strlen(sql), "ALTER TABLE %s ALTER COLUMN way SET NOT NULL;\n", tables[i].name);
res = PQexec(sql_conn, sql);
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "%s failed: %s\n", sql, PQerrorMessage(sql_conn));
PQclear(res);
exit_nicely();
}
PQclear(res);
}
sql[0] = '\0';
sprintf(sql, "COPY %s FROM STDIN", tables[i].name);
res = PQexec(sql_conn, sql);
if (PQresultStatus(res) != PGRES_COPY_IN) {
fprintf(stderr, "%s failed: %s\n", sql, PQerrorMessage(sql_conn));
PQclear(res);
exit_nicely();
}
PQclear(res);
}
return 0;
}
#define __unused __attribute__ ((unused))
static void pgsql_out_stop(__unused int append)
{
char sql[1024], tmp[128];
PGresult *res;
unsigned int i;
for (i=0; i<num_tables; i++) {
PGconn *sql_conn = sql_conns[i];
sprintf(tmp, "%d", i);
/* Terminate any pending COPY */
int stop = PQputCopyEnd(sql_conn, NULL);
if (stop != 1) {
fprintf(stderr, "COPY_END for %s failed: %s\n", tables[i].name, PQerrorMessage(sql_conn));
exit_nicely();
}
res = PQgetResult(sql_conn);
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "COPY_END for %s failed: %s\n", tables[i].name, PQerrorMessage(sql_conn));
PQclear(res);
exit_nicely();
}
PQclear(res);
// Commit transaction
res = PQexec(sql_conn, "COMMIT");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "COMMIT %s failed: %s\n", tables[i].name, PQerrorMessage(sql_conn));
PQclear(res);
exit_nicely();
}
PQclear(res);
sql[0] = '\0';
strcat(sql, "ANALYZE ");
strcat(sql, tables[i].name);
strcat(sql, ";\n");
strcat(sql, "CREATE TABLE tmp AS SELECT * FROM ");
strcat(sql, tables[i].name);
strcat(sql, " ORDER BY way;\n");
strcat(sql, "DROP TABLE ");
strcat(sql, tables[i].name);
strcat(sql, ";\n");
strcat(sql, "ALTER TABLE tmp RENAME TO ");
strcat(sql, tables[i].name);
strcat(sql, ";\n");
strcat(sql, "CREATE INDEX ");
strcat(sql, tables[i].name);
strcat(sql, "_index ON ");
strcat(sql, tables[i].name);
strcat(sql, " USING GIST (way GIST_GEOMETRY_OPS);\n");
strcat(sql, "GRANT SELECT ON ");
strcat(sql, tables[i].name);
strcat(sql, " TO PUBLIC;\n");
strcat(sql, "ANALYZE ");
strcat(sql, tables[i].name);
strcat(sql, ";\n");
res = PQexec(sql_conn, sql);
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "%s failed: %s\n", sql, PQerrorMessage(sql_conn));
PQclear(res);
exit_nicely();
}
PQclear(res);
}
pgsql_out_cleanup();
}
struct output_t out_pgsql = {
start: pgsql_out_start,
stop: pgsql_out_stop,
cleanup: pgsql_out_cleanup,
node: pgsql_out_node,
way: pgsql_out_way,
relation: pgsql_out_relation
};
|