Project

General

Profile

1
<?xml version="1.0" encoding="UTF-8"?>
2

    
3
<xsl:stylesheet
4

    
5
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
6

    
7
  xmlns:gml="http://www.opengis.net/gml"
8

    
9
  version="1.0">
10

    
11
<!--
12

    
13
Description: Convert GML to HTML Graphics. 
14

    
15
Author:      Cameron Shorter cameron ATshorter.net
16

    
17
Licence:     LGPL as per: http://www.gnu.org/copyleft/lesser.html
18

    
19

    
20

    
21
$Id$
22

    
23
$Name$
24

    
25
-->
26

    
27
  <xsl:output method="xml" encoding="utf-8"/>
28

    
29
  
30

    
31
  <xsl:param name="width" select="400"/>
32

    
33
  <xsl:param name="height" select="200"/>
34

    
35
  <xsl:param name="bBoxMinX" select="-180"/>
36

    
37
  <xsl:param name="bBoxMinY" select="-90"/>
38

    
39
  <xsl:param name="bBoxMaxX" select="180"/>
40

    
41
  <xsl:param name="bBoxMaxY" select="90"/>
42

    
43
  <xsl:param name="color" select="red"/>
44

    
45
  <xsl:param name="lineWidth" select="1"/>
46

    
47
  <xsl:param name="crossSize" select="0"/>
48

    
49
  <xsl:param name="skinDir"/>
50

    
51
  <xsl:param name="pointDiameter" select="10"/>
52

    
53

    
54

    
55
  <xsl:variable name="xRatio" select="$width div ( $bBoxMaxX - $bBoxMinX )"/>
56

    
57
  <xsl:variable name="yRatio" select="$height div ( $bBoxMaxY - $bBoxMinY )"/>
58

    
59

    
60

    
61

    
62

    
63
  <!-- Root node -->
64

    
65
  <xsl:template match="/">
66

    
67
    <div style="position:relative; width:{$width}; height:{$height}">
68

    
69
      <xsl:apply-templates/>
70

    
71
    </div>
72

    
73
  </xsl:template>
74

    
75

    
76

    
77
  <!-- Match and render a GML Point -->
78

    
79
  <xsl:template match="gml:pointMember/gml:Point">
80

    
81
    <xsl:variable name="x0" select="floor((number(gml:coord/gml:X)-$bBoxMinX)*$xRatio - number($pointDiameter) div 2)"/>
82

    
83
    <xsl:variable name="y0" select="floor($height - (number(gml:coord/gml:Y)-$bBoxMinY)*$yRatio - $pointDiameter div 2)"/>
84

    
85

    
86

    
87
    <div style="position:absolute; left:{$x0}px; top:{$y0}px; width:{$pointDiameter}px; height:{$pointDiameter}px">
88

    
89
      <img src="{$skinDir}/images/Dot.gif"/>
90

    
91
    </div>
92

    
93
  </xsl:template>
94

    
95

    
96

    
97
  <!-- Match and render a GML Envelope -->
98

    
99
  <xsl:template match="gml:Envelope">
100

    
101
    <xsl:variable name="x0" select="floor((number(gml:coord[position()=1]/gml:X)-$bBoxMinX)*$xRatio)"/>
102

    
103
    <xsl:variable name="y0" select="floor($height - (number(gml:coord[position()=1]/gml:Y) -$bBoxMinY)*$yRatio)"/>
104

    
105
    <xsl:variable name="x1" select="floor((number(gml:coord[position()=2]/gml:X)-$bBoxMinX)*$xRatio)"/>
106

    
107
    <xsl:variable name="y1" select="floor($height - (number(gml:coord[position()=2]/gml:Y)-$bBoxMinY)*$yRatio)"/>
108

    
109

    
110

    
111
    <xsl:choose>
112

    
113
      <!-- If envelope is small, draw a cross instead of a box -->
114

    
115
      <xsl:when test="($x0 - $x1 &lt; $crossSize) and ($x1 - $x0 &lt; $crossSize) and ($y0 - $y1 &lt; $crossSize) and ($y1 - $y0 &lt; $crossSize)">
116

    
117
        <debug drawCross="x"/>
118

    
119
        <xsl:variable name="xMid" select="floor(($x0 + $x1) div 2)"/>
120

    
121
        <xsl:variable name="yMid" select="floor(($y0 + $y1) div 2)"/>
122

    
123
        <xsl:variable name="crossHalf" select="floor($crossSize div 2)"/>
