we now have DBI::Pg >3, so we can properly handle utf8.
[osmrrze.git] / scripts / osmbuildings-json-generator.pl
index 5cd216f370c7e975a1c6f5dde7c327a9ca8dd965..d0f5a986fb11289194f974420dd2b396b5305aff 100755 (executable)
@@ -143,11 +143,11 @@ $tx2 = $dbh->selectrow_array("select ST_X(ST_transform(ST_GeomFromText('POINT($x
 $ty2 = $dbh->selectrow_array("select ST_Y(ST_transform(ST_GeomFromText('POINT($x2 $y2)', 4326), 900913))");
 # Why do crappy perl libraries always insist on using an "Object Oriented" interface
 # for things where this does not make any sense at all?
-my $crappyjsonoo = JSON->new()->latin1(); # Note: has to be latin1 instead of utf8
-# for now, because the strings we get from the DB are WTF8, but are not marked
-# as such. Therefore, we must not tell the JSON module to do utf8(), because
-# otherwise it would encode the WTF8 twice.
-# This will change with DBI::Pg modules >3 where UTF8 is properly handled.
+my $crappyjsonoo = JSON->new()->utf8(); # Note: This has to be latin1 instead of
+# utf8 for DBI::Pg modules with version <3, because in that case, the strings we
+# get from the DB are WTF8, but are not marked as such. Therefore, we must not
+# tell the JSON module to do utf8(), because otherwise it would encode the WTF8
+# twice.
 if ($verblev > 0) {
   $crappyjsonoo = $crappyjsonoo->pretty();
 }
@@ -177,7 +177,8 @@ foreach $xtable ('planet_osm_polygon1', 'planet_osm_polygon2', 'planet_osm_line'
     exit(1);
   }
   my $sth2 = $dbh->prepare("select ST_X(points) as x, ST_Y(points) as y"
-                         . " from (select ST_astext((ST_dumppoints(ST_transform(?, 4326))).geom) as points) as points");
+                         . " from (select ST_astext((ST_dumppoints(ST_transform(geometry(?), 4326))).geom) as points) as points");
+  my $sth4 = $dbh->prepare("select (ST_dumprings(?)).geom as rings");
   while ($row = $sth->fetchrow_hashref()) {
     if ($xtable eq 'planet_osm_polygon1') {
       my $hassubparts = $dbh->selectrow_array("select count(*)"
@@ -224,17 +225,33 @@ foreach $xtable ('planet_osm_polygon1', 'planet_osm_polygon2', 'planet_osm_line'
     if (defined($row->{'name'})) {
       $jso->{'name'} = $row->{'name'};
     }
-    unless ($sth2->execute($row->{'way'})) {
-      print(STDERR "Sorry, decoding the way data exploded.\n");
-      exit(1);
+    my @waystodecode = ();
+    {
+      local $sth4->{'PrintError'};  # We're fully aware that the execute can
+      local $sth4->{'PrintWarn'};   # fail, no need to spam about it.
+      if ($sth4->execute($row->{'way'})) {
+        while ((my $nxtway) = $sth4->fetchrow_array()) {
+          push(@waystodecode, $nxtway);
+        }
+      } else {
+        push(@waystodecode, $row->{'way'});
+      }
     }
-    my ($onex, $oney);
-    my @geomarr1 = ();
-    while (($onex, $oney) = $sth2->fetchrow_array()) {
-      my @onecoord = ( sprintf("%.5f", $onex), sprintf("%.5f", $oney));
-      push (@geomarr1, \@onecoord);
+    my @geomarr2 = ();
+    foreach my $nxtway (@waystodecode) {
+      unless ($sth2->execute($nxtway)) {
+        print(STDERR "Sorry, decoding the way data exploded.\n");
+        exit(1);
+      }
+      my ($onex, $oney);
+      my @geomarr1 = ();
+      while (($onex, $oney) = $sth2->fetchrow_array()) {
+        my @onecoord = ( sprintf("%.5f", $onex), sprintf("%.5f", $oney));
+        push (@geomarr1, \@onecoord);
+      }
+      push(@geomarr2, \@geomarr1);
+      $sth2->finish();
     }
-    my @geomarr2 = (\@geomarr1);
     my $geomhsh = { "type" => "Polygon",
                     "coordinates" => \@geomarr2 };
     $jso->{'geometry'} = $geomhsh;
@@ -277,8 +294,8 @@ foreach $xtable ('planet_osm_polygon1', 'planet_osm_polygon2', 'planet_osm_line'
     # Now dump it all.
     print($crappyjsonoo->encode($jso));
   }
-  $sth2->finish();
-  undef($sth2);
+  $sth4->finish();
+  undef($sth4);
   $sth->finish();
   undef($sth);
 }
This page took 0.066047 seconds and 4 git commands to generate.