Code Monkey home page Code Monkey logo

pysld's Introduction

Downloads License: MIT

Pysld

The library is useful for generating the SLD file for raster as well as vector datasets. The package can generate the 4 types of styles,

  1. Simple style
  2. Categorized style
  3. Classified style
  4. Raster style

The package is available in official PyPI: https://pypi.org/project/pysld/

The complete documentation of this library is found here: https://pysld.readthedocs.io

The library is the small version of QGIS symbology. This library is very useful for the visual appearance on the map. Below is the example of categorized style in qgis.

categorized style qgis

Installation

pip install pysld

Some example

# Import library
from pysld.style import StyleSld

# Simple style for polygon feature
simple_sld = StyleSld(style_name='polygonStyle', geom_type='polygon', fill_color='#ffffff', stroke_color='#333333')
simple_sld_style = simple_sld.generate_simple_style()
print(simple_sld_style)

# Categorized style for polygon feature
categorized_sld = StyleSld(
            style_name='polygonStyle',
            geom_type='polygon',
            attribute_name='USE',
            values=['Agriculture', 'Residential', 'Restaurant', 'Storehouse'],
            color_palette='Spectral_r')
categorized_sld_style = categorized_sld.generate_categorized_style()
print(categorized_sld_style)

# Classified style for polygon feature
classified_sld = StyleSld(
            style_name='polygonStyle',
            geom_type='polygon',
            attribute_name='USE',
            values=[1,2,3,34,23,122,12,2,3,21,23,32,1,23,42,1,23,1,1,23,4,3,54,6,768,8,554,3,43,543,6,657,7,75,4,4],
            number_of_class=5,
            classification_method='natural_break',
            color_palette='Spectral_r')
classified_sld_style = classified_sld.generate_classified_style()
print(classified_sld_style)

# Raster style
raster_style = StyleSld(style_name='polygonStyle',color_palette='Spectral_r',continuous_legend=True)
raster_sld_style = raster_sld.generate_raster_style(max_value=100, min_value=0)

# Categorized style for PostGIS data
categorized_sld = StyleSld(
            style_name='polygonStyle',
            geom_type='polygon',
            attribute_name='USE',
            color_palette='Spectral_r',
            # Postgres connection parameters
            dbname='postgres',
            user='postgres',
            password='admin',
            host='localhost',
            port='5432',
            schema='public',
            pg_table_name='postgres_table_name')
print(categorized.values) # It will print the unique values from postgres_table_name table
categorized_style_sld = categorized.generate_categorized_style()

Generated SLD file example

Simple SLD
<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>style</Name>
        <UserStyle>
        <Title>style</Title>
        <FeatureTypeStyle>
    <Rule>
        <PolygonSymbolizer>
            <Fill>
                <CssParameter name="fill">#ffffff</CssParameter>
                <CssParameter name="fill-opacity">1</CssParameter>
            </Fill>
            <Stroke>
                <CssParameter name="stroke">#333333</CssParameter>
                <CssParameter name="stroke-width">1</CssParameter>
            </Stroke>
        </PolygonSymbolizer>
    </Rule>
        </FeatureTypeStyle>
        </UserStyle>
    </NamedLayer>
</StyledLayerDescriptor>
Raster SLD
<StyledLayerDescriptor xmlns="http://www.opengis.net/sld" xmlns:gml="http://www.opengis.net/gml" version="1.0.0" xmlns:ogc="http://www.opengis.net/ogc" xmlns:sld="http://www.opengis.net/sld">
    <UserLayer>
        <sld:LayerFeatureConstraints>
        <sld:FeatureTypeConstraint/>
        </sld:LayerFeatureConstraints>
        <sld:UserStyle>
        <sld:Name>polygonStyle</sld:Name>
        <sld:FeatureTypeStyle>
            <sld:Rule>
            <sld:RasterSymbolizer>
                <Opacity>1</Opacity>
                <sld:ChannelSelection>
                <sld:GrayChannel>
                    <sld:SourceChannelName>1</sld:SourceChannelName>
                </sld:GrayChannel>
                </sld:ChannelSelection>
                <sld:ColorMap type="range">
                    <sld:ColorMapEntry color="#54aead" label=" 0.0" quantity="0.0"/>
                    <sld:ColorMapEntry color="#bfe5a0" label=" 25.0" quantity="25.0"/>
                    <sld:ColorMapEntry color="#fffebe" label=" 50.0" quantity="50.0"/>
                    <sld:ColorMapEntry color="#fdbf6f" label=" 75.0" quantity="75.0"/>
                    <sld:ColorMapEntry color="#e95c47" label=" 100.0" quantity="100.0"/>
                </sld:ColorMap>
            </sld:RasterSymbolizer>
            </sld:Rule>
        </sld:FeatureTypeStyle>
        </sld:UserStyle>
    </UserLayer>
