--- osmarender.xsl.orig	2007-09-26 22:20:51.000000000 +0200
+++ osmarender.xsl.new	2007-09-29 15:49:47.000000000 +0200
@@ -1080,28 +1080,28 @@
 
 		<xsl:variable name='left'>
 			<xsl:call-template name='areaExtentLeft'>
-				<xsl:with-param name='seg' select='./seg[1]'/> <!-- First segment -->
+				<xsl:with-param name='seg' select='./seg'/> <!-- First segment -->
 				<xsl:with-param name='left' select='key("nodeById",key("segmentById",./seg[1]/@id)/@from)/@lon'/> <!-- Longitude of first segment -->
 			</xsl:call-template>
 		</xsl:variable>
 
 		<xsl:variable name='right'>
 			<xsl:call-template name='areaExtentRight'>
-				<xsl:with-param name='seg' select='./seg[1]'/> <!-- First segment -->
+				<xsl:with-param name='seg' select='./seg'/> <!-- First segment -->
 				<xsl:with-param name='right' select='key("nodeById",key("segmentById",./seg[1]/@id)/@from)/@lon'/> <!-- Longitude of first segment -->
 			</xsl:call-template>
 		</xsl:variable>
 
 		<xsl:variable name='top'>
 			<xsl:call-template name='areaExtentTop'>
-				<xsl:with-param name='seg' select='./seg[1]'/> <!-- First segment -->
+				<xsl:with-param name='segments' select='./seg'/> <!-- list of segments -->
 				<xsl:with-param name='top' select='key("nodeById",key("segmentById",./seg[1]/@id)/@from)/@lat'/> <!-- Latitude of first segment -->
 			</xsl:call-template>
 		</xsl:variable>
 
 		<xsl:variable name='bottom'>
 			<xsl:call-template name='areaExtentBottom'>
-				<xsl:with-param name='seg' select='./seg[1]'/> <!-- First segment -->
+				<xsl:with-param name='segments' select='./seg'/> <!-- list of segments -->
 				<xsl:with-param name='bottom' select='key("nodeById",key("segmentById",./seg[1]/@id)/@from)/@lat'/> <!-- Latitude of first segment -->
 			</xsl:call-template>
 		</xsl:variable>
@@ -1123,132 +1123,167 @@
 
 
 	<xsl:template name='areaExtentLeft'>
-		<xsl:param name='seg'/>
-		<xsl:param name='left'/>
-		
-		<xsl:variable name='fromLon' select='key("nodeById",key("segmentById",$seg/@id)/@from)/@lon'/>
-		<xsl:variable name='toLon' select='key("nodeById",key("segmentById",$seg/@id)/@to)/@lon'/>
-		<xsl:variable name='leftmostLon'>
+		<xsl:param name='segments'/>
+		<xsl:param name='left'/> <!-- initialized with value of first segment -->
 			<xsl:choose>
-				<xsl:when test='$fromLon>$toLon'><xsl:value-of select='$toLon'/></xsl:when>
-				<xsl:otherwise><xsl:value-of select='$fromLon'/></xsl:otherwise>
+			<xsl:when test='count($segments) = 1'> <!-- 1 segment : just calculate left -->
+				<xsl:variable name='seg' select='$segments[1]'/>
+				<xsl:variable name='fromLat' select='key("nodeById",key("segmentById",$seg/@id)/@from)/@lat'/>
+				<xsl:variable name='toLat' select='key("nodeById",key("segmentById",$seg/@id)/@to)/@lat'/>
+				<xsl:variable name='locleft'>
+					<xsl:choose>
+						<xsl:when test='$fromLat > $toLat'><xsl:value-of select='$toLat'/></xsl:when>
+						<xsl:otherwise><xsl:value-of select='$fromLat'/></xsl:otherwise>
 			</xsl:choose>
 		</xsl:variable>
-		<xsl:variable name='newLeft'>
 			<xsl:choose>
-				<xsl:when test='$left>$leftmostLon'><xsl:value-of select='$leftmostLon'/></xsl:when>
-				<xsl:otherwise><xsl:value-of select='$left'/></xsl:otherwise>
+					<xsl:when test='$locleft > $left'><xsl:value-of select='$left'/></xsl:when>
+					<xsl:otherwise><xsl:value-of select='$locleft'/></xsl:otherwise>
 			</xsl:choose>
+			</xsl:when>
+			<xsl:otherwise> <!-- more than 1 segment, split and recurse -->
+				<xsl:variable name='splitSegs' select='floor(count($segments) div 2)'/>
+				<xsl:variable name='left1'>
+					<xsl:call-template name='areaExtentLeft'>
+						<xsl:with-param name='segments' select='$segments[position() &lt;= $splitSegs ]'/>
+						<xsl:with-param name='left' select='$left'/>
+					</xsl:call-template>
 		</xsl:variable>
-	
-		<xsl:choose>
-			<xsl:when test="$seg/following-sibling::seg[1]">
+				<xsl:variable name='left2'>
 				<xsl:call-template name='areaExtentLeft'>
