Revision 550

View differences:

org.gvsig.sld/tags/org.gvsig.sld-2.0.101/pom.xml
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2
  <modelVersion>4.0.0</modelVersion>
3
  <parent>
4
    <groupId>org.gvsig</groupId>
5
    <artifactId>org.gvsig.desktop</artifactId>
6
    <version>2.0.283</version>
7
  </parent>
8

  
9
  <artifactId>org.gvsig.sld</artifactId>
10
  <version>2.0.101</version>
11
  <packaging>pom</packaging>
12
  <name>org.gvsig.sld</name>
13

  
14
    <url>https://redmine.gvsig.net/redmine/projects/gvsig-sldtools</url>
15
  <scm>
16
      <connection>scm:svn:https://devel.gvsig.org/svn/gvsig-sldtools/org.gvsig.sld/tags/org.gvsig.sld-2.0.101</connection>
17
      <developerConnection>scm:svn:https://devel.gvsig.org/svn/gvsig-sldtools/org.gvsig.sld/tags/org.gvsig.sld-2.0.101</developerConnection>
18
      <url>scm:svn:https://devel.gvsig.org/svn/gvsig-sldtools/org.gvsig.sld/tags/org.gvsig.sld-2.0.101</url>
19
  </scm>
20
  <repositories>
21
    <repository>
22
      <id>gvsig-public-http-repository</id>
23
      <name>gvSIG maven public HTTP repository</name>
24
      <url>http://devel.gvsig.org/m2repo/j2se</url>
25
      <releases>
26
        <enabled>true</enabled>
27
        <updatePolicy>daily</updatePolicy>
28
        <checksumPolicy>warn</checksumPolicy>
29
      </releases>
30
      <snapshots>
31
        <enabled>true</enabled>
32
        <updatePolicy>daily</updatePolicy>
33
        <checksumPolicy>warn</checksumPolicy>
34
      </snapshots>
35
    </repository>
36
  </repositories>
37

  
38
  <build>
39
    <plugins>
40
      <plugin>
41
        <groupId>org.apache.maven.plugins</groupId>
42
        <artifactId>maven-release-plugin</artifactId>
43
        <configuration>
44
          <tagBase>https://devel.gvsig.org/svn/gvsig-sldtools/org.gvsig.sld/tags</tagBase>
45
        </configuration>
46
      </plugin>
47
    </plugins>
48
  </build>
49

  
50
  <modules>
51
    <module>org.gvsig.sld.app</module>
52
    <module>org.gvsig.sldconverter</module>
53
    <module>org.gvsig.sldsupport</module>
54
  </modules>
55
  <dependencyManagement>
56
    <dependencies>
57
      <dependency>
58
        <groupId>org.gvsig</groupId>
59
        <artifactId>org.gvsig.sldsupport.lib.api</artifactId>
60
        <version>2.0.101</version>
61
      </dependency>
62
      <dependency>
63
        <groupId>org.gvsig</groupId>
64
        <artifactId>org.gvsig.sldconverter.lib.api</artifactId>
65
        <version>2.0.101</version>
66
      </dependency>
67
<!--       <dependency>
68
        <groupId>org.gvsig</groupId>
69
        <artifactId>org.gvsig.xmlpull.lib.api</artifactId>
70
        <version>2.0.1</version>
71
      </dependency> -->
72
      <dependency>
73
        <groupId>org.gvsig</groupId>
74
        <artifactId>org.gvsig.sldsupport.lib.impl</artifactId>
75
        <version>2.0.101</version>
76
      </dependency>
77
      <dependency>
78
        <groupId>org.gvsig</groupId>
79
        <artifactId>org.gvsig.sldconverter.lib.impl</artifactId>
80
        <version>2.0.101</version>
81
      </dependency>
82
    </dependencies>
83
  </dependencyManagement>
84
</project>
org.gvsig.sld/tags/org.gvsig.sld-2.0.101/org.gvsig.sldconverter/org.gvsig.sldconverter.lib/org.gvsig.sldconverter.lib.impl/pom.xml
1
<?xml version="1.0" encoding="ISO-8859-1"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3
  <modelVersion>4.0.0</modelVersion>
4
  <artifactId>org.gvsig.sldconverter.lib.impl</artifactId>
5
  <packaging>jar</packaging>
6
  <name>${project.artifactId}</name>
7
  <parent>
8
    <groupId>org.gvsig</groupId>
9
    <artifactId>org.gvsig.sldconverter.lib</artifactId>
10
    <version>2.0.101</version>
11
  </parent>
12

  
13
  <dependencies>
14
  
15
	<dependency>
16
		<groupId>org.gvsig</groupId>
17
		<artifactId>org.gvsig.sldconverter.lib.api</artifactId>
18
      	<scope>compile</scope>
19
	</dependency>
20
	
21
	<dependency>
22
		<groupId>org.gvsig</groupId>
23
      	<artifactId>org.gvsig.sldsupport.lib.api</artifactId>
24
      	<scope>compile</scope>
25
	</dependency>
26
    <dependency>
27
      <groupId>org.gvsig</groupId>
28
      <artifactId>org.gvsig.symbology.lib.api</artifactId>
29
      <scope>compile</scope>
30
    </dependency>
31
    <dependency>
32
      <groupId>org.gvsig</groupId>
33
      <artifactId>org.gvsig.fmap.mapcontext.api</artifactId>
34
      <scope>compile</scope>
35
    </dependency>	
36
	
37
  </dependencies>
38

  
39

  
40
  <build>
41
  
42
		<resources>
43
			<resource>
44
				<!--
45
				<targetPath>org/gvsig/gui/beans/resources/translations</targetPath>
46
				<filtering>false</filtering>
47
				-->
48
				<directory>src/main/resources</directory>
49
				<!--
50
				<includes>
51
					<include>**/*.png</include>
52
					<include>**/*.png</include>
53
				</includes>
54
				-->
55
			</resource>
56
		</resources>
57
  
58
  
59
    <plugins>
60

  
61
        <!-- Skip compilation tests 
62
        <plugin>
63
          <groupId>org.apache.maven.plugins</groupId>
64
          <artifactId>maven-compiler-plugin</artifactId>
65
          <executions>
66
            <execution>
67
              <id>default-testCompile</id>
68
              <phase>process-test-sources</phase>
69
              <goals>
70
                <goal>testCompile</goal>
71
              </goals>
72
              <configuration>
73
                <skip>true</skip>
74
              </configuration>
75
            </execution>
76
          </executions>
77
        </plugin>
78
        -->
79

  
80
      <!-- TODO: MAKE TESTS WORK AND REMOVE THIS OPTION -->
81
      
82
      <!--
83
      <plugin>
84
        <groupId>org.apache.maven.plugins</groupId>
