Re: [xsl] grouping and merging problem

Subject: Re: [xsl] grouping and merging problem
From: Sylvain Rouillard <RouillardSy@xxxxxxxx>
Date: Tue, 22 Nov 2005 21:55:47 +0100
Le Mardi 22 Novembre 2005 15:47, Jon Gorman a icrit :
> On 11/22/05, Sylvain Rouillard <RouillardSy@xxxxxxxx> wrote:
> > Hi list!
> >
> > This is my first post in here and I'm really new to XSLT things, so I
> > hope my question will not sound too dumb. I've been googling around a lot
> > and I cannot find anything similar enough to my problem, hence me comming
> > here.
> >
> > I am using xslt 1.0 to transform an xml file into an svg image.
> > Basically, the xml file is a map of pixels (//row), with an @x, @y and
> > @Altitude, and I render each pixel with a square (<rect>) positionned at
> > (@x,@y) with an opacity proportionnal to the altitude. An exemple of my
> > xml input and of the xslt I use is given below.
> >
> > The next step for me is to trim the output size, by merging neighbour
> > pixels that have the same opacity (ie altitudes that are close enough
> > from each other to be rendered by the same opacity on screen). To
> > approach this problem, I decided that I would try and merge only the
> > pixels on a same row (same @y), so that I don't have to be
> > bi-directionnal. So, ideally, in the for-each loop, I should test if the
> > next pixel in the loop has the same opacity as the current one. If not,
> > then I can just output the same rect with width=3 and height=3. If the
> > next pixel has the same opacity, then I should test the one after and so
> > forth (until the end of the row), and output a rect with an appropriate
> > width attribute. In this case, the subsequent pixels (//row) that have
> > been covered in a previous enlarged rect should be skipped.
>
> Look at some of the grouping examples in the FAQ of this list.  You
> should be able to modify this, although it will take a bit of elbow
> grease.  Otherwise this would also fit well into a recursive template
> (at least in my humble opinion).  Don't think of necessarily going
> through the list item by item and then doing an action, think of it as
> going through the sets you want a the list.  It's a subtle distinction
> perhaps.  For every element that doesn't have the "similar enough"
> opacity following it, do one thing.  For every group of elements with
> similar enough opacity, do something else (average it or something
> along those lines).
>
> > I hope this makes sense, feel free to ask more details otherwise.
>
> Yes, actually seems to be a pretty nice summary.  Just wish I had time
> to work with it.  Best of luck.

Thank you for your support.

I ended up doing it in a way that I think is not in the spirit of xslt, but
heck that works, and performance is not really an issue in my context, so I'm
happy with it. To follow up for anybody who may cross this post, my solution
is posted below.

Cheers

<?xml version="1.0" encoding="iso-8859-15"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:svg="http://www.w3.org/2000/svg";>
  <xsl:output
    method="xml"
    version="1.0"
    encoding="ISO-8859-1"
    omit-xml-declaration="yes"
    standalone="yes"
    indent="no"
    media-type="image/svg+xml"/>

  <xsl:param name="LandID" select="0"/>

  <xsl:variable name="minX">
    <xsl:for-each select="/Schema1/row/@x">
      <xsl:sort data-type="number" order="ascending"/>
      <xsl:if test="position()=1">
        <xsl:value-of select="."/>
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>

  <xsl:variable name="minY">
    <xsl:for-each select="/Schema1/row/@y">
      <xsl:sort data-type="number" order="ascending"/>
      <xsl:if test="position()=1">
        <xsl:value-of select="."/>
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>

  <xsl:variable name="maxX">
    <xsl:for-each select="/Schema1/row/@x">
      <xsl:sort data-type="number" order="descending"/>
      <xsl:if test="position()=1">
        <xsl:value-of select=". + 3"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>

  <xsl:variable name="maxY">
    <xsl:for-each select="/Schema1/row/@y">
      <xsl:sort data-type="number" order="descending"/>
      <xsl:if test="position()=1">
        <xsl:value-of select=". + 3"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>

  <xsl:template match="/Schema1">
    <xsl:element name="svg:svg">
      <xsl:attribute name="version">
        <xsl:value-of select="'1.1'" />
      </xsl:attribute>
      <xsl:attribute name="id">
        <xsl:value-of select="concat($LandID,'_Districts')" />
      </xsl:attribute>
      <xsl:attribute name="viewBox">
        <xsl:value-of select="concat(string($minX),' ',string($minY),'
',string($maxX - $minX),' ',string($maxY - $minY))" />
      </xsl:attribute>
      <xsl:attribute name="shape-rendering">
        <xsl:value-of select="'optimizeSpeed'" />
      </xsl:attribute>

      <xsl:for-each select="row">
        <xsl:call-template name="draw">
          <xsl:with-param name="position0" select="position()"/>
          <xsl:with-param name="position" select="position()"/>
        </xsl:call-template>
      </xsl:for-each>

    </xsl:element>
  </xsl:template>

  <xsl:template name="draw">
    <xsl:param name="position0"/>
    <xsl:param name="position"/>
    <xsl:variable name="grey" select="round(/Schema1/row[$position0]/@Altitude
* 15)"/>
    <xsl:choose>
      <xsl:when test="$grey = round(/Schema1/row[$position + 1]/@Altitude *
15) and /Schema1/row[$position]/@y = /Schema1/row[$position + 1]/@y">
        <xsl:call-template name="draw">
          <xsl:with-param name="position0" select="$position0"/>
          <xsl:with-param name="position" select="$position + 1"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:if test="$grey != round(/Schema1/row[$position0 - 1]/@Altitude *
15) or /Schema1/row[$position0]/@y != /Schema1/row[$position0 - 1]/@y">
          <xsl:element name="svg:rect">
            <xsl:attribute name="x">
              <xsl:value-of select="/Schema1/row[$position0]/@x" />
            </xsl:attribute>
            <xsl:attribute name="y">
              <xsl:value-of select="/Schema1/row[$position0]/@y" />
            </xsl:attribute>
            <!--xsl:attribute name="opacity">
              <xsl:value-of select="round(/Schema1/row[position() =
$position0]/@Altitude * 10) div 100" />
            </xsl:attribute-->
            <xsl:attribute name="fill">
              <xsl:value-of
select="concat('rgb(',150-$grey,',',150-$grey,',',150-$grey,')')" />
            </xsl:attribute>
            <xsl:attribute name="width">
              <xsl:value-of select="($position - $position0 + 1) * 3" />
            </xsl:attribute>
            <xsl:attribute name="height">
              <xsl:value-of select="3" />
            </xsl:attribute>
          </xsl:element>
        </xsl:if>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>





___________________________________________________________________________
Appel audio GRATUIT partout dans le monde avec le nouveau Yahoo! Messenger
Tilichargez cette version sur http://fr.messenger.yahoo.com

Current Thread