-					<xsl:with-param name='seg' select='$seg/following-sibling::seg[1]'/> <!-- next segment -->
-					<xsl:with-param name='left' select='$newLeft'/>
+						<xsl:with-param name='segments' select='$segments[position() &gt; $splitSegs]'/>
+						<xsl:with-param name='left' select='$left'/>
 				</xsl:call-template>
-			</xsl:when>
-			<xsl:otherwise>
-				<xsl:value-of select='$newLeft'/>
+				</xsl:variable>
+				<xsl:choose>
+					<xsl:when test='$left1 > $left2'><xsl:value-of select='$left2'/></xsl:when>
+					<xsl:otherwise><xsl:value-of select='$left1'/></xsl:otherwise>
+				</xsl:choose>
 			</xsl:otherwise>
 		</xsl:choose>
 	</xsl:template>
 
 
 	<xsl:template name='areaExtentRight'>
-		<xsl:param name='seg'/>
-		<xsl:param name='right'/>
-		
+		<xsl:param name='segments'/>
+		<xsl:param name='right'/> <!-- initialized with value of first segment -->
+		<xsl:choose>
+			<xsl:when test='count($segments) = 1'> <!-- 1 segment : just calculate top -->
+				<xsl:variable name='seg' select='$segments[1]'/>
 		<xsl:variable name='fromLon' select='key("nodeById",key("segmentById",$seg/@id)/@from)/@lon'/>
 		<xsl:variable name='toLon' select='key("nodeById",key("segmentById",$seg/@id)/@to)/@lon'/>
-		<xsl:variable name='rightmostLon'>
+				<xsl:variable name='locright'>
 			<xsl:choose>
 				<xsl:when test='$fromLon>$toLon'><xsl:value-of select='$fromLon'/></xsl:when>
 				<xsl:otherwise><xsl:value-of select='$toLon'/></xsl:otherwise>
 			</xsl:choose>
 		</xsl:variable>
-		<xsl:variable name='newRight'>
 			<xsl:choose>
-				<xsl:when test='$rightmostLon>$right'><xsl:value-of select='$rightmostLon'/></xsl:when>
+					<xsl:when test='$locright > $right'><xsl:value-of select='$locright'/></xsl:when>
 				<xsl:otherwise><xsl:value-of select='$right'/></xsl:otherwise>
 			</xsl:choose>
+			</xsl:when>
+			<xsl:otherwise> <!-- more than 1 segment, split and recurse -->
+				<xsl:variable name='splitSegs' select='floor(count($segments) div 2)'/>
+				<xsl:variable name='right1'>
+					<xsl:call-template name='areaExtentRight'>
+						<xsl:with-param name='segments' select='$segments[position() &lt;= $splitSegs ]'/>
+						<xsl:with-param name='right' select='$right'/>
+					</xsl:call-template>
 		</xsl:variable>
-	
-		<xsl:choose>
-			<xsl:when test="$seg/following-sibling::seg[1]">
+				<xsl:variable name='right2'>
 				<xsl:call-template name='areaExtentRight'>
-					<xsl:with-param name='seg' select='$seg/following-sibling::seg[1]'/> <!-- next segment -->
-					<xsl:with-param name='right' select='$newRight'/>
+						<xsl:with-param name='segments' select='$segments[position() &gt; $splitSegs]'/>
+						<xsl:with-param name='right' select='$right'/>
 				</xsl:call-template>
-			</xsl:when>
-			<xsl:otherwise>
-				<xsl:value-of select='$newRight'/>
+				</xsl:variable>
+				<xsl:choose>
+					<xsl:when test='$right1 > $right2'><xsl:value-of select='$right1'/></xsl:when>
+					<xsl:otherwise><xsl:value-of select='$right2'/></xsl:otherwise>
+				</xsl:choose>
 			</xsl:otherwise>
 		</xsl:choose>
 	</xsl:template>
 
-
 	<xsl:template name='areaExtentTop'>
-		<xsl:param name='seg'/>
-		<xsl:param name='top'/>
-		
+		<xsl:param name='segments'/>
+		<xsl:param name='top'/> <!-- initialized with value of first segment -->
+		<xsl:choose>
+			<xsl:when test='count($segments) = 1'> <!-- 1 segment : just calculate top -->
+				<xsl:variable name='seg' select='$segments[1]'/>
 		<xsl:variable name='fromLat' select='key("nodeById",key("segmentById",$seg/@id)/@from)/@lat'/>
 		<xsl:variable name='toLat' select='key("nodeById",key("segmentById",$seg/@id)/@to)/@lat'/>
-		<xsl:variable name='topmostLat'>
+				<xsl:variable name='loctop'>
 			<xsl:choose>
 				<xsl:when test='$fromLat>$toLat'><xsl:value-of select='$fromLat'/></xsl:when>
 				<xsl:otherwise><xsl:value-of select='$toLat'/></xsl:otherwise>
 			</xsl:choose>
 		</xsl:variable>
-		<xsl:variable name='newTop'>
 			<xsl:choose>