</StyledLayerDescriptor>

pysld's People

Contributors

iamtekson avatar rabinatwayana avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pysld's Issues

SLD problem: java.lang.IllegalArgumentException: Ranges 2.0 / 1.0 overlap.

In raster SLD creation, we need to order the colormap entry values in ascending order. Otherwise it throw this error.

Following is the erroneous SLD file,

<sld:ColorMapEntry color="#54aead" quantity="2.0" label=" 2.0"/>
              <sld:ColorMapEntry color="#bfe5a0" quantity="1.75" label=" 1.75"/>
              <sld:ColorMapEntry color="#fffebe" quantity="1.5" label=" 1.5"/>
              <sld:ColorMapEntry color="#fdbf6f" quantity="1.25" label=" 1.25"/>
              <sld:ColorMapEntry color="#e95c47" quantity="1.0" label=" 1.0"/>

Correct SLD,

<sld:ColorMapEntry quantity="1" color="#d7191c" label="1"/>
              <sld:ColorMapEntry quantity="1.25" color="#fdae61" label="1.25"/>
              <sld:ColorMapEntry quantity="1.5" color="#ffffbf" label="1.5"/>
              <sld:ColorMapEntry quantity="1.75" color="#abdda4" label="1.75"/>
              <sld:ColorMapEntry quantity="2" color="#2b83ba" label="2"/>

Cannot install pysld because of dependency conflict

My system specs are Ubuntu 22.04.2 LTS, python 3.10.6. I am trying to install the pysld package in python using

pip install pysld

but I am getting the following error:

ERROR: Cannot install pysld==0.0.1, pysld==0.0.2, pysld==0.0.3, pysld==0.0.4, pysld==0.0.5 and pysld==0.0.6 because these package versions have conflicting dependencies.

The conflict is caused by:
    pysld 0.0.6 depends on scipy==1.6.2
    pysld 0.0.5 depends on scipy==1.6.2
    pysld 0.0.4 depends on scipy==1.6.2
    pysld 0.0.3 depends on scipy==1.6.2
    pysld 0.0.2 depends on scipy==1.6.2
    pysld 0.0.1 depends on scipy==1.6.2

To solve this issue I tried removing the scipy package from the directory of packages and install the scipy==1.6.2. Unfortunately, pip cannot find this version and gives the error:

ERROR: Ignored the following versions that require a different python version: 1.6.2 Requires-Python >=3.7,<3.10; 1.6.3 Requires-Python >=3.7,<3.10; 1.7.0 Requires-Python >=3.7,<3.10; 1.7.0rc1 Requires-Python >=3.7,<3.10; 1.7.0rc2 Requires-Python >=3.7,<3.10; 1.7.1 Requires-Python >=3.7,<3.10
ERROR: Could not find a version that satisfies the requirement scipy==1.6.2 (from versions: 0.8.0, 0.9.0, 0.10.0, 0.10.1, 0.11.0, 0.12.0, 0.12.1, 0.13.0, 0.13.1, 0.13.2, 0.13.3, 0.14.0, 0.14.1, 0.15.0, 0.15.1, 0.16.0, 0.16.1, 0.17.0, 0.17.1, 0.18.0, 0.18.1, 0.19.0, 0.19.1, 1.0.0, 1.0.1, 1.1.0, 1.2.0, 1.2.1, 1.2.2, 1.2.3, 1.3.0, 1.3.1, 1.3.2, 1.3.3, 1.4.0, 1.4.1, 1.5.0, 1.5.1, 1.5.2, 1.5.3, 1.5.4, 1.6.0, 1.6.1, 1.7.2, 1.7.3, 1.8.0rc1, 1.8.0rc2, 1.8.0rc3, 1.8.0rc4, 1.8.0, 1.8.1, 1.9.0rc1, 1.9.0rc2, 1.9.0rc3, 1.9.0, 1.9.1, 1.9.2, 1.9.3, 1.10.0rc1, 1.10.0rc2, 1.10.0, 1.10.1)
ERROR: No matching distribution found for scipy==1.6.2

After that I tried downloading directly the whl package from https://www.piwheels.org/project/scipy/ and installing it using pip. I got the following error with all the versions of the whl file for scipy 1.6.2:

pip install scipy-1.6.2-cp39-cp39-linux_armv7l.whl 
Defaulting to user installation because normal site-packages is not writeable
ERROR: scipy-1.6.2-cp39-cp39-linux_armv7l.whl is not a supported wheel on this platform.

Is there a way to fix this issue?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.