-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiPolygon.pm
140 lines (106 loc) · 2.39 KB
/
MultiPolygon.pm
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
package MultiPolygon;
use strict;
use JSON;
use Data::Dumper;
use Geo::WKT::Simple;
use Mojo::Log;
sub new {
my ($class, $row) = @_;
my $self={
row => $row,
nid => -1,
nodes => {},
tags => {},
parsed => from_json($row->{geojson}),
log => Mojo::Log->new
};
bless $self, $class;
$self->_parse();
return $self;
}
my $nid=-1;
sub node_new {
my ($self, $pos) = @_;
my $posid=sprintf("%s-%s", $pos->[0], $pos->[1]);
if (defined($self->{nodes}{$posid})) {
return $self->{nodes}{$posid};
}
my $n={
id => $self->{nid}--,
lon => $pos->[0],
lat => $pos->[1]
};
$self->{nodes}{$posid}=$n;
return $n;
}
sub _parse {
my ($self) = @_;
my $geom=$self->{parsed}{coordinates};
my $mp=$geom;
if ($self->{parsed}{type} =~ /MultiPolygon/) {
$mp=@{$geom}[0];
}
for my $ring ( @{$mp} ) {
my @r;
for my $pos ( @{$ring} ) {
my $n=$self->node_new($pos);
push @r, $n->{id};
}
push @{$self->{rings}}, \@r;
}
$self->{log}->debug(sprintf("Count: %d", scalar @{$geom}));
$self->{log}->debug(sprintf("Outer: %d", scalar @{$mp}));
}
sub tags_add {
my ($self, $tags) = @_;
foreach my $k ( keys %{$tags} ) {
$self->{tags}{$k}=$tags->{$k};
}
}
sub tag_value {
my ($self, $tag) = @_;
return $self->{tags}{$tag};
}
sub tags {
my ($self, $debug) = @_;
my @tags;
foreach my $k ( keys %{$self->{tags}} ) {
push @tags, sprintf("\t<tag k=\"%s\" v=\"%s\"/>",
$k, $self->{tags}{$k});
}
if (defined($debug) && $debug) {
foreach my $k ( keys %{$self->{row}} ) {
my $v=$self->{row}{$k};
next if ($v =~ /POLYGON/i);
$v=~s/["<>\/]//g;
push @tags, sprintf("\t<tag k=\"debug:%s\" v=\"%s\"/>",
$k, $self->{row}{$k});
}
}
return @tags;
}
sub osmxml {
my ($self, $debug, $tags) = @_;
my @nodes=map {
sprintf('<node id="%d" lat="%f" lon="%f"/>',
$_->{id}, $_->{lat}, $_->{lon});
} sort { $b->{id} <=> $a->{id} } values %{$self->{nodes}};
my @rings=map {
my @nodes=map {
sprintf("\t<nd ref=\"%s\"/>", $_);
} @{$_};
sprintf("<way id=\"%s\">\n%s\n%s\n</way>\n",
$self->{nid}--,
join("\n", @nodes),
join("\n", $self->tags($debug, $tags))
);
} @{$self->{rings}};
# <tag k="building" v="house"/>
return "<?xml version=\"1.0\"?>\n"
. "<osm version=\"0.6\">\n"
. join("\n", @nodes)
. "\n"
. join("\n", @rings)
. "</osm>\n";
};
1;