85
        <artifactId>maven-surefire-plugin</artifactId>
86
        <configuration>
87
          <skipTests>true</skipTests>
88
        </configuration>
89
      </plugin>
90
    -->
91

  
92
    </plugins>
93
    
94
  </build>
95
</project>
96

  
97

  
org.gvsig.sld/tags/org.gvsig.sld-2.0.101/org.gvsig.sldconverter/org.gvsig.sldconverter.lib/org.gvsig.sldconverter.lib.impl/src/main/java/org/gvsig/sldconverter/impl/DefaultSLDConverterManager.java
1
/*******************************************************************************
2
 *
3
 *   gvSIG. Desktop Geographic Information System.
4
 *  
5
 *   Copyright (C) 2007-2013 gvSIG Association.
6
 *  
7
 *   This program is free software; you can redistribute it and/or
8
 *   modify it under the terms of the GNU General Public License
9
 *   as published by the Free Software Foundation; either version 3
10
 *   of the License, or (at your option) any later version.
11
 *  
12
 *   This program is distributed in the hope that it will be useful,
13
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *   GNU General Public License for more details.
16
 *  
17
 *   You should have received a copy of the GNU General Public License
18
 *   along with this program; if not, write to the Free Software
19
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
 *   MA  02110-1301, USA.
21
 *  
22
 *   For any additional information, do not hesitate to contact us
23
 *   at info AT gvsig.com, or visit our website www.gvsig.com.
24
 *   
25
 *******************************************************************************/
26
package org.gvsig.sldconverter.impl;
27

  
28
import java.util.HashSet;
29
import java.util.Iterator;
30
import java.util.Set;
31

  
32
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
33
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
34
import org.gvsig.sldconverter.SLDConverterManager;
35
import org.gvsig.sldconverter.exception.UnsupportedLegendException;
36
import org.gvsig.sldconverter.exception.UnsupportedSymbolException;
37
import org.gvsig.sldconverter.legend.LegendToSLDConverter;
38
import org.gvsig.sldconverter.legend.LegendToSLDConverterFactory;
39
import org.gvsig.sldconverter.legend.SLDToLegendConverter;
40
import org.gvsig.sldconverter.legend.SLDToLegendConverterFactory;
41
import org.gvsig.sldconverter.symbol.SLDToSymbolConverter;
42
import org.gvsig.sldconverter.symbol.SLDToSymbolConverterFactory;
43
import org.gvsig.sldconverter.symbol.SymbolToSLDConverter;
44
import org.gvsig.sldconverter.symbol.SymbolToSLDConverterFactory;
45
import org.gvsig.sldsupport.exception.UnsupportedSLDObjectException;
46
import org.gvsig.sldsupport.sld.layer.SLDLayer;
47
import org.gvsig.sldsupport.sld.symbol.SLDSymbol;
48

  
49
public class DefaultSLDConverterManager implements  SLDConverterManager {
50

  
51
	private Set<SLDToLegendConverterFactory> legendFromSLD =
52
			new HashSet<SLDToLegendConverterFactory>();
53
	private Set<LegendToSLDConverterFactory> legendToSLD =
54
			new HashSet<LegendToSLDConverterFactory>();
55
	// =========================================================
56
	private Set<SLDToSymbolConverterFactory> symbolFromSLD =
57
			new HashSet<SLDToSymbolConverterFactory>();
58
	private Set<SymbolToSLDConverterFactory> symbolToSLD =
59
			new HashSet<SymbolToSLDConverterFactory>();
60
	// =========================================================
61
	
62
	
63
	public SLDSymbol toSLDSymbol(ISymbol sym) throws UnsupportedSymbolException {
64
		
65
		if (sym == null) {
66
			throw new UnsupportedSymbolException("ISymbol", "Null");
67
		}
68
		
69
		int max = 0;
70
		SymbolToSLDConverterFactory factToUse = null;
71
		Iterator<SymbolToSLDConverterFactory> iter = symbolToSLD.iterator();
72
		SymbolToSLDConverterFactory fact = null;
73
		int aux = 0;
74
		while (iter.hasNext()) {
75
			fact = iter.next();
76
			aux = fact.canConvert(sym);
77
			if (aux > max) {
78
				max = aux;
79
				factToUse = fact;
80
			}
81
		}
82
		
83
		if (max == 0) {
84
			throw new UnsupportedSymbolException(
85
					sym.getClass().getName(), "No converter found.");
86
		}
87
		
88
		SymbolToSLDConverter conv = factToUse.createSymbolToSLDConverter();
89
		return conv.convert(sym);
90
	}
91
	
92

  
93
	public ISymbol toSymbol(SLDSymbol sldsym) throws UnsupportedSLDObjectException {
94

  
95
		if (sldsym == null) {
96
			throw new UnsupportedSLDObjectException("SLDSymbol", "Null");
97
		}
98
		int max = 0;
99
		SLDToSymbolConverterFactory factToUse = null;
100
		Iterator<SLDToSymbolConverterFactory> iter = symbolFromSLD.iterator();
101
		SLDToSymbolConverterFactory fact = null;
102
		int aux = 0;
103
		while (iter.hasNext()) {
104
			fact = iter.next();
105
			aux = fact.canConvert(sldsym);
106
			if (aux > max) {
107
				max = aux;
108
				factToUse = fact;
109
			}
110
		}
111
		
112
		if (max == 0) {
113
			throw new UnsupportedSLDObjectException(
114
					sldsym.getClass().getSimpleName(), "No converter found.");
115
		}
116
		
117
		SLDToSymbolConverter conv = factToUse.createSLDToSymbolConverter();
118
		return conv.convert(sldsym);		
119
	}
120

  
121
	public SLDLayer toSLDLayer(ILegend legend)
122
			throws UnsupportedLegendException, UnsupportedSymbolException {
123
		
124
		if (legend == null) {
125
			throw new UnsupportedLegendException("ILegend", "Null");
126
		}
127
		
128
		int max = 0;
129
		LegendToSLDConverterFactory factToUse = null;
130
		Iterator<LegendToSLDConverterFactory> iter = legendToSLD.iterator();
131
		LegendToSLDConverterFactory fact = null;
132
		int aux = 0;
133
		while (iter.hasNext()) {
134
			fact = iter.next();
135
			aux = fact.canConvert(legend);
136
			if (aux > max) {
137
				max = aux;
138
				factToUse = fact;
139
			}
140
		}
141
		
142
		if (max == 0) {
143
			throw new UnsupportedLegendException(
144
					legend.getClass().getName(), "No converter found.");
145
		}
146
		
147
		LegendToSLDConverter conv = factToUse.createLegendToSLDConverter();
148
		return conv.convert(legend);		
149
	}
150

  
151
	public ILegend toLegend(SLDLayer sldlayer) throws UnsupportedSLDObjectException {
152

  
153
		if (sldlayer == null) {
154
			throw new UnsupportedSLDObjectException("SLDLayer", "Null");
155
		}
156
		int max = 0;
157
		SLDToLegendConverterFactory factToUse = null;
158
		Iterator<SLDToLegendConverterFactory> iter = legendFromSLD.iterator();
159
		SLDToLegendConverterFactory fact = null;
160
		int aux = 0;
161
		while (iter.hasNext()) {
162
			fact = iter.next();
163
			aux = fact.canConvert(sldlayer);
164
			if (aux > max) {
165
				max = aux;
166
				factToUse = fact;
167
			}
168
		}
169
		
170
		if (max == 0) {
171
			throw new UnsupportedSLDObjectException(
172
					sldlayer.getClass().getSimpleName(), "No converter found.");
173
		}
174
		
175
		SLDToLegendConverter conv = factToUse.createSLDToLegendConverter();
176
		return conv.convert(sldlayer);		
177
	}
178
	
179
	// =============================================================
180
	// =============================================================
181

  
182
	public void registerSLDToSymbolConverter(SLDToSymbolConverterFactory fact) {
183
		if (fact != null) {
184
			this.symbolFromSLD.add(fact);
185
		}
186
	}
187

  
188
	public void registerSymbolToSLDConverter(SymbolToSLDConverterFactory fact) {
189
		if (fact != null) {
190
			this.symbolToSLD.add(fact);
191
		}
192
	}
193

  
194
	public void registerSLDToLegendConverter(SLDToLegendConverterFactory fact) {
195
		if (fact != null) {
196
			this.legendFromSLD.add(fact);
197
		}
198
	}
199

  
200
	public void registerLegendToSLDConverter(LegendToSLDConverterFactory fact) {
201
		if (fact != null) {
202
			this.legendToSLD.add(fact);
203
		}
204
	}
205

  
206
}
org.gvsig.sld/tags/org.gvsig.sld-2.0.101/org.gvsig.sldconverter/org.gvsig.sldconverter.lib/org.gvsig.sldconverter.lib.impl/src/main/java/org/gvsig/sldconverter/impl/util/BasicUtils.java
1
/*******************************************************************************
2
 *
3
 *   gvSIG. Desktop Geographic Information System.
4
 *  
5
 *   Copyright (C) 2007-2013 gvSIG Association.
6
 *  
7
 *   This program is free software; you can redistribute it and/or
8
 *   modify it under the terms of the GNU General Public License
9
 *   as published by the Free Software Foundation; either version 3
10
 *   of the License, or (at your option) any later version.
11
 *  
12
 *   This program is distributed in the hope that it will be useful,
13
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *   GNU General Public License for more details.
16
 *  
17
 *   You should have received a copy of the GNU General Public License
18
 *   along with this program; if not, write to the Free Software
19
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
 *   MA  02110-1301, USA.
21
 *  
22
 *   For any additional information, do not hesitate to contact us
23
 *   at info AT gvsig.com, or visit our website www.gvsig.com.
24
 *   
25
 *******************************************************************************/
