Geo-Web

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

  边转换坐标边画图?还是先转换坐标后画图?

  我在看《GML可视化学习(二)》中的xslt代码时,有一个递归很吸引人,里面同时有坐标转换和svg路径的加入,也就是边转换坐标边画图。有没有一种可能,把他们分开,一段是专门转换坐标,一段专门加入路径?答案是肯定的。可是,这两种方法哪个效率更高,还是根本就是一样的?这个问题我目前还没有思路。。。

  对比前一次的railway.xslt和今天写的railway12.xslt(转换结果一模一样)。

  代码如下:

railway.xslt
1 <?xml version="1.0" encoding="UTF-8"?>
2  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:gml="http://www.opengis.net/gml" xmlns:xlink="http://www.w3.org/1999/xlink">
3 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
4 <xsl:preserve-space elements="*"/>
5 <xsl:template match="/">
6 <xsl:element name="svg">
7 <xsl:attribute name="width">800</xsl:attribute>
8 <xsl:attribute name="height">600</xsl:attribute>
9 <xsl:apply-templates select="//gml:coordinates"/>
10 </xsl:element>
11 </xsl:template>
12 <xsl:template name="transform-x">
13 <xsl:param name="coordinate-pair"/>
14 <xsl:param name="x-offset" select="38497864"/>
15 <xsl:param name="scale" select="156900"/>
16 <xsl:value-of select="(substring-before($coordinate-pair,',') - $x-offset) * 3780 div $scale"/>
17 </xsl:template>
18 <xsl:template name="transform-y">
19 <xsl:param name="coordinate-pair"/>
20 <xsl:param name="y-offset" select="3996815"/>
21 <xsl:param name="scale" select="156900"/>
22 <xsl:value-of select="(substring-after($coordinate-pair,',') - $y-offset) * -3780 div $scale + 600"/>
23 </xsl:template>
24 <xsl:template match="gml:coordinates">
25 <xsl:call-template name="linestring">
26 <xsl:with-param name="list" select="."/>
27 </xsl:call-template>
28 </xsl:template>
29 <xsl:template name="linestring">
30 <xsl:param name="list"/>
31 <xsl:variable name="current" select="substring-before($list,' ')"/>
32 <xsl:variable name="rest" select="substring-after($list,' ')"/>
33 <xsl:variable name="start-x">
34 <xsl:call-template name="transform-x">
35 <xsl:with-param name="coordinate-pair" select="$current"/>
36 </xsl:call-template>
37 </xsl:variable>
38 <xsl:variable name="start-y">
39 <xsl:call-template name="transform-y">
40 <xsl:with-param name="coordinate-pair" select="$current"/>
41 </xsl:call-template>
42 </xsl:variable>
43 <xsl:variable name="start" select="concat( 'M ',$start-x,' ',$start-y)"/>
44 <xsl:variable name="other">
45 <xsl:call-template name="gmlcoords-to-svgpath">
46 <xsl:with-param name="list" select="$rest"/>
47 </xsl:call-template>
48 </xsl:variable>
49 <xsl:element name="path">
50 <xsl:attribute name="d"><xsl:value-of select="concat($start,$other)"/></xsl:attribute>
51 <xsl:attribute name="style">fill:none;stroke:black;stroke-width:1</xsl:attribute>
52 </xsl:element>
53 </xsl:template>
54 <xsl:template name="gmlcoords-to-svgpath">
55 <xsl:param name="list"/>
56 <xsl:variable name="current" select="substring-before($list,' ')"/>
57 <xsl:variable name="rest" select="substring-after($list,' ')"/>
58 <xsl:variable name="x">
59 <xsl:call-template name="transform-x">
60 <xsl:with-param name="coordinate-pair" select="$current"/>
61 </xsl:call-template>
62 </xsl:variable>
63 <xsl:variable name="y">
64 <xsl:call-template name="transform-y">
65 <xsl:with-param name="coordinate-pair" select="$current"/>
66 </xsl:call-template>
67 </xsl:variable>
68 <xsl:choose>
69 <xsl:when test="string-length($rest) > 0">
70 <xsl:variable name="other">
71 <xsl:call-template name="gmlcoords-to-svgpath">
72 <xsl:with-param name="list" select="$rest"/>
73 </xsl:call-template>
74 </xsl:variable>
75 <xsl:value-of select="concat(' L ',$x,' ',$y,$other)"/>
76 </xsl:when>
77 <xsl:otherwise>
78 <xsl:variable name="final-x">
79 <xsl:call-template name="transform-x">
80 <xsl:with-param name="coordinate-pair" select="$list"/>
81 </xsl:call-template>
82 </xsl:variable>
83 <xsl:variable name="final-y">
84 <xsl:call-template name="transform-y">
85 <xsl:with-param name="coordinate-pair" select="$list"/>
86 </xsl:call-template>
87 </xsl:variable>
88 <xsl:value-of select="concat(' L ',$final-x,' ',$final-y)"/>
89 </xsl:otherwise>
90 </xsl:choose>
91 </xsl:template>
92  </xsl:stylesheet>
93

 