124

    
125
        <xsl:call-template name="drawLine">
126

    
127
          <xsl:with-param name="x0" select="$xMid"/>
128

    
129
          <xsl:with-param name="y0" select="$yMid - $crossHalf"/>
130

    
131
          <xsl:with-param name="x1" select="$xMid"/>
132

    
133
          <xsl:with-param name="y1" select="$yMid + $crossHalf"/>
134

    
135
        </xsl:call-template>
136

    
137
        <xsl:call-template name="drawLine">
138

    
139
          <xsl:with-param name="x0" select="$xMid - $crossHalf"/>
140

    
141
          <xsl:with-param name="y0" select="$yMid"/>
142

    
143
          <xsl:with-param name="x1" select="$xMid + $crossHalf"/>
144

    
145
          <xsl:with-param name="y1" select="$yMid"/>
146

    
147
        </xsl:call-template>
148

    
149
      </xsl:when>
150

    
151

    
152

    
153
      <!-- draw a box -->
154

    
155
      <xsl:otherwise>
156

    
157
        <debug drawBox="x"/>
158

    
159
        <xsl:call-template name="drawLine">
160

    
161
          <xsl:with-param name="x0" select="$x0"/>
162

    
163
          <xsl:with-param name="y0" select="$y0"/>
164

    
165
          <xsl:with-param name="x1" select="$x1"/>
166

    
167
          <xsl:with-param name="y1" select="$y0"/>
168

    
169
        </xsl:call-template>
170

    
171
        <xsl:call-template name="drawLine">
172

    
173
          <xsl:with-param name="x0" select="$x1"/>
174

    
175
          <xsl:with-param name="y0" select="$y0"/>
176

    
177
          <xsl:with-param name="x1" select="$x1"/>
178

    
179
          <xsl:with-param name="y1" select="$y1"/>
180

    
181
        </xsl:call-template>
182

    
183
        <xsl:call-template name="drawLine">
184

    
185
          <xsl:with-param name="x0" select="$x1"/>
186

    
187
          <xsl:with-param name="y0" select="$y1"/>
188

    
189
          <xsl:with-param name="x1" select="$x0"/>
190

    
191
          <xsl:with-param name="y1" select="$y1"/>
192

    
193
        </xsl:call-template>
194

    
195
        <xsl:call-template name="drawLine">
196

    
197
          <xsl:with-param name="x0" select="$x0"/>
198

    
199
          <xsl:with-param name="y0" select="$y1"/>
200

    
201
          <xsl:with-param name="x1" select="$x0"/>
202

    
203
          <xsl:with-param name="y1" select="$y0"/>
204

    
205
        </xsl:call-template>
206

    
207

    
208

    
209
      </xsl:otherwise>
210

    
211
    </xsl:choose>
212

    
213
  </xsl:template>
214

    
215

    
216

    
217
  <!-- Match and render a LineString -->
218

    
219
  <xsl:template match="gml:LineString">
220

    
221
    <xsl:for-each select="gml:coord">
222

    
223
      <debug lineString="x"
224

    
225
        x0="{floor((number(gml:X)-$bBoxMinX)*$xRatio)}"
226

    
227
        y0="{floor($height - (number(gml:Y) -$bBoxMinY)*$yRatio)}"
228

    
229
        x1="{floor((number(following-sibling::gml:coord[position()=1]/gml:X)-$bBoxMinX)*$xRatio)}"
230

    
231
        y1="{floor($height - (number(following-sibling::gml:coord[position()=1]/gml:Y)-$bBoxMinY)*$yRatio)}"/>
232

    
233
      <xsl:if test="following-sibling::gml:coord">
234

    
235
        <xsl:call-template name="drawLine">
236

    
237
          <xsl:with-param name="x0" select="floor((number(gml:X)-$bBoxMinX)*$xRatio)"/>
238

    
239
          <xsl:with-param name="y0" select="floor($height - (number(gml:Y) -$bBoxMinY)*$yRatio)"/>
240

    
241
          <xsl:with-param name="x1" select="floor((number(following-sibling::gml:coord[position()=1]/gml:X)-$bBoxMinX)*$xRatio)"/>
242

    
243
          <xsl:with-param name="y1" select="floor($height - (number(following-sibling::gml:coord[position()=1]/gml:Y)-$bBoxMinY)*$yRatio)"/>
244

    
245
        </xsl:call-template>
246

    
247
      </xsl:if>
248

    
249
    </xsl:for-each>