26
package org.gvsig.sldconverter.impl.util;
27

  
28
import java.awt.Color;
29
import java.net.URL;
30
import java.text.DecimalFormat;
31
import java.text.DecimalFormatSymbols;
32
import java.util.ArrayList;
33
import java.util.HashMap;
34
import java.util.Iterator;
35
import java.util.List;
36
import java.util.Map;
37

  
38
import org.slf4j.Logger;
39
import org.slf4j.LoggerFactory;
40

  
41
import org.gvsig.fmap.dal.DataTypes;
42
import org.gvsig.fmap.geom.Geometry;
43
import org.gvsig.fmap.mapcontext.MapContextLocator;
44
import org.gvsig.fmap.mapcontext.MapContextManager;
45
import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol;
46
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
47
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
48
import org.gvsig.sldconverter.SLDConverterLocator;
49
import org.gvsig.sldconverter.SLDConverterManager;
50
import org.gvsig.sldsupport.SLDSupportLocator;
51
import org.gvsig.sldsupport.SLDSupportManager;
52
import org.gvsig.sldsupport.exception.UnsupportedSLDObjectException;
53
import org.gvsig.sldsupport.sld.filter.FilterTags;
54
import org.gvsig.sldsupport.sld.filter.SLDFilter;
55
import org.gvsig.sldsupport.sld.filter.SLDFilterOperator;
56
import org.gvsig.sldsupport.sld.filter.expression.SLDExpression;
57
import org.gvsig.sldsupport.sld.filter.expression.operator.SLDLiteral;
58
import org.gvsig.sldsupport.sld.filter.expression.operator.SLDPropertyName;
59
import org.gvsig.sldsupport.sld.filter.operator.SLDComparisonOperator;
60
import org.gvsig.sldsupport.sld.filter.operator.comparison.SLDBinaryComparisonOperator;
61
import org.gvsig.sldsupport.sld.layer.SLDLayer;
62
import org.gvsig.sldsupport.sld.layer.SLDNamedLayer;
63
import org.gvsig.sldsupport.sld.layer.SLDUserLayer;
64
import org.gvsig.sldsupport.sld.rule.SLDRule;
65
import org.gvsig.sldsupport.sld.style.layer.SLDUserStyle;
66
import org.gvsig.sldsupport.sld.symbol.SLDLineSymbol;
67
import org.gvsig.sldsupport.sld.symbol.SLDPointSymbol;
68
import org.gvsig.sldsupport.sld.symbol.SLDPolygonSymbol;
69
import org.gvsig.sldsupport.sld.symbol.SLDSymbol;
70
import org.gvsig.sldsupport.sld.symbol.misc.SLDParameterValue;
71
import org.gvsig.symbology.SymbologyLocator;
72
import org.gvsig.symbology.SymbologyManager;
73
import org.gvsig.tools.ToolsLocator;
74
import org.gvsig.tools.dataTypes.CoercionException;
75
import org.gvsig.tools.dataTypes.DataType;
76
import org.gvsig.tools.dataTypes.DataTypesManager;
77

  
78
public class BasicUtils {
79
	
80
	private static Logger logger = LoggerFactory.getLogger(BasicUtils.class);
81
	
82
	public static DecimalFormat df = null;
83
	static {
84
		DecimalFormatSymbols dformater_rules = new DecimalFormatSymbols();
85
		dformater_rules.setDecimalSeparator ('.');
86
		df = new DecimalFormat("##########.0##########", dformater_rules);
87
	}
88
	
89
	public static SLDConverterManager sldMan() {
90
		return SLDConverterLocator.getInstance().getSLDConverterManager();
91
	}
92
	
93
	public static SLDSupportManager supMan() {
94
		return SLDSupportLocator.getInstance().getSLDSupportManager();
95
	}
96
	
97
	public static SymbologyManager symMan() {
98
		return SymbologyLocator.getSymbologyManager();
99
	}
100
	
101
	public static SymbolManager syMan() {
102
		return MapContextLocator.getSymbolManager();
103
	}
104
	
105
	public static MapContextManager mapMan() {
106
		return MapContextLocator.getMapContextManager();
107
	}
108
	
109
	public static DataTypesManager datMan() {
110
		return ToolsLocator.getDataTypesManager();
111
	}
112
	
113

  
114

  
115

  
116

  
117
	
118

  
119

  
120
	
121

  
122
	
123
	
124
	
125
	
126
	
127
	public static String getFirstPropertyName(List<SLDRule> rules) {
128
		if (rules == null || rules.size() == 0) {
129
			return null;
130
		}
131
		
132
		SLDRule rule = null;
133
		String fieldName = null;
134
		for (int i=0; i<rules.size(); i++) {
135
			rule = rules.get(i);
136
			fieldName = getFirstPropertyName(rule);
137
			if (fieldName != null) {
138
				return fieldName;
139
			}
140
		}
141
		return null;
142
	}
143
	
144
	/**
145
	 * Gets data type from literals.
146
	 * Possible responses:
147
	 * 
148
	 * - DataTypes.DOUBLE
149
	 * - DataTypes.INTEGER
150
	 * - DataTypes.STRING
151
	 * 
152
	 * @param lits
153
	 * @return
154
	 */
155
	public static DataType guessDataType(List<SLDLiteral> lits) {
156
		
157
		boolean isDouble = true;
158
		boolean isInt = true;
159
		Iterator<SLDLiteral> iter = lits.iterator();
160
		SLDLiteral lit = null;
161
		
162
		while (iter.hasNext()) {
163
			lit = iter.next();
164
			try { Double.parseDouble(lit.getValue()); } catch (Exception exc) {
165
				isDouble = false;
166
				break;
167
			}
168
		}
169
		if (isDouble) {
170
			return datMan().get(DataTypes.DOUBLE);
171
		}
172
		// =======================================
173
		iter = lits.iterator();
174
		isInt = true;
175
		while (iter.hasNext()) {
176
			lit = iter.next();
177
			try { Integer.parseInt(lit.getValue()); } catch (Exception exc) {
178
				isInt = false;
179
				break;
180
			}
181
		}
182
		if (isInt) {
183
			return datMan().get(DataTypes.INT);
184
		} else {
185
			return datMan().get(DataTypes.STRING);
186
		}
187
	}
188
	
189
	/**
190
	 * Coerces literal value using the dataType
191
	 * Returns null if coercion was not possible
192
	 * @param lit
193
	 * @param dt
194
	 * @return
195
	 */
196
	public static Object getValue(SLDLiteral lit, DataType dt) {
197
		
198
		if (lit == null) {
199
			return null;
200
		}
201
		try {
202
			return dt.coerce(lit.getValue());
203
		} catch (CoercionException e) {
204
			logger.info("While coercing literal: " + lit.getValue(), e);
205
			return null;
206
		}
207
	}
208
	
209
	public static List<SLDLiteral> getComparisonLiterals(List<SLDRule> rules) {
210
		List<SLDLiteral> resp = new ArrayList<SLDLiteral>();
211
		SLDRule rule = null;
212
		SLDComparisonOperator item = null;
213
		for (int i=0; i<rules.size(); i++) {
214
			rule = rules.get(i);
215
			SLDFilter filt = rule.getFilter();
216
			if (filt != null && !filt.isElse()) {
217
				SLDFilterOperator oper = filt.getFilterOperator();
218
				if (oper instanceof SLDComparisonOperator) {
219
					item = (SLDComparisonOperator) oper;
220
					List<SLDExpression> list = item.getExpressions();
221
					for (int j=0; j<list.size(); j++) {
222
						if (list.get(j) instanceof SLDLiteral) {
223
							resp.add((SLDLiteral) list.get(j));
224
						}
225
					}
226
				}
227
			}
228
		}
229
		return resp;
230
	}
231
	
232

  
233

  
234

  
235
	public static boolean isElse(SLDRule rule) {
236
		SLDFilter filt = rule.getFilter();
237
		return filt != null && filt.isElse();
238
	}
239

  
240
	
241
	private static String getFirstPropertyName(SLDRule rule) {
242
		SLDFilter filt = rule.getFilter();
243
		if (filt == null) {
244
			return null;
245
		}
246
		SLDFilterOperator oper = filt.getFilterOperator();
247
		if (oper instanceof SLDComparisonOperator) {
248
			
249
			SLDComparisonOperator cop = (SLDComparisonOperator) oper;
250
			List<SLDExpression> list = cop.getExpressions();
251
			for (int i=0; i<list.size(); i++) {
252
				if (list.get(i) instanceof SLDPropertyName) {
253
					return ((SLDPropertyName) list.get(i)).getPropertyName();
254
				}
255
			}
256
			return null;
257
		} else {
258
			return null;
259
		}
260
		
261
	}
262

  
263

  
264

  
265
	
266

  
267
	
268
	
269
	public static boolean isNumeric(SLDLiteral lit) {
270
		try {
271
			Double.parseDouble(lit.getValue());
272
			return true;
273
		} catch (Exception exc) {
274
			return false;
275
		}
276
	}
277
	
278
	
279

  
280

  
281

  
282
	public static SLDUserStyle getFirstUserStyle(SLDLayer layer) {
283
		
284
		if (layer instanceof SLDNamedLayer) {
285
			SLDNamedLayer nl = (SLDNamedLayer) layer;
286
			for (int i=0; i<nl.getStyles().size(); i++) {
287
				if (nl.getStyles().get(i) instanceof SLDUserStyle) {
288
					// return first found
289
					return (SLDUserStyle) nl.getStyles().get(i); 
290
				}
291
			}
292
			return null;
293
		} else {
294
			if (layer instanceof SLDUserLayer) {
295
				SLDUserLayer ul = (SLDUserLayer) layer;
296
				if (ul.getStyles().size() == 1) {
297
					return ul.getStyles().get(0);
298
				} else {
299
					return null;
300
				}
301
			} else {
302
				return null;
303
			}
304
		}
305
	}
306
	
307
	/**
308
	 *
309
	 * Geometry.TYPES.POINT
310
	 * Geometry.TYPES.CURVE
311
	 * Geometry.TYPES.SURFACE
312
	 * or
313
	 * Geometry.TYPES.NULL if there is not consistency in the list
314
	 * 
315
	 * @return
316
	 */
317
	public static int getGeometryTypeForSymbols(List<SLDSymbol> list) {
318
		
319
		if (list == null || list.size() == 0) {
320
			return Geometry.TYPES.NULL;
321
		}
322
		
323
		SLDSymbol ss = list.get(0);
324
		if (ss instanceof SLDPointSymbol) {
325
			if (allItemsOfClass(list, SLDPointSymbol.class)) {
326
				return Geometry.TYPES.POINT;
327
			}
328
		} else {
329
			if (ss instanceof SLDLineSymbol) {
330
				if (allItemsOfClass(list, SLDLineSymbol.class)) {
331
					return Geometry.TYPES.CURVE;
332
				}
333
			} else {
334
				if (ss instanceof SLDPolygonSymbol) {
335
					if (allItemsOfClass(list, SLDPolygonSymbol.class)) {
336
						return Geometry.TYPES.SURFACE;
337
					}
338
				} else {
339
					// Unknown
340
					return Geometry.TYPES.NULL;
341
				}
342
			}
343
		}
344
		// Inconsistent list
345
		return Geometry.TYPES.NULL;
346
	}
347
	
348
	
349

  
350
	private static boolean allItemsOfClass(List<SLDSymbol> list, Class clazz) {
351
		for (int i=0; i<list.size(); i++) {
352
			if (list.get(i).getClass() != clazz) {
353
				return false;
354
			}
355
		}
356
		return true;
357
	}
358

  
359
	public static ISymbol combineSymbols(List<ISymbol> gsyms, int geotype) {
360
		
361
		if (gsyms.size() == 1) {
362
			return gsyms.get(0);
363
		}
364
		
365
		IMultiLayerSymbol resp = syMan().createMultiLayerSymbol(geotype);
366
		for (int i=0; i<gsyms.size(); i++) {
367
			resp.addLayer(gsyms.get(i));
368
		}
369
		return resp;
370
	}
371

  
372
	// ==========================================================
373
	// ==========================================================
374
	// ==========================================================
375
	/*
376
	 * Symbol utilities...
377
	 */
378
	
379

  
380
	
381

  
382
	// ==========================================================
383
	// ==========================================================
384
	
385

  
386

  
387
	public static Double toDouble(SLDExpression width)
388
			throws UnsupportedSLDObjectException {
389
		
390
		if (width instanceof SLDLiteral) {
391
			
392
			SLDLiteral lit = (SLDLiteral) width;
393
			try {
394
				double resp = Double.parseDouble(lit.getValue());
395
				return new Double(resp);
396
			} catch (Exception exc) {
397
				logger.info("Non parseable double: " + exc.getMessage());
398
				return null;
399
			}
400
			
401
		} else {
402
			throw new UnsupportedSLDObjectException(
403
					"SLDExpression",
404
					"Not supported as Double: " + width.getClass().getName());
405
		}
406
	}
407
	
408
	private static Integer toInteger(SLDExpression width) {
409
		
410
		if (width instanceof SLDLiteral) {
411
			
412
			SLDLiteral lit = (SLDLiteral) width;
413
			try {
414
				int resp = Integer.parseInt(lit.getValue());
415
				return new Integer(resp);
416
			} catch (Exception exc) {
417
				logger.info("Non parseable integer: " + exc.getMessage());
418
				return null;
419
			}
420
			
421
		} else {
422
			// TODO parse integer expression
423
			return null;
424
		}
425
	}
426
	
427
	/**
428
	 * 
429
	 * Input must be like "#fa5e02", "0x000", etc
430
	 * 
431
	 * @param hex
432
	 * @return Null if not parseable
433
	 */
434
	public static Color parseColor(String colorStr) {
435
		Color co = null;
436
		try {
437
			co = Color.decode(colorStr.trim());
438
		} catch (Exception exc) {
439
			logger.info("Non parseable color: " + exc.getMessage());
440
		}
441
		return co;
442
	}
443
	
444
	/**
445
	 * Examples:
446
	 * #ff0000
447
	 * 0x000000cd9983
448
	 * 
449
	 * Returns null is color is null
450
	 * 
451
	 * @param co
452
	 * @param length
453
	 * @param prefix
454
	 * @return
455
	 */
456
	public static String toHexadecimal(Color co, int length, String prefix) {
457
		
458
		if (co == null) {
459
			return null;
460
		}
461
		
462
		int v = (co.getRed() << 16) + (co.getGreen() << 8) + co.getBlue();
463
		String resp = Integer.toHexString(v);
464
		int len = resp.length();
465
		for (int i=0; i<(length-len); i++) {
466
			resp = "0" + resp;
467
		}
468
		resp = resp.substring(resp.length() - length);
469
		if (prefix != null) {
470
			resp = prefix + resp;
471
		}
472
		return resp;
473
	}
474
	
475
	
476
	/**
477
	 * Returns null if no data found
478
	 * @param valu
479
	 * @return
480
	 * @throws UnsupportedSLDObjectException
481
	 */
482
	public static Color getColor(SLDParameterValue valu) {
483
		List<SLDExpression> list = valu.getExpressionList();
484
		if (list == null || list.size() == 0) {
485
			return null;
486
		}
487
		SLDExpression expr = list.get(0);
488
		if (expr instanceof SLDLiteral) {
489
			SLDLiteral lit = (SLDLiteral) expr;
490
			return parseColor(lit.getValue());
491
		} else {
492
			// TODO Use expression to get color (?)
493
			return null;
494
		}
495
	}
496
	
497
	public static Color getColor(SLDExpression valu) {
498

  
499
		if (valu instanceof SLDLiteral) {
500
			SLDLiteral lit = (SLDLiteral) valu;
501
			return parseColor(lit.getValue());
502
		} else {
503
			// TODO Use expression to get color (?)
504
			return null;
505
		}
506
	}
507
	
508
	/**
509
	 * Returns null if no data found
510
	 * @param valu
511
	 * @return
512
	 * @throws UnsupportedSLDObjectException 
513
	 */
514
	public static Double getDouble(SLDParameterValue valu)
515
			throws UnsupportedSLDObjectException {
516
		
517
		List<SLDExpression> list = valu.getExpressionList();
518
		if (list == null || list.size() == 0) {
519
			return null;
520
		}
521
		return toDouble(list.get(0));
522
	}
523
	
524
	/**
525
	 * Returns null if no data found
526
	 * @param valu
527
	 * @return
528
	 * @throws UnsupportedSLDObjectException 
529
	 */
530
	public static Integer getAlphaValue(SLDParameterValue valu)
531
			throws UnsupportedSLDObjectException {
532
		
533
		List<SLDExpression> list = valu.getExpressionList();
534
		if (list == null || list.size() == 0) {
535
			return null;
536
		}
537
		Double d = toDouble(list.get(0));
538
		if (d == null) {
539
			return null;
540
		} else {
541
			if (d.doubleValue() <= 0) {
542
				return new Integer(0);
543
			} else {
544
				if (d.doubleValue() >= 1.0) {
545
					return new Integer(255);
546
				} else {
547
					return new Integer((int) Math.round(255*d.doubleValue()));
548
				}
549
			}
550
		}
551
	}
552

  
553
	public static Color toColor(SLDExpression color) 
554
			throws UnsupportedSLDObjectException {
555

  
556
		if (color instanceof SLDLiteral) {
557
			
558
			SLDLiteral lit = (SLDLiteral) color;
559
			Color resp = null;
560
			resp = parseColor(lit.getValue());
561
			return resp;
562
			
563
		} else {
564
			throw new UnsupportedSLDObjectException(
565
					"SLDExpression",
566
					"Not supported as Color: " + color.getClass().getName());
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
	public static String getFormat(URL u) {
596
		
597
		if (u == null) {
598
			return "";
599
		} else {
600
			String str = u.toString();
601
			int lastp = str.lastIndexOf(".");
602
			if (lastp == -1) {
603
				return ""; 
604
			} else {
605
				str = str.substring(lastp + 1);
606
				return "image/" + str;
607
			}
608
		}
609
	}
610

  
611

  
612

  
613
	
614

  
615
	public static ISymbol getElseSymbol(List<SLDRule> rules)
616
			throws UnsupportedSLDObjectException {
617
		
618
		if (rules == null || rules.size() == 0) {
619
			return null;
620
		}
621
		
622
		SLDFilter filt = null;
623
		List<SLDSymbol> symList = null;
624
		for (int i=0; i<rules.size(); i++) {
625
			filt = rules.get(i).getFilter();
626
			if (filt != null && filt.isElse()) {
627
				symList = rules.get(i).getSymbols();
628
				if (symList != null && symList.size() > 0) {
629
					/*
630
					 * Using first symbol only
631
					 */
632
					return sldMan().toSymbol(symList.get(0));
633
				}
634
			}
635
		}
636
		// not found
637
		return null;
638
	}
639

  
640
	
641
	private static boolean isEqualsTo(SLDFilterOperator oper) {
642
		if (oper instanceof SLDBinaryComparisonOperator) {
643
			SLDBinaryComparisonOperator binoper = (SLDBinaryComparisonOperator) oper;
644
			return binoper.getComparisonOperator().compareToIgnoreCase(
645
					FilterTags.PROPERTYISEQUALTO) == 0;
646
		}
647
		return false;
648
	}
649
	
650
	/**
651
	 * Translates rules to a value-symbol correspondence.
652
	 * In the filters, only the simple "is equal to" operators
653
	 * will be used only when the expression is a SLDLiteral
654
	 * 
655
	 * @param rules
656
	 * @param dt
657
	 * @return
658
	 * @throws UnsupportedSLDObjectException 
659
	 * @throws CoercionException 
660
	 */
661
	public static Map<Object, ISymbol> getValueToSymbol(
662
			List<SLDRule> rules, DataType dt, String pname)
663
					throws UnsupportedSLDObjectException {
664
		
665
		Map<Object, ISymbol> resp = new HashMap<Object, ISymbol>();
666
		if (rules == null || rules.size() == 0) {
667
			return resp;
668
		}
669
		SLDFilter filt = null;
670
		List<SLDSymbol> symList = null;
671
		SLDFilterOperator oper = null;
672
		SLDBinaryComparisonOperator binoper = null;
673
		ISymbol sym = null;
674
		SLDPropertyName sldpname = null;
675
		SLDLiteral sldlit = null;
676
		Object val = null;
677
		for (int i=0; i<rules.size(); i++) {
678
			symList = rules.get(i).getSymbols();
679
			if (symList == null || symList.size() == 0) {
680
				/*
681
				 * No symbols found
682
				 */
683
				continue;
684
			}
685
			filt = rules.get(i).getFilter();
686
			if (filt != null && !filt.isElse()) {
687
				oper = filt.getFilterOperator();
688
				if (oper instanceof SLDBinaryComparisonOperator && isEqualsTo(oper)) {
689
					/*
690
					 * Must be binary comparison operator "equals to"
691
					 */
692
					binoper = (SLDBinaryComparisonOperator) oper;
693
					/*
694
					 * Must be using property name and literal
695
					 */
696
					if (binoper.getFirstExpression() instanceof SLDLiteral
697
							&& binoper.getSecondExpression() instanceof SLDPropertyName) {
698
						
699
						sldpname = (SLDPropertyName) binoper.getSecondExpression(); 
700
						if (sldpname.getPropertyName().compareTo(pname) == 0) {
701
							sldlit = (SLDLiteral) binoper.getFirstExpression();
702
							val = BasicUtils.getValue(sldlit, dt);
703
							sym = sldMan().toSymbol(symList.get(0));
704
							if (val != null && sym != null) {
705
								sym.setDescription(sldlit.getValue());
706
								resp.put(val, sym);
707
							}
708
						}
709
					}
710
					// =======================================
711
					/*
712
					 * Must be  using property  name and literal
713
					 * (reversed)
714
					 */
715
					if (binoper.getFirstExpression() instanceof SLDPropertyName
716
							&& binoper.getSecondExpression() instanceof SLDLiteral) {
717
						
718
						sldpname = (SLDPropertyName) binoper.getFirstExpression(); 
719
						if (sldpname.getPropertyName().compareTo(pname) == 0) {
720
							sldlit = (SLDLiteral) binoper.getSecondExpression();
721
							val = BasicUtils.getValue(sldlit, dt);
722
							sym = sldMan().toSymbol(symList.get(0));
723
							if (val != null && sym != null) {
724
								resp.put(val, sym);
725
							}
726
						}
727
					}
728
					// =======================================
729
				}
730
			}
731
		}
732
		return resp;
733
	}
734
	
735
	
736

  
737

  
738
	public static SLDLiteral getLiteral(List<SLDExpression> exps) {
739
		
740
		for (int i=0; i<exps.size(); i++) {
741
			if (exps.get(i) instanceof SLDLiteral) {
742
				return (SLDLiteral) exps.get(i); 
743
			}
744
		}
745
		return null;
746
	}
747

  
748

  
749

  
750
	// ======================================
751
	
752

  
753
	
754
	
755
	
756

  
757
	public static String valueToString(Object val) {
758
		
759
		if (val instanceof Double) {
760
			return BasicUtils.df.format(((Double) val).doubleValue());
761
		} else {
762
			if (val instanceof Float) {
763
				return BasicUtils.df.format(((Float) val).doubleValue());
764
			} else {
765
				return val == null ? "" : val.toString();
766
			}
767
		}
768
	}
769

  
770
	
771
	
772

  
773

  
774

  
775

  
776

  
777
}
org.gvsig.sld/tags/org.gvsig.sld-2.0.101/org.gvsig.sldconverter/org.gvsig.sldconverter.lib/org.gvsig.sldconverter.lib.impl/src/main/java/org/gvsig/sldconverter/impl/legend/BasicLegendConverterFactory.java
1
/*******************************************************************************
2
 *
3
 *   gvSIG. Desktop Geographic Information System.
4
 *  
5
 *   Copyright (C) 2007-2013 gvSIG Association.
6
 *  
7
 *   This program is free software; you can redistribute it and/or
8
 *   modify it under the terms of the GNU General Public License
9
 *   as published by the Free Software Foundation; either version 3
10
 *   of the License, or (at your option) any later version.
11
 *  
12
 *   This program is distributed in the hope that it will be useful,
13
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *   GNU General Public License for more details.
16
 *  
17
 *   You should have received a copy of the GNU General Public License
18
 *   along with this program; if not, write to the Free Software
19
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
 *   MA  02110-1301, USA.
21
 *  
22
 *   For any additional information, do not hesitate to contact us
23
 *   at info AT gvsig.com, or visit our website www.gvsig.com.
24
 *   
25
 *******************************************************************************/
26
package org.gvsig.sldconverter.impl.legend;
27

  
28
import java.util.List;
29

  
30
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
31
import org.gvsig.fmap.mapcontext.rendering.legend.ISingleSymbolLegend;
32
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialIntervalLegend;
33
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialUniqueValueLegend;
34
import org.gvsig.sldconverter.SLDConverterManager;
35
import org.gvsig.sldconverter.legend.LegendToSLDConverter;
36
import org.gvsig.sldconverter.legend.LegendToSLDConverterFactory;
37
import org.gvsig.sldconverter.legend.SLDToLegendConverter;
38
import org.gvsig.sldconverter.legend.SLDToLegendConverterFactory;
39
import org.gvsig.sldsupport.sld.layer.SLDLayer;
40
import org.gvsig.sldsupport.sld.layer.SLDNamedLayer;
41
import org.gvsig.sldsupport.sld.layer.SLDUserLayer;
42
import org.gvsig.sldsupport.sld.rule.SLDRule;
43
import org.gvsig.sldsupport.sld.style.SLDFeatureStyle;
44
import org.gvsig.sldsupport.sld.style.layer.SLDLayerStyle;
45
import org.gvsig.sldsupport.sld.style.layer.SLDUserStyle;
46

  
47
public class BasicLegendConverterFactory
48
implements LegendToSLDConverterFactory, SLDToLegendConverterFactory {
49
	
50
	public static int LEGEND_PARTIALLY_CONVERTED = 50;
51

  
52
	public int canConvert(ILegend legend) {
53
		if (legend instanceof ISingleSymbolLegend
54
				|| legend instanceof IVectorialUniqueValueLegend
55
				|| legend instanceof IVectorialIntervalLegend) {
56
			
57
			/*
58
			 * This allows precedence of converters for specific classes
59
			 */
60
			return SLDConverterManager.CAN_CONVERT_PARTIALLY;
61
		} else {
62
			return SLDConverterManager.CAN_NOT_CONVERT;
63
		}
64
	}
65

  
66
	public LegendToSLDConverter createLegendToSLDConverter() {
67
		return new BasicLegendConverter();
68
	}
69
	
70
	// =======================================
71
	
72
	public SLDToLegendConverter createSLDToLegendConverter() {
73
		return new BasicLegendConverter();
74
	}
75
	
76
	
77
	public int canConvert(SLDLayer layer) {
78
		
79
		if (layer == null ) {
80
			return SLDConverterManager.CAN_NOT_CONVERT;
81
		}
82
		
83
		if (layer instanceof SLDNamedLayer) {
84
			return canConvertNamedLayer((SLDNamedLayer) layer);
85
		} else {
86
			if (layer instanceof SLDUserLayer) {
87
				return canConvertUserLayer((SLDUserLayer) layer);
88
			} else {
89
				// Unknown layer
90
				return SLDConverterManager.CAN_NOT_CONVERT;
91
			}
92
		}
93
	}
94
	
95
	
96
	private int canConvertNamedLayer(SLDNamedLayer layer) {
97
		
98
		List<SLDLayerStyle> stys = layer.getStyles();
99
		if (stys.size() == 0) {
100
			return SLDConverterManager.CAN_NOT_CONVERT;
101
		} else {
102
			if (stys.size() == 1 && stys.get(0) instanceof SLDUserStyle) {
103
				return canConvertUserStyle(
104
						(SLDUserStyle) stys.get(0),
105
						SLDConverterManager.CAN_CONVERT_PARTIALLY);
106
			} else {
107
				for (int i=0; i<stys.size(); i++) {
108
					if (stys.get(i) instanceof SLDUserStyle) {
109
						/*
110
						 * Get first USerStyle and devaluate result
111
						 */
112
						return canConvertUserStyle(
113
								(SLDUserStyle) stys.get(0),
114
								/*result devaluated*/
115
								SLDConverterManager.CAN_CONVERT_PARTIALLY);
116
					}
117
				}
118
				// No user style found:
119
				return SLDConverterManager.CAN_NOT_CONVERT;
120
			}
121
		}
122
	}
123
	
124
	
125
	private int canConvertUserStyle(SLDUserStyle style, int max) {
126
		
127
		List<SLDFeatureStyle> fstys = style.getFeatureStyles();
128
		if (fstys.size() == 0) {
129
			return SLDConverterManager.CAN_NOT_CONVERT;
130
		} else {
131
			if (fstys.size() == 1) {
132
				return canConvertFeatureStyle(fstys.get(0), max);
133
			} else {
134
				return canConvertFeatureStyle(fstys.get(0), max / 2);
135
			}
136
		}
137
	}
138
	
139
	
140
	private int canConvertUserLayer(SLDUserLayer layer) {
141
		List<SLDUserStyle> stys = layer.getStyles();
142
		if (stys.size() == 0) {
143
			return SLDConverterManager.CAN_NOT_CONVERT;
144
		} else {
145
			if (stys.size() == 1) {
146
				return canConvertUserStyle(
147
						stys.get(0),
148
						SLDConverterManager.CAN_CONVERT_PARTIALLY);
149
			} else {
150
				return canConvertUserStyle(
151
						stys.get(0),
152
						/*result devaluated*/
153
						SLDConverterManager.CAN_CONVERT_PARTIALLY);
154
			}
155
		}
156
	}	
157
	
158
	
159
	private int canConvertFeatureStyle(SLDFeatureStyle sldFeatureStyle, int max) {
160
		
161
		List<SLDRule> rules = sldFeatureStyle.getRules();
162
		if (rules == null || rules.size() == 0) {
163
			return SLDConverterManager.CAN_NOT_CONVERT;
164
		}
165
		
166
		if (SingleSymbolLegendUtils.areRulesOfSingleSymbol(rules)) {
167
			return max;
168
		} else {
169
			if (UniqueSymbolLegendUtils.areRulesOfUniqueSymbol(rules)) {
170
				return max;
171
			} else {
172
				if (IntervalsLegendUtils.areRulesOfIntervals(rules)) {
173
					return max;
174
				} else {
175
					return SLDConverterManager.CAN_NOT_CONVERT;
176
				}
177
			}
178
		}
179
	}	
180

  
181

  
182

  
183

  
184
}
org.gvsig.sld/tags/org.gvsig.sld-2.0.101/org.gvsig.sldconverter/org.gvsig.sldconverter.lib/org.gvsig.sldconverter.lib.impl/src/main/java/org/gvsig/sldconverter/impl/legend/IntervalsLegendUtils.java
1
/*******************************************************************************
2
 *
3
 *   gvSIG. Desktop Geographic Information System.
4
 *  
5
 *   Copyright (C) 2007-2013 gvSIG Association.
6
 *  
7
 *   This program is free software; you can redistribute it and/or
8
 *   modify it under the terms of the GNU General Public License
9
 *   as published by the Free Software Foundation; either version 3
10
 *   of the License, or (at your option) any later version.
11
 *  
12
 *   This program is distributed in the hope that it will be useful,
13
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *   GNU General Public License for more details.
16
 *  
17
 *   You should have received a copy of the GNU General Public License
18
 *   along with this program; if not, write to the Free Software
19
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
 *   MA  02110-1301, USA.
21
 *  
22
 *   For any additional information, do not hesitate to contact us
23
 *   at info AT gvsig.com, or visit our website www.gvsig.com.
24
 *   
25
 *******************************************************************************/
26
package org.gvsig.sldconverter.impl.legend;
27

  
28
import java.util.HashMap;
29
import java.util.Iterator;
30
import java.util.List;
31
import java.util.Map;
32

  
33
import org.gvsig.fmap.mapcontext.rendering.legend.IInterval;
34
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorialIntervalLegend;
35
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
36
import org.gvsig.sldconverter.exception.UnsupportedSymbolException;
37
import org.gvsig.sldconverter.impl.util.BasicUtils;
38
import org.gvsig.sldsupport.exception.UnsupportedSLDObjectException;
39
import org.gvsig.sldsupport.sld.filter.FilterTags;
40
import org.gvsig.sldsupport.sld.filter.SLDFilter;
41
import org.gvsig.sldsupport.sld.filter.SLDFilterOperator;
42
import org.gvsig.sldsupport.sld.filter.expression.SLDExpression;
43
import org.gvsig.sldsupport.sld.filter.expression.operator.SLDLiteral;
44
import org.gvsig.sldsupport.sld.filter.expression.operator.SLDPropertyName;
45
import org.gvsig.sldsupport.sld.filter.operator.SLDComparisonOperator;
46
import org.gvsig.sldsupport.sld.filter.operator.comparison.SLDBinaryComparisonOperator;
47
import org.gvsig.sldsupport.sld.filter.operator.comparison.SLDIsBetweenOperator;
48
import org.gvsig.sldsupport.sld.layer.SLDLayer;
49
import org.gvsig.sldsupport.sld.layer.SLDNamedLayer;
50
import org.gvsig.sldsupport.sld.rule.SLDRule;
51
import org.gvsig.sldsupport.sld.style.SLDFeatureStyle;
52
import org.gvsig.sldsupport.sld.style.layer.SLDUserStyle;
53
import org.gvsig.sldsupport.sld.symbol.SLDSymbol;
54
import org.gvsig.tools.dataTypes.CoercionException;
55
import org.gvsig.tools.dataTypes.DataType;
56

  
57
public class IntervalsLegendUtils {
58
	
59
	
60
	public static IVectorialIntervalLegend toIntervalsLegend(List<SLDRule> rules)
61
			throws UnsupportedSLDObjectException {
62
		
63
		IVectorialIntervalLegend intleg = (IVectorialIntervalLegend) 
64
				BasicUtils.mapMan().createLegend(
65
						IVectorialIntervalLegend.LEGEND_NAME);
66
		
67
		String pname = BasicUtils.getFirstPropertyName(rules);
68
		if (pname == null || pname.length() == 0) {
69
			throw new UnsupportedSLDObjectException(
70
					"SLDFeatureStyle", "Cannot interpret rules (no property name)");
71
		}
72
		List<SLDLiteral> lits = BasicUtils.getComparisonLiterals(rules);
73
		if (lits == null || lits.size() == 0) {
74
			throw new UnsupportedSLDObjectException(
75
					"SLDFeatureStyle", "Cannot interpret rules (no literals)");
76
		}
77
		DataType dt = BasicUtils.guessDataType(lits);
78
		ISymbol sym = BasicUtils.getElseSymbol(rules);
79
		// sym can be null
80
		Map<IInterval, ISymbol> map =
81
				IntervalsLegendUtils.getIntervalToSymbol(rules, dt, pname);
82
		Iterator iter = map.keySet().iterator();
83
		Object k = null;
84
		while (iter.hasNext()) {
85
			k = iter.next();
86
			intleg.addSymbol(k, map.get(k));
87
		}
88
		// Setting Default symbol
89
		if (sym != null) {
90
			sym.setDescription("Default");
91
			intleg.setDefaultSymbol(sym);
92
			intleg.useDefaultSymbol(true);
93
			// String among the intervals
94
			intleg.addSymbol(sym.getDescription(), sym);
95
		} else {
96
			intleg.useDefaultSymbol(false);
97
		}
98
		
99
		String[] atts = new String[1];
100
		atts[0] = pname;
101
		intleg.setClassifyingFieldNames(atts);
102
		int[] typs = new int[1];
103
		typs[0] = dt.getType();
104
		intleg.setClassifyingFieldTypes(typs);
105
		return intleg;	
106
	
107
	}
108
	
109
	
110
	public static boolean areRulesOfIntervals(List<SLDRule> rules) {
111

  
112
		if (rules == null || rules.size() == 0) {
113
			return false;
114
		}
115
		
116
		SLDRule rule = null;
117
		String fieldName = null;
118
		for (int i=0; i<rules.size(); i++) {
119
			rule = rules.get(i);
120
			fieldName = getComparisonToNumericLiteralFieldName(rule);
121
			if (fieldName != null) {
122
				break;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff