Module: GeoserverMigrations::SldHelpers

Included in:
LayerConfig
Defined in:
lib/geoserver_migrations/sld_helpers.rb

Instance Method Summary collapse

Instance Method Details

#build_sld_filter(filter_hash) ⇒ Object



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
# File 'lib/geoserver_migrations/sld_helpers.rb', line 5

def build_sld_filter(filter_hash)
  filter_text = ""
  filters_count = filter_hash.keys.count
  if filters_count > 0
    if filters_count == 1
      filter_text = <<-FILTER
        <ogc:Filter>
          <ogc:PropertyIsEqualTo>
            <ogc:PropertyName>#{filter_hash.keys[0]}</ogc:PropertyName>
            <ogc:Literal>#{filter_hash.values[0]}</ogc:Literal>
          </ogc:PropertyIsEqualTo>
        </ogc:Filter>
      FILTER
    elsif filters_count == 2
      filter_text = <<-FILTER
        <ogc:Filter>
          <ogc:And>
            <ogc:PropertyIsEqualTo>
              <ogc:PropertyName>#{filter_hash.keys[0]}</ogc:PropertyName>
              <ogc:Literal>#{filter_hash.values[0]}</ogc:Literal>
            </ogc:PropertyIsEqualTo>
            <ogc:PropertyIsEqualTo>
              <ogc:PropertyName>#{filter_hash.keys[1]}</ogc:PropertyName>
              <ogc:Literal>#{filter_hash.values[1]}</ogc:Literal>
            </ogc:PropertyIsEqualTo>
          </ogc:And>  
        </ogc:Filter>
      FILTER
    else
      raise StandardError.new("Unsupported filter! For now we only support 1 or 2 filter-values :( :(")
    end
  end
  filter_text
end

#icon_style_with_label(title, icon_name, icon_style_options = {}) ⇒ Object



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
# File 'lib/geoserver_migrations/sld_helpers.rb', line 41

def icon_style_with_label(title, icon_name, icon_style_options={})
  display_label = icon_style_options[:display_label] || title
  abstract      = icon_style_options[:abstract] || title

  filter_text = ""
  if icon_style_options[:filter].present? && icon_style_options[:filter].is_a?(Hash)
    filter_text = build_sld_filter(icon_style_options[:filter])
  end

  max_scale_text = ""
  if icon_style_options[:max_scale_denominator].present?
    max_scale_text = "<MaxScaleDenominator>#{icon_style_options[:max_scale_denominator]}</MaxScaleDenominator>"
  end

  @options[:sld] = <<-SLD.strip_heredoc 
    <?xml version="1.0" encoding="UTF-8"?>
    <StyledLayerDescriptor version="1.0.0" xmlns="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc"
      xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd">
      <NamedLayer>
        <Name>#{title}</Name>
        <UserStyle>
          <Name>#{title}</Name>
          <Title>#{title}</Title>
          <Abstract>#{abstract}</Abstract>
          <FeatureTypeStyle>
            <Rule>
              <Title>#{display_label}</Title>  
              #{filter_text}
              #{max_scale_text}  
              <PointSymbolizer>
                <Graphic>
                  <ExternalGraphic>
                    <OnlineResource xlink:type="simple" xlink:href="#{icon_name}" />
                    <Format>image/png</Format>
                  </ExternalGraphic>
                </Graphic>
                <VendorOption name="labelObstacle">true</VendorOption>
              </PointSymbolizer>
              <TextSymbolizer>
                <Label>
                  <ogc:PropertyName>name</ogc:PropertyName>
                </Label>
                <Font>
                  <CssParameter name="font-family">Arial</CssParameter>
                  <CssParameter name="font-size">11</CssParameter>
                  <CssParameter name="font-style">normal</CssParameter>
                  <CssParameter name="font-weight">bold</CssParameter>
                </Font>
                <LabelPlacement>
                  <PointPlacement>
                    <AnchorPoint>
                      <AnchorPointX>0</AnchorPointX>
                      <AnchorPointY>0</AnchorPointY>
                    </AnchorPoint>
                  </PointPlacement>
                </LabelPlacement>
                <Halo>
                  <Radius>
                    <ogc:Literal>1</ogc:Literal>
                  </Radius>
                  <Fill>
                    <CssParameter name="fill">#ffffff</CssParameter>
                  </Fill>
                </Halo>
                <Fill>
                  <CssParameter name="fill">#2f3133</CssParameter>
                </Fill>
                <VendorOption name="maxDisplacement">40</VendorOption>
                <!--VendorOption name="displacementMode">S, SW, W, NW, N</VendorOption-->
                <VendorOption name="conflictResolution">true</VendorOption>   
                <VendorOption name="partials">true</VendorOption>
              </TextSymbolizer>          
            </Rule>
          </FeatureTypeStyle>
        </UserStyle>
      </NamedLayer>
    </StyledLayerDescriptor>
  SLD

end

