Dropped old perl-and-SQL converter

This commit is contained in:
Komяpa 2010-07-06 18:39:14 +03:00
parent a507e78b10
commit 96561a243b
6 changed files with 0 additions and 469 deletions

View file

@ -1,15 +0,0 @@
#! /bin/bash
rm -f $1
cat osm.sql | grep -vi "CREATE INDEX" | sqlite3 $1
cat spatial.sql | sqlite3 $1
echo "Importing $2 into $1"
./import_osm.pl $1 < $2
echo "Creating indexes"
cat osm.sql | grep -i "CREATE INDEX" | sqlite3 $1
echo 'Analyzing'
sqlite3 $1 'analyze'
echo 'Creating spatial index'
sqlite3 $1 'insert into way_index (id,minLat,maxLat,minLon,maxLon) select way_id,min(lat),max(lat),min(lon),max(lon) from way_node,node where way_node.node_id=node.id group by way_id'
echo 'Creating way coords'
sqlite3 $1 'insert into way_coord_text (id,lat,lon) select way_id,group_concat(lat),group_concat(lon) from (select way_id,lat,lon from way_node,node where node_id=id order by way_id,node_number) group by way_id'

View file

@ -1,164 +0,0 @@
#! /usr/bin/perl -w
use XML::Parser;
use DBI;
use strict;
my $dbname = shift;
my $count = 0;
my $parser = new XML::Parser(ErrorContext => 2);
$parser->setHandlers(Char => \&char_handler,
Start => \&start_handler,
End => \&end_handler);
# Default => \&default_handler);
my $file = shift;
my $dbargs = {AutoCommit => 0,
PrintError => 1,
};
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname","","",$dbargs);
$dbh->do("PRAGMA synchronous = OFF");
if ($dbh->err()) { die "$DBI::errstr\n"; }
print "*\n";
if(defined $file and -f $file){
$parser->parsefile($file);
}
else{
$parser->parse(\*STDIN);
}
$dbh->commit();
$dbh->disconnect();
################
## End of main
################
my $id;
my %tags;
my %node_attrs;
my $node_count = 0;
my $node_id;
my $relation_count = 0;
my $sequence;
my $node_count;
my $way_count;
my $relation_count;
my $way_id;
my $relation_id;
my $node_number;
sub start_handler
{
my( $parseinst, $element, %attrs ) = @_;
SWITCH: {
if ($element eq "node") {
$dbh->do("insert into node (id, lat, lon) values ($attrs{id},$attrs{lat},$attrs{lon})");
if ($dbh->err()) { die "$DBI::errstr\n"; }
$node_count += 1;
if($node_count % 1000 == 0){
print "nodes: ",$node_count,"\n";
}
$node_id = $attrs{id};
if($node_count % 100000 == 0){
$dbh->commit();
if ($dbh->err()) { die "$DBI::errstr\n"; }
# $dbh->begin();
# if ($dbh->err()) { die "$DBI::errstr\n"; }
}
last SWITCH;
}
if ($element eq "way") {
$dbh->do("insert into way (id) values ($attrs{id})");
if ($dbh->err()) { die "$DBI::errstr\n"; }
$way_id = $attrs{id};
$node_number = 0;
$way_count += 1;
if($way_count % 1000 == 0){
print "ways: ",$way_count,"\n";
}
last SWITCH;
}
if ($element eq "relation") {
$dbh->do("insert into relation (id) values ($attrs{id})");
if ($dbh->err()) { die "$DBI::errstr\n"; }
$relation_id = $attrs{id};
$relation_count += 1;
$sequence = 0;
if($relation_count % 1000 == 0){
print "relations: ",$relation_count,"\n";
}
last SWITCH;
}
if ($element eq "nd" and defined $way_id) {
$dbh->do("insert into way_node (way_id,node_id,node_number) values ($way_id,$attrs{ref},$node_number)");
if ($dbh->err()) { die "$DBI::errstr\n"; }
$node_number += 1;
last SWITCH;
}
if ($element eq "tag" ) {
if(defined $way_id){
my $q = "insert into way_tag (id,key,value) values ($way_id,".$dbh->quote($attrs{k}).",".$dbh->quote($attrs{v}).")";
$dbh->do("$q");
if ($dbh->err()) { print $q,"\n"; die "$DBI::errstr\n"; }
}
elsif(defined $node_id){
my $q = "insert into node_tag (id,key,value) values ($node_id,".$dbh->quote($attrs{k}).",".$dbh->quote($attrs{v}).")";
$dbh->do("$q");
if ($dbh->err()) { print $q,"\n"; die "$DBI::errstr\n"; }
}
elsif(defined $relation_id){
my $q = "insert into relation_tag (id,key,value) values ($relation_id,".$dbh->quote($attrs{k}).",".$dbh->quote($attrs{v}).")";
$dbh->do("$q");
if ($dbh->err()) { print $q,"\n"; die "$DBI::errstr\n"; }
}
last SWITCH;
}
if ($element eq "member" ) {
my $q;
if($attrs{type} eq 'node'){
$q = "insert into relation_member_node (id,node_id,role,sequence_number) values ($relation_id,".$dbh->quote($attrs{ref}).",".$dbh->quote($attrs{role}).",$sequence)";
}
if($attrs{type} eq 'way'){
$q = "insert into relation_member_way (id,way_id,role,sequence_number) values ($relation_id,".$dbh->quote($attrs{ref}).",".$dbh->quote($attrs{role}).",$sequence)";
}
if($attrs{type} eq 'relation'){
$q = "insert into relation_member_relation (id,relation_id,role,sequence_number) values ($relation_id,".$dbh->quote($attrs{ref}).",".$dbh->quote($attrs{role}).",$sequence)";
}
$sequence += 1;
if(defined $q){
$dbh->do("$q");
if ($dbh->err()) { print $q,"\n"; die "$DBI::errstr\n"; }
}
}
}
return;
}
sub end_handler
{
my( $parseinst, $element ) = @_;
if($element eq 'way'){
undef $way_id;
}elsif($element eq 'node'){
undef $node_id;
}elsif($element eq 'relation'){
undef $relation_id;
}
return;
}
sub char_handler
{
# This is just here to reduce the noise seen by
# the default handler
return;
} # End of char_handler