250

    
251
  </xsl:template>
252

    
253

    
254

    
255
  <!-- Draw a line.  -->
256

    
257
  <xsl:template name="drawLine">
258

    
259
    <xsl:param name="x0"/>
260

    
261
    <xsl:param name="y0"/>
262

    
263
    <xsl:param name="x1"/>
264

    
265
    <xsl:param name="y1"/>
266

    
267
    <xsl:variable name="slope">
268

    
269
      <xsl:choose>
270

    
271
        <xsl:when test="$x0 = $x1">0</xsl:when>
272

    
273
        <xsl:otherwise>
274

    
275
          <xsl:value-of select="($y1 - $y0) div ($x1 - $x0)"/>
276

    
277
        </xsl:otherwise>
278

    
279
      </xsl:choose>
280

    
281
    </xsl:variable>
282

    
283

    
284

    
285
    <debug select="drawLine{$x0},{$y0},{$x1},{$y1} slope={$slope}"/>
286

    
287
    <xsl:choose>
288

    
289
      <xsl:when test="$x0 = $x1">
290

    
291
        <xsl:call-template name="fillBox">
292

    
293
          <xsl:with-param name="x0" select="$x0 - floor($lineWidth div 2)"/>
294

    
295
          <xsl:with-param name="y0" select="$y0"/>
296

    
297
          <xsl:with-param name="x1" select="$x0 + floor($lineWidth div 2)"/>
298

    
299
          <xsl:with-param name="y1" select="$y1"/>
300

    
301
        </xsl:call-template>
302

    
303
      </xsl:when>
304

    
305
      <xsl:when test="$y0 = $y1">
306

    
307
        <xsl:call-template name="fillBox">
308

    
309
          <xsl:with-param name="x0" select="$x0"/>
310

    
311
          <xsl:with-param name="y0" select="$y0 - floor($lineWidth div 2)"/>
312

    
313
          <xsl:with-param name="x1" select="$x1"/>
314

    
315
          <xsl:with-param name="y1" select="$y1 + floor($lineWidth div 2)"/>
316

    
317
        </xsl:call-template>
318

    
319
      </xsl:when>
320

    
321
      <xsl:when test="$slope > 0.5 or $slope &lt; -0.5">
322

    
323
        <xsl:call-template name="drawSteepLine">
324

    
325
          <xsl:with-param name="slope" select="$slope"/>
326

    
327
          <xsl:with-param name="x0" select="$x0"/>
328

    
329
          <xsl:with-param name="y0" select="$y0"/>
330

    
331
          <xsl:with-param name="x1" select="$x1"/>
332

    
333
          <xsl:with-param name="y1" select="$y1"/>
334

    
335
        </xsl:call-template>
336

    
337
      </xsl:when>
338

    
339
      <xsl:otherwise>
340

    
341
        <xsl:call-template name="drawFlatLine">
342

    
343
          <xsl:with-param name="slope" select="$slope"/>
344

    
345
          <xsl:with-param name="x0" select="$x0"/>
346

    
347
          <xsl:with-param name="y0" select="$y0"/>
348

    
349
          <xsl:with-param name="x1" select="$x1"/>
350

    
351
          <xsl:with-param name="y1" select="$y1"/>
352

    
353
        </xsl:call-template>
354

    
355
      </xsl:otherwise>
356

    
357
    </xsl:choose>
358

    
359
  </xsl:template>
360

    
361
  
362

    
363
  <!-- Draw Line with height > width.  Recursively calls itself drawing a series
364

    
365
  of vertical lines with each recursion. -->
366

    
367
  <xsl:template name="drawSteepLine">
368

    
369
    <xsl:param name="slope"/> <!-- height/width -->
370

    
371
    <xsl:param name="x0"/>
372

    
373
    <xsl:param name="y0"/>
374

    
375
    <xsl:param name="x1"/>
376

    
377
    <xsl:param name="y1"/>
378

    
379
    
380

    
381
    <xsl:variable name="inc">
382

    
383
      <xsl:choose>
384

    
385
        <xsl:when test="$x0 &lt; $x1">1</xsl:when>
386

    
387
        <xsl:otherwise>-1</xsl:otherwise>
388

    
389
      </xsl:choose>
390

    
391
    </xsl:variable>
392

    
393

    
394

    
395
    <debug select="drawSteepLine {$x0},{$y0},{$x1},{$y1} slope={$slope} inc={$inc}"/>