#line_style(title, style_options = {}) ⇒ Object



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
# File 'lib/geoserver_migrations/sld_helpers.rb', line 199

def line_style(title, style_options={})
  display_label = style_options[:display_label] || title
  abstract      = style_options[:abstract] || title

  stroke_colour = style_options[:stroke_colour] || "#000000"
  stroke_width  = style_options[:stroke_width]  || "1"


  filter_text = ""
  if style_options[:filter].present? && style_options[:filter].is_a?(Hash)
    filter_text = build_sld_filter(style_options[:filter])
  end

  max_scale_text = ""
  if style_options[:max_scale_denominator].present?
    max_scale_text = "<MaxScaleDenominator>#{style_options[:max_scale_denominator]}</MaxScaleDenominator>"
  end

  @options[:sld] = <<-SLD.strip_heredoc
    <?xml version="1.0" encoding="UTF-8"?>
    <StyledLayerDescriptor version="1.0.0" 
     xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd" 
     xmlns="http://www.opengis.net/sld" 
     xmlns:ogc="http://www.opengis.net/ogc" 
     xmlns:xlink="http://www.w3.org/1999/xlink" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <NamedLayer>
        <Name>#{title}</Name>
        <UserStyle>
          <Name>#{title}</Name>
          <Title>#{title}</Title>
          <Abstract>#{abstract}</Abstract>
          <FeatureTypeStyle>
            <Rule>
              <Title>#{display_label}</Title>  
              #{filter_text}
              #{max_scale_text}  
              <LineSymbolizer>
                <Stroke>
                  <CssParameter name="stroke">#{stroke_colour}</CssParameter>
                  <CssParameter name="stroke-width">#{stroke_width}</CssParameter>
               </Stroke>
              </LineSymbolizer>
            </Rule>
          </FeatureTypeStyle>
        </UserStyle>
      </NamedLayer>
    </StyledLayerDescriptor>
  SLD

end

#polygon_style(title, style_options = {}) ⇒ Object



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
# File 'lib/geoserver_migrations/sld_helpers.rb', line 124

def polygon_style(title, style_options={})
  display_label = style_options[:display_label] || title
  abstract      = style_options[:abstract] || title

  fill_colour   = style_options[:fill_colour]   || "#aaaaaa"
  fill_opacity  = style_options[:fill_opacity]  || "1"
  stroke_colour = style_options[:stroke_colour] || "#000000"
  stroke_width  = style_options[:stroke_width]  || "1"

  filter_text = ""
  if style_options[:filter].present? && style_options[:filter].is_a?(Hash)
    filter_text = build_sld_filter(style_options[:filter])
  end

  fill_style = ""
  unless style_options[:no_fill] == true
    fill_styles = ["<Fill>"]
    fill_styles << "                      <CssParameter name=\"fill\">#{fill_colour}</CssParameter>"
    fill_styles << "                      <CssParameter name=\"opacity\">#{fill_opacity}</CssParameter>" if style_options[:fill_opacity].present?
    fill_styles << "                    </Fill>"
    fill_style = fill_styles.join("\n")
  end


  stroke_styles = []
  stroke_styles << "<Stroke>"
  stroke_styles << "                      <CssParameter name=\"stroke\">#{stroke_colour}</CssParameter>"
  stroke_styles << "                      <CssParameter name=\"stroke-width\">#{stroke_width}</CssParameter>"

  if style_options[:stroke_dasharray].present?
    stroke_styles << "                      <CssParameter name=\"stroke-dasharray\">"
    stroke_styles << "                           <ogc:Literal>#{style_options[:stroke_dasharray]}</ogc:Literal>"
    stroke_styles << "                      </CssParameter>"
  end
  stroke_styles << "                    </Stroke>"
  stroke_style = stroke_styles.join("\n")

  max_scale_text = ""
  if style_options[:max_scale_denominator].present?
    max_scale_text = "<MaxScaleDenominator>#{style_options[:max_scale_denominator]}</MaxScaleDenominator>"
  end

  @options[:sld] = <<-SLD.strip_heredoc
    <?xml version="1.0" encoding="UTF-8"?>
    <StyledLayerDescriptor version="1.0.0" 
     xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd" 
     xmlns="http://www.opengis.net/sld" 
     xmlns:ogc="http://www.opengis.net/ogc" 
     xmlns:xlink="http://www.w3.org/1999/xlink" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <NamedLayer>
        <Name>#{title}</Name>
        <UserStyle>
          <Name>#{title}</Name>
          <Title>#{title}</Title>
          <Abstract>#{abstract}</Abstract>
          <FeatureTypeStyle>
            <Rule>
              <Title>#{display_label}</Title>  
              #{filter_text}
              #{max_scale_text}  
              <PolygonSymbolizer>   
                #{fill_style}
                #{stroke_style}
              </PolygonSymbolizer>
            </Rule>
          </FeatureTypeStyle>
        </UserStyle>
      </NamedLayer>
    </StyledLayerDescriptor>
  SLD

end