A Ruby DSL for simpler SLD creation for styling maps
Map styles are often defined using SLD, or Style Layer Descriptor documents. These XML documents are very long, verbose and complex to maintain. Amanzi:SLD is a simpler DSL designed to generate SLD documents, but using a very much simpler syntax. Many common cases might be a single line only, while the simplest SLD is usually a dozen lines or more.
What is SLD?
There are great tutorials on the GeoServer wiki for SLD styling. Please take a look at GeoServer Styling
In brief, SLD is an XML syntax for specifying map layer styling. That is how to draw map layers. This includes simple things like what color and thickness a line should be as well as more complex topics like applying the style only to geometries matching certain filters and combining styles to create interesting effects. In principle it should be possible to create maps as good looking as one normally sees on sites like maps.google.com and openstreetmap.org.
Why use Amanzi::SLD?
Well, The very simplest document in Amanzi::SLD is three lines long, while the same one in normal XML is almost 30 lines long. Consider:
require 'amanzi/sld' sld = Amanzi::SLD::Document.new('Test') sld.add_line_symbolizer :stroke => '#3050e0'
This will become the SLD document:
<?xml version="1.0" encoding="ISO-8859-1"?> <StyledLayerDescriptor xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd" xmlns:ogc="http://www.opengis.net/ogc" version="1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opengis.net/sld"> <NamedLayer> <Name>Test</Name> <UserStyle> <Name>Test</Name> <FeatureTypeStyle> <Rule> <LineSymbolizer> <Stroke> <CssParameter name="stroke">#000000</CssParameter> <CssParameter name="stroke-width">1</CssParameter> </Stroke> </LineSymbolizer> </Rule> </FeatureTypeStyle> <FeatureTypeStyle> <Rule> <LineSymbolizer> <Stroke> <CssParameter name="stroke">#000000</CssParameter> <CssParameter name="stroke-width">1</CssParameter> </Stroke> </LineSymbolizer> </Rule> </FeatureTypeStyle> </UserStyle> </NamedLayer> </StyledLayerDescriptor>
How to get started?
The easiest way to get started would be to install the ruby gem using:
sudo gem install amanzi-sld
There is a chance that the gem is not yet available, in which case your next option is to download from source using:
git clone git://github.com/craigtaverner/amanzi-sld.git cd amanzi-sld rake install
Then you can create your ruby script with an appropriate content, for example:
require 'rubygems' require 'amanzi/sld' sld = Amanzi::SLD::Document.new 'My SLD Document' sld.add_line_symbolizer :stroke => '#004400', :stroke_width => 2, :geometry => 'LineString' sld.add_polygon_symbolizer :stroke => '#004400', :stroke_width => 2, :geometry => 'Polygon'
I will write this up when I get a chance. In the meantime I recommend looking at the more complex examples in the examples/ directory. Better yet, run them and see what the output looks like.
This actually uses several redundant combinations of syntax to test the SLD as well as the underlying XML DSL on which it is based
This is also run by the normal rake tests, called from test/test_file.rb
The file test.sld is the expected output which the test will compare with
This advanced example works with the OpenStreetMap model imported into Neo4j Spatial, and then exposed as one or more layers to GeoServer or uDig
I know the colors are ugly, but it is just a demonstration of some of the options available, including labeling
Have a look at Neo4j Spatial for more on the data being styled. That project actually uses Amanzi:SLD for some of the default styles being built into the OSM DataStore.
This example works with the highway Shapefile generated from OSM data by CloudMade. See the CloudMade downloads
You can also read the test code in the test/ directory for information on some of the syntax options being tested.
Note on Patches/Pull Requests
Fork the project.
Make your feature addition or bug fix.
Add tests for it. This is important so I don't break it in a future version unintentionally.
Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
Send me a pull request. Bonus points for topic branches.
Copyright © 2010 Craig Taverner. See LICENSE for details.