-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path__init__.py
More file actions
1552 lines (1215 loc) · 51 KB
/
__init__.py
File metadata and controls
1552 lines (1215 loc) · 51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
StyledLayerDescriptor library for generating SLD documents.
SLD documents are used to style cartographic representations of geometric
features in most professional and desktop GIS applications.
Specification
=============
The SLD specification is available from the Open Geospatial Consortium,
at U{http://www.opengeospatial.org/standards/sld}
License
=======
Copyright 2011-2014 David Zwarg <U{david.a@zwarg.com}>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
U{http://www.apache.org/licenses/LICENSE-2.0}
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
@author: David Zwarg
@contact: david.a@zwarg.com
@copyright: 2011-2014, Azavea
@license: Apache 2.0
@version: 1.0.10
@newfield prop: Property, Properties
"""
from lxml.etree import parse, Element, XMLSchema, tostring
try:
from urllib2 import urlopen
except ImportError:
from urllib.request import urlopen
from tempfile import NamedTemporaryFile
import os
import copy
import logging
class SLDNode(object):
"""
A base class for all python objects that relate directly to SLD elements.
An SLDNode contains references to the underlying parent node, underlying
element node, and the namespace map.
The SLDNode base class also contains utility methods to construct properties
for child SLDNode objects.
"""
_nsmap = {
'sld': "http://www.opengis.net/sld",
'ogc': "http://www.opengis.net/ogc",
'xlink': "http://www.w3.org/1999/xlink",
'xsi': "http://www.w3.org/2001/XMLSchema-instance"
}
"""Defined namespaces in SLD documents."""
def __init__(self, parent, descendant=True):
"""
Create a new SLDNode. It is not necessary to call this directly, because
all child classes should initialize the SLDNode internally.
@type parent: L{SLDNode}
@param parent: The parent class object.
@type descendant: boolean
@param descendant: Does this element descend from the parent, or is it a sibling?
"""
if parent is None:
self._parent = None
elif descendant:
self._parent = parent._node
else:
self._parent = parent._parent
self._node = None
@staticmethod
def makeproperty(ns, cls=None, name=None, docstring='', descendant=True):
"""
Make a property on an instance of an SLDNode. If cls is omitted, the
property is assumed to be a text node, with no corresponding class
object. If name is omitted, the property is assumed to be a complex
node, with a corresponding class wrapper.
@type ns: string
@param ns: The namespace of this property's node.
@type cls: class
@param cls: Optional. The class of the child property.
@type name: string
@param name: Optional. The name of the child property.
@type docstring: string
@param docstring: Optional. The docstring to attach to the new property.
@type descendant: boolean
@param descendant: Does this element descend from the parent, or is it a sibling?
@rtype: property attribute
@return: A property attribute for this named property.
"""
def get_property(self):
"""
A generic property getter.
"""
if cls is None:
xpath = '%s:%s' % (ns, name)
else:
xpath = '%s:%s' % (ns, cls.__name__)
xpath = self._node.xpath(xpath, namespaces=SLDNode._nsmap)
if len(xpath) == 1:
if cls is None:
return xpath[0].text
else:
elem = cls.__new__(cls)
cls.__init__(elem, self, descendant=descendant)
return elem
else:
return None
def set_property(self, value):
"""
A generic property setter.
"""
if cls is None:
xpath = '%s:%s' % (ns, name)
else:
xpath = '%s:%s' % (ns, cls.__name__)
xpath = self._node.xpath(xpath, namespaces=SLDNode._nsmap)
if len(xpath) == 1:
if cls is None:
xpath[0].text = value
else:
xpath[0] = value._node
else:
if cls is None:
elem = self._node.makeelement('{%s}%s' % (SLDNode._nsmap[ns], name), nsmap=SLDNode._nsmap)
elem.text = value
self._node.append(elem)
else:
self._node.append(value._node)
def del_property(self):
"""
A generic property deleter.
"""
if cls is None:
xpath = '%s:%s' % (ns, name)
else:
xpath = '%s:%s' % (ns, cls.__name__)
xpath = self._node.xpath(xpath, namespaces=SLDNode._nsmap)
if len(xpath) == 1:
self._node.remove(xpath[0])
return property(get_property, set_property, del_property, docstring)
def get_or_create_element(self, ns, name):
"""
Attempt to get the only child element from this SLDNode. If the node
does not exist, create the element, attach it to the DOM, and return
the class object that wraps the node.
@type ns: string
@param ns: The namespace of the new element.
@type name: string
@param name: The name of the new element.
@rtype: L{SLDNode}
@return: The wrapped node, in the parent's property class. This will
always be a descendent of SLDNode.
"""
if len(self._node.xpath('%s:%s' % (ns, name), namespaces=SLDNode._nsmap)) == 1:
return getattr(self, name)
return self.create_element(ns, name)
def create_element(self, ns, name):
"""
Create an element as a child of this SLDNode.
@type ns: string
@param ns: The namespace of the new element.
@type name: string
@param name: The name of the new element.
@rtype: L{SLDNode}
@return: The wrapped node, in the parent's property class. This will
always be a descendent of SLDNode.
"""
elem = self._node.makeelement('{%s}%s' % (SLDNode._nsmap[ns], name), nsmap=SLDNode._nsmap)
self._node.append(elem)
return getattr(self, name)
class CssParameter(SLDNode):
"""
A css styling parameter. May be a child of L{Fill}, L{Font}, and L{Stroke}.
"""
def __init__(self, parent, index, descendant=True):
"""
Create a new CssParameter from an existing StyleItem.
@type parent: L{StyleItem}
@param parent: The parent class object.
@type index: integer
@param index: The index of the node in the list of all CssParameters in the parent.
@type descendant: boolean
@param descendant: Does this element descend from the parent, or is it a sibling?
"""
super(CssParameter, self).__init__(parent, descendant=descendant)
self._node = self._parent.xpath('sld:CssParameter', namespaces=SLDNode._nsmap)[index]
def get_name(self):
"""
Get the name attribute.
@rtype: string
@return: The value of the 'name' attribute.
"""
return self._node.attrib['name']
def set_name(self, value):
"""
Set the name attribute.
@type value: string
@param value: The value of the 'name' attribute.
"""
self._node.attrib['name'] = value
def del_name(self):
"""
Delete the name attribute.
"""
del self._node.attrib['name']
Name = property(get_name, set_name, del_name, "The value of the 'name' attribute.")
"""The value of the 'name' attribute."""
def get_value(self):
"""
Get the text content.
@rtype: string
@return: The text content.
"""
return self._node.text
def set_value(self, value):
"""
Set the text content.
@type value: string
@param value: The text content.
"""
self._node.text = value
def del_value(self):
"""
Delete the text content.
"""
self._node.clear()
Value = property(get_value, set_value, del_value, "The value of the parameter.")
"""The value of the parameter."""
class CssParameters(SLDNode):
"""
A collection of L{CssParameter} nodes. This is a pythonic helper (list of
nodes) that does not correspond to a true element in the SLD spec.
"""
def __init__(self, parent):
"""
Create a new list of CssParameters from the specified parent node.
@type parent: L{StyleItem}
@param parent: The parent class item.
"""
super(CssParameters, self).__init__(parent)
self._node = None
self._nodes = self._parent.xpath('sld:CssParameter', namespaces=SLDNode._nsmap)
def __len__(self):
"""
Get the number of L{CssParameter} nodes in this list.
@rtype: integer
@return: The number of L{CssParameter} nodes.
"""
return len(self._nodes)
def __getitem__(self, key):
"""
Get one of the L{CssParameter} nodes in the list.
@type key: integer
@param key: The index of the child node.
@rtype: L{CssParameter}
@return: The specific L{CssParameter} node.
"""
return CssParameter(self, key, descendant=False)
def __setitem__(self, key, value):
"""
Set one of the L{CssParameter} nodes in the list with a new value.
@type key: integer
@param key: The index of the child node.
@type value: L{CssParameter}, etree.Element
@param value: The new value of the specific child node.
"""
if isinstance(value, CssParameter):
self._nodes.replace(self._nodes[key], value._node)
elif isinstance(value, Element):
self._nodes.replace(self._nodes[key], value)
def __delitem__(self, key):
"""
Delete one of the L{CssParameter} nodes from the list.
@type key: integer
@param key: The index of the child node.
"""
self._nodes.remove(self._nodes[key])
class StyleItem(SLDNode):
"""
Abstract base class for all nodes that contain a list of L{CssParameter} nodes.
"""
def __init__(self, parent, name, descendant=True):
"""
Create a new StyleItem.
@type parent: L{Symbolizer}
@param parent: The parent class object.
@type name: string
@param name: The name of the node.
@type descendant: boolean
@param descendant: Does this element descend from the parent, or is it a sibling?
"""
super(StyleItem, self).__init__(parent, descendant=descendant)
xpath = self._parent.xpath('sld:' + name, namespaces=SLDNode._nsmap)
if len(xpath) < 1:
self._node = self._parent.makeelement('{%s}%s' % (SLDNode._nsmap['sld'], name), nsmap=SLDNode._nsmap)
self._parent.append(self._node)
else:
self._node = xpath[0]
@property
def CssParameters(self):
"""
Get the list of L{CssParameter} nodes in a friendly L{CssParameters} helper list.
@rtype: L{CssParameters}
@return: A pythonic list of L{CssParameter} children.
"""
return CssParameters(self)
def create_cssparameter(self, name=None, value=None):
"""
Create a new L{CssParameter} node as a child of this element, and attach it to the DOM.
Optionally set the name and value of the parameter, if they are both provided.
@type name: string
@param name: Optional. The name of the L{CssParameter}
@type value: string
@param value: Optional. The value of the L{CssParameter}
@rtype: L{CssParameter}
@return: A new style parameter, set to the name and value.
"""
elem = self._node.makeelement('{%s}CssParameter' % SLDNode._nsmap['sld'], nsmap=SLDNode._nsmap)
self._node.append(elem)
if not (name is None or value is None):
elem.attrib['name'] = name
elem.text = value
return CssParameter(self, len(self._node) - 1)
class Fill(StyleItem):
"""
A style specification for fill types. This class contains a
L{CssParameters} list, which can include:
- fill
- fill-opacity
This class is a property of any L{Symbolizer}.
"""
def __init__(self, parent, descendant=True):
"""
Create a new Fill node from the specified parent.
@type parent: L{Symbolizer}
@param parent: The parent class object.
@type descendant: boolean
@param descendant: A flag indicating if this is a descendant node of the parent.
"""
super(Fill, self).__init__(parent, 'Fill', descendant=descendant)
class Font(StyleItem):
"""
A style specification for font types. This class contains a
L{CssParameters} list, which can include:
- font-family
- font-size
- font-style
- font-weight
This class is a property of any L{Symbolizer}.
"""
def __init__(self, parent, descendant=True):
"""
Create a new Font node from the specified parent.
@type parent: L{Symbolizer}
@param parent: The parent class object.
@type descendant: boolean
@param descendant: A flag indicating if this is a descendant node of the parent.
"""
super(Font, self).__init__(parent, 'Font', descendant=descendant)
class Stroke(StyleItem):
"""
A style specification for stroke types. This class contains a
L{CssParameters} list, which can include:
- stroke
- stroke-dasharray
- stroke-dashoffset
- stroke-linecap
- stroke-linejoin
- stroke-opacity
- stroke-width
This class is a property of any L{Symbolizer}.
"""
def __init__(self, parent, descendant=True):
"""
Create a new Stroke node from the specified parent.
@type parent: L{Symbolizer}
@param parent: The parent class object.
@type descendant: boolean
@param descendant: A flag indicating if this is a descendant node of the parent.
"""
super(Stroke, self).__init__(parent, 'Stroke', descendant=descendant)
class Symbolizer(SLDNode):
"""
Abstract base class for all symbolizer nodes. Symbolizer nodes are those
that contain L{Fill}, L{Font}, or L{Stroke} children.
All derived Symbolizer classes have access to the Fill, Font, and Stroke properties.
@prop: B{Fill}
The element that contains the L{CssParameter} nodes for describing the polygon fill styles.
I{Type}: L{Fill}
@prop: B{Font}
The element that contains the L{CssParameter} nodes for describing the font styles.
I{Type}: L{Font}
@prop: B{Stroke}
The element that contains the L{CssParameter} nodes for describing the line styles.
I{Type}: L{Stroke}
"""
def __init__(self, parent, name, descendant=True):
"""
Create a new Symbolizer node. If the specified node is not found in the
DOM, the node will be created and attached to the parent.
@type parent: L{Rule}
@param parent: The parent class object.
@type name: string
@param name: The type of symbolizer node. If this parameter ends with
the character '*', the '*' will get expanded into 'Symbolizer'.
@type descendant: boolean
@param descendant: A flag indicating if this is a descendant node of the parent.
"""
super(Symbolizer, self).__init__(parent, descendant=descendant)
if name[len(name) - 1] == '*':
name = name[0:-1] + 'Symbolizer'
xpath = self._parent.xpath('sld:%s' % name, namespaces=SLDNode._nsmap)
if len(xpath) < 1:
self._node = self._parent.makeelement('{%s}%s' % (SLDNode._nsmap['sld'], name), nsmap=SLDNode._nsmap)
self._parent.append(self._node)
else:
self._node = xpath[0]
setattr(self.__class__, 'Fill', SLDNode.makeproperty('sld', cls=Fill,
docstring="The parameters for describing the fill styling."))
setattr(self.__class__, 'Font', SLDNode.makeproperty('sld', cls=Font,
docstring="The parameters for describing the font styling."))
setattr(self.__class__, 'Stroke', SLDNode.makeproperty('sld', cls=Stroke,
docstring="The parameters for describing the stroke styling."))
def create_fill(self):
"""
Create a new L{Fill} element on this Symbolizer.
@rtype: L{Fill}
@return: A new fill element, attached to this symbolizer.
"""
return self.create_element('sld', 'Fill')
def create_font(self):
"""
Create a new L{Font} element on this Symbolizer.
@rtype: L{Font}
@return: A new font element, attached to this symbolizer.
"""
return self.create_element('sld', 'Font')
def create_stroke(self):
"""
Create a new L{Stroke} element on this Symbolizer.
@rtype: L{Stroke}
@return: A new stroke element, attached to this symbolizer.
"""
return self.create_element('sld', 'Stroke')
class PolygonSymbolizer(Symbolizer):
"""
A symbolizer for polygon geometries. A PolygonSymbolizer is a child of a
L{Rule} element.
@prop: Fill
The element that contains the L{CssParameter} nodes for describing the
polygon fill styles.
I{Type}: L{Fill}
@prop: Stroke
The element that contains the L{CssParameter} nodes for describing the line
styles.
I{Type}: L{Stroke}
"""
def __init__(self, parent, descendant=True):
"""
Create a new PolygonSymbolizer node, as a child of the specified parent.
@type parent: L{Rule}
@param parent: The parent class object.
@type descendant: boolean
@param descendant: A flag indicating if this is a descendant node of the parent.
"""
super(PolygonSymbolizer, self).__init__(parent, 'Polygon*', descendant)
class LineSymbolizer(Symbolizer):
"""
A symbolizer for line geometries. A LineSymbolizer is a child of a
L{Rule} element.
@prop: Stroke
The element that contains the L{CssParameter} nodes for describing the line
styles.
I{Type}: L{Stroke}
"""
def __init__(self, parent, descendant=True):
"""
Create a new LineSymbolizer node, as a child of the specified parent.
@type parent: L{Rule}
@param parent: The parent class object.
@type descendant: boolean
@param descendant: A flag indicating if this is a descendant node of the parent.
"""
super(LineSymbolizer, self).__init__(parent, 'Line*', descendant)
class TextSymbolizer(Symbolizer):
"""
A symbolizer for text labels. A TextSymbolizer is a child of a L{Rule}
element.
@prop: Fill
The element that contains the L{CssParameter} nodes for describing the
character fill styles.
I{Type}: L{Fill}
"""
def __init__(self, parent, descendant=True):
"""
Create a new TextSymbolizer node, as a child of the specified parent.
@type parent: L{Rule}
@param parent: The parent class object.
@type descendant: boolean
@param descendant: A flag indicating if this is a descendant node of the parent.
"""
super(TextSymbolizer, self).__init__(parent, 'Text*', descendant=descendant)
class Mark(Symbolizer):
"""
A graphic mark for describing points. A Mark is a child of a L{Graphic}
element.
@prop: Fill
The element that contains the L{CssParameter} nodes for describing the
fill styles.
I{Type}: L{Fill}
@prop: Stroke
The element that contains the L{CssParameter} nodes for describing the
line styles.
I{Type}: L{Stroke}
@prop: WellKnownName
A string describing the Mark, which may be one of:
- circle
- cross
- square
- star
- triangle
- x
I{Type}: string
"""
def __init__(self, parent, descendant=True):
"""
Create a new Mark node, as a child of the specified parent.
@type parent: L{Graphic}
@param parent: The parent class object.
@type descendant: boolean
@param descendant: A flag indicating if this is a descendant node of the parent.
"""
super(Mark, self).__init__(parent, 'Mark', descendant=descendant)
setattr(self.__class__, 'WellKnownName', SLDNode.makeproperty('sld', name='WellKnownName',
docstring="The well known name for the mark."))
class Graphic(SLDNode):
"""
A Graphic node represents a graphical mark for representing points. A
Graphic is a child of a L{PointSymbolizer} element.
@prop: Mark
The element that contains the L{CssParameter} nodes for describing the point styles.
I{Type}: L{Mark}
@prop: Opacity
Bewteen 0 (completely transparent) and 1 (completely opaque)
I{Type}: float
@prop: Size
The size of the graphic, in pixels.
I{Type}: integer
@prop: Rotation
Clockwise degrees of rotation.
I{Type}: float
"""
def __init__(self, parent, descendant=True):
"""
Create a new Graphic node, as a child of the specified parent.
@type parent: L{PointSymbolizer}
@param parent: The parent class object.
@type descendant: boolean
@param descendant: A flag indicating if this is a descendant node of the parent.
"""
super(Graphic, self).__init__(parent, descendant=descendant)
xpath = self._parent.xpath('sld:Graphic', namespaces=SLDNode._nsmap)
if len(xpath) < 1:
self._node = self._parent.makeelement('{%s}Graphic' % SLDNode._nsmap['sld'], nsmap=SLDNode._nsmap)
self._parent.append(self._node)
else:
self._node = xpath[0]
setattr(self.__class__, 'Mark', SLDNode.makeproperty('sld', cls=Mark,
docstring="The graphic's mark styling."))
setattr(self.__class__, 'Opacity', SLDNode.makeproperty('sld', name='Opacity',
docstring="The opacity of the graphic."))
setattr(self.__class__, 'Size', SLDNode.makeproperty('sld', name='Size',
docstring="The size of the graphic, in pixels."))
setattr(self.__class__, 'Rotation', SLDNode.makeproperty('sld', name='Rotation',
docstring="The rotation of the graphic, in degrees clockwise."))
class PointSymbolizer(SLDNode):
"""
A symbolizer for point geometries. A PointSymbolizer is a child of a
L{Rule} element.
@prop: Graphic
The configuration of the point graphic.
I{Type}: L{Graphic}
"""
def __init__(self, parent, descendant=True):
"""
Create a new PointSymbolizer node, as a child of the specified parent.
@type parent: L{Rule}
@param parent: The parent class object.
@type descendant: boolean
@param descendant: A flag indicating if this is a descendant node of the parent.
"""
super(PointSymbolizer, self).__init__(parent, descendant=descendant)
xpath = self._parent.xpath('sld:PointSymbolizer', namespaces=SLDNode._nsmap)
if len(xpath) < 1:
self._node = self._parent.makeelement('{%s}PointSymbolizer' % SLDNode._nsmap['sld'], nsmap=SLDNode._nsmap)
self._parent.append(self._node)
else:
self._node = xpath[0]
setattr(self.__class__, 'Graphic', SLDNode.makeproperty('sld', cls=Graphic,
docstring="The graphic settings for this point geometry."))
class PropertyCriterion(SLDNode):
"""
General property criterion class for all property comparitors.
A PropertyCriterion is a child of a L{Filter} element.
Valid property comparitors that are represented by this class are:
- PropertyIsNotEqual
- PropertyIsLessThan
- PropertyIsLessThanOrEqual
- PropertyIsEqual
- PropertyIsGreaterThan
- PropertyIsGreaterThanOrEqual
- PropertyIsLike
@prop: PropertyName
The name of the property to use in the comparison.
I{Type}: string
@prop: Literal
The value of the property.
I{Type}: string
"""
def __init__(self, parent, name, descendant=True):
"""
Create a new PropertyCriterion node, as a child of the specified parent.
A PropertyCriterion is not represented in the SLD Spec. This class
is a generalization of many of the PropertyIs... elements present in
the OGC Filter spec.
@type parent: L{Filter}
@param parent: The parent class object.
"""
super(PropertyCriterion, self).__init__(parent, descendant=descendant)
xpath = self._parent.xpath('ogc:' + name, namespaces=SLDNode._nsmap)
if len(xpath) < 1:
self._node = self._parent.makeelement('{%s}%s' % (SLDNode._nsmap['ogc'], name), nsmap=SLDNode._nsmap)
self._parent.append(self._node)
else:
self._node = xpath[0]
setattr(self.__class__, 'PropertyName', SLDNode.makeproperty('ogc', name='PropertyName',
docstring="The name of the property to compare."))
setattr(self.__class__, 'Literal', SLDNode.makeproperty('ogc', name='Literal',
docstring="The literal value of the property to compare against."))
class Filter(SLDNode):
"""
A filter object that stores the property comparitors. A Filter is a child
of a L{Rule} element. Filter nodes are pythonic, and have some syntactic
sugar that allows the creation of simple logical combinations.
To create an AND logical filter, use the '+' operator:
>>> rule.Filter = filter1 + filter2
To create an OR logical filter, use the '|' operator:
>>> rule.Filter = filter1 | filter2
Complex combinations can be created by chaining these operations together:
>>> rule.Filter = filter1 | (filter2 + filter3)
@prop: PropertyIsEqualTo
A specification of property (=) equality.
I{Type}: L{PropertyCriterion}
@prop: PropertyIsNotEqualTo
A specification of property (!=) inequality.
I{Type}: L{PropertyCriterion}
@prop: PropertyIsLessThan
A specification of property less-than (<) comparison.
I{Type}: L{PropertyCriterion}
@prop: PropertyIsLessThanOrEqualTo
A specification of property less-than-or-equal-to (<=) comparison.
I{Type}: L{PropertyCriterion}
@prop: PropertyIsGreaterThan
A specification of property greater-than (>) comparison,
I{Type}: L{PropertyCriterion}
@prop: PropertyIsGreaterThanOrEqualTo
A specification of property greater-than-or-equal-to (>=) comparison.
I{Type}: L{PropertyCriterion}
"""
def __init__(self, parent, descendant=True):
"""
Create a new Filter node.
@type parent: L{Rule}
@param parent: The parent class object.
@type descendant: boolean
@param descendant: A flag indicating if this is a descendant node of the parent.
"""
super(Filter, self).__init__(parent, descendant=descendant)
xpath = self._parent.xpath('ogc:Filter', namespaces=SLDNode._nsmap)
if len(xpath) == 1:
self._node = xpath[0]
else:
self._node = self._parent.makeelement('{%s}Filter' % SLDNode._nsmap['ogc'], nsmap=SLDNode._nsmap)
def __add__(self, other):
"""
Add two filters together to create one AND logical filter.
@type other: L{Filter}
@param other: A filter to AND with this one.
@rtype: L{Filter}
@return: A new filter with an ogc:And element as its child.
"""
if not self._node.getparent() is None:
self._node.getparent().remove(self._node)
elem = self._node.makeelement('{%s}And' % SLDNode._nsmap['ogc'])
elem.append(copy.copy(self._node[0]))
elem.append(copy.copy(other._node[0]))
f = Filter(self)
f._node.append(elem)
return f
def __or__(self, other):
"""
Or two filters together to create on OR logical filter.
@type other: L{Filter}
@param other: A filter to OR with this one.
@rtype: L{Filter}
@return: A new filter with an ogc:Or element as its child.
"""
elem = self._node.makeelement('{%s}Or' % SLDNode._nsmap['ogc'])
elem.append(copy.copy(self._node[0]))
elem.append(copy.copy(other._node[0]))
f = Filter(self)
f._node.append(elem)
return f
def __getattr__(self, name):
"""
Get a named attribute from this Filter instance. This method allows
properties with the prefix of 'PropertyIs' to be set, and raises
an AttributeError for all other property names.
@type name: string
@param name: The name of the property.
@rtype: L{PropertyCriterion}
@return: The property comparitor.
"""
if not name.startswith('PropertyIs'):
raise AttributeError('Property name must be one of: PropertyIsEqualTo, PropertyIsNotEqualTo, PropertyIsLessThan, PropertyIsLessThanOrEqualTo, PropertyIsGreaterThan, PropertyIsGreaterThanOrEqualTo, PropertyIsLike.')
xpath = self._node.xpath('ogc:' + name, namespaces=SLDNode._nsmap)
if len(xpath) == 0:
return None
return PropertyCriterion(self, name)
def __setattr__(self, name, value):
"""
Set a named attribute on this Filter instance. If the property name
begins with 'PropertyIs', the node value will be appended to the filter.
@type name: string
@param name: The name of the property.
@type value: L{PropertyCriterion}
@param value: The new property comparitor.
"""
if not name.startswith('PropertyIs'):
object.__setattr__(self, name, value)
return
xpath = self._node.xpath('ogc:' + name, namespaces=SLDNode._nsmap)
if len(xpath) > 0:
xpath[0] = value
else:
elem = self._node.makeelement('{%s}%s' % (SLDNode._nsmap['ogc'], name), nsmap=SLDNode._nsmap)
self._node.append(elem)
def __delattr__(self, name):
"""
Delete the property from the Filter. This removes the child node
of this name from the Filter.
@type name: string
@param name: The name of the property.
"""
xpath = self._node.xpath('ogc:' + name, namespaces=SLDNode._nsmap)
if len(xpath) > 0:
self._node.remove(xpath[0])
class Rule(SLDNode):
"""
A rule object contains a title, an optional L{Filter}, and one or more
L{Symbolizer}s. A Rule is a child of a L{FeatureTypeStyle}.
@prop: Title
The title of this rule. This is required for a valid SLD.
I{Type}: string
@prop: Filter
Optional. A filter defines logical comparisons against properties.
I{Type}: L{Filter}
@prop: PolygonSymbolizer
A symbolizer that defines how polygons should be rendered.
I{Type}: L{PolygonSymbolizer}
@prop: LineSymbolizer
A symbolizer that defines how lines should be rendered.
I{Type}: L{LineSymbolizer}
@prop: TextSymbolizer