396

    
397

    
398

    
399
    <xsl:call-template name="fillBox">
400

    
401
      <xsl:with-param name="x0" select="$x0 - floor(($lineWidth - 1) div 2)"/>
402

    
403

    
404

    
405
      <xsl:with-param name="y0">
406

    
407
        <xsl:choose>
408

    
409
          <xsl:when test="$y0"> <!-- Start of line -->
410

    
411
            <xsl:value-of select="$y0"/>
412

    
413
          </xsl:when>
414

    
415
          <xsl:otherwise>
416

    
417
            <xsl:value-of select="$y1 + floor($slope * ($x0 - $x1 + 0.5 * $inc))"/>
418

    
419
          </xsl:otherwise>
420

    
421
        </xsl:choose>
422

    
423
      </xsl:with-param>
424

    
425

    
426

    
427
      <xsl:with-param name="x1" select="$x0 + floor(($lineWidth - 1) div 2)"/>
428

    
429

    
430

    
431
      <xsl:with-param name="y1">
432

    
433
        <xsl:choose>
434

    
435
          <xsl:when test="$x0 = $x1"> <!-- End of line -->
436

    
437
            <xsl:value-of select="$y1"/>
438

    
439
          </xsl:when>
440

    
441
          <xsl:otherwise>
442

    
443
            <xsl:value-of select="$y1 + floor($slope * ($x0 - $x1 - 0.5 * $inc))"/>
444

    
445
          </xsl:otherwise>
446

    
447
        </xsl:choose>
448

    
449
      </xsl:with-param>
450

    
451
    </xsl:call-template>
452

    
453
    
454

    
455
    <xsl:if test="$x0 != $x1">
456

    
457
      <xsl:call-template name="drawSteepLine">
458

    
459
        <xsl:with-param name="x0" select="$x0 + $inc"/>
460

    
461
        <xsl:with-param name="x1" select="$x1"/>
462

    
463
        <xsl:with-param name="y1" select="$y1"/>
464

    
465
        <xsl:with-param name="slope" select="$slope"/>
466

    
467
      </xsl:call-template>
468

    
469
    </xsl:if>
470

    
471
  </xsl:template>
472

    
473

    
474

    
475
  <!--
476

    
477
  Draw Line with width > height.  Recursively calls itself drawing a series
478

    
479
  of horizontal lines with each recursion.
480

    
481
  -->
482

    
483
  <xsl:template name="drawFlatLine">
484

    
485
    <xsl:param name="slope"/> <!-- height/width -->
486

    
487
    <xsl:param name="x0"/> <!-- Only defined on first call -->
488

    
489
    <xsl:param name="y0"/>
490

    
491
    <xsl:param name="x1"/>
492

    
493
    <xsl:param name="y1"/>
494

    
495
    
496

    
497
    <xsl:variable name="inc">
498

    
499
      <xsl:choose>
500

    
501
        <xsl:when test="$y0 &lt; $y1">1</xsl:when>
502

    
503
        <xsl:otherwise>-1</xsl:otherwise>
504

    
505
      </xsl:choose>
506

    
507
    </xsl:variable>
508

    
509

    
510

    
511
    <debug select="drawFlatLine {$x0},{$y0},{$x1},{$y1} slope={$slope} inc={$inc}"/>
512

    
513

    
514

    
515
    <xsl:call-template name="fillBox">
516

    
517

    
518

    
519
      <xsl:with-param name="x0">
520

    
521
        <xsl:choose>
522

    
523
          <xsl:when test="$x0"> <!-- Start of line -->
524

    
525
            <xsl:value-of select="$x0"/>
526

    
527
          </xsl:when>
528

    
529
          <xsl:otherwise>
530

    
531
            <xsl:value-of select="$x1 - floor(($y1 - $y0 + 0.5 * $inc) div $slope)"/>
532

    
533
          </xsl:otherwise>
534

    
535
        </xsl:choose>
536

    
537
      </xsl:with-param>
538

    
539

    
540

    
541
      <xsl:with-param name="y0" select="$y0 - floor(($lineWidth - 1) div 2)"/>
542

    
543

    
544

    
545
      <xsl:with-param name="x1">
546

    
547
        <xsl:choose>
548

    
549
          <xsl:when test="$y0 = $y1"> <!-- End of line -->
550

    
551
            <xsl:value-of select="$x1"/>
552

    
553
          </xsl:when>
554

    
555
          <xsl:otherwise>
