#
#
# PERL osm module by gary68
#
# !!! store as osm.pm in folder OSM in lib directory !!!
#
# This module contains a lot of useful functions for working with osm files and data. it also
# includes functions for calculation and output.
#
#
# Copyright (C) 2008, 2009, 2010 Gerhard Schwanz
#
# This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program; if not, see
#
#
# version 2
# - added html table functions
#
# Version 3
# - added project and angle
# - added support for bz2 files
#
# Version 4
# - add support for relations
# - select multiple ways in JOSM link
# - getNode2, getWay2: return tags as arrays
#
# Version 4.1
# - getBugs added
#
# Version 4.2
# - map compare link added
#
# Version 4.3
# - regex for k/v changed
#
# Version 4.4
# -added relation analyzer link
#
# Version 4.41 (gary68)
# - changed regex for openosmfile from /node/ to / tag.
#
# Version 6.0
# - regex improvements
#
# Version 7.0
# - skip nodes and skip ways performance boost by using seek in files
#
# Version 7.1
# - get...Xml functions added
#
#
# Version 8.0
# - read xml subs implemented for different getXXX
#
# Version 8.1
# - diff db sub
#
# Version 8.2
# - latexSanitizeString
#
# Version 8.4
# - negative IDs
#
#
# USAGE
#
# analyzerLink ($id) > $htmlString, link to relation analyzer
# angle (x1,y1,x2,y2) > angle (N=0,E=90...)
# APIgetWay ($id) > ($wayId, $wayUser, \@wayNodes, \@wayTags)
# binSearch ($value, @ref) > $index or -1
# closeOsmFile ()
# checkOverlap (w1xMin, w1yMin, w1xMax, w1yMax, w2xMin, w2yMin, w2xMax, w2yMax) > 0=no overlap, 1=overlap
# crossing (g1x1,g1y1,g1x2,g1y2,g2x1,g2y1,g2x2,g2y2) > ($sx, $sy)
# distance (x1,y1,x2,y2) > $distance in km
# getBugs ($lon, $lat, $bugsDownDist, $bugsMaxDist) > pos, down dist in deg, max dist in km -> html text
# getNode2 () > ($gId, $gLon, $gLat, $gU, \@gTags) ; # in main @array = @$ref // returns k/v as array, not string!
# getNode3 () > (\%nodeProperties \@nodeTags) ; # in main @array = @$ref // returns k/v as array, not string!
# getNodeXml () > ($gId, $xml) ;
# getRelation
# getRelation3 > (\%properties, \@members, \@tags)
# getWay2 () > ($gId, $gU, \@gNodes, \@gTags) ; # in main @array = @$ref // returns k/v as array, not string!
# getWay3 () > (\%properties, \@Nodes, \@Tags) ;
# getWayXml () > ($gId, $xml) ;
# getRelationXml () > ($gId, $xml) ;
# hashValue ($lon, $lat) > $hashValue 0.1 deg
# hashValue2 ($lon, $lat) > $hashValue 0.01 deg
# historyLink ($type, $key) > $htmlString
# josmLinkDontSelect ($lon, $lat, $span) > $htmlString
# josmLinkSelectWay ($lon, $lat, $span, $wayId) > $htmlString
# josmLinkSelectWays ($lon, $lat, $span, @wayIds) > $htmlString
# josmLinkSelectNode ($lon, $lat, $span, $nodeId) > $htmlString
# josmLinkSelectNodes ($lon, $lat, $span, @nodes) > $htmlString
# DON'T USE ANYMORE! josmLink ($lon, $lat, $span, $wayId) > $htmlString
# mapCompareLink ($lon, $lat, $zoom) > $htmlString
# openOsmFile ($file) > osm file open and $line set to first node (*.osm or *.osm.bz2)
# osbLink ($lon, $lat, $zoom) > $htmlString
# osmLink ($lon, $lat, $zoom) > $htmlString
# osmLinkMarkerWay ($lon, $lat, $zoom, $way) > $htmlString
# picLinkMapnik ($lon, $lat, $zoom) > $htmlString
# picLinkOsmarender ($lon, $lat, $zoom) > $htmlString
# printGPXHeader ($file)
# printGPXFoot ($file)
# printGPXWaypoint ($file, $lon, $lat, $text)
# printHTMLCellCenter ($file, $value)
# printHTMLCellLeft ($file, $value)
# printHTMLCellLeftEM
# printHTMLCellLeftTwoValues
# printHTMLCellRight ($file, $value)
# printHTMLFoot ($file) > print foot to file
# printHTMLHeader ($file, $title) > print header to file
# printHTMLHeaderiFrame ($file) > print iFrame code for josm links, call before body
# printHTMLRowStart ($file)
# printHTMLRowEnd ($file)
# printHTMLTableFoot ($file)
# printHTMLTableHead ($file)
# printHTMLTableHeadings ($file, @list)
# printHTMLTableRowLeft ($file, @list)
# printHTMLTableRowRight ($file, @list)
# printNodeList ($file, @list)
# printProgress ($program, $osm, $startTime, $fullCount, $actualCount)
# printWayList ($file, @list)
# project (x1, y1, angle, dist) > (x2,y2)
# shortestDistance ($gx1, $gy1, $gx2, $gy2, $nx, $ny) > roughly the distance of node to segment in km
# skipNodes ()
# skipWays ()
# stringFileInfo ($file) > $string
# stringTimeSpent ($timeSpent in seconds) > $string
# tileNumber ($lon,$lat,$zoom) > ($xTile, $yTile)
#
package OSM::osm ;
use strict;
use warnings;
use OSM::osmDB ;
use LWP::Simple;
use Math::Trig;
# use IO::Handle ;
use File::stat;
use Time::localtime;
use List::Util qw[min max] ;
use Compress::Bzip2 ; # install packet "libcompress-bzip2-perl"
# if you have problems with this module/library then just comment out all lines using these functions
# and don't use zipped files
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK) ;
$VERSION = '8.4' ;
my $apiUrl = "http://www.openstreetmap.org/api/0.6/" ; # way/Id
require Exporter ;
@ISA = qw ( Exporter AutoLoader ) ;
@EXPORT = qw (analyzerLink applyDiffFile getBugs getNode getNode2 getNode3 getNodeXml getWay getWay2 getWay3 getWayXml getRelation getRelation3 getRelationXml crossing historyLink hashValue hashValue2 tileNumber openOsmFile osmLink osmLinkMarkerWay osbLink mapCompareLink josmLink josmLinkDontSelect josmLinkSelectWay josmLinkSelectWays josmLinkSelectNode josmLinkSelectNodes printHTMLHeader printHTMLFoot stringTimeSpent distance angle project picLinkMapnik picLinkOsmarender stringFileInfo closeOsmFile skipNodes skipWays binSearch printProgress printNodeList printWayList printGPXHeader printGPXFoot printGPXWaypoint checkOverlap shortestDistance printHTMLTableHead printHTMLTableFoot printHTMLTableHeadings printHTMLTableRowLeft printHTMLTableRowRight printHTMLCellLeft printHTMLCellLeftEM printHTMLCellLeftTwoValues printHTMLCellCenter printHTMLCellRight printHTMLRowStart printHTMLRowEnd printHTMLiFrameHeader APIgetWay convertToLatex replaceHTMLCode ) ;
our $line ;
our $file ;
our $fileName ;
my $bufferSize = 512000 ;
my $bz ; my $isBz2 ;
######
# file
######
sub openOsmFile {
$fileName = shift ;
if (grep /.bz2/, $fileName) { $isBz2 = 1 ; } else { $isBz2 = 0 ; }
if ($isBz2) {
$bz = bzopen($fileName, "rb") or die "Cannot open $fileName: $bzerrno\n" ;
}
else {
open ($file, "<", $fileName) || die "can't open osm file" ;
}
nextLine() ;
while ( ! (grep /\bzclose() ;
}
else {
close ($file) ;
}
}
sub stringFileInfo {
my $file = shift ;
my $string = "file " . $file . " " . ctime(stat($file)->mtime) ;
return ($string) ;
}
sub nextLine {
do {
if ($isBz2) {
$bz->bzreadline($line) ;
}
else {
$line = <$file> ;
}
} while ($line =~ /^