Commit | Line | Data |
---|---|---|
d58b8e00 MPM |
1 | #!/usr/bin/perl -w |
2 | ||
3 | # Our database | |
4 | $dbname = 'osm'; | |
5 | ||
6 | # Default verbosity level? Can be increased with -v, decreased with -q | |
7 | $verblev = 0; | |
8 | ||
9 | # The area which we cover | |
10 | $x1 = 11.01; | |
11 | $y1 = 49.56; | |
12 | $x2 = 11.04; | |
13 | $y2 = 49.58; | |
14 | ||
15 | # There should be no need to touch anything below this line. | |
16 | # --------------------------------------------------------------------------- | |
17 | ||
18 | use DBI; | |
19 | use POSIX qw(strftime mktime); | |
20 | use Pg::hstore; | |
21 | ||
22 | # Par. 0: Level on which this gets printed | |
23 | # Par. 1: Text | |
24 | sub printlev($$) { | |
25 | unless (defined($printlev_atbeginofline)) { $printlev_atbeginofline = 1; } | |
26 | if ($verblev >= $_[0]) { | |
27 | if ($printlev_atbeginofline) { | |
28 | print(strftime("[%Y%m%d-%H%M%S] ", localtime(time()))); | |
29 | } | |
30 | print($_[1]); | |
31 | if ($_[1] =~ m/\n$/) { | |
32 | $printlev_atbeginofline = 1; | |
33 | } else { | |
34 | $printlev_atbeginofline = 0; | |
35 | } | |
36 | } | |
37 | } | |
38 | ||
39 | # Par. 0: default value to return if nothing is defined | |
40 | # Par. 1: the hashref containing our variables. | |
41 | # Par. 2: variables to query. The LAST one that exists will be returned. | |
42 | sub fetchlastofhr($$@) { | |
43 | my $res; my $hr; my @vns; | |
44 | ($res, $hr, @vns) = @_; | |
45 | for (my $i = 0; $i < @vns; $i++) { | |
46 | my $vn = $vns[$i]; | |
47 | if (defined($hr->{$vn})) { | |
48 | $res = $hr->{$vn}; | |
49 | } | |
50 | } | |
51 | return $res; | |
52 | } | |
53 | ||
9de6d037 MPM |
54 | # Helper functions for the CGI variant. Taken from the openstreetmap-Wiki. |
55 | use Math::Trig; | |
56 | sub Project { | |
57 | my ($X, $Y, $Zoom) = @_; | |
58 | my $Unit = 1 / (2 ** $Zoom); | |
59 | my $relY1 = $Y * $Unit; | |
60 | my $relY2 = $relY1 + $Unit; | |
61 | ||
62 | # note: $LimitY = ProjectF(degrees(atan(sinh(pi)))) = log(sinh(pi)+cosh(pi)) = pi | |
63 | # note: degrees(atan(sinh(pi))) = 85.051128.. | |
64 | #my $LimitY = ProjectF(85.0511); | |
65 | ||
66 | # so stay simple and more accurate | |
67 | my $LimitY = pi; | |
68 | my $RangeY = 2 * $LimitY; | |
69 | $relY1 = $LimitY - $RangeY * $relY1; | |
70 | $relY2 = $LimitY - $RangeY * $relY2; | |
71 | my $Lat1 = ProjectMercToLat($relY1); | |
72 | my $Lat2 = ProjectMercToLat($relY2); | |
73 | $Unit = 360 / (2 ** $Zoom); | |
74 | my $Long1 = -180 + $X * $Unit; | |
75 | return ($Lat2, $Long1, $Lat1, $Long1 + $Unit); # S,W,N,E | |
76 | } | |
77 | sub ProjectMercToLat($){ | |
78 | my $MercY = shift; | |
79 | return rad2deg(atan(sinh($MercY))); | |
80 | } | |
81 | sub ProjectF | |
82 | { | |
83 | my $Lat = shift; | |
84 | $Lat = deg2rad($Lat); | |
85 | my $Y = log(tan($Lat) + sec($Lat)); | |
86 | return $Y; | |
87 | } | |
88 | ||
e79ebcd6 MPM |
89 | # print that drops whitespace when not in verbose mode. |
90 | sub printwows($) { | |
91 | my $res = $_[0]; | |
92 | if ($verblev > 0) { | |
93 | print($res); return; | |
94 | } | |
95 | $res =~ s/^\s+//g; | |
96 | $res =~ s/"\s*:\s*/":/g; | |
97 | $res =~ s/[\r\n]//g; | |
98 | print($res); | |
99 | } | |
100 | ||
d58b8e00 MPM |
101 | # ---------------------------------------------------------------------------- |
102 | # main() | |
103 | # ---------------------------------------------------------------------------- | |
104 | ||
9de6d037 MPM |
105 | $iscgi = 0; |
106 | ||
d58b8e00 MPM |
107 | # Parse commandline |
108 | foreach $a (@ARGV) { | |
109 | if ($a eq '-q') { | |
110 | $verblev--; | |
111 | } elsif ($a eq '-v') { | |
112 | $verblev++; | |
9de6d037 MPM |
113 | } elsif ($a eq '--cgi') { |
114 | $iscgi = 1; | |
d58b8e00 MPM |
115 | } else { |
116 | print("Unknown parameter: $a\n"); | |
117 | print("Syntax: $0 [-q] [-v]\n"); | |
118 | print(" -q decreases verbosity, -v increases verbosity.\n"); | |
119 | exit(1); | |
120 | } | |
121 | } | |
122 | ||
9de6d037 MPM |
123 | if (defined($ENV{'REQUEST_URI'})) { |
124 | $iscgi = 1; | |
e79ebcd6 MPM |
125 | print("Content-type: application/json\n"); |
126 | print("Access-Control-Allow-Origin: *\n"); | |
127 | print("Cache-Control: public, max-age=3600\n"); | |
128 | print("\n"); | |
9de6d037 MPM |
129 | unless ($ENV{'REQUEST_URI'} =~ m!(\d+)/(\d+)/(\d+)\.json$!) { |
130 | print("{\n\"_comment\": \"Sorry, query parameters not understood.\"\n}\n"); | |
131 | exit(0); | |
132 | } | |
133 | my $cgiz = int($1); my $cgix = int($2); my $cgiy = int($3); | |
134 | #print("DEBUG: z=$cgiz x=$cgix y=$cgiy\n"); | |
135 | if ($cgiz < 15) { | |
136 | print("{\n\"_comment\": \"No data will be returned at this zoom level.\"\n}\n"); | |
137 | } | |
138 | ($y1, $x1, $y2, $x2) = Project($cgix, $cgiy, $cgiz); | |
139 | #print("DEBUG: mapped to ($x1 $y1) ($x2 $y2)\n"); | |
140 | } | |
141 | ||
d58b8e00 MPM |
142 | unless ($dbh = DBI->connect("dbi:Pg:dbname=$dbname","","")) { |
143 | print(STDERR "Failed to open database. Please try again later.\n"); exit(1); | |
144 | } | |
145 | ||
146 | $tx1 = $dbh->selectrow_array("select ST_X(ST_transform(ST_GeomFromText('POINT($x1 $y1)', 4326), 900913))"); | |
147 | $ty1 = $dbh->selectrow_array("select ST_Y(ST_transform(ST_GeomFromText('POINT($x1 $y1)', 4326), 900913))"); | |
148 | $tx2 = $dbh->selectrow_array("select ST_X(ST_transform(ST_GeomFromText('POINT($x2 $y2)', 4326), 900913))"); | |
149 | $ty2 = $dbh->selectrow_array("select ST_Y(ST_transform(ST_GeomFromText('POINT($x2 $y2)', 4326), 900913))"); | |
150 | my $cntr = 0; | |
9de6d037 MPM |
151 | if ($iscgi == 0) { |
152 | print("// Note: this file has been autogenerated by $0\n"); | |
e79ebcd6 | 153 | printwows("var FAUGeoJSON = {\n"); |
9de6d037 | 154 | } else { |
e79ebcd6 | 155 | printwows("{\n"); |
9de6d037 | 156 | } |
e79ebcd6 MPM |
157 | printwows(" \"type\": \"FeatureCollection\",\n"); |
158 | printwows(" \"features\": [\n"); | |
9de6d037 MPM |
159 | foreach $xtable ('planet_osm_polygon1', 'planet_osm_polygon2', 'planet_osm_line') { # 'planet_osm_polygon', 'planet_osm_line' |
160 | my $querypart1 = " ((tags->'building:part') is not null)"; | |
161 | my $table = 'planet_osm_polygon'; | |
162 | if ($xtable eq 'planet_osm_polygon1') { | |
163 | $querypart1 = " (building is not null)"; | |
164 | } elsif ($xtable eq 'planet_osm_line') { | |
165 | $table = $xtable; | |
166 | } | |
167 | # Note: && in this case is the postgis operator for 'bounding box overlaps'. | |
d58b8e00 | 168 | my $sth = $dbh->prepare("select osm_id, way, building, 'tower:type', name, tags" |
9de6d037 MPM |
169 | . " from $table where $querypart1" |
170 | . " and (way && ST_MakeEnvelope($tx1, $ty1, $tx2, $ty2))"); | |
d58b8e00 | 171 | unless ($sth->execute()) { |
9de6d037 | 172 | print(STDERR "Sorry, database query blew up.!\n"); |
d58b8e00 MPM |
173 | exit(1); |
174 | } | |
175 | my $sth2 = $dbh->prepare("select ST_X(points) as x, ST_Y(points) as y" | |
176 | . " from (select ST_astext((ST_dumppoints(ST_transform(?, 4326))).geom) as points) as points"); | |
177 | while ($row = $sth->fetchrow_hashref()) { | |
9de6d037 MPM |
178 | if ($xtable eq 'planet_osm_polygon1') { |
179 | my $hassubparts = $dbh->selectrow_array("select count(*)" | |
180 | . " from planet_osm_line" | |
181 | . " where ((tags->'building:part') is not null)" | |
182 | . " and (ST_Covers(" . $dbh->quote($row->{'way'}) . ", way) = true)"); | |
183 | # The subparts usually cover the whole building, so we do NOT draw the | |
184 | # whole building but only the subparts. | |
185 | if ($hassubparts > 0) { next; } | |
186 | } | |
d58b8e00 MPM |
187 | my $hstoredec; |
188 | if (defined($row->{'tags'})) { | |
189 | $hstoredec = Pg::hstore::decode($row->{'tags'}); | |
190 | } | |
9de6d037 MPM |
191 | my $levels = fetchlastofhr(1, $hstoredec, 'levels', 'building:levels'); |
192 | my $height = fetchlastofhr($height, $hstoredec, 'height', 'building:height'); | |
193 | unless ($height =~ m/^[0-9.]+$/) { undef($height); } | |
194 | my $minlevel = fetchlastofhr(0, $hstoredec, | |
195 | 'min_levels', 'building:min_levels', 'min_level', 'building:min_level'); | |
196 | my $minheight = fetchlastofhr($minheight, $hstoredec, 'min_height', 'building:min_height'); | |
197 | unless ($minheight =~ m/^[0-9.]+$/) { undef($minheight); } | |
d58b8e00 MPM |
198 | my $wallcolor = fetchlastofhr(undef, $hstoredec, 'building:color', 'building:colour'); |
199 | my $roofcolor = fetchlastofhr(undef, $hstoredec, | |
200 | 'roof:color', 'roof:colour', 'building:roof:color', 'building:roof:colour'); | |
201 | my $roofshape = fetchlastofhr(undef, $hstoredec, 'roof:shape', 'building:roof:shape'); | |
202 | my $roofheight = fetchlastofhr(undef, $hstoredec, 'roof:height', 'building:roof:height'); | |
e79ebcd6 | 203 | my $roofmaterial = fetchlastofhr(undef, $hstoredec, 'roof:material', 'building:roof:material'); |
9de6d037 | 204 | if ($cntr != 0) { |
e79ebcd6 | 205 | printwows(",\n"); |
9de6d037 | 206 | } |
d58b8e00 | 207 | $cntr++; |
e79ebcd6 MPM |
208 | printwows(" {\n"); |
209 | printwows(" \"type\": \"Feature\",\n"); | |
9de6d037 | 210 | if (defined($row->{'osm_id'})) { |
e79ebcd6 | 211 | printwows(" \"id\": " . $row->{'osm_id'} . ",\n"); |
9de6d037 | 212 | } else { |
e79ebcd6 | 213 | printwows(" \"id\": $cntr,\n"); |
9de6d037 MPM |
214 | } |
215 | if (defined($row->{'name'})) { | |
216 | $row->{'name'} =~ s/"//g; | |
e79ebcd6 | 217 | printwows(" \"name\": \"" . $row->{'name'} . "\",\n"); |
9de6d037 | 218 | } |
e79ebcd6 MPM |
219 | printwows(" \"geometry\": {\n"); |
220 | printwows(" \"type\": \"Polygon\",\n"); | |
221 | printwows(" \"coordinates\": [[\n"); | |
d58b8e00 MPM |
222 | unless ($sth2->execute($row->{'way'})) { |
223 | print(STDERR "Sorry, decoding the way data exploded.\n"); | |
224 | exit(1); | |
225 | } | |
226 | my ($onex, $oney); | |
9de6d037 | 227 | my $firstcoord = 1; |
d58b8e00 | 228 | while (($onex, $oney) = $sth2->fetchrow_array()) { |
9de6d037 MPM |
229 | if ($firstcoord == 1) { |
230 | $firstcoord = 0; | |
231 | } else { | |
e79ebcd6 | 232 | printwows(",\n"); |
9de6d037 | 233 | } |
e79ebcd6 | 234 | printwows(sprintf(" [ %.5f, %.5f ]", $onex, $oney)); |
d58b8e00 | 235 | } |
e79ebcd6 MPM |
236 | printwows("\n"); |
237 | printwows(" ]]\n"); | |
238 | printwows(" },\n"); | |
239 | printwows(" \"properties\": {\n"); | |
9de6d037 | 240 | if (defined($height)) { |
e79ebcd6 | 241 | printwows(" \"height\": $height,\n"); |
9de6d037 | 242 | } |
e79ebcd6 MPM |
243 | if (defined($minheight)) { |
244 | printwows(" \"minHeight\": $minheight,\n"); | |
9de6d037 MPM |
245 | } |
246 | if (defined($levels)) { | |
e79ebcd6 | 247 | printwows(" \"levels\": $levels,\n"); |
9de6d037 MPM |
248 | } |
249 | if (defined($minlevel)) { | |
e79ebcd6 | 250 | printwows(" \"minLevel\": $minlevel,\n"); |
9de6d037 | 251 | } |
d58b8e00 | 252 | if (defined($wallcolor)) { |
e79ebcd6 | 253 | printwows(" \"wallColor\": \"$wallcolor\",\n"); |
d58b8e00 MPM |
254 | } |
255 | if (defined($roofcolor)) { | |
9de6d037 | 256 | $roofcolor =~ s/"//g; |
e79ebcd6 | 257 | printwows(" \"roofColor\": \"$roofcolor\",\n"); |
d58b8e00 MPM |
258 | } |
259 | if (defined($roofshape)) { | |
9de6d037 | 260 | $roofshape =~ s/"//g; |
e79ebcd6 MPM |
261 | if ($roofshape eq 'pyramidal') { $roofshape = 'pyramid'; } |
262 | printwows(" \"roofShape\": \"$roofshape\",\n"); | |
263 | } | |
264 | if (defined($roofmaterial)) { | |
265 | $roofshape =~ s/"//g; | |
266 | printwows(" \"roofMaterial\": \"$roofmaterial\",\n"); | |
d58b8e00 MPM |
267 | } |
268 | if (defined($roofheight)) { | |
269 | $roofheight = sprintf("%.1f", $roofheight); | |
e79ebcd6 | 270 | printwows(" \"roofHeight\": \"$roofheight\",\n"); |
d58b8e00 | 271 | } |
e79ebcd6 MPM |
272 | # this line is completely useless, but we need it to guarantee JSON has |
273 | # no trailing comma. osmbuildings will accept JSON with trailing commas | |
274 | # just fine when you give it static, but NOT when requesting it dynamically. | |
275 | printwows(" \"K9\": \"\"\n"); | |
276 | printwows(" }\n"); | |
277 | printwows(" }"); | |
d58b8e00 MPM |
278 | } |
279 | $sth2->finish(); | |
280 | undef($sth2); | |
281 | $sth->finish(); | |
282 | undef($sth); | |
283 | } | |
e79ebcd6 | 284 | printwows("\n ]\n"); |
9de6d037 MPM |
285 | if ($iscgi == 0) { |
286 | print("};\n"); | |
287 | } else { | |
288 | print("}\n"); | |
289 | } |