556

    
557
            <xsl:value-of select="$x1 - floor(($y1 - $y0 - 0.5 * $inc) div $slope)"/>
558

    
559
          </xsl:otherwise>
560

    
561
        </xsl:choose>
562

    
563
      </xsl:with-param>
564

    
565

    
566

    
567
      <xsl:with-param name="y1" select="$y0 + floor(($lineWidth - 1) div 2)"/>
568

    
569
    </xsl:call-template>
570

    
571
    
572

    
573
    <xsl:if test="$y0 != $y1">
574

    
575
      <xsl:call-template name="drawFlatLine">
576

    
577
        <xsl:with-param name="y0" select="$y0 + $inc"/>
578

    
579
        <xsl:with-param name="x1" select="$x1"/>
580

    
581
        <xsl:with-param name="y1" select="$y1"/>
582

    
583
        <xsl:with-param name="slope" select="$slope"/>
584

    
585
      </xsl:call-template>
586

    
587
    </xsl:if>
588

    
589
  </xsl:template>
590

    
591

    
592

    
593
  <!-- Render a solid box -->
594

    
595
  <xsl:template name="fillBox">
596

    
597
    <xsl:param name="x0"/>
598

    
599
    <xsl:param name="y0"/>
600

    
601
    <xsl:param name="x1"/>
602

    
603
    <xsl:param name="y1"/>
604

    
605

    
606

    
607
    <debug select="fillBox {$x0},{$y0},{$x1},{$y1}"/>
608

    
609
    <xsl:variable name="xMax">
610

    
611
      <xsl:choose>
612

    
613
        <xsl:when test="$x1 > $x0">
614

    
615
          <xsl:value-of select="$x1"/>
616

    
617
        </xsl:when>
618

    
619
        <xsl:otherwise>
620

    
621
          <xsl:value-of select="$x0"/>
622

    
623
        </xsl:otherwise>
624

    
625
      </xsl:choose>
626

    
627
    </xsl:variable>
628

    
629

    
630

    
631
    <xsl:variable name="xMin">
632

    
633
      <xsl:choose>
634

    
635
        <xsl:when test="$x1 > $x0">
636

    
637
          <xsl:value-of select="$x0"/>
638

    
639
        </xsl:when>
640

    
641
        <xsl:otherwise>
642

    
643
          <xsl:value-of select="$x1"/>
644

    
645
        </xsl:otherwise>
646

    
647
      </xsl:choose>
648

    
649
    </xsl:variable>
650

    
651

    
652

    
653
    <xsl:variable name="yMin">
654

    
655
      <xsl:choose>
656

    
657
        <xsl:when test="$y1 > $y0">
658

    
659
          <xsl:value-of select="$y0"/>
660

    
661
        </xsl:when>
662

    
663
        <xsl:otherwise>
664

    
665
          <xsl:value-of select="$y1"/>
666

    
667
        </xsl:otherwise>
668

    
669
      </xsl:choose>
670

    
671
    </xsl:variable>
672

    
673

    
674

    
675
    <xsl:variable name="yMax">
676

    
677
      <xsl:choose>
678

    
679
        <xsl:when test="$y1 > $y0">
680

    
681
          <xsl:value-of select="$y1"/>
682

    
683
        </xsl:when>
684

    
685
        <xsl:otherwise>
686

    
687
          <xsl:value-of select="$y0"/>
688

    
689
        </xsl:otherwise>
690

    
691
      </xsl:choose>
692

    
693
    </xsl:variable>
694

    
695

    
696

    
697
    <div style="position:absolute; left:{$xMin}px; top:{$yMin}px; width:{$xMax - $xMin +1}px; height:{$yMax -$yMin +1}px; background-color:{$color}"><i/></div>
698

    
699
  </xsl:template>
700

    
701

    
702

    
703
  <!-- Render a <div> box -->
704

    
705
  <xsl:template name="mkDiv">
706

    
707
    <xsl:param name="x"/>
708

    
709
    <xsl:param name="y"/>
710

    
711
    <xsl:param name="w"/>
712

    
713
    <xsl:param name="h"/>
714

    
715

    
716

    
717
    <div style="position:absolute; left:{$x}px; top:{$y}px; width:{$w}px; height:{$h}px; background-color:{$color}"><i/></div>
718

    
719
  </xsl:template>
720

    
721

    
722

    
723
  <xsl:template match="text()|@*"/>
724

    
725
  
726

    
727
</xsl:stylesheet>
728

    
(46-46/145)