railway12.xslt
1 <?xml version="1.0" encoding="UTF-8"?>
2 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:gml="http://www.opengis.net/gml" xmlns:xlink="http://www.w3.org/1999/xlink">
3 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
4 <xsl:preserve-space elements="*"/>
5 <xsl:template match="/">
6 <xsl:element name="svg">
7 <xsl:attribute name="width">800</xsl:attribute>
8 <xsl:attribute name="height">600</xsl:attribute>
9 <xsl:apply-templates select="//gml:coordinates"/>
10 </xsl:element>
11 </xsl:template>
12 <xsl:template name="transform-x">
13 <xsl:param name="coordinate-pair"/>
14 <xsl:param name="x-offset" select="38497864"/>
15 <xsl:param name="scale" select="156900"/>
16 <xsl:value-of select="(substring-before($coordinate-pair,',') - $x-offset) * 3780 div $scale"/>
17 </xsl:template>
18 <xsl:template name="transform-y">
19 <xsl:param name="coordinate-pair"/>
20 <xsl:param name="y-offset" select="3996815"/>
21 <xsl:param name="scale" select="156900"/>
22 <xsl:value-of select="(substring-after($coordinate-pair,',') - $y-offset) * -3780 div $scale + 600"/>
23 </xsl:template>
24 <xsl:template name="transform-all">
25 <xsl:param name="list"/>
26 <xsl:variable name="current" select="substring-before($list,' ')"/>
27 <xsl:variable name="rest" select="substring-after($list,' ')"/>
28 <xsl:variable name="x">
29 <xsl:call-template name="transform-x">
30 <xsl:with-param name="coordinate-pair" select="$current"/>
31 </xsl:call-template>
32 </xsl:variable>
33 <xsl:variable name="y">
34 <xsl:call-template name="transform-y">
35 <xsl:with-param name="coordinate-pair" select="$current"/>
36 </xsl:call-template>
37 </xsl:variable>
38 <xsl:choose>
39 <xsl:when test="string-length($rest) > 0">
40 <xsl:variable name="other">
41 <xsl:call-template name="transform-all">
42 <xsl:with-param name="list" select="$rest"/>
43 </xsl:call-template>
44 </xsl:variable>
45 <xsl:value-of select="concat($x,',',$y,' ',$other)"/>
46 </xsl:when>
47 <xsl:otherwise>
48 <xsl:variable name="final-x">
49 <xsl:call-template name="transform-x">
50 <xsl:with-param name="coordinate-pair" select="$list"/>
51 </xsl:call-template>
52 </xsl:variable>
53 <xsl:variable name="final-y">
54 <xsl:call-template name="transform-y">
55 <xsl:with-param name="coordinate-pair" select="$list"/>
56 </xsl:call-template>
57 </xsl:variable>
58 <xsl:value-of select="concat($final-x,',',$final-y)"/>
59 </xsl:otherwise>
60 </xsl:choose>
61 </xsl:template>
62 <xsl:template match="gml:coordinates">
63 <xsl:variable name="screen-list">
64 <xsl:call-template name="transform-all">
65 <xsl:with-param name="list" select="."/>
66 </xsl:call-template>
67 </xsl:variable>
68 <xsl:call-template name="linestring">
69 <xsl:with-param name="list" select="$screen-list"/>
70 </xsl:call-template>
71 </xsl:template>
72 <xsl:template name="linestring">
73 <xsl:param name="list"/>
74 <xsl:variable name="current" select="substring-before($list,' ')"/>
75 <xsl:variable name="rest" select="substring-after($list,' ')"/>
76 <xsl:variable name="start-x" select="substring-before($current,',')"/>
77 <xsl:variable name="start-y" select="substring-after($current,',')"/>
78 <xsl:variable name="start" select="concat( 'M ',$start-x,' ',$start-y)"/>
79 <xsl:variable name="other">
80 <xsl:call-template name="gmlcoords-to-svgpath">
81 <xsl:with-param name="list" select="$rest"/>
82 </xsl:call-template>
83 </xsl:variable>
84 <xsl:element name="path">
85 <xsl:attribute name="d"><xsl:value-of select="concat($start,$other)"/></xsl:attribute>
86 <xsl:attribute name="style">fill:none;stroke:black;stroke-width:1</xsl:attribute>
87 </xsl:element>
88 </xsl:template>
89 <xsl:template name="gmlcoords-to-svgpath">
90 <xsl:param name="list"/>
91 <xsl:variable name="current" select="substring-before($list,' ')"/>
92 <xsl:variable name="rest" select="substring-after($list,' ')"/>
93 <xsl:variable name="x" select="substring-before($current,',')"/>
94 <xsl:variable name="y" select="substring-after($current,',')"/>
95 <xsl:choose>
96 <xsl:when test="string-length($rest) > 0">
97 <xsl:variable name="other">
98 <xsl:call-template name="gmlcoords-to-svgpath">
99 <xsl:with-param name="list" select="$rest"/>
100 </xsl:call-template>
101 </xsl:variable>
102 <xsl:value-of select="concat(' L ',$x,' ',$y,$other)"/>
103 </xsl:when>
104 <xsl:otherwise>
105 <xsl:variable name="final-x" select="substring-before($list,',')"/>
106 <xsl:variable name="final-y" select="substring-after($list,',')"/>
107 <xsl:value-of select="concat(' L ',$final-x,' ',$final-y)"/>
108 </xsl:otherwise>
109 </xsl:choose>
110 </xsl:template>
111 </xsl:stylesheet>
112

 

posted on 2010-04-09 14:39  Geo-Web  阅读(827)  评论(6编辑  收藏  举报