View file

@ -1,149 +0,0 @@
#!/usr/bin/perl -w
use DBI;
use Time::HiRes qw(gettimeofday tv_interval);
use strict;
use File::Spec;
use navigator_lib;
my $dbname = $ARGV[0] or die "Usage: ./maketiles.pl database.db";
my $dbargs = {AutoCommit => 0,
PrintError => 1,
};
my $latmin;
my $latmax;
my $lonmin;
my $lonmax;
my $latcenter = 55.75;
my $loncenter = 37.62;
#lat=55.7543&lon=37.6211
#my ( $width, $height ) = ( 240, 320 );
#my ( $width, $height ) = ( 640,480 );
#my $zoom = $width/0.02;
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname","","",$dbargs);
#$dbh->do("PRAGMA synchronous = OFF");
if ($dbh->err()) { die "$DBI::errstr\n"; }
my $i;
my $j;
my $n = 50;
my $tile_step = 0.01;
for($i=-$n;$i<=$n;$i+=1){
for($j=-$n;$j<=$n;$j+=1){
$latmin = $latcenter + $i*$tile_step;
$latmax = $latmin + $tile_step;
$lonmin = $loncenter + $j*$tile_step;
$lonmax = $lonmin + $tile_step;
my $filename = latlon_to_filename($latmin, $lonmin);
my ($vol, $dir, $fname) = File::Spec->splitpath($filename);
# print $vol,$dir," xxx\n";
my $p = '.';
foreach my $cd (File::Spec->splitdir($dir)){
$p = File::Spec->catdir($p, $cd);
mkdir $p;
}
open my $file, '>', $filename or die "create map file: $!";
my $q = 'select way_coord_text.id,lat,lon,key,value from way_index,way_tag,way_coord_text where maxLat >= '.$latmin.' and minLat <= '.$latmax.' and maxLon >= '.$lonmin.' and minLon <= '.$lonmax.' and way_index.id=way_coord_text.id and way_coord_text.id = way_tag.id and (key like \'highway\' or key = \'natural\' or key = \'landuse\'or key=\'building\' or key=\'waterway\' or key=\'leisure\' )';
print $q,"\n";
my $t0 = [gettimeofday];
my $res = $dbh->selectall_arrayref($q);
my $el1 = tv_interval ( $t0, [gettimeofday]);
if ($dbh->err()) { die "$DBI::errstr\n"; }
my $r;
my $way_count=0;
my $node_count=0;
foreach $r (@$res){
my @wlat = split(',', $r->[1]);
my @wlon = split(',', $r->[2]);
my @coord;
$way_count += 1;
while(@wlat){
my ($sx, $sy) = ((shift @wlat), (shift @wlon));
push @coord, $sx, $sy;
$node_count += 1;
}
my $k = $r->[3];
my $v = $r->[4];
if($k eq 'highway' and ($v eq 'primary' or $v eq 'motorway' or $v eq 'trunk')){
line1($file, $r->[0], 1, @coord);
}
elsif($k eq 'highway' and ($v eq 'motorway_link' or $v eq 'trunk_link' or $v eq 'primary_link')){
line1($file, $r->[0], 2, @coord);
}
elsif($k eq 'highway' and $v eq 'secondary'){
line1($file, $r->[0], 3, @coord);
}
elsif($k eq 'highway' and ($v eq 'tertiary' or $v eq 'residential' or $v eq 'living_street')){
line1($file, $r->[0], 4, @coord);
}
elsif($k eq 'highway' and ($v eq 'service' or $v eq 'unclassified')){
line1($file, $r->[0], 5, @coord);
}
elsif($k eq 'building' and $v eq 'yes'){
poly($file, $r->[0], 6, @coord);
}
elsif($k eq 'highway' and $v eq 'footway' or $v eq 'path' or $v eq 'track'){
}
elsif(($k eq 'natural' and $v eq 'wood') or ($k eq 'landuse' and $v eq 'forest') or ($k eq 'leisure' and $v eq 'park')){
poly($file, $r->[0], 7, @coord);
}
elsif($k eq 'highway'){
line1($file, $r->[0], 8, @coord);
}
elsif($k eq 'landuse' and $v eq 'industrial'){
poly($file, $r->[0], 9, @coord);
}
elsif(($k eq 'natural' and $v eq 'water') or ($k eq 'waterway' and $v eq 'riverbank')){
poly($file, $r->[0], 10, @coord);
}
elsif($k eq 'landuse' and $v eq 'residential'){
poly($file, $r->[0], 11, @coord);
}
elsif(($k eq 'waterway' and $v eq 'river')){
line1($file, $r->[0], 12, @coord);
}
elsif(($k eq 'waterway' and $v eq 'stream')){
line1($file, $r->[0], 13, @coord);
}
elsif(($k eq 'landuse' and $v eq 'allotments')){
poly($file, $r->[0], 14, @coord);
}
elsif(($k eq 'landuse')){
poly($file, $r->[0], 15, @coord);
}
}
my $el2 = tv_interval ( $t0, [gettimeofday]);
print "Ways: $way_count nodes: $node_count\n";
print "SQL: $el1 draw: ",$el2-$el1,"\n";
close $file;
}
}
sub line1{
my $a;
my $f = shift;
my $id = shift;
my $t = shift;
print $f "L $id $t";
foreach $a (@_){
print $f " $a";
}
print $f "\n";
return 0;
}
sub poly{
my $a;
my $f = shift;
my $id = shift;
my $t = shift;
print $f "P $id $t";
foreach $a (@_){
print $f " $a";
}
print $f "\n";
return 0;
}