-				<xsl:when test='$top>$topmostLat'><xsl:value-of select='$top'/></xsl:when>
-				<xsl:otherwise><xsl:value-of select='$topmostLat'/></xsl:otherwise>
+					<xsl:when test='$loctop > $top'><xsl:value-of select='$loctop'/></xsl:when>
+					<xsl:otherwise><xsl:value-of select='$top'/></xsl:otherwise>
 			</xsl:choose>
+			</xsl:when>
+			<xsl:otherwise> <!-- more than 1 segment, split and recurse -->
+				<xsl:variable name='splitSegs' select='floor(count($segments) div 2)'/>
+				<xsl:variable name='top1'>
+					<xsl:call-template name='areaExtentTop'>
+						<xsl:with-param name='segments' select='$segments[position() &lt;= $splitSegs ]'/>
+						<xsl:with-param name='top' select='$top'/>
+					</xsl:call-template>
 		</xsl:variable>
-	
-		<xsl:choose>
-			<xsl:when test="$seg/following-sibling::seg[1]">
+				<xsl:variable name='top2'>
 				<xsl:call-template name='areaExtentTop'>
-					<xsl:with-param name='seg' select='$seg/following-sibling::seg[1]'/> <!-- next segment -->
-					<xsl:with-param name='top' select='$newTop'/>
+						<xsl:with-param name='segments' select='$segments[position() &gt; $splitSegs]'/>
+						<xsl:with-param name='top' select='$top'/>
 				</xsl:call-template>
-			</xsl:when>
-			<xsl:otherwise>
-				<xsl:value-of select='$newTop'/>
+				</xsl:variable>
+				<xsl:choose>
+					<xsl:when test='$top1 > $top2'><xsl:value-of select='$top1'/></xsl:when>
+					<xsl:otherwise><xsl:value-of select='$top2'/></xsl:otherwise>
+				</xsl:choose>
 			</xsl:otherwise>
 		</xsl:choose>
 	</xsl:template>
 
 
 	<xsl:template name='areaExtentBottom'>
-		<xsl:param name='seg'/>
-		<xsl:param name='bottom'/>
-		
+		<xsl:param name='segments'/>
+		<xsl:param name='bottom'/> <!-- initialized with value of first segment -->
+		<xsl:choose>
+			<xsl:when test='count($segments) = 1'> <!-- 1 segment : just calculate bottom -->
+				<xsl:variable name='seg' select='$segments[1]'/>
 		<xsl:variable name='fromLat' select='key("nodeById",key("segmentById",$seg/@id)/@from)/@lat'/>
 		<xsl:variable name='toLat' select='key("nodeById",key("segmentById",$seg/@id)/@to)/@lat'/>
-		<xsl:variable name='bottommostLat'>
+				<xsl:variable name='locbottom'>
 			<xsl:choose>
 				<xsl:when test='$fromLat>$toLat'><xsl:value-of select='$toLat'/></xsl:when>
 				<xsl:otherwise><xsl:value-of select='$fromLat'/></xsl:otherwise>
 			</xsl:choose>
 		</xsl:variable>
-		<xsl:variable name='newBottom'>
 			<xsl:choose>
-				<xsl:when test='$bottommostLat>$bottom'><xsl:value-of select='$bottom'/></xsl:when>
-				<xsl:otherwise><xsl:value-of select='$bottommostLat'/></xsl:otherwise>
+					<xsl:when test='$locbottom > $bottom'><xsl:value-of select='$bottom'/></xsl:when>
+					<xsl:otherwise><xsl:value-of select='$locbottom'/></xsl:otherwise>
 			</xsl:choose>
+			</xsl:when>
+			<xsl:otherwise> <!-- more than 1 segment, split and recurse -->
+				<xsl:variable name='splitSegs' select='floor(count($segments) div 2)'/>
+				<xsl:variable name='bottom1'>
+					<xsl:call-template name='areaExtentBottom'>
+						<xsl:with-param name='segments' select='$segments[position() &lt;= $splitSegs ]'/>
+						<xsl:with-param name='bottom' select='$bottom'/>
+					</xsl:call-template>
 		</xsl:variable>
-	
-		<xsl:choose>
-			<xsl:when test="$seg/following-sibling::seg[1]">
+				<xsl:variable name='bottom2'>
 				<xsl:call-template name='areaExtentBottom'>
-					<xsl:with-param name='seg' select='$seg/following-sibling::seg[1]'/> <!-- next segment -->
-					<xsl:with-param name='bottom' select='$newBottom'/>
+						<xsl:with-param name='segments' select='$segments[position() &gt; $splitSegs]'/>
+						<xsl:with-param name='bottom' select='$bottom'/>
 				</xsl:call-template>
-			</xsl:when>
-			<xsl:otherwise>
-				<xsl:value-of select='$newBottom'/>
+				</xsl:variable>
+				<xsl:choose>
+					<xsl:when test='$bottom1 > $bottom2'><xsl:value-of select='$bottom2'/></xsl:when>
+					<xsl:otherwise><xsl:value-of select='$bottom1'/></xsl:otherwise>
+				</xsl:choose>
 			</xsl:otherwise>
 		</xsl:choose>
 	</xsl:template>