View file

@ -1,14 +0,0 @@
package navigator_lib;
use Exporter;
use base Exporter;
@EXPORT = qw(latlon_to_filename);
sub latlon_to_filename{
my ($i, $j) = @_;
$i = int($i*100+0.5);
$j = int($j*100+0.5);
return "data/".int($i/100).'/'.int($j/100).'/'.($i%100).'/'.($j%100).'.map';
}
1;

View file

@ -1,111 +0,0 @@
create table node (
id INTEGER PRIMARY KEY,
lat FLOAT,
lon FLOAT
);
create table way(
id INTEGER PRIMARY KEY
);
create table way_node (
node_number INTEGER,
way_id INTEGER,
node_id INTEGER,
FOREIGN KEY(way_id) REFERENCES way(id),
FOREIGN KEY(node_id) REFERENCES node(id)
);
CREATE INDEX way_node_way_id_idx on way_node (way_id);
CREATE INDEX way_node_node_id_idx on way_node (node_id);
create table way_tag(
id INTEGER,
key VARCHAR(20),
value VARCHAR(20),
FOREIGN KEY(id) REFERENCES way(id)
);
CREATE INDEX way_tag_id_idx on way_tag (id);
CREATE INDEX way_tag_key_idx on way_tag (key);
CREATE INDEX way_tag_value_idx on way_tag (value);
CREATE INDEX way_tag_key_value_idx on way_tag (key,value);
create table node_tag(
id INTEGER,
key VARCHAR(20),
value VARCHAR(20),
FOREIGN KEY(id) REFERENCES node(id)
);
CREATE INDEX node_tag_id_idx on node_tag (id);
CREATE INDEX node_tag_key_idx on node_tag (key);
CREATE INDEX node_tag_value_idx on node_tag (value);
CREATE INDEX node_tag_key_value_idx on node_tag (key,value);
create table relation(
id INTEGER PRIMARY KEY
);
create table relation_tag(
id INTEGER,
key VARCHAR(20),
value VARCHAR(20),
FOREIGN KEY(id) REFERENCES relation(id)
);
CREATE INDEX relation_tag_id_idx on relation_tag (id);
CREATE INDEX relation_tag_key_idx on relation_tag (key);
CREATE INDEX relation_tag_value_idx on relation_tag (value);
CREATE INDEX relation_tag_key_value_idx on relation_tag (key,value);
create table relation_member_node(
id INTEGER,
node_id INTEGER,
sequence_number INTEGER NOT NULL,
role VARCHAR(20) NOT NULL,
FOREIGN KEY(id) REFERENCES relation(id)
FOREIGN KEY(node_id) REFERENCES node(id)
);
CREATE INDEX relation_member_node_id_idx on relation_member_node (id);
CREATE INDEX relation_member_node_node_id_idx on relation_member_node (node_id);
CREATE INDEX relation_member_node_role_idx on relation_member_node (role);
create table relation_member_way(
id INTEGER,
way_id INTEGER,
sequence_number INTEGER NOT NULL,
role VARCHAR(20) NOT NULL,
FOREIGN KEY(id) REFERENCES relation(id)
FOREIGN KEY(way_id) REFERENCES way(id)
);
CREATE INDEX relation_member_way_id_idx on relation_member_way (id);
CREATE INDEX relation_member_way_way_id_idx on relation_member_way (way_id);
CREATE INDEX relation_member_way_role_idx on relation_member_way (role);
create table relation_member_relation(
id INTEGER,
relation_id INTEGER,
sequence_number INTEGER NOT NULL,
role VARCHAR(20) NOT NULL,
FOREIGN KEY(id) REFERENCES relation(id)
FOREIGN KEY(relation_id) REFERENCES relation(id)
);
CREATE INDEX relation_member_relation_id_idx on relation_member_relation (id);
CREATE INDEX relation_member_relation_relation_id_idx on relation_member_relation (relation_id);
CREATE INDEX relation_member_relation_role_idx on relation_member_relation (role);
-- CREATE VIEW pt_route AS SELECT id,value AS type FROM relation_tag WHERE key = 'route' AND value IN ('bus', 'trolleybus', 'tram');
create view pt_route as select id,value as type from relation_tag where key = 'route' and value in ('bus', 'trolleybus', 'tram') and id in (select id from relation_tag where key = 'type' and value = 'route');
-- create view pt_route_ref as select pt_route.id as id, pt_route.type as type,value as ref from pt_route,relation_tag where pt_route.id = relation_tag.id and key='ref';
create view pt_route_ref as select pt_route.id as id, pt_route.type as type,value as ref from pt_route,relation_tag where relation_tag.id in (select id from pt_route) and pt_route.id = relation_tag.id and key='ref';
-- create view pt_way as select distinct way_id as id,type,ref from relation_member_way,pt_route_ref where relation_member_way.id = pt_route_ref.id;
create view pt_way_ref as select distinct way_id as id,type,ref from pt_route_ref,relation_member_way where relation_member_way.id = pt_route_ref.id;
create view pt_way_id as select distinct way_id as id from pt_route_ref,relation_member_way where relation_member_way.id = pt_route_ref.id;
--create view pt_way_node as select way_id,node_id from way_node where way_id in (select id from pt_way_id);
create view pt_way_node as select way_id,node_id,node_number from way_node where way_id in (select id from pt_way_id);
create view pt_stop as select distinct id,node_id from relation_member_node where id in (select id from pt_route) and role in ('forward:stop','backward:stop','stop');
create view pt_stop_ref as select distinct node_id as id,type,ref from pt_route_ref,relation_member_node where relation_member_node.id = pt_route_ref.id;

View file

@ -1,16 +0,0 @@
CREATE VIRTUAL TABLE way_index USING rtree(
id, -- Integer primary key
minLat, maxLat, -- Minimum and maximum X coordinate
minLon, maxLon -- Minimum and maximum Y coordinate
);
CREATE TABLE way_coord(
id INTEGER PRIMARY KEY,
coord BLOB
);
CREATE TABLE way_coord_text(
id INTEGER PRIMARY KEY,
lat TEXT,
lon